diff --git a/DataStorage/Session/ConsoleSession.php b/DataStorage/Session/ConsoleSession.php new file mode 100644 index 000000000..39aae541e --- /dev/null +++ b/DataStorage/Session/ConsoleSession.php @@ -0,0 +1,212 @@ +inactivityInterval = $inactivityInterval; + + if (\session_status() !== \PHP_SESSION_ACTIVE) { + \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(); + } + + /** + * {@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(); + } +} diff --git a/DataStorage/Session/ConsoleSessionHandler.php b/DataStorage/Session/ConsoleSessionHandler.php new file mode 100644 index 000000000..793f36f73 --- /dev/null +++ b/DataStorage/Session/ConsoleSessionHandler.php @@ -0,0 +1,166 @@ +savePath = $path; + } + + /** + * Create a session id string + * + * @return string + * + * @since 1.0.0 + */ + public function create_sid() : string + { + return \session_create_id('console-'); + } + + /** + * Open the session storage + * + * @param string $savePath Path of the session data + * @param string $sessionName Name of the session + * + * @return bool + * + * @since 1.0.0 + */ + public function open($savePath, $sessionName) + { + $this->savePath = $savePath; + if (!\is_dir($this->savePath)) { + \mkdir($this->savePath, 0777); + } + + return true; + } + + /** + * Close the session + * + * @return bool + * + * @since 1.0.0 + */ + public function close() : bool + { + return true; + } + + /** + * Read the session data + * + * @param string $id Session id + * + * @return string + * + * @since 1.0.0 + */ + public function read($id) + { + return (string) @\file_get_contents($this->savePath . '/sess_' . $id); + } + + /** + * Write session data + * + * @param string $id Session id + * @param string $data Session data + * + * @return bool + * + * @since 1.0.0 + */ + public function write($id, $data) + { + return \file_put_contents($this->savePath . '/sess_' . $id, $data) === false ? false : true; + } + + /** + * Destroy the session + * + * @param string $id Session id + * + * @return bool + * + * @since 1.0.0 + */ + public function destroy($id) + { + $file = $this->savePath . '/sess_' . $id; + if (\file_exists($file)) { + unlink($file); + } + + return true; + } + + /** + * Garbage collect session data + * + * @param int $maxlifetime Maximum session data life time + * + * @return bool + * + * @since 1.0.0 + */ + public function gc($maxlifetime) + { + $files = \glob("$this->savePath/sess_*"); + + if ($files === false) { + return false; + } + + foreach ($files as $file) { + if (\filemtime($file) + $maxlifetime < \time() && \file_exists($file)) { + unlink($file); + } + } + + return true; + } +}