| Total Complexity | 129 |
| Total Lines | 954 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 29 | class Session implements \ArrayAccess, \IteratorAggregate, LoggerAwareInterface |
||
| 30 | {
|
||
| 31 | /** |
||
| 32 | * Session Config |
||
| 33 | * |
||
| 34 | * @var Kernel\DataStructures\Config |
||
| 35 | */ |
||
| 36 | protected $config; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Logger Instance |
||
| 40 | * |
||
| 41 | * @var LoggerInterface |
||
| 42 | */ |
||
| 43 | protected $logger; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Session Cache Platform Handler |
||
| 47 | * |
||
| 48 | * @var AbstractHandler |
||
| 49 | */ |
||
| 50 | protected $handler; |
||
| 51 | |||
| 52 | protected $sidRegexp; |
||
| 53 | |||
| 54 | // ------------------------------------------------------------------------ |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Session::__construct |
||
| 58 | * |
||
| 59 | * @param Kernel\DataStructures\Config $config |
||
| 60 | * |
||
| 61 | * @return Session |
||
| 62 | */ |
||
| 63 | public function __construct(Kernel\DataStructures\Config $config) |
||
| 64 | {
|
||
| 65 | language() |
||
| 66 | ->addFilePath(__DIR__ . DIRECTORY_SEPARATOR) |
||
| 67 | ->loadFile('session');
|
||
| 68 | |||
| 69 | $this->config = $config; |
||
| 70 | |||
| 71 | if ($this->config->offsetExists('handler')) {
|
||
| 72 | $handlerClassName = '\O2System\Session\Handlers\\' . ucfirst($this->config->handler) . 'Handler'; |
||
| 73 | |||
| 74 | if (class_exists($handlerClassName)) {
|
||
| 75 | $this->handler = new $handlerClassName(clone $this->config); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | // ------------------------------------------------------------------------ |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Session::isSupported |
||
| 84 | * |
||
| 85 | * Checks if server is support cache storage platform. |
||
| 86 | * |
||
| 87 | * @param string $platform Platform name. |
||
| 88 | * |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public static function isSupported($platform) |
||
| 92 | {
|
||
| 93 | $handlerClassName = '\O2System\Session\Handlers\\' . ucfirst($platform) . 'Handler'; |
||
| 94 | |||
| 95 | if (class_exists($handlerClassName)) {
|
||
| 96 | return (new $handlerClassName)->isSupported(); |
||
| 97 | } |
||
| 98 | |||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | // ------------------------------------------------------------------------ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Session::setLogger |
||
| 106 | * |
||
| 107 | * Sets a logger instance on the object |
||
| 108 | * |
||
| 109 | * @param LoggerInterface $logger |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | public function setLogger(LoggerInterface $logger) |
||
| 114 | {
|
||
| 115 | $this->logger =& $logger; |
||
| 116 | |||
| 117 | // Load Session Language |
||
| 118 | language()->loadFile('session');
|
||
| 119 | |||
| 120 | if (isset($this->handler)) {
|
||
| 121 | $this->handler->setLogger($this->logger); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // ------------------------------------------------------------------------ |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Session::start |
||
| 129 | * |
||
| 130 | * Initialize Native PHP Session. |
||
| 131 | * |
||
| 132 | * @return void |
||
| 133 | */ |
||
| 134 | public function start() |
||
| 135 | {
|
||
| 136 | if (php_sapi_name() === 'cli') {
|
||
| 137 | if ($this->logger instanceof LoggerInterface) {
|
||
|
|
|||
| 138 | $this->logger->debug('DEBUG_SESSION_CLI_ABORTED');
|
||
| 139 | } |
||
| 140 | |||
| 141 | return; |
||
| 142 | } elseif ((bool)ini_get('session.auto_start')) {
|
||
| 143 | if ($this->logger instanceof LoggerInterface) {
|
||
| 144 | $this->logger->error('DEBUG_SESSION_AUTO_START_ABORTED');
|
||
| 145 | } |
||
| 146 | |||
| 147 | return; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ( ! $this->handler instanceof \SessionHandlerInterface) {
|
||
| 151 | $this->logger->error('E_SESSION_HANDLER_INTERFACE', [$this->handler->getPlatform()]);
|
||
| 152 | } |
||
| 153 | |||
| 154 | $this->setConfiguration(); |
||
| 155 | |||
| 156 | session_set_save_handler($this->handler, true); |
||
| 157 | |||
| 158 | // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers |
||
| 159 | if (isset($_COOKIE[ $this->config[ 'name' ] ]) && ( |
||
| 160 | ! is_string($_COOKIE[ $this->config[ 'name' ] ]) || ! preg_match('#\A' . $this->sidRegexp . '\z#',
|
||
| 161 | $_COOKIE[ $this->config[ 'name' ] ]) |
||
| 162 | ) |
||
| 163 | ) {
|
||
| 164 | unset($_COOKIE[ $this->config[ 'name' ] ]); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * If throwing error |
||
| 169 | * session_start(): Failed to initialize storage module: user (path: ) |
||
| 170 | * |
||
| 171 | * Change the php.ini |
||
| 172 | * session.save_path = "N;/path" **to** session.save_path = "/tmp" |
||
| 173 | */ |
||
| 174 | session_start(); |
||
| 175 | |||
| 176 | $this->initializeVariables(); |
||
| 177 | |||
| 178 | // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers |
||
| 179 | if (isset($_COOKIE[ $this->config[ 'name' ] ]) && ( |
||
| 180 | ! is_string($this->config[ 'name' ]) || |
||
| 181 | ! preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[ $this->config[ 'name' ] ]
|
||
| 182 | ) |
||
| 183 | ) |
||
| 184 | ) {
|
||
| 185 | unset($_COOKIE[ $this->config[ 'name' ] ]); |
||
| 186 | } |
||
| 187 | |||
| 188 | // Is session ID auto-regeneration configured? (ignoring ajax requests) |
||
| 189 | if ((empty($_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) || |
||
| 190 | strtolower($_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) !== 'xmlhttprequest') && |
||
| 191 | ($regenerateTime = $this->config[ 'regenerate' ]->lifetime) > 0 |
||
| 192 | ) {
|
||
| 193 | if ( ! isset($_SESSION[ 'last_regenerate' ])) {
|
||
| 194 | $_SESSION[ 'last_regenerate' ] = time(); |
||
| 195 | } elseif ($_SESSION[ 'last_regenerate' ] < (time() - $regenerateTime)) {
|
||
| 196 | $this->regenerate(); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | // Another work-around ... PHP doesn't seem to send the session cookie |
||
| 200 | // unless it is being currently created or regenerated |
||
| 201 | elseif (isset($_COOKIE[ $this->config[ 'name' ] ]) && $_COOKIE[ $this->config[ 'name' ] ] === session_id() |
||
| 202 | ) {
|
||
| 203 | setcookie( |
||
| 204 | $this->config[ 'name' ], |
||
| 205 | session_id(), |
||
| 206 | (empty($this->config[ 'lifetime' ]) ? 0 : time() + $this->config[ 'lifetime' ]), |
||
| 207 | $this->config[ 'cookie' ]->path, |
||
| 208 | '.' . ltrim($this->config[ 'cookie' ]->domain, '.'), |
||
| 209 | $this->config[ 'cookie' ]->secure, |
||
| 210 | true |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($this->logger instanceof LoggerInterface) {
|
||
| 215 | $this->logger->debug('DEBUG_SESSION_INITIALIZED', [$this->handler->getPlatform()]);
|
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | //-------------------------------------------------------------------- |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Session::setConfiguration |
||
| 223 | * |
||
| 224 | * Handle input binds and configuration defaults. |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | private function setConfiguration() |
||
| 229 | {
|
||
| 230 | ini_set('session.name', $this->config[ 'name' ]);
|
||
| 231 | |||
| 232 | if (empty($this->config[ 'lifetime' ])) {
|
||
| 233 | $this->config[ 'lifetime' ] = (int)ini_get('session.gc_maxlifetime');
|
||
| 234 | } else {
|
||
| 235 | ini_set('session.gc_maxlifetime', (int)$this->config[ 'lifetime' ]);
|
||
| 236 | } |
||
| 237 | |||
| 238 | if (empty($this->config[ 'cookie' ]->domain)) {
|
||
| 239 | $this->config[ 'cookie' ]->domain = (isset($_SERVER[ 'HTTP_HOST' ]) ? $_SERVER[ 'HTTP_HOST' ] |
||
| 240 | : (isset($_SERVER[ 'SERVER_NAME' ]) ? $_SERVER[ 'SERVER_NAME' ] : null)); |
||
| 241 | } |
||
| 242 | |||
| 243 | $this->config[ 'cookie' ]->domain = ltrim($this->config[ 'cookie' ]->domain, '.'); |
||
| 244 | |||
| 245 | ini_set('session.cookie_domain', '.' . $this->config[ 'cookie' ]->domain);
|
||
| 246 | ini_set('session.cookie_path', $this->config[ 'cookie' ]->path);
|
||
| 247 | |||
| 248 | // Security is king |
||
| 249 | ini_set('session.cookie_lifetime', 0);
|
||
| 250 | ini_set('session.use_cookies', 'On');
|
||
| 251 | ini_set('session.use_only_cookies', 'On');
|
||
| 252 | ini_set('session.use_strict_mode', 'On');
|
||
| 253 | ini_set('session.cookie_httponly', 'On');
|
||
| 254 | ini_set('ssession.cookie_secure', (is_https() ? 'On' : 'Off'));
|
||
| 255 | ini_set('session.use_trans_sid', 'Off');
|
||
| 256 | |||
| 257 | $this->configureSidLength(); |
||
| 258 | } |
||
| 259 | |||
| 260 | //-------------------------------------------------------------------- |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Configure session ID length |
||
| 264 | * |
||
| 265 | * To make life easier, we used to force SHA-1 and 4 bits per |
||
| 266 | * character on everyone. And of course, someone was unhappy. |
||
| 267 | * |
||
| 268 | * Then PHP 7.1 broke backwards-compatibility because ext/session |
||
| 269 | * is such a mess that nobody wants to touch it with a pole stick, |
||
| 270 | * and the one guy who does, nobody has the energy to argue with. |
||
| 271 | * |
||
| 272 | * So we were forced to make changes, and OF COURSE something was |
||
| 273 | * going to break and now we have this pile of shit. -- Narf |
||
| 274 | * |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | protected function configureSidLength() |
||
| 278 | {
|
||
| 279 | if (PHP_VERSION_ID < 70100) {
|
||
| 280 | $bits = 160; |
||
| 281 | $hash_function = ini_get('session.hash_function');
|
||
| 282 | if (ctype_digit($hash_function)) {
|
||
| 283 | if ($hash_function !== '1') {
|
||
| 284 | ini_set('session.hash_function', 1);
|
||
| 285 | $bits = 160; |
||
| 286 | } |
||
| 287 | } elseif ( ! in_array($hash_function, hash_algos(), true)) {
|
||
| 288 | ini_set('session.hash_function', 1);
|
||
| 289 | $bits = 160; |
||
| 290 | } elseif (($bits = strlen(hash($hash_function, 'dummy', false)) * 4) < 160) {
|
||
| 291 | ini_set('session.hash_function', 1);
|
||
| 292 | $bits = 160; |
||
| 293 | } |
||
| 294 | $bits_per_character = (int)ini_get('session.hash_bits_per_character');
|
||
| 295 | $sid_length = (int)ceil($bits / $bits_per_character); |
||
| 296 | } else {
|
||
| 297 | $bits_per_character = (int)ini_get('session.sid_bits_per_character');
|
||
| 298 | $sid_length = (int)ini_get('session.sid_length');
|
||
| 299 | if (($sid_length * $bits_per_character) < 160) {
|
||
| 300 | $bits = ($sid_length * $bits_per_character); |
||
| 301 | // Add as many more characters as necessary to reach at least 160 bits |
||
| 302 | $sid_length += (int)ceil((160 % $bits) / $bits_per_character); |
||
| 303 | ini_set('session.sid_length', $sid_length);
|
||
| 304 | } |
||
| 305 | } |
||
| 306 | // Yes, 4,5,6 are the only known possible values as of 2016-10-27 |
||
| 307 | switch ($bits_per_character) {
|
||
| 308 | case 4: |
||
| 309 | $this->sidRegexp = '[0-9a-f]'; |
||
| 310 | break; |
||
| 311 | case 5: |
||
| 312 | $this->sidRegexp = '[0-9a-v]'; |
||
| 313 | break; |
||
| 314 | case 6: |
||
| 315 | $this->sidRegexp = '[0-9a-zA-Z,-]'; |
||
| 316 | break; |
||
| 317 | } |
||
| 318 | $this->sidRegexp .= '{' . $sid_length . '}';
|
||
| 319 | } |
||
| 320 | |||
| 321 | //-------------------------------------------------------------------- |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Session::initializeVariables |
||
| 325 | * |
||
| 326 | * Handle flash and temporary session variables. Clears old "flash" session variables, |
||
| 327 | * marks the new one for deletion and handles "temp" session variables deletion. |
||
| 328 | * |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | private function initializeVariables() |
||
| 332 | {
|
||
| 333 | if (empty($_SESSION[ 'system_variables' ])) {
|
||
| 334 | return; |
||
| 335 | } |
||
| 336 | |||
| 337 | $currentTime = time(); |
||
| 338 | |||
| 339 | foreach ($_SESSION[ 'system_variables' ] as $key => &$value) {
|
||
| 340 | if ($value === 'new') {
|
||
| 341 | $_SESSION[ 'system_variables' ][ $key ] = 'old'; |
||
| 342 | } |
||
| 343 | // Hacky, but 'old' will (implicitly) always be less than time() ;) |
||
| 344 | // DO NOT move this above the 'new' check! |
||
| 345 | elseif ($value < $currentTime) {
|
||
| 346 | unset($_SESSION[ $key ], $_SESSION[ 'system_variables' ][ $key ]); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | if (empty($_SESSION[ 'system_variables' ])) {
|
||
| 351 | unset($_SESSION[ 'system_variables' ]); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Session::regenerate |
||
| 357 | * |
||
| 358 | * Regenerates the session ID |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function regenerate() |
||
| 363 | {
|
||
| 364 | $_SESSION[ 'last_regenerate' ] = time(); |
||
| 365 | session_regenerate_id($this->config[ 'regenerate' ]->destroy); |
||
| 366 | } |
||
| 367 | |||
| 368 | //-------------------------------------------------------------------- |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Session::isStarted |
||
| 372 | * |
||
| 373 | * Check if the PHP Session is has been started. |
||
| 374 | * |
||
| 375 | * @access public |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | public function isStarted() |
||
| 379 | {
|
||
| 380 | if (php_sapi_name() !== 'cli') {
|
||
| 381 | return session_status() === PHP_SESSION_ACTIVE ? true : false; |
||
| 382 | } |
||
| 383 | |||
| 384 | return false; |
||
| 385 | } |
||
| 386 | |||
| 387 | //-------------------------------------------------------------------- |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Does a full stop of the session: |
||
| 391 | * |
||
| 392 | * - destroys the session |
||
| 393 | * - unsets the session id |
||
| 394 | * - destroys the session cookie |
||
| 395 | */ |
||
| 396 | public function stop() |
||
| 397 | {
|
||
| 398 | setcookie( |
||
| 399 | $this->config[ 'name' ], |
||
| 400 | session_id(), |
||
| 401 | 1, |
||
| 402 | $this->config[ 'cookie' ]->path, |
||
| 403 | '.' . ltrim($this->config[ 'cookie' ]->domain, '.'), |
||
| 404 | $this->config[ 'cookie' ]->secure, |
||
| 405 | true |
||
| 406 | ); |
||
| 407 | |||
| 408 | session_regenerate_id(true); |
||
| 409 | } |
||
| 410 | |||
| 411 | // ------------------------------------------------------------------------ |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Session::destroy |
||
| 415 | * |
||
| 416 | * Destroys the current session. |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | public function destroy() |
||
| 421 | {
|
||
| 422 | session_destroy(); |
||
| 423 | } |
||
| 424 | |||
| 425 | // ------------------------------------------------------------------------ |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Session::__isset |
||
| 429 | * |
||
| 430 | * Implementing magic method __isset to simplify when checks if offset exists on PHP native session variable, |
||
| 431 | * just simply calling isset( $session[ 'offset' ] ). |
||
| 432 | * |
||
| 433 | * @param mixed $offset PHP native session offset. |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | public function has($offset) |
||
| 438 | {
|
||
| 439 | return $this->offsetExists($offset); |
||
| 440 | } |
||
| 441 | |||
| 442 | // ------------------------------------------------------------------------ |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Session::offsetExists |
||
| 446 | * |
||
| 447 | * Checks if offset exists on PHP native session variable. |
||
| 448 | * |
||
| 449 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 450 | * |
||
| 451 | * @param mixed $offset <p> |
||
| 452 | * An offset to check for. |
||
| 453 | * </p> |
||
| 454 | * |
||
| 455 | * @return boolean true on success or false on failure. |
||
| 456 | * </p> |
||
| 457 | * <p> |
||
| 458 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 459 | * @since 5.0.0 |
||
| 460 | */ |
||
| 461 | public function offsetExists($offset) |
||
| 462 | {
|
||
| 463 | return (bool)isset($_SESSION[ $offset ]); |
||
| 464 | } |
||
| 465 | |||
| 466 | // ------------------------------------------------------------------------ |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Session::__isset |
||
| 470 | * |
||
| 471 | * Implementing magic method __isset to simplify when checks if offset exists on PHP native session variable, |
||
| 472 | * just simply calling isset( $session[ 'offset' ] ). |
||
| 473 | * |
||
| 474 | * @param mixed $offset PHP native session offset. |
||
| 475 | * |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | public function __isset($offset) |
||
| 479 | {
|
||
| 480 | return $this->offsetExists($offset); |
||
| 481 | } |
||
| 482 | |||
| 483 | // ------------------------------------------------------------------------ |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Session::__get |
||
| 487 | * |
||
| 488 | * Implementing magic method __get to simplify gets PHP native session variable by requested offset, |
||
| 489 | * just simply calling isset( $session[ 'offset' ] ). |
||
| 490 | * |
||
| 491 | * @param $offset |
||
| 492 | * |
||
| 493 | * @return mixed |
||
| 494 | */ |
||
| 495 | public function &__get($offset) |
||
| 496 | {
|
||
| 497 | if ($offset === 'id') {
|
||
| 498 | $_SESSION[ 'id' ] = session_id(); |
||
| 499 | } |
||
| 500 | |||
| 501 | if ( ! isset($_SESSION[ $offset ])) {
|
||
| 502 | $_SESSION[ $offset ] = null; |
||
| 503 | } |
||
| 504 | |||
| 505 | return $_SESSION[ $offset ]; |
||
| 506 | } |
||
| 507 | |||
| 508 | // ------------------------------------------------------------------------ |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Session::__set |
||
| 512 | * |
||
| 513 | * Implementing magic method __set to simplify set PHP native session variable, |
||
| 514 | * just simply calling $session->offset = 'foo'. |
||
| 515 | * |
||
| 516 | * @param mixed $offset PHP native session offset. |
||
| 517 | * @param mixed $value PHP native session offset value to set. |
||
| 518 | */ |
||
| 519 | public function __set($offset, $value) |
||
| 520 | {
|
||
| 521 | $this->offsetSet($offset, $value); |
||
| 522 | } |
||
| 523 | |||
| 524 | // ------------------------------------------------------------------------ |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Session::offsetSet |
||
| 528 | * |
||
| 529 | * Sets session data into PHP native session global variable. |
||
| 530 | * |
||
| 531 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 532 | * |
||
| 533 | * @param mixed $offset <p> |
||
| 534 | * The offset to assign the value to. |
||
| 535 | * </p> |
||
| 536 | * @param mixed $value <p> |
||
| 537 | * The value to set. |
||
| 538 | * </p> |
||
| 539 | * |
||
| 540 | * @return void |
||
| 541 | * @since 5.0.0 |
||
| 542 | */ |
||
| 543 | public function offsetSet($offset, $value) |
||
| 544 | {
|
||
| 545 | $_SESSION[ $offset ] =& $value; |
||
| 546 | } |
||
| 547 | |||
| 548 | // ------------------------------------------------------------------------ |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Session::__unset |
||
| 552 | * |
||
| 553 | * Implementing magic method __unset to simplify unset method, just simply calling |
||
| 554 | * unset( $session[ 'offset' ] ). |
||
| 555 | * |
||
| 556 | * @param mixed $offset PHP Native session offset |
||
| 557 | * |
||
| 558 | * @return void |
||
| 559 | */ |
||
| 560 | public function __unset($offset) |
||
| 561 | {
|
||
| 562 | $this->offsetUnset($offset); |
||
| 563 | } |
||
| 564 | |||
| 565 | // ------------------------------------------------------------------------ |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Session::offsetUnset |
||
| 569 | * |
||
| 570 | * Remove session data from PHP native session global variable. |
||
| 571 | * |
||
| 572 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 573 | * |
||
| 574 | * @param mixed $offset <p> |
||
| 575 | * The offset to unset. |
||
| 576 | * </p> |
||
| 577 | * |
||
| 578 | * @return void |
||
| 579 | * @since 5.0.0 |
||
| 580 | */ |
||
| 581 | public function offsetUnset($offset) |
||
| 582 | {
|
||
| 583 | if (isset($_SESSION[ $offset ])) {
|
||
| 584 | unset($_SESSION[ $offset ]); |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | // ------------------------------------------------------------------------ |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Session::getIterator |
||
| 592 | * |
||
| 593 | * Retrieve an external iterator |
||
| 594 | * |
||
| 595 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 596 | * @return Traversable An instance of an object implementing <b>Iterator</b> or |
||
| 597 | * <b>Traversable</b> |
||
| 598 | * @since 5.0.0 |
||
| 599 | */ |
||
| 600 | public function getIterator() |
||
| 601 | {
|
||
| 602 | return new ArrayIterator($_SESSION); |
||
| 603 | } |
||
| 604 | |||
| 605 | //-------------------------------------------------------------------- |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Session::setFlash |
||
| 609 | * |
||
| 610 | * Sets flash data into the session that will only last for a single request. |
||
| 611 | * Perfect for use with single-use status update messages. |
||
| 612 | * |
||
| 613 | * If $offset is an array, it is interpreted as an associative array of |
||
| 614 | * key/value pairs for flash session variables. |
||
| 615 | * Otherwise, it is interpreted as the identifier of a specific |
||
| 616 | * flash session variable, with $value containing the property value. |
||
| 617 | * |
||
| 618 | * @param mixed $offset Flash session variable string offset identifier or associative array of values. |
||
| 619 | * @param mixed|null $value Flash session variable offset value. |
||
| 620 | */ |
||
| 621 | public function setFlash($offset, $value = null) |
||
| 622 | {
|
||
| 623 | $this->set($offset, $value); |
||
| 624 | $this->markFlash(is_array($offset) ? array_keys($offset) : $offset); |
||
| 625 | } |
||
| 626 | |||
| 627 | //-------------------------------------------------------------------- |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Session::set |
||
| 631 | * |
||
| 632 | * Sets session data into PHP native session global variable. |
||
| 633 | * |
||
| 634 | * If $offset is a string, then it is interpreted as a session property |
||
| 635 | * key, and $value is expected to be non-null. |
||
| 636 | * |
||
| 637 | * If $offset is an array, it is expected to be an array of key/value pairs |
||
| 638 | * to be set as session values. |
||
| 639 | * |
||
| 640 | * @param string $offset Session offset or associative array of session values |
||
| 641 | * @param mixed $value Session offset value. |
||
| 642 | */ |
||
| 643 | public function set($offset, $value = null) |
||
| 644 | {
|
||
| 645 | if (is_array($offset)) {
|
||
| 646 | foreach ($offset as $key => &$value) {
|
||
| 647 | $_SESSION[ $key ] = $value; |
||
| 648 | } |
||
| 649 | |||
| 650 | return; |
||
| 651 | } |
||
| 652 | |||
| 653 | $_SESSION[ $offset ] =& $value; |
||
| 654 | } |
||
| 655 | |||
| 656 | //-------------------------------------------------------------------- |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Session::markAsFlash |
||
| 660 | * |
||
| 661 | * Mark an session offset variables as flash session variables. |
||
| 662 | * |
||
| 663 | * @param string|array $offset Flash session variable string offset identifier or array of offsets. |
||
| 664 | * |
||
| 665 | * @return bool Returns FALSE if any flash session variables are not already set. |
||
| 666 | */ |
||
| 667 | public function markFlash($offset) |
||
| 668 | {
|
||
| 669 | if (is_array($offset)) {
|
||
| 670 | for ($i = 0, $c = count($offset); $i < $c; $i++) {
|
||
| 671 | if ( ! isset($_SESSION[ $offset[ $i ] ])) {
|
||
| 672 | return false; |
||
| 673 | } |
||
| 674 | } |
||
| 675 | |||
| 676 | $new = array_fill_keys($offset, 'new'); |
||
| 677 | |||
| 678 | $_SESSION[ 'system_variables' ] = isset($_SESSION[ 'system_variables' ]) ? array_merge( |
||
| 679 | $_SESSION[ 'system_variables' ], |
||
| 680 | $new |
||
| 681 | ) : $new; |
||
| 682 | |||
| 683 | return true; |
||
| 684 | } |
||
| 685 | |||
| 686 | if ( ! isset($_SESSION[ $offset ])) {
|
||
| 687 | return false; |
||
| 688 | } |
||
| 689 | |||
| 690 | $_SESSION[ 'system_variables' ][ $offset ] = 'new'; |
||
| 691 | |||
| 692 | return true; |
||
| 693 | } |
||
| 694 | |||
| 695 | // ------------------------------------------------------------------------ |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Session::get |
||
| 699 | * |
||
| 700 | * @param string $offset Session offset or associative array of session values |
||
| 701 | * |
||
| 702 | * @return mixed |
||
| 703 | */ |
||
| 704 | public function get($offset) |
||
| 705 | {
|
||
| 706 | return $this->offsetGet($offset); |
||
| 707 | } |
||
| 708 | |||
| 709 | //-------------------------------------------------------------------- |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Session::offsetGet |
||
| 713 | * |
||
| 714 | * Gets PHP native session variable value by requested offset. |
||
| 715 | * |
||
| 716 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 717 | * |
||
| 718 | * @param mixed $offset <p> |
||
| 719 | * The offset to retrieve. |
||
| 720 | * </p> |
||
| 721 | * |
||
| 722 | * @return mixed Can return all value types. |
||
| 723 | * @since 5.0.0 |
||
| 724 | */ |
||
| 725 | public function offsetGet($offset) |
||
| 726 | {
|
||
| 727 | if ($offset === 'id') {
|
||
| 728 | $_SESSION[ 'id' ] = session_id(); |
||
| 729 | } |
||
| 730 | |||
| 731 | return (isset($_SESSION[ $offset ])) ? $_SESSION[ $offset ] : false; |
||
| 732 | } |
||
| 733 | |||
| 734 | //-------------------------------------------------------------------- |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Session::getFlash |
||
| 738 | * |
||
| 739 | * Retrieve one or more items of flash data from the session. |
||
| 740 | * If the offset is null, it will returns all flash session variables. |
||
| 741 | * |
||
| 742 | * @param string $offset Flash session variable string offset identifier |
||
| 743 | * |
||
| 744 | * @return array|null The requested property value, or an assoo2systemative array of them |
||
| 745 | */ |
||
| 746 | public function getFlash($offset = null) |
||
| 747 | {
|
||
| 748 | if (isset($offset)) {
|
||
| 749 | return (isset($_SESSION[ 'system_variables' ], $_SESSION[ 'system_variables' ][ $offset ], $_SESSION[ $offset ]) && |
||
| 750 | ! is_int($_SESSION[ 'system_variables' ][ $offset ])) ? $_SESSION[ $offset ] : null; |
||
| 751 | } |
||
| 752 | |||
| 753 | $flashVariables = []; |
||
| 754 | |||
| 755 | if ( ! empty($_SESSION[ 'system_variables' ])) {
|
||
| 756 | foreach ($_SESSION[ 'system_variables' ] as $offset => &$value) {
|
||
| 757 | is_int($value) OR $flashVariables[ $offset ] = $_SESSION[ $offset ]; |
||
| 758 | } |
||
| 759 | } |
||
| 760 | |||
| 761 | return $flashVariables; |
||
| 762 | } |
||
| 763 | |||
| 764 | //-------------------------------------------------------------------- |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Session::keepFlash |
||
| 768 | * |
||
| 769 | * Keeps a single piece of flash data alive for one more request. |
||
| 770 | * |
||
| 771 | * @param string|array $offset Flash session variable string offset identifier or array of offsets. |
||
| 772 | */ |
||
| 773 | public function keepFlash($offset) |
||
| 774 | {
|
||
| 775 | $this->markFlash($offset); |
||
| 776 | } |
||
| 777 | |||
| 778 | //-------------------------------------------------------------------- |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Session::unsetFlash |
||
| 782 | * |
||
| 783 | * Unset flash session variables. |
||
| 784 | * |
||
| 785 | * @param string $offset Flash session variable string offset identifier or array of offsets. |
||
| 786 | */ |
||
| 787 | public function unsetFlash($offset) |
||
| 806 | } |
||
| 807 | } |
||
| 808 | |||
| 809 | //-------------------------------------------------------------------- |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Session::getFlashOffsets |
||
| 813 | * |
||
| 814 | * Gets all flash session variable offsets. |
||
| 815 | * |
||
| 816 | * @return array Returns array of flash session variable offsets. |
||
| 817 | */ |
||
| 818 | public function getFlashOffsets() |
||
| 819 | {
|
||
| 820 | if ( ! isset($_SESSION[ 'system_variables' ])) {
|
||
| 821 | return []; |
||
| 822 | } |
||
| 823 | |||
| 824 | $offsets = []; |
||
| 825 | foreach (array_keys($_SESSION[ 'system_variables' ]) as $offset) {
|
||
| 826 | is_int($_SESSION[ 'system_variables' ][ $offset ]) OR $offsets[] = $offset; |
||
| 827 | } |
||
| 828 | |||
| 829 | return $offsets; |
||
| 830 | } |
||
| 831 | |||
| 832 | //-------------------------------------------------------------------- |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Session::setTemp |
||
| 836 | * |
||
| 837 | * Sets temporary session variable. |
||
| 838 | * |
||
| 839 | * @param mixed $offset Temporary session variable string offset identifier or array of offsets. |
||
| 840 | * @param null $value Temporary session variable offset value. |
||
| 841 | * @param int $ttl Temporary session variable Time-to-live in seconds |
||
| 842 | */ |
||
| 843 | public function setTemp($offset, $value = null, $ttl = 300) |
||
| 847 | } |
||
| 848 | |||
| 849 | //-------------------------------------------------------------------- |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Session::markTemp |
||
| 853 | * |
||
| 854 | * Mark one of more pieces of data as being temporary, meaning that |
||
| 855 | * it has a set lifespan within the session. |
||
| 856 | * |
||
| 857 | * @param string $offset Temporary session variable string offset identifier. |
||
| 858 | * @param int $ttl Temporary session variable Time-to-live, in seconds |
||
| 859 | * |
||
| 860 | * @return bool Returns FALSE if none temporary session variable is set. |
||
| 861 | */ |
||
| 862 | public function markTemp($offset, $ttl = 300) |
||
| 863 | {
|
||
| 864 | $ttl += time(); |
||
| 865 | |||
| 866 | if (is_array($offset)) {
|
||
| 867 | $temp = []; |
||
| 868 | |||
| 869 | foreach ($offset as $key => $value) {
|
||
| 870 | // Do we have a key => ttl pair, or just a key? |
||
| 871 | if (is_int($key)) {
|
||
| 872 | $key = $value; |
||
| 873 | $value = $ttl; |
||
| 874 | } else {
|
||
| 875 | $value += time(); |
||
| 876 | } |
||
| 877 | |||
| 878 | if ( ! isset($_SESSION[ $key ])) {
|
||
| 879 | return false; |
||
| 880 | } |
||
| 881 | |||
| 882 | $temp[ $key ] = $value; |
||
| 883 | } |
||
| 884 | |||
| 885 | $_SESSION[ 'system_variables' ] = isset($_SESSION[ 'system_variables' ]) ? array_merge( |
||
| 886 | $_SESSION[ 'system_variables' ], |
||
| 887 | $temp |
||
| 888 | ) : $temp; |
||
| 889 | |||
| 890 | return true; |
||
| 891 | } |
||
| 892 | |||
| 893 | if ( ! isset($_SESSION[ $offset ])) {
|
||
| 894 | return false; |
||
| 895 | } |
||
| 896 | |||
| 897 | $_SESSION[ 'system_variables' ][ $offset ] = $ttl; |
||
| 898 | |||
| 899 | return true; |
||
| 900 | } |
||
| 901 | |||
| 902 | //-------------------------------------------------------------------- |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Session::getTemp |
||
| 906 | * |
||
| 907 | * Gets either a single piece or all temporary session variables. |
||
| 908 | * |
||
| 909 | * @param string $offset Temporary session variable string offset identifier. |
||
| 910 | * |
||
| 911 | * @return array Returns temporary session variables. |
||
| 912 | */ |
||
| 913 | public function getTemp($offset = null) |
||
| 914 | {
|
||
| 915 | if (isset($offset)) {
|
||
| 916 | return (isset($_SESSION[ 'system_variables' ], $_SESSION[ 'system_variables' ][ $offset ], $_SESSION[ $offset ]) && |
||
| 917 | is_int($_SESSION[ 'system_variables' ][ $offset ])) ? $_SESSION[ $offset ] : null; |
||
| 918 | } |
||
| 919 | |||
| 920 | $tempVariables = []; |
||
| 921 | |||
| 922 | if ( ! empty($_SESSION[ 'system_variables' ])) {
|
||
| 923 | foreach ($_SESSION[ 'system_variables' ] as $offset => &$value) {
|
||
| 924 | is_int($value) && $tempVariables[ $offset ] = $_SESSION[ $offset ]; |
||
| 925 | } |
||
| 926 | } |
||
| 927 | |||
| 928 | return $tempVariables; |
||
| 929 | } |
||
| 930 | |||
| 931 | // ------------------------------------------------------------------------ |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Session::unsetTemp |
||
| 935 | * |
||
| 936 | * Unset temporary session variable. |
||
| 937 | * |
||
| 938 | * @param mixed $offset Temporary session variable string offset identifier. |
||
| 939 | */ |
||
| 940 | public function unsetTemp($offset) |
||
| 941 | {
|
||
| 942 | if (empty($_SESSION[ 'system_variables' ])) {
|
||
| 943 | return; |
||
| 944 | } |
||
| 945 | |||
| 946 | is_array($offset) OR $offset = [$offset]; |
||
| 947 | |||
| 948 | foreach ($offset as $key) {
|
||
| 949 | if (isset($_SESSION[ 'system_variables' ][ $key ]) && is_int( |
||
| 950 | $_SESSION[ 'system_variables' ][ $key ] |
||
| 951 | ) |
||
| 952 | ) {
|
||
| 953 | unset($_SESSION[ 'system_variables' ][ $key ]); |
||
| 954 | } |
||
| 955 | } |
||
| 956 | |||
| 957 | if (empty($_SESSION[ 'system_variables' ])) {
|
||
| 958 | unset($_SESSION[ 'system_variables' ]); |
||
| 959 | } |
||
| 960 | } |
||
| 961 | |||
| 962 | // ------------------------------------------------------------------------ |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Session::getTempOffsets |
||
| 966 | * |
||
| 967 | * Gets all temporary session variable offsets identifier. |
||
| 968 | * |
||
| 969 | * @return array Returns array of temporary session variable offsets identifier. |
||
| 970 | */ |
||
| 971 | public function getTempOffsets() |
||
| 983 | } |
||
| 984 | } |