Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
566 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
566 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
566 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
566 | daniel-mar | 26 | class OIDplusAuthContentStoreSession extends OIDplusAuthContentStore { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @return OIDplusSessionHandler |
||
30 | */ |
||
31 | protected static function getSessionHandler(): OIDplusSessionHandler { |
||
567 | daniel-mar | 32 | static $sesHandler = null; |
33 | if (is_null($sesHandler)) { |
||
34 | $sesHandler = new OIDplusSessionHandler(); |
||
35 | } |
||
36 | return $sesHandler; |
||
37 | } |
||
38 | |||
566 | daniel-mar | 39 | // Override abstract functions |
585 | daniel-mar | 40 | # TODO: shouldn't we just include OIDplusSessionHandler in this class? |
566 | daniel-mar | 41 | |
1116 | daniel-mar | 42 | /** |
43 | * @param string $name |
||
44 | * @param mixed|null $default |
||
45 | * @return mixed|null |
||
46 | * @throws OIDplusException |
||
47 | */ |
||
48 | public function getValue(string $name, $default = NULL) { |
||
716 | daniel-mar | 49 | try { |
50 | return self::getSessionHandler()->getValue($name, $default); |
||
1050 | daniel-mar | 51 | } catch (\Exception $e) { |
716 | daniel-mar | 52 | self::getSessionHandler()->destroySession(); |
847 | daniel-mar | 53 | // TODO: For some reason If destroySession() is called, we won't get this Exception?! |
716 | daniel-mar | 54 | throw new OIDplusException(_L('Internal error with session. Please reload the page and log-in again. %1', $e->getMessage())); |
55 | } |
||
566 | daniel-mar | 56 | } |
57 | |||
1116 | daniel-mar | 58 | /** |
59 | * @param string $name |
||
60 | * @param mixed $value |
||
61 | * @return void |
||
62 | * @throws OIDplusException |
||
63 | */ |
||
64 | public function setValue(string $name, $value) { |
||
65 | self::getSessionHandler()->setValue($name, $value); |
||
566 | daniel-mar | 66 | } |
67 | |||
1116 | daniel-mar | 68 | /** |
69 | * @param string $name |
||
70 | * @return bool |
||
71 | * @throws OIDplusException |
||
72 | */ |
||
73 | public function exists(string $name): bool { |
||
585 | daniel-mar | 74 | return self::getSessionHandler()->exists($name); |
569 | daniel-mar | 75 | } |
76 | |||
1116 | daniel-mar | 77 | /** |
78 | * @param string $name |
||
79 | * @return void |
||
80 | * @throws OIDplusException |
||
81 | */ |
||
82 | public function delete(string $name) { |
||
83 | self::getSessionHandler()->delete($name); |
||
569 | daniel-mar | 84 | } |
85 | |||
1116 | daniel-mar | 86 | /** |
87 | * @return void |
||
88 | * @throws OIDplusException |
||
89 | */ |
||
585 | daniel-mar | 90 | public function destroySession() { |
1116 | daniel-mar | 91 | self::getSessionHandler()->destroySession(); |
566 | daniel-mar | 92 | } |
93 | |||
1116 | daniel-mar | 94 | /** |
95 | * @return OIDplusAuthContentStoreSession|null |
||
96 | */ |
||
97 | public static function getActiveProvider()/*: ?OIDplusAuthContentStore*/ { |
||
585 | daniel-mar | 98 | static $contentProvider = null; |
99 | |||
1300 | daniel-mar | 100 | $rel_url = substr($_SERVER['REQUEST_URI'], strlen(OIDplus::webpath(null, OIDplus::PATH_RELATIVE_TO_ROOT))); |
101 | if (str_starts_with($rel_url, 'rest/')) { // <== TODO: Find a way how to move this into the plugin, since REST does not belong to the core. (Maybe some kind of "stateless mode" that is enabled by the REST plugin) |
||
102 | // For REST, we must only allow JWT from Bearer and nothing else! So disable cookies if we are accessing the REST plugin |
||
103 | return null; |
||
104 | } |
||
105 | |||
585 | daniel-mar | 106 | if (!$contentProvider) { |
107 | if (self::getSessionHandler()->isActive()) { |
||
108 | $contentProvider = new OIDplusAuthContentStoreSession(); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | return $contentProvider; |
||
113 | } |
||
114 | |||
1116 | daniel-mar | 115 | /** |
116 | * @param string $email |
||
117 | * @param string $loginfo |
||
118 | * @return void |
||
119 | */ |
||
120 | public function raLoginEx(string $email, string &$loginfo) { |
||
585 | daniel-mar | 121 | $this->raLogin($email); |
122 | if (is_null(self::getActiveProvider())) { |
||
123 | $loginfo = 'into new PHP session'; |
||
124 | } else { |
||
125 | $loginfo = 'into existing PHP session'; |
||
126 | } |
||
127 | } |
||
128 | |||
1116 | daniel-mar | 129 | /** |
130 | * @param string $loginfo |
||
131 | * @return void |
||
132 | */ |
||
133 | public function adminLoginEx(string &$loginfo) { |
||
585 | daniel-mar | 134 | $this->adminLogin(); |
135 | if (is_null(self::getActiveProvider())) { |
||
136 | $loginfo = 'into new PHP session'; |
||
137 | } else { |
||
138 | $loginfo = 'into existing PHP session'; |
||
139 | } |
||
140 | } |
||
141 | |||
1116 | daniel-mar | 142 | /** |
143 | * @param string $email |
||
144 | * @param string $loginfo |
||
145 | * @return void |
||
146 | */ |
||
147 | public function raLogoutEx(string $email, string &$loginfo) { |
||
585 | daniel-mar | 148 | $this->raLogout($email); |
149 | $loginfo = 'from PHP session'; |
||
150 | } |
||
151 | |||
1116 | daniel-mar | 152 | /** |
153 | * @param string $loginfo |
||
154 | * @return void |
||
155 | */ |
||
156 | public function adminLogoutEx(string &$loginfo) { |
||
585 | daniel-mar | 157 | $this->adminLogout(); |
158 | $loginfo = 'from PHP session'; |
||
159 | } |
||
160 | |||
1116 | daniel-mar | 161 | /** |
162 | * @return void |
||
163 | */ |
||
585 | daniel-mar | 164 | public function activate() { |
165 | # Sessions automatically activate during setValue() |
||
166 | } |
||
167 | |||
569 | daniel-mar | 168 | } |