| Total Complexity | 83 |
| Total Lines | 592 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Session often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Session, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 90 | class Session |
||
| 91 | { |
||
| 92 | use Configurable; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set session timeout in seconds. |
||
| 96 | * |
||
| 97 | * @var int |
||
| 98 | * @config |
||
| 99 | */ |
||
| 100 | private static $timeout = 0; |
||
|
|
|||
| 101 | |||
| 102 | /** |
||
| 103 | * @config |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private static $session_ips = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @config |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private static $cookie_domain; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @config |
||
| 116 | * @var string |
||
| 117 | */ |
||
| 118 | private static $cookie_path; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @config |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | private static $session_store_path; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @config |
||
| 128 | * @var boolean |
||
| 129 | */ |
||
| 130 | private static $cookie_secure = false; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @config |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | private static $cookie_name_secure = 'SECSESSID'; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Must be "Strict", "Lax", or "None". |
||
| 140 | * @config |
||
| 141 | */ |
||
| 142 | private static string $cookie_samesite = 'Lax'; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Name of session cache limiter to use. |
||
| 146 | * Defaults to '' to disable cache limiter entirely. |
||
| 147 | * |
||
| 148 | * @see https://secure.php.net/manual/en/function.session-cache-limiter.php |
||
| 149 | * @var string|null |
||
| 150 | */ |
||
| 151 | private static $sessionCacheLimiter = ''; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Invalidate the session if user agent header changes between request. Defaults to true. Disabling this checks is |
||
| 155 | * not recommended. |
||
| 156 | * @var bool |
||
| 157 | * @config |
||
| 158 | */ |
||
| 159 | private static $strict_user_agent_check = true; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Session data. |
||
| 163 | * Will be null if session has not been started |
||
| 164 | * |
||
| 165 | * @var array|null |
||
| 166 | */ |
||
| 167 | protected $data = null; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var bool |
||
| 171 | */ |
||
| 172 | protected $started = false; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * List of keys changed. This is a nested array which represents the |
||
| 176 | * keys modified in $this->data. The value of each item is either "true" |
||
| 177 | * or a nested array. |
||
| 178 | * |
||
| 179 | * If a value is in changedData but not in data, it must be removed |
||
| 180 | * from the destination during save(). |
||
| 181 | * |
||
| 182 | * Only highest level changes are stored. E.g. changes to `Base.Sub` |
||
| 183 | * and then `Base` only records `Base` as the change. |
||
| 184 | * |
||
| 185 | * E.g. |
||
| 186 | * [ |
||
| 187 | * 'Base' => true, |
||
| 188 | * 'Key' => [ |
||
| 189 | * 'Nested' => true, |
||
| 190 | * ], |
||
| 191 | * ] |
||
| 192 | * |
||
| 193 | * @var array |
||
| 194 | */ |
||
| 195 | protected $changedData = []; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get user agent for this request |
||
| 199 | * |
||
| 200 | * @param HTTPRequest $request |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | protected function userAgent(HTTPRequest $request) |
||
| 204 | { |
||
| 205 | return $request->getHeader('User-Agent'); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Start PHP session, then create a new Session object with the given start data. |
||
| 210 | * |
||
| 211 | * @param array|null|Session $data Can be an array of data (such as $_SESSION) or another Session object to clone. |
||
| 212 | * If null, this session is treated as unstarted. |
||
| 213 | */ |
||
| 214 | public function __construct($data) |
||
| 215 | { |
||
| 216 | if ($data instanceof Session) { |
||
| 217 | $data = $data->getAll(); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->data = $data; |
||
| 221 | $this->started = isset($data); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Init this session instance before usage, |
||
| 226 | * if a session identifier is part of the passed in request. |
||
| 227 | * Otherwise, a session might be started in {@link save()} |
||
| 228 | * if session data needs to be written with a new session identifier. |
||
| 229 | * |
||
| 230 | * @param HTTPRequest $request |
||
| 231 | */ |
||
| 232 | public function init(HTTPRequest $request) |
||
| 233 | { |
||
| 234 | if (!$this->isStarted() && $this->requestContainsSessionId($request)) { |
||
| 235 | $this->start($request); |
||
| 236 | } |
||
| 237 | |||
| 238 | // Funny business detected! |
||
| 239 | if (self::config()->get('strict_user_agent_check') && isset($this->data['HTTP_USER_AGENT'])) { |
||
| 240 | if ($this->data['HTTP_USER_AGENT'] !== $this->userAgent($request)) { |
||
| 241 | $this->clearAll(); |
||
| 242 | $this->restart($request); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Destroy existing session and restart |
||
| 249 | * |
||
| 250 | * @param HTTPRequest $request |
||
| 251 | */ |
||
| 252 | public function restart(HTTPRequest $request) |
||
| 253 | { |
||
| 254 | $this->destroy(true, $request); |
||
| 255 | $this->start($request); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Determine if this session has started |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | public function isStarted() |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param HTTPRequest $request |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | public function requestContainsSessionId(HTTPRequest $request) |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Begin session, regardless if a session identifier is present in the request, |
||
| 281 | * or whether any session data needs to be written. |
||
| 282 | * See {@link init()} if you want to "lazy start" a session. |
||
| 283 | * |
||
| 284 | * @param HTTPRequest $request The request for which to start a session |
||
| 285 | */ |
||
| 286 | public function start(HTTPRequest $request) |
||
| 287 | { |
||
| 288 | if ($this->isStarted()) { |
||
| 289 | throw new BadMethodCallException("Session has already started"); |
||
| 290 | } |
||
| 291 | |||
| 292 | $path = $this->config()->get('cookie_path'); |
||
| 293 | if (!$path) { |
||
| 294 | $path = Director::baseURL(); |
||
| 295 | } |
||
| 296 | $domain = $this->config()->get('cookie_domain'); |
||
| 297 | $session_path = $this->config()->get('session_store_path'); |
||
| 298 | $timeout = $this->config()->get('timeout'); |
||
| 299 | |||
| 300 | // Director::baseURL can return absolute domain names - this extracts the relevant parts |
||
| 301 | // for the session otherwise we can get broken session cookies |
||
| 302 | if (Director::is_absolute_url($path)) { |
||
| 303 | $urlParts = parse_url($path ?? ''); |
||
| 304 | $path = $urlParts['path']; |
||
| 305 | if (!$domain) { |
||
| 306 | $domain = $urlParts['host']; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | // If the session cookie is already set, then the session can be read even if headers_sent() = true |
||
| 311 | // This helps with edge-case such as debugging. |
||
| 312 | $data = []; |
||
| 313 | if (!session_id() && (!headers_sent() || $this->requestContainsSessionId($request))) { |
||
| 314 | if (!headers_sent()) { |
||
| 315 | $sameSite = static::config()->get('cookie_samesite'); |
||
| 316 | $secure = $this->isCookieSecure($sameSite, Director::is_https($request)); |
||
| 317 | session_set_cookie_params([ |
||
| 318 | 'lifetime' => $timeout ?: 0, |
||
| 319 | 'path' => $path, |
||
| 320 | 'domain' => $domain ?: null, |
||
| 321 | 'secure' => $secure, |
||
| 322 | 'httponly' => true, |
||
| 323 | 'samesite' => $sameSite, |
||
| 324 | ]); |
||
| 325 | |||
| 326 | $limiter = $this->config()->get('sessionCacheLimiter'); |
||
| 327 | if (isset($limiter)) { |
||
| 328 | session_cache_limiter($limiter); |
||
| 329 | } |
||
| 330 | |||
| 331 | // Allow storing the session in a non standard location |
||
| 332 | if ($session_path) { |
||
| 333 | session_save_path($session_path); |
||
| 334 | } |
||
| 335 | |||
| 336 | // If we want a secure cookie for HTTPS, use a separate session name. This lets us have a |
||
| 337 | // separate (less secure) session for non-HTTPS requests |
||
| 338 | // if headers_sent() is true then it's best to throw the resulting error rather than risk |
||
| 339 | // a security hole. |
||
| 340 | if ($secure) { |
||
| 341 | session_name($this->config()->get('cookie_name_secure')); |
||
| 342 | } |
||
| 343 | |||
| 344 | session_start(); |
||
| 345 | |||
| 346 | // Session start emits a cookie, but only if there's no existing session. If there is a session timeout |
||
| 347 | // tied to this request, make sure the session is held for the entire timeout by refreshing the cookie age. |
||
| 348 | if ($timeout && $this->requestContainsSessionId($request)) { |
||
| 349 | Cookie::set(session_name(), session_id(), $timeout / 86400, $path, $domain ?: null, $secure, true); |
||
| 350 | } |
||
| 351 | } else { |
||
| 352 | // If headers are sent then we can't have a session_cache_limiter otherwise we'll get a warning |
||
| 353 | session_cache_limiter(null); |
||
| 354 | } |
||
| 355 | |||
| 356 | if (isset($_SESSION)) { |
||
| 357 | // Initialise data from session store if present |
||
| 358 | $data = $_SESSION; |
||
| 359 | |||
| 360 | // Merge in existing in-memory data, taking priority over session store data |
||
| 361 | $this->recursivelyApply((array)$this->data, $data); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | // Save any modified session data back to the session store if present, otherwise initialise it to an array. |
||
| 366 | $this->data = $data; |
||
| 367 | |||
| 368 | $this->started = true; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Determines what the value for the `secure` cookie attribute should be. |
||
| 373 | */ |
||
| 374 | private function isCookieSecure(string $sameSite, bool $isHttps): bool |
||
| 375 | { |
||
| 376 | if ($sameSite === 'None') { |
||
| 377 | return true; |
||
| 378 | } |
||
| 379 | return $isHttps && $this->config()->get('cookie_secure'); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Destroy this session |
||
| 384 | * |
||
| 385 | * @param bool $removeCookie |
||
| 386 | * @param HTTPRequest $request The request for which to destroy a session |
||
| 387 | */ |
||
| 388 | public function destroy($removeCookie = true, HTTPRequest $request = null) |
||
| 389 | { |
||
| 390 | if (session_id()) { |
||
| 391 | if ($removeCookie) { |
||
| 392 | if (!$request) { |
||
| 393 | $request = Controller::curr()->getRequest(); |
||
| 394 | } |
||
| 395 | $path = $this->config()->get('cookie_path') ?: Director::baseURL(); |
||
| 396 | $domain = $this->config()->get('cookie_domain'); |
||
| 397 | $secure = Director::is_https($request) && $this->config()->get('cookie_secure'); |
||
| 398 | Cookie::force_expiry(session_name(), $path, $domain, $secure, true); |
||
| 399 | } |
||
| 400 | session_destroy(); |
||
| 401 | } |
||
| 402 | // Clean up the superglobal - session_destroy does not do it. |
||
| 403 | // http://nz1.php.net/manual/en/function.session-destroy.php |
||
| 404 | unset($_SESSION); |
||
| 405 | $this->data = null; |
||
| 406 | $this->started = false; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Set session value |
||
| 411 | * |
||
| 412 | * @param string $name |
||
| 413 | * @param mixed $val |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | public function set($name, $val) |
||
| 417 | { |
||
| 418 | $var = &$this->nestedValueRef($name, $this->data); |
||
| 419 | |||
| 420 | // Mark changed |
||
| 421 | if ($var !== $val) { |
||
| 422 | $var = $val; |
||
| 423 | $this->markChanged($name); |
||
| 424 | } |
||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Mark key as changed |
||
| 430 | * |
||
| 431 | * @internal |
||
| 432 | * @param string $name |
||
| 433 | */ |
||
| 434 | protected function markChanged($name) |
||
| 435 | { |
||
| 436 | $diffVar = &$this->changedData; |
||
| 437 | foreach (explode('.', $name ?? '') as $namePart) { |
||
| 438 | if (!isset($diffVar[$namePart])) { |
||
| 439 | $diffVar[$namePart] = []; |
||
| 440 | } |
||
| 441 | $diffVar = &$diffVar[$namePart]; |
||
| 442 | |||
| 443 | // Already diffed |
||
| 444 | if ($diffVar === true) { |
||
| 445 | return; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | // Mark changed |
||
| 449 | $diffVar = true; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Merge value with array |
||
| 454 | * |
||
| 455 | * @param string $name |
||
| 456 | * @param mixed $val |
||
| 457 | */ |
||
| 458 | public function addToArray($name, $val) |
||
| 459 | { |
||
| 460 | $names = explode('.', $name ?? ''); |
||
| 461 | |||
| 462 | // We still want to do this even if we have strict path checking for legacy code |
||
| 463 | $var = &$this->data; |
||
| 464 | $diffVar = &$this->changedData; |
||
| 465 | |||
| 466 | foreach ($names as $n) { |
||
| 467 | $var = &$var[$n]; |
||
| 468 | $diffVar = &$diffVar[$n]; |
||
| 469 | } |
||
| 470 | |||
| 471 | $var[] = $val; |
||
| 472 | $diffVar[sizeof($var) - 1] = $val; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Get session value |
||
| 477 | * |
||
| 478 | * @param string $name |
||
| 479 | * @return mixed |
||
| 480 | */ |
||
| 481 | public function get($name) |
||
| 482 | { |
||
| 483 | return $this->nestedValue($name, $this->data); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Clear session value |
||
| 488 | * |
||
| 489 | * @param string $name |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | public function clear($name) |
||
| 493 | { |
||
| 494 | // Get var by path |
||
| 495 | $var = $this->nestedValue($name, $this->data); |
||
| 496 | |||
| 497 | // Unset var |
||
| 498 | if ($var !== null) { |
||
| 499 | // Unset parent key |
||
| 500 | $parentParts = explode('.', $name ?? ''); |
||
| 501 | $basePart = array_pop($parentParts); |
||
| 502 | if ($parentParts) { |
||
| 503 | $parent = &$this->nestedValueRef(implode('.', $parentParts), $this->data); |
||
| 504 | unset($parent[$basePart]); |
||
| 505 | } else { |
||
| 506 | unset($this->data[$name]); |
||
| 507 | } |
||
| 508 | $this->markChanged($name); |
||
| 509 | } |
||
| 510 | return $this; |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Clear all values |
||
| 515 | */ |
||
| 516 | public function clearAll() |
||
| 517 | { |
||
| 518 | if ($this->data && is_array($this->data)) { |
||
| 519 | foreach (array_keys($this->data ?? []) as $key) { |
||
| 520 | $this->clear($key); |
||
| 521 | } |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get all values |
||
| 527 | * |
||
| 528 | * @return array|null |
||
| 529 | */ |
||
| 530 | public function getAll() |
||
| 531 | { |
||
| 532 | return $this->data; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Set user agent key |
||
| 537 | * |
||
| 538 | * @param HTTPRequest $request |
||
| 539 | */ |
||
| 540 | public function finalize(HTTPRequest $request) |
||
| 541 | { |
||
| 542 | $this->set('HTTP_USER_AGENT', $this->userAgent($request)); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Save data to session |
||
| 547 | * Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned. |
||
| 548 | * |
||
| 549 | * @param HTTPRequest $request |
||
| 550 | */ |
||
| 551 | public function save(HTTPRequest $request) |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Recursively apply the changes represented in $data to $dest. |
||
| 567 | * Used to update $_SESSION |
||
| 568 | * |
||
| 569 | * @deprecated 4.1.0:5.0.0 Use recursivelyApplyChanges() instead |
||
| 570 | * @param array $data |
||
| 571 | * @param array $dest |
||
| 572 | */ |
||
| 573 | protected function recursivelyApply($data, &$dest) |
||
| 574 | { |
||
| 575 | Deprecation::notice('5.0', 'Use recursivelyApplyChanges() instead'); |
||
| 576 | foreach ($data as $k => $v) { |
||
| 577 | if (is_array($v)) { |
||
| 578 | if (!isset($dest[$k]) || !is_array($dest[$k])) { |
||
| 579 | $dest[$k] = []; |
||
| 580 | } |
||
| 581 | $this->recursivelyApply($v, $dest[$k]); |
||
| 582 | } else { |
||
| 583 | $dest[$k] = $v; |
||
| 584 | } |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Returns the list of changed keys |
||
| 590 | * |
||
| 591 | * @return array |
||
| 592 | */ |
||
| 593 | public function changedData() |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Navigate to nested value in source array by name, |
||
| 600 | * creating a null placeholder if it doesn't exist. |
||
| 601 | * |
||
| 602 | * @internal |
||
| 603 | * @param string $name |
||
| 604 | * @param array $source |
||
| 605 | * @return mixed Reference to value in $source |
||
| 606 | */ |
||
| 607 | protected function &nestedValueRef($name, &$source) |
||
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Navigate to nested value in source array by name, |
||
| 625 | * returning null if it doesn't exist. |
||
| 626 | * |
||
| 627 | * @internal |
||
| 628 | * @param string $name |
||
| 629 | * @param array $source |
||
| 630 | * @return mixed Value in array in $source |
||
| 631 | */ |
||
| 632 | protected function nestedValue($name, $source) |
||
| 633 | { |
||
| 634 | // Find var to change |
||
| 635 | $var = $source; |
||
| 636 | foreach (explode('.', $name ?? '') as $namePart) { |
||
| 637 | if (!isset($var[$namePart])) { |
||
| 638 | return null; |
||
| 639 | } |
||
| 640 | $var = $var[$namePart]; |
||
| 641 | } |
||
| 642 | return $var; |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Apply all changes using separate keys and data sources and a destination |
||
| 647 | * |
||
| 648 | * @internal |
||
| 649 | * @param array $changes |
||
| 650 | * @param array $source |
||
| 651 | * @param array $destination |
||
| 652 | */ |
||
| 653 | protected function recursivelyApplyChanges($changes, $source, &$destination) |
||
| 669 | } |
||
| 670 | } |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Regenerate session id |
||
| 675 | * |
||
| 676 | * @internal This is for internal use only. Isn't a part of public API. |
||
| 677 | */ |
||
| 678 | public function regenerateSessionId() |
||
| 682 | } |
||
| 683 | } |
||
| 684 | } |
||
| 685 |