From 009a8bb939554270a1a2d42bb770baf81e39bd99 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Mon, 9 Dec 2019 23:09:18 +0100 Subject: [PATCH] Initializing SessionHandlers with Dependency Injection --- src/Core/Session.php | 15 ++++++- src/Core/Session/CacheSessionHandler.php | 40 +++++++++++++------ src/Core/Session/DatabaseSessionHandler.php | 44 +++++++++++++++------ src/Model/User/Cookie.php | 16 ++++---- 4 files changed, 80 insertions(+), 35 deletions(-) diff --git a/src/Core/Session.php b/src/Core/Session.php index 542307a5ca..140781d1c6 100644 --- a/src/Core/Session.php +++ b/src/Core/Session.php @@ -7,12 +7,15 @@ namespace Friendica\Core; use Friendica\App; use Friendica\BaseObject; +use Friendica\Core\Cache\ICache; use Friendica\Core\Session\CacheSessionHandler; use Friendica\Core\Session\DatabaseSessionHandler; +use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\Model\Contact; use Friendica\Model\User; use Friendica\Util\Strings; +use Psr\Log\LoggerInterface; /** * High-level Session service class @@ -37,9 +40,17 @@ class Session $session_handler = Config::get('system', 'session_handler', 'database'); if ($session_handler != 'native') { if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') { - $SessionHandler = new CacheSessionHandler(); + $SessionHandler = new CacheSessionHandler( + BaseObject::getClass(ICache::class), + BaseObject::getClass(LoggerInterface::class), + $_SERVER + ); } else { - $SessionHandler = new DatabaseSessionHandler(); + $SessionHandler = new DatabaseSessionHandler( + BaseObject::getClass(Database::class), + BaseObject::getClass(LoggerInterface::class), + $_SERVER + ); } session_set_save_handler($SessionHandler); diff --git a/src/Core/Session/CacheSessionHandler.php b/src/Core/Session/CacheSessionHandler.php index 6a1b32bfb1..218ec1440f 100644 --- a/src/Core/Session/CacheSessionHandler.php +++ b/src/Core/Session/CacheSessionHandler.php @@ -2,10 +2,9 @@ namespace Friendica\Core\Session; -use Friendica\BaseObject; -use Friendica\Core\Cache; -use Friendica\Core\Logger; +use Friendica\Core\Cache\ICache; use Friendica\Core\Session; +use Psr\Log\LoggerInterface; use SessionHandlerInterface; /** @@ -13,8 +12,29 @@ use SessionHandlerInterface; * * @author Hypolite Petovan */ -class CacheSessionHandler extends BaseObject implements SessionHandlerInterface +class CacheSessionHandler implements SessionHandlerInterface { + /** @var ICache */ + private $cache; + /** @var LoggerInterface */ + private $logger; + /** @var array The $_SERVER array */ + private $server; + + /** + * CacheSessionHandler constructor. + * + * @param ICache $cache + * @param LoggerInterface $logger + * @param array $server + */ + public function __construct(ICache $cache, LoggerInterface $logger, array $server) + { + $this->cache = $cache; + $this->logger = $logger; + $this->server = $server; + } + public function open($save_path, $session_name) { return true; @@ -26,13 +46,13 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface return ''; } - $data = Cache::get('session:' . $session_id); + $data = $this->cache->get('session:' . $session_id); if (!empty($data)) { Session::$exists = true; return $data; } - Logger::notice('no data for session', ['session_id' => $session_id, 'uri' => $_SERVER['REQUEST_URI']]); + $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']); return ''; } @@ -59,9 +79,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface return true; } - $return = Cache::set('session:' . $session_id, $session_data, Session::$expire); - - return $return; + return $this->cache->set('session:' . $session_id, $session_data, Session::$expire); } public function close() @@ -71,9 +89,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface public function destroy($id) { - $return = Cache::delete('session:' . $id); - - return $return; + return $this->cache->delete('session:' . $id); } public function gc($maxlifetime) diff --git a/src/Core/Session/DatabaseSessionHandler.php b/src/Core/Session/DatabaseSessionHandler.php index e3e95f9ed1..5d8441e354 100644 --- a/src/Core/Session/DatabaseSessionHandler.php +++ b/src/Core/Session/DatabaseSessionHandler.php @@ -2,10 +2,9 @@ namespace Friendica\Core\Session; -use Friendica\BaseObject; -use Friendica\Core\Logger; use Friendica\Core\Session; -use Friendica\Database\DBA; +use Friendica\Database\Database; +use Psr\Log\LoggerInterface; use SessionHandlerInterface; /** @@ -13,8 +12,29 @@ use SessionHandlerInterface; * * @author Hypolite Petovan */ -class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface +class DatabaseSessionHandler implements SessionHandlerInterface { + /** @var Database */ + private $dba; + /** @var LoggerInterface */ + private $logger; + /** @var array The $_SERVER variable */ + private $server; + + /** + * DatabaseSessionHandler constructor. + * + * @param Database $dba + * @param LoggerInterface $logger + * @param array $server + */ + public function __construct(Database $dba, LoggerInterface $logger, array $server) + { + $this->dba = $dba; + $this->logger = $logger; + $this->server = $server; + } + public function open($save_path, $session_name) { return true; @@ -26,13 +46,13 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa return ''; } - $session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]); - if (DBA::isResult($session)) { + $session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]); + if ($this->dba->isResult($session)) { Session::$exists = true; return $session['data']; } - Logger::notice('no data for session', ['session_id' => $session_id, 'uri' => $_SERVER['REQUEST_URI']]); + $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']); return ''; } @@ -65,10 +85,10 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa if (Session::$exists) { $fields = ['data' => $session_data, 'expire' => $expire]; $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire]; - DBA::update('session', $fields, $condition); + $this->dba->update('session', $fields, $condition); } else { $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data]; - DBA::insert('session', $fields); + $this->dba->insert('session', $fields); } return true; @@ -81,13 +101,11 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa public function destroy($id) { - DBA::delete('session', ['sid' => $id]); - return true; + return $this->dba->delete('session', ['sid' => $id]); } public function gc($maxlifetime) { - DBA::delete('session', ["`expire` < ?", time()]); - return true; + return $this->dba->delete('session', ["`expire` < ?", time()]); } } diff --git a/src/Model/User/Cookie.php b/src/Model/User/Cookie.php index 79882d6415..f85d818686 100644 --- a/src/Model/User/Cookie.php +++ b/src/Model/User/Cookie.php @@ -18,7 +18,7 @@ class Cookie const PATH = '/'; /** @var string The domain name of the Friendica cookie */ const DOMAIN = ''; - /** @var bool True, if the cookie should only be accessable through HTTP */ + /** @var bool True, if the cookie should only be accessible through HTTP */ const HTTPONLY = true; /** @var string The remote address of this node */ @@ -68,10 +68,10 @@ class Cookie /** * Set the Friendica cookie for a user * - * @param int $uid The user id - * @param string $password The user password - * @param string $privateKey The user private key - * @param int|null $seconds optional the seconds + * @param int $uid The user id + * @param string $password The user password + * @param string $privateKey The user private key + * @param int|null $seconds optional the seconds * * @return bool */ @@ -142,9 +142,9 @@ class Cookie * @link https://php.net/manual/en/function.setcookie.php * * @param string $name - * @param string $value [optional] - * @param int $expire [optional] - * @param bool $secure [optional] + * @param string $value [optional] + * @param int $expire [optional] + * @param bool $secure [optional] * * @return bool If output exists prior to calling this function, *