From 0b66b6e0d5aea495363551be9f1954114883f052 Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 20 Oct 2022 20:26:54 +0200 Subject: [PATCH] Move Session "exists" and "expire" to new class --- src/Core/Session.php | 3 -- .../Handler/AbstractSessionHandler.php | 28 +++++++++++++++++++ src/Core/Session/Handler/Cache.php | 7 ++--- src/Core/Session/Handler/Database.php | 12 ++++---- 4 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 src/Core/Session/Handler/AbstractSessionHandler.php diff --git a/src/Core/Session.php b/src/Core/Session.php index a944cf8f5..d39db4861 100644 --- a/src/Core/Session.php +++ b/src/Core/Session.php @@ -31,9 +31,6 @@ use Friendica\Util\Strings; */ class Session { - public static $exists = false; - public static $expire = 180000; - /** * Returns the user id of locally logged in user or false. * diff --git a/src/Core/Session/Handler/AbstractSessionHandler.php b/src/Core/Session/Handler/AbstractSessionHandler.php new file mode 100644 index 000000000..a16242be5 --- /dev/null +++ b/src/Core/Session/Handler/AbstractSessionHandler.php @@ -0,0 +1,28 @@ +. + * + */ + +namespace Friendica\Core\Session\Handler; + +abstract class AbstractSessionHandler implements \SessionHandlerInterface +{ + /** @var int Duration of the Session */ + public const EXPIRE = 180000; +} diff --git a/src/Core/Session/Handler/Cache.php b/src/Core/Session/Handler/Cache.php index 519bad222..4fcc51c17 100644 --- a/src/Core/Session/Handler/Cache.php +++ b/src/Core/Session/Handler/Cache.php @@ -23,14 +23,12 @@ namespace Friendica\Core\Session\Handler; use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\Cache\Exception\CachePersistenceException; -use Friendica\Core\Session; use Psr\Log\LoggerInterface; -use SessionHandlerInterface; /** * SessionHandler using Friendica Cache */ -class Cache implements SessionHandlerInterface +class Cache extends AbstractSessionHandler { /** @var ICanCache */ private $cache; @@ -57,7 +55,6 @@ class Cache implements SessionHandlerInterface try { $data = $this->cache->get('session:' . $id); if (!empty($data)) { - Session::$exists = true; return $data; } } catch (CachePersistenceException $exception) { @@ -91,7 +88,7 @@ class Cache implements SessionHandlerInterface } try { - return $this->cache->set('session:' . $id, $data, Session::$expire); + return $this->cache->set('session:' . $id, $data, static::EXPIRE); } catch (CachePersistenceException $exception) { $this->logger->warning('Cannot write session', ['id' => $id, 'exception' => $exception]); return false; diff --git a/src/Core/Session/Handler/Database.php b/src/Core/Session/Handler/Database.php index 714471f9f..41ccb6b33 100644 --- a/src/Core/Session/Handler/Database.php +++ b/src/Core/Session/Handler/Database.php @@ -21,15 +21,13 @@ namespace Friendica\Core\Session\Handler; -use Friendica\Core\Session; use Friendica\Database\Database as DBA; use Psr\Log\LoggerInterface; -use SessionHandlerInterface; /** * SessionHandler using database */ -class Database implements SessionHandlerInterface +class Database extends AbstractSessionHandler { /** @var DBA */ private $dba; @@ -37,6 +35,8 @@ 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. @@ -66,7 +66,7 @@ class Database implements SessionHandlerInterface try { $session = $this->dba->selectFirst('session', ['data'], ['sid' => $id]); if ($this->dba->isResult($session)) { - Session::$exists = true; + $this->sessionExists = true; return $session['data']; } } catch (\Exception $exception) { @@ -101,11 +101,11 @@ class Database implements SessionHandlerInterface return $this->destroy($id); } - $expire = time() + Session::$expire; + $expire = time() + static::EXPIRE; $default_expire = time() + 300; try { - if (Session::$exists) { + if ($this->sessionExists) { $fields = ['data' => $data, 'expire' => $expire]; $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire]; $this->dba->update('session', $fields, $condition);