| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation\Session\Storage; |
| 13 |
|
| 14 |
use Symfony\Component\HttpFoundation\Session\SessionBagInterface; |
| 15 |
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler; |
| 16 |
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy; |
| 17 |
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; |
| 18 |
|
| 19 |
|
| 20 |
class_exists(MetadataBag::class); |
| 21 |
class_exists(StrictSessionHandler::class); |
| 22 |
class_exists(SessionHandlerProxy::class); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class NativeSessionStorage implements SessionStorageInterface |
| 30 |
{ |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
protected array $bags = []; |
| 35 |
protected bool $started = false; |
| 36 |
protected bool $closed = false; |
| 37 |
protected AbstractProxy|\SessionHandlerInterface $saveHandler; |
| 38 |
protected MetadataBag $metadataBag; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function __construct(array $options = [], AbstractProxy|\SessionHandlerInterface|null $handler = null, ?MetadataBag $metaBag = null) |
| 77 |
{ |
| 78 |
if (!\extension_loaded('session')) { |
| 79 |
throw new \LogicException('PHP extension "session" is required.'); |
| 80 |
} |
| 81 |
|
| 82 |
$options += [ |
| 83 |
'cache_limiter' => '', |
| 84 |
'cache_expire' => 0, |
| 85 |
'use_cookies' => 1, |
| 86 |
'lazy_write' => 1, |
| 87 |
'use_strict_mode' => 1, |
| 88 |
]; |
| 89 |
|
| 90 |
session_register_shutdown(); |
| 91 |
|
| 92 |
$this->setMetadataBag($metaBag); |
| 93 |
$this->setOptions($options); |
| 94 |
$this->setSaveHandler($handler); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function getSaveHandler(): AbstractProxy|\SessionHandlerInterface |
| 101 |
{ |
| 102 |
return $this->saveHandler; |
| 103 |
} |
| 104 |
|
| 105 |
public function start(): bool |
| 106 |
{ |
| 107 |
if ($this->started) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
if (\PHP_SESSION_ACTIVE === session_status()) { |
| 112 |
throw new \RuntimeException('Failed to start the session: already started by PHP.'); |
| 113 |
} |
| 114 |
|
| 115 |
if (filter_var(\ini_get('session.use_cookies'), \FILTER_VALIDATE_BOOL) && headers_sent($file, $line)) { |
| 116 |
throw new \RuntimeException(\sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line)); |
| 117 |
} |
| 118 |
|
| 119 |
$sessionId = $_COOKIE[session_name()] ?? null; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if ($sessionId && $this->saveHandler instanceof AbstractProxy && 'files' === $this->saveHandler->getSaveHandlerName() && !preg_match('/^[a-zA-Z0-9,-]{22,250}$/', $sessionId)) { |
| 153 |
|
| 154 |
session_id(session_create_id()); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if (!session_start()) { |
| 159 |
throw new \RuntimeException('Failed to start the session.'); |
| 160 |
} |
| 161 |
|
| 162 |
$this->loadSession(); |
| 163 |
|
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
public function getId(): string |
| 168 |
{ |
| 169 |
return $this->saveHandler->getId(); |
| 170 |
} |
| 171 |
|
| 172 |
public function setId(string $id): void |
| 173 |
{ |
| 174 |
$this->saveHandler->setId($id); |
| 175 |
} |
| 176 |
|
| 177 |
public function getName(): string |
| 178 |
{ |
| 179 |
return $this->saveHandler->getName(); |
| 180 |
} |
| 181 |
|
| 182 |
public function setName(string $name): void |
| 183 |
{ |
| 184 |
$this->saveHandler->setName($name); |
| 185 |
} |
| 186 |
|
| 187 |
public function regenerate(bool $destroy = false, ?int $lifetime = null): bool |
| 188 |
{ |
| 189 |
|
| 190 |
if (\PHP_SESSION_ACTIVE !== session_status()) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
if (headers_sent()) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
if (null !== $lifetime && $lifetime != \ini_get('session.cookie_lifetime')) { |
| 199 |
$this->save(); |
| 200 |
ini_set('session.cookie_lifetime', $lifetime); |
| 201 |
$this->start(); |
| 202 |
} |
| 203 |
|
| 204 |
if ($destroy) { |
| 205 |
$this->metadataBag->stampNew(); |
| 206 |
} |
| 207 |
|
| 208 |
return session_regenerate_id($destroy); |
| 209 |
} |
| 210 |
|
| 211 |
public function save(): void |
| 212 |
{ |
| 213 |
|
| 214 |
$session = $_SESSION; |
| 215 |
|
| 216 |
foreach ($this->bags as $bag) { |
| 217 |
if (empty($_SESSION[$key = $bag->getStorageKey()])) { |
| 218 |
unset($_SESSION[$key]); |
| 219 |
} |
| 220 |
} |
| 221 |
if ($_SESSION && [$key = $this->metadataBag->getStorageKey()] === array_keys($_SESSION)) { |
| 222 |
unset($_SESSION[$key]); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
$previousHandler = set_error_handler(function ($type, $msg, $file, $line) use (&$previousHandler) { |
| 227 |
if (\E_WARNING === $type && str_starts_with($msg, 'session_write_close():')) { |
| 228 |
$handler = $this->saveHandler instanceof SessionHandlerProxy ? $this->saveHandler->getHandler() : $this->saveHandler; |
| 229 |
$msg = \sprintf('session_write_close(): Failed to write session data with "%s" handler', $handler::class); |
| 230 |
} |
| 231 |
|
| 232 |
return $previousHandler ? $previousHandler($type, $msg, $file, $line) : false; |
| 233 |
}); |
| 234 |
|
| 235 |
try { |
| 236 |
session_write_close(); |
| 237 |
} finally { |
| 238 |
restore_error_handler(); |
| 239 |
|
| 240 |
|
| 241 |
if ($_SESSION) { |
| 242 |
$_SESSION = $session; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
$this->closed = true; |
| 247 |
$this->started = false; |
| 248 |
} |
| 249 |
|
| 250 |
public function clear(): void |
| 251 |
{ |
| 252 |
|
| 253 |
foreach ($this->bags as $bag) { |
| 254 |
$bag->clear(); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
$_SESSION = []; |
| 259 |
|
| 260 |
|
| 261 |
$this->loadSession(); |
| 262 |
} |
| 263 |
|
| 264 |
public function registerBag(SessionBagInterface $bag): void |
| 265 |
{ |
| 266 |
if ($this->started) { |
| 267 |
throw new \LogicException('Cannot register a bag when the session is already started.'); |
| 268 |
} |
| 269 |
|
| 270 |
$this->bags[$bag->getName()] = $bag; |
| 271 |
} |
| 272 |
|
| 273 |
public function getBag(string $name): SessionBagInterface |
| 274 |
{ |
| 275 |
if (!isset($this->bags[$name])) { |
| 276 |
throw new \InvalidArgumentException(\sprintf('The SessionBagInterface "%s" is not registered.', $name)); |
| 277 |
} |
| 278 |
|
| 279 |
if (!$this->started && $this->saveHandler->isActive()) { |
| 280 |
$this->loadSession(); |
| 281 |
} elseif (!$this->started) { |
| 282 |
$this->start(); |
| 283 |
} |
| 284 |
|
| 285 |
return $this->bags[$name]; |
| 286 |
} |
| 287 |
|
| 288 |
public function setMetadataBag(?MetadataBag $metaBag): void |
| 289 |
{ |
| 290 |
$this->metadataBag = $metaBag ?? new MetadataBag(); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public function getMetadataBag(): MetadataBag |
| 297 |
{ |
| 298 |
return $this->metadataBag; |
| 299 |
} |
| 300 |
|
| 301 |
public function isStarted(): bool |
| 302 |
{ |
| 303 |
return $this->started; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function setOptions(array $options): void |
| 317 |
{ |
| 318 |
if (headers_sent() || \PHP_SESSION_ACTIVE === session_status()) { |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
$validOptions = array_flip([ |
| 323 |
'cache_expire', 'cache_limiter', 'cookie_domain', 'cookie_httponly', |
| 324 |
'cookie_lifetime', 'cookie_path', 'cookie_secure', 'cookie_samesite', |
| 325 |
'gc_divisor', 'gc_maxlifetime', 'gc_probability', |
| 326 |
'lazy_write', 'name', 'referer_check', |
| 327 |
'serialize_handler', 'use_strict_mode', 'use_cookies', |
| 328 |
'use_only_cookies', 'use_trans_sid', |
| 329 |
'sid_length', 'sid_bits_per_character', 'trans_sid_hosts', 'trans_sid_tags', |
| 330 |
]); |
| 331 |
|
| 332 |
foreach ($options as $key => $value) { |
| 333 |
if (\in_array($key, ['referer_check', 'use_only_cookies', 'use_trans_sid', 'trans_sid_hosts', 'trans_sid_tags', 'sid_length', 'sid_bits_per_character'], true)) { |
| 334 |
trigger_deprecation('symfony/http-foundation', '7.2', 'NativeSessionStorage\'s "%s" option is deprecated and will be ignored in Symfony 8.0.', $key); |
| 335 |
} |
| 336 |
|
| 337 |
if (isset($validOptions[$key])) { |
| 338 |
if ('cookie_secure' === $key && 'auto' === $value) { |
| 339 |
continue; |
| 340 |
} |
| 341 |
ini_set('session.'.$key, $value); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public function setSaveHandler(AbstractProxy|\SessionHandlerInterface|null $saveHandler): void |
| 365 |
{ |
| 366 |
|
| 367 |
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) { |
| 368 |
$saveHandler = new SessionHandlerProxy($saveHandler); |
| 369 |
} elseif (!$saveHandler instanceof AbstractProxy) { |
| 370 |
$saveHandler = new SessionHandlerProxy(new StrictSessionHandler(new \SessionHandler())); |
| 371 |
} |
| 372 |
$this->saveHandler = $saveHandler; |
| 373 |
|
| 374 |
if (headers_sent() || \PHP_SESSION_ACTIVE === session_status()) { |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
if ($this->saveHandler instanceof SessionHandlerProxy) { |
| 379 |
session_set_save_handler($this->saveHandler, false); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
protected function loadSession(?array &$session = null): void |
| 392 |
{ |
| 393 |
if (null === $session) { |
| 394 |
$session = &$_SESSION; |
| 395 |
} |
| 396 |
|
| 397 |
$bags = array_merge($this->bags, [$this->metadataBag]); |
| 398 |
|
| 399 |
foreach ($bags as $bag) { |
| 400 |
$key = $bag->getStorageKey(); |
| 401 |
$session[$key] = isset($session[$key]) && \is_array($session[$key]) ? $session[$key] : []; |
| 402 |
$bag->initialize($session[$key]); |
| 403 |
} |
| 404 |
|
| 405 |
$this->started = true; |
| 406 |
$this->closed = false; |
| 407 |
} |
| 408 |
} |