X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FSession%2FHandler%2FDatabase.php;h=41ccb6b33f834e4f315fef4b6b78a5f2813bebff;hb=2edbd1a3e2b90430b378de32f79a7cbd3537d9de;hp=e8151dddc897432f5e2ebd8096d6e88a0feb6fd2;hpb=e7ea5dac2464e790142c134c7f3d0f01aab39fce;p=friendica.git diff --git a/src/Core/Session/Handler/Database.php b/src/Core/Session/Handler/Database.php index e8151dddc8..41ccb6b33f 100644 --- a/src/Core/Session/Handler/Database.php +++ b/src/Core/Session/Handler/Database.php @@ -1,18 +1,33 @@ . + * + */ namespace Friendica\Core\Session\Handler; -use Friendica\Core\Session; use Friendica\Database\Database as DBA; use Psr\Log\LoggerInterface; -use SessionHandlerInterface; /** * SessionHandler using database - * - * @author Hypolite Petovan */ -final class Database implements SessionHandlerInterface +class Database extends AbstractSessionHandler { /** @var DBA */ private $dba; @@ -20,6 +35,8 @@ final class Database implements SessionHandlerInterface private $logger; /** @var array The $_SERVER variable */ private $server; + /** @var bool global check, if the current Session exists */ + private $sessionExists = false; /** * DatabaseSessionHandler constructor. @@ -35,78 +52,97 @@ final class Database implements SessionHandlerInterface $this->server = $server; } - public function open($save_path, $session_name) + public function open($path, $name): bool { return true; } - public function read($session_id) + public function read($id) { - if (empty($session_id)) { + if (empty($id)) { return ''; } - $session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]); - if ($this->dba->isResult($session)) { - Session::$exists = true; - return $session['data']; + try { + $session = $this->dba->selectFirst('session', ['data'], ['sid' => $id]); + if ($this->dba->isResult($session)) { + $this->sessionExists = true; + return $session['data']; + } + } catch (\Exception $exception) { + $this->logger->warning('Cannot read session.', ['id' => $id, 'exception' => $exception]); + return ''; } - $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']); + $this->logger->notice('no data for session', ['session_id' => $id, 'uri' => $this->server['REQUEST_URI'] ?? '']); return ''; } /** - * @brief Standard PHP session write callback + * Standard PHP session write callback * * This callback updates the DB-stored session data and/or the expiration depending * on the case. Uses the Session::expire global for existing session, 5 minutes * for newly created session. * - * @param string $session_id Session ID with format: [a-z0-9]{26} - * @param string $session_data Serialized session data + * @param string $id Session ID with format: [a-z0-9]{26} + * @param string $data Serialized session data * - * @return boolean Returns false if parameters are missing, true otherwise - * @throws \Exception + * @return bool Returns false if parameters are missing, true otherwise */ - public function write($session_id, $session_data) + public function write($id, $data): bool { - if (!$session_id) { + if (!$id) { return false; } - if (!$session_data) { - return true; + if (!$data) { + return $this->destroy($id); } - $expire = time() + Session::$expire; + $expire = time() + static::EXPIRE; $default_expire = time() + 300; - if (Session::$exists) { - $fields = ['data' => $session_data, 'expire' => $expire]; - $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire]; - $this->dba->update('session', $fields, $condition); - } else { - $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data]; - $this->dba->insert('session', $fields); + try { + if ($this->sessionExists) { + $fields = ['data' => $data, 'expire' => $expire]; + $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire]; + $this->dba->update('session', $fields, $condition); + } else { + $fields = ['sid' => $id, 'expire' => $default_expire, 'data' => $data]; + $this->dba->insert('session', $fields); + } + } catch (\Exception $exception) { + $this->logger->warning('Cannot write session.', ['id' => $id, 'exception' => $exception]); + return false; } return true; } - public function close() + public function close(): bool { return true; } - public function destroy($id) + public function destroy($id): bool { - return $this->dba->delete('session', ['sid' => $id]); + try { + return $this->dba->delete('session', ['sid' => $id]); + } catch (\Exception $exception) { + $this->logger->warning('Cannot destroy session.', ['id' => $id, 'exception' => $exception]); + return false; + } } - public function gc($maxlifetime) + public function gc($max_lifetime): bool { - return $this->dba->delete('session', ["`expire` < ?", time()]); + try { + return $this->dba->delete('session', ["`expire` < ?", time()]); + } catch (\Exception $exception) { + $this->logger->warning('Cannot use garbage collector.', ['exception' => $exception]); + return false; + } } }