| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Session\SessionUtils;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* This abstract session handler provides a generic implementation
|
|
|
18 |
* of the PHP 7.0 SessionUpdateTimestampHandlerInterface,
|
|
|
19 |
* enabling strict and lazy session handling.
|
|
|
20 |
*
|
|
|
21 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
22 |
*/
|
|
|
23 |
abstract class AbstractSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
|
|
|
24 |
{
|
|
|
25 |
private string $sessionName;
|
|
|
26 |
private string $prefetchId;
|
|
|
27 |
private string $prefetchData;
|
|
|
28 |
private ?string $newSessionId = null;
|
|
|
29 |
private string $igbinaryEmptyData;
|
|
|
30 |
|
|
|
31 |
public function open(string $savePath, string $sessionName): bool
|
|
|
32 |
{
|
|
|
33 |
$this->sessionName = $sessionName;
|
|
|
34 |
if (!headers_sent() && !\ini_get('session.cache_limiter') && '0' !== \ini_get('session.cache_limiter')) {
|
|
|
35 |
header(sprintf('Cache-Control: max-age=%d, private, must-revalidate', 60 * (int) \ini_get('session.cache_expire')));
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
return true;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
abstract protected function doRead(string $sessionId): string;
|
|
|
42 |
|
|
|
43 |
abstract protected function doWrite(string $sessionId, string $data): bool;
|
|
|
44 |
|
|
|
45 |
abstract protected function doDestroy(string $sessionId): bool;
|
|
|
46 |
|
|
|
47 |
public function validateId(string $sessionId): bool
|
|
|
48 |
{
|
|
|
49 |
$this->prefetchData = $this->read($sessionId);
|
|
|
50 |
$this->prefetchId = $sessionId;
|
|
|
51 |
|
|
|
52 |
return '' !== $this->prefetchData;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function read(string $sessionId): string
|
|
|
56 |
{
|
|
|
57 |
if (isset($this->prefetchId)) {
|
|
|
58 |
$prefetchId = $this->prefetchId;
|
|
|
59 |
$prefetchData = $this->prefetchData;
|
|
|
60 |
unset($this->prefetchId, $this->prefetchData);
|
|
|
61 |
|
|
|
62 |
if ($prefetchId === $sessionId || '' === $prefetchData) {
|
|
|
63 |
$this->newSessionId = '' === $prefetchData ? $sessionId : null;
|
|
|
64 |
|
|
|
65 |
return $prefetchData;
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$data = $this->doRead($sessionId);
|
|
|
70 |
$this->newSessionId = '' === $data ? $sessionId : null;
|
|
|
71 |
|
|
|
72 |
return $data;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public function write(string $sessionId, string $data): bool
|
|
|
76 |
{
|
|
|
77 |
// see https://github.com/igbinary/igbinary/issues/146
|
|
|
78 |
$this->igbinaryEmptyData ??= \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
|
|
|
79 |
if ('' === $data || $this->igbinaryEmptyData === $data) {
|
|
|
80 |
return $this->destroy($sessionId);
|
|
|
81 |
}
|
|
|
82 |
$this->newSessionId = null;
|
|
|
83 |
|
|
|
84 |
return $this->doWrite($sessionId, $data);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function destroy(string $sessionId): bool
|
|
|
88 |
{
|
|
|
89 |
if (!headers_sent() && filter_var(\ini_get('session.use_cookies'), \FILTER_VALIDATE_BOOL)) {
|
|
|
90 |
if (!isset($this->sessionName)) {
|
|
|
91 |
throw new \LogicException(sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', static::class));
|
|
|
92 |
}
|
|
|
93 |
$cookie = SessionUtils::popSessionCookie($this->sessionName, $sessionId);
|
|
|
94 |
|
|
|
95 |
/*
|
|
|
96 |
* We send an invalidation Set-Cookie header (zero lifetime)
|
|
|
97 |
* when either the session was started or a cookie with
|
|
|
98 |
* the session name was sent by the client (in which case
|
|
|
99 |
* we know it's invalid as a valid session cookie would've
|
|
|
100 |
* started the session).
|
|
|
101 |
*/
|
|
|
102 |
if (null === $cookie || isset($_COOKIE[$this->sessionName])) {
|
|
|
103 |
$params = session_get_cookie_params();
|
|
|
104 |
unset($params['lifetime']);
|
|
|
105 |
setcookie($this->sessionName, '', $params);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
return $this->newSessionId === $sessionId || $this->doDestroy($sessionId);
|
|
|
110 |
}
|
|
|
111 |
}
|