Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/Session/Storage/Handler/AbstractSessionHandler.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 <[email protected]>
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(#[\SensitiveParameter] string $sessionId): string;
42
43 abstract protected function doWrite(#[\SensitiveParameter] string $sessionId, string $data): bool;
44
45 abstract protected function doDestroy(#[\SensitiveParameter] string $sessionId): bool;
46
47 public function validateId(#[\SensitiveParameter] 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(#[\SensitiveParameter] 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 updateTimestamp(#[\SensitiveParameter] string $sessionId, string $data): bool
76 {
77 $this->igbinaryEmptyData ??= \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
78 if ('' === $data || $this->igbinaryEmptyData === $data) {
79 return $this->destroy($sessionId);
80 }
81
82 return true;
83 }
84
85 public function write(#[\SensitiveParameter] string $sessionId, string $data): bool
86 {
87 // see https://github.com/igbinary/igbinary/issues/146
88 $this->igbinaryEmptyData ??= \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
89 if ('' === $data || $this->igbinaryEmptyData === $data) {
90 return $this->destroy($sessionId);
91 }
92 $this->newSessionId = null;
93
94 return $this->doWrite($sessionId, $data);
95 }
96
97 public function destroy(#[\SensitiveParameter] string $sessionId): bool
98 {
99 if (!headers_sent() && filter_var(\ini_get('session.use_cookies'), \FILTER_VALIDATE_BOOL)) {
100 if (!isset($this->sessionName)) {
101 throw new \LogicException(\sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', static::class));
102 }
103 $cookie = SessionUtils::popSessionCookie($this->sessionName, $sessionId);
104
105 /*
106 * We send an invalidation Set-Cookie header (zero lifetime)
107 * when either the session was started or a cookie with
108 * the session name was sent by the client (in which case
109 * we know it's invalid as a valid session cookie would've
110 * started the session).
111 */
112 if (null === $cookie || isset($_COOKIE[$this->sessionName])) {
113 $params = session_get_cookie_params();
114 unset($params['lifetime']);
115 setcookie($this->sessionName, '', $params);
116 }
117 }
118
119 return $this->newSessionId === $sessionId || $this->doDestroy($sessionId);
120 }
121 }