inactivityInterval = $inactivityInterval; if (\session_status() !== \PHP_SESSION_ACTIVE && !\headers_sent()) { \session_set_cookie_params($liftetime, '/', '', false, true); // @codeCoverageIgnore \session_start(); // @codeCoverageIgnore } if ($this->inactivityInterval > 0 && ($this->inactivityInterval + ($_SESSION['lastActivity'] ?? 0) < \time())) { $this->destroy(); // @codeCoverageIgnore } $this->sessionData = $_SESSION ?? []; $_SESSION = null; $this->sessionData['lastActivity'] = \time(); $this->sid = \session_id(); $this->setCsrfProtection(); } /** * Set Csrf protection for forms. * * @return void * * @since 1.0.0 */ private function setCsrfProtection() : void { $this->set('UID', 0, false); if (($csrf = $this->get('CSRF')) === null) { $csrf = StringUtils::generateString(10, 16); $this->set('CSRF', $csrf, false); } UriFactory::setQuery('$CSRF', $csrf); } /** * {@inheritdoc} */ public function set($key, $value, bool $overwrite = false) : bool { if (!$this->isLocked && ($overwrite || !isset($this->sessionData[$key]))) { $this->sessionData[$key] = $value; return true; } return false; } /** * {@inheritdoc} */ public function get($key) { return $this->sessionData[$key] ?? null; } /** * {@inheritdoc} */ public function lock() : void { $this->isLocked = true; } /** * Check if session is locked. * * @return bool Lock status * * @since 1.0.0 */ public function isLocked() : bool { return $this->isLocked; } /** * {@inheritdoc} */ public function save() : void { if (!$this->isLocked) { $_SESSION = $this->sessionData; \session_write_close(); } } /** * {@inheritdoc} */ public function remove($key) : bool { if (!$this->isLocked && isset($this->sessionData[$key])) { unset($this->sessionData[$key]); return true; } return false; } /** * {@inheritdoc} */ public function getSID() { return $this->sid; } /** * {@inheritdoc} */ public function setSID($sid) : void { $this->sid = $sid; } /** * Destroy the current session. * * @return void * * @since 1.0.0 * @codeCoverageIgnore */ private function destroy() : void { if (\session_status() !== \PHP_SESSION_NONE) { \session_destroy(); $this->sessionData = []; \session_start(); } } /** * Destruct session. * * @since 1.0.0 */ public function __destruct() { $this->save(); } }