table = $table; $this->setConnectionManager($connectionManager); } /** * Set the connection manager. * * @param \Directory\ConnectionManagerInterface $connectionManager The connection manager. * * @return \SessionHandlerInterface Return this class. */ public function setConnectionManager(ConnectionManagerInterface $connectionManager): SessionHandlerInterface { $this->connectionManager = $connectionManager; $this->SQLManager = new SQLDatabaseHandler($this->connectionManager); return $this; } /** * Set the store object. * * @param StoreInterface $storeType The default store type. * * @return void Returns nothing. */ public function setStore(StoreInterface $storeType): void { $this->storeType = $storeType; } /** * Create the table. * * @return bool Returns true after execution. */ public function install(): bool { $link = $this->connectionManager->getConnectionString(); $link->query('CREATE TABLE IF NOT EXISTS ' . $this->table . ' ( session_id varchar(32) NOT NULL, session_time int(10) unsigned DEFAULT NULL, session_data text, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;'); $link = \null; return \true; } /** * {@inheritdoc} */ public function open($savePath, $sessionName) { return \true; } /** * {@inheritdoc} */ public function close() { return \true; } /** * {@inheritdoc} */ public function read($sessionId) { $table = $this->table; $data = $this->SQLManager->select("SELECT * FROM $table WHERE session_id = :id", ['id' => $sessionId]); if (count($data) == 1) { /** @psalm-suppress PossiblyNullReference **/ return $this->storeType->decrypt($data[0]['session_data']); } return ''; } /** * {@inheritdoc} */ public function write($sessionId, $data) { $table = $this->table; $result = $this->SQLManager->select("SELECT * FROM $table WHERE session_id = :id", ['id' => $sessionId]); if (count($result) == 1) { /** @psalm-suppress PossiblyNullReference **/ $this->SQLManager->update($table, ['session_data' => $this->storeType->encrypt($data)], "session_id = :id", ["id" => $sessionId]); } else { /** @psalm-suppress PossiblyNullReference **/ $this->SQLManager->insert($table, ['session_id' => $sessionId, 'session_data' => $this->storeType->encrypt($data), 'session_time' => \time()]); } return \true; } /** * {@inheritdoc} */ public function destroy($sessionId) { $table = $this->table; $this->SQLManager->delete($table, "session_id = :id", ["id" => $sessionId]); return \true; } /** * {@inheritdoc} */ public function gc($lifetime) { $table = $this->table; $this->SQLManager->delete($table, "session_time >= :time", ['time' => $lifetime]); return \true; } }