1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Session\Abstracts; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Kernel\DataStructures\Config; |
||
19 | use Psr\Log\LoggerAwareInterface; |
||
20 | use Psr\Log\LoggerInterface; |
||
21 | |||
22 | /** |
||
23 | * Class AbstractHandler |
||
24 | * |
||
25 | * Base class of session platform handlers. |
||
26 | * |
||
27 | * @package O2System\Session\Handler |
||
28 | */ |
||
29 | abstract class AbstractHandler implements \SessionHandlerInterface, LoggerAwareInterface |
||
30 | { |
||
31 | /** |
||
32 | * Session Handler Platform Name |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $platform; |
||
37 | |||
38 | /** |
||
39 | * Session Handler Config |
||
40 | * |
||
41 | * @var Config |
||
42 | */ |
||
43 | protected $config; |
||
44 | |||
45 | /** |
||
46 | * Session Cache Key Prefix |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $prefixKey = 'o2session:'; |
||
51 | |||
52 | /** |
||
53 | * Session Lock Key |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $lockKey; |
||
58 | |||
59 | /** |
||
60 | * Session Data Fingerprint |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $fingerprint; |
||
65 | |||
66 | /** |
||
67 | * Session Is Locked Flag |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $isLocked = false; |
||
72 | |||
73 | /** |
||
74 | * Current session ID |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $sessionId; |
||
79 | |||
80 | /** |
||
81 | * Success and failure return values |
||
82 | * |
||
83 | * Necessary due to a bug in all PHP 5 versions where return values |
||
84 | * from userspace handlers are not handled properly. PHP 7 fixes the |
||
85 | * bug, so we need to return different values depending on the version. |
||
86 | * |
||
87 | * @see https://wiki.php.net/rfc/session.user.return-value |
||
88 | * @var mixed |
||
89 | */ |
||
90 | protected $success, $failure; |
||
91 | |||
92 | /** |
||
93 | * Logger Instance |
||
94 | * |
||
95 | * @var LoggerInterface |
||
96 | */ |
||
97 | protected $logger; |
||
98 | |||
99 | //-------------------------------------------------------------------- |
||
100 | |||
101 | /** |
||
102 | * AbstractHandler::__construct |
||
103 | * |
||
104 | * @param \O2System\Kernel\DataStructures\Config $config |
||
105 | * |
||
106 | * @return AbstractHandler |
||
107 | */ |
||
108 | public function __construct(Config $config) |
||
109 | { |
||
110 | $this->config = $config; |
||
111 | $this->config->offsetUnset('handler'); |
||
112 | $this->setPrefixKey($this->config[ 'name' ]); |
||
113 | |||
114 | if (is_php('7')) { |
||
115 | $this->success = true; |
||
116 | $this->failure = false; |
||
117 | } else { |
||
118 | $this->success = 0; |
||
119 | $this->failure = -1; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | //-------------------------------------------------------------------- |
||
124 | |||
125 | /** |
||
126 | * AbstractHandler::setPrefixKey |
||
127 | * |
||
128 | * Sets cache prefix key |
||
129 | * |
||
130 | * @param $prefixKey |
||
131 | */ |
||
132 | public function setPrefixKey($prefixKey) |
||
133 | { |
||
134 | $this->prefixKey = rtrim($prefixKey, ':') . ':'; |
||
135 | } |
||
136 | |||
137 | // ------------------------------------------------------------------------ |
||
138 | |||
139 | /** |
||
140 | * AbstractHandler::setLogger |
||
141 | * |
||
142 | * Sets a logger instance on the object |
||
143 | * |
||
144 | * @param LoggerInterface $logger |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | public function setLogger(LoggerInterface $logger) |
||
149 | { |
||
150 | $this->logger =& $logger; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * AbstractHandler::getPlatform |
||
155 | * |
||
156 | * Get Current Platform |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getPlatform() |
||
161 | { |
||
162 | return $this->platform; |
||
163 | } |
||
164 | |||
165 | //-------------------------------------------------------------------- |
||
166 | |||
167 | /** |
||
168 | * AbstractHandler::open |
||
169 | * |
||
170 | * Initialize session |
||
171 | * |
||
172 | * @link http://php.net/manual/en/sessionhandlerinterface.open.php |
||
173 | * |
||
174 | * @param string $savePath The path where to store/retrieve the session. |
||
175 | * @param string $name The session name. |
||
176 | * |
||
177 | * @return bool <p> |
||
178 | * The return value (usually TRUE on success, FALSE on failure). |
||
179 | * Note this value is returned internally to PHP for processing. |
||
180 | * </p> |
||
181 | * @since 5.4.0 |
||
182 | */ |
||
183 | abstract public function open($savePath, $name); |
||
184 | |||
185 | /** |
||
186 | * AbstractHandler::close |
||
187 | * |
||
188 | * Close the session |
||
189 | * |
||
190 | * @link http://php.net/manual/en/sessionhandlerinterface.close.php |
||
191 | * @return bool <p> |
||
192 | * The return value (usually TRUE on success, FALSE on failure). |
||
193 | * Note this value is returned internally to PHP for processing. |
||
194 | * </p> |
||
195 | * @since 5.4.0 |
||
196 | */ |
||
197 | abstract public function close(); |
||
198 | |||
199 | /** |
||
200 | * AbstractHandler::destroy |
||
201 | * |
||
202 | * Destroy a session |
||
203 | * |
||
204 | * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php |
||
205 | * |
||
206 | * @param string $sessionId The session ID being destroyed. |
||
207 | * |
||
208 | * @return bool <p> |
||
209 | * The return value (usually TRUE on success, FALSE on failure). |
||
210 | * Note this value is returned internally to PHP for processing. |
||
211 | * </p> |
||
212 | * @since 5.4.0 |
||
213 | */ |
||
214 | abstract public function destroy($sessionId); |
||
215 | |||
216 | /** |
||
217 | * AbstractHandler::gc |
||
218 | * |
||
219 | * Cleanup old sessions |
||
220 | * |
||
221 | * @link http://php.net/manual/en/sessionhandlerinterface.gc.php |
||
222 | * |
||
223 | * @param int $maxlifetime <p> |
||
224 | * Sessions that have not updated for |
||
225 | * the last maxlifetime seconds will be removed. |
||
226 | * </p> |
||
227 | * |
||
228 | * @return bool <p> |
||
229 | * The return value (usually TRUE on success, FALSE on failure). |
||
230 | * Note this value is returned internally to PHP for processing. |
||
231 | * </p> |
||
232 | * @since 5.4.0 |
||
233 | */ |
||
234 | abstract public function gc($maxlifetime); |
||
235 | |||
236 | /** |
||
237 | * AbstractHandler::read |
||
238 | * |
||
239 | * Read session data |
||
240 | * |
||
241 | * @link http://php.net/manual/en/sessionhandlerinterface.read.php |
||
242 | * |
||
243 | * @param string $sessionId The session id to read data for. |
||
244 | * |
||
245 | * @return string <p> |
||
246 | * Returns an encoded string of the read data. |
||
247 | * If nothing was read, it must return an empty string. |
||
248 | * Note this value is returned internally to PHP for processing. |
||
249 | * </p> |
||
250 | * @since 5.4.0 |
||
251 | */ |
||
252 | abstract public function read($sessionId); |
||
253 | |||
254 | /** |
||
255 | * AbstractHandler::write |
||
256 | * |
||
257 | * Write session data |
||
258 | * |
||
259 | * @link http://php.net/manual/en/sessionhandlerinterface.write.php |
||
260 | * |
||
261 | * @param string $sessionId The session id. |
||
262 | * @param string $sessionData <p> |
||
263 | * The encoded session data. This data is the |
||
264 | * result of the PHP internally encoding |
||
265 | * the $_SESSION superglobal to a serialized |
||
266 | * string and passing it as this parameter. |
||
267 | * Please note sessions use an alternative serialization method. |
||
268 | * </p> |
||
269 | * |
||
270 | * @return bool <p> |
||
271 | * The return value (usually TRUE on success, FALSE on failure). |
||
272 | * Note this value is returned internally to PHP for processing. |
||
273 | * </p> |
||
274 | * @since 5.4.0 |
||
275 | */ |
||
276 | abstract public function write($sessionId, $sessionData); |
||
277 | |||
278 | /** |
||
279 | * AbstractHandler::isSupported |
||
280 | * |
||
281 | * Checks if this platform is supported on this system. |
||
282 | * |
||
283 | * @return bool Returns FALSE if not supported. |
||
284 | */ |
||
285 | abstract public function isSupported(); |
||
286 | |||
287 | //-------------------------------------------------------------------- |
||
288 | |||
289 | /** |
||
290 | * AbstractHandler::_lockSession |
||
291 | * |
||
292 | * A dummy method allowing drivers with no locking functionality |
||
293 | * (databases other than PostgreSQL and MySQL) to act as if they |
||
294 | * do acquire a lock. |
||
295 | * |
||
296 | * @param string $sessionId |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | protected function lockSession($sessionId) |
||
0 ignored issues
–
show
|
|||
301 | { |
||
302 | $this->isLocked = true; |
||
303 | |||
304 | return true; |
||
305 | } |
||
306 | |||
307 | //-------------------------------------------------------------------- |
||
308 | |||
309 | /** |
||
310 | * AbstractHandler::_lockRelease |
||
311 | * |
||
312 | * Releases the lock, if any. |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | protected function lockRelease() |
||
317 | { |
||
318 | $this->isLocked = false; |
||
319 | |||
320 | return true; |
||
321 | } |
||
322 | |||
323 | //-------------------------------------------------------------------- |
||
324 | |||
325 | /** |
||
326 | * AbstractHandler::_destroyCookie |
||
327 | * |
||
328 | * Internal method to force removal of a cookie by the client |
||
329 | * when session_destroy() is called. |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | protected function destroyCookie() |
||
334 | { |
||
335 | return setcookie( |
||
336 | $this->config[ 'name' ], |
||
337 | null, |
||
338 | 1, |
||
339 | $this->config[ 'cookie' ]->path, |
||
340 | '.' . ltrim($this->config[ 'cookie' ]->domain, '.'), |
||
341 | $this->config[ 'cookie' ]->secure, |
||
342 | true |
||
343 | ); |
||
344 | } |
||
345 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.