Initializing SessionHandlers with Dependency Injection

This commit is contained in:
nupplaPhil 2019-12-09 23:09:18 +01:00
parent 94a8a60841
commit 009a8bb939
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
4 changed files with 80 additions and 35 deletions

View File

@ -7,12 +7,15 @@ namespace Friendica\Core;
use Friendica\App; use Friendica\App;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Session\CacheSessionHandler; use Friendica\Core\Session\CacheSessionHandler;
use Friendica\Core\Session\DatabaseSessionHandler; use Friendica\Core\Session\DatabaseSessionHandler;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Util\Strings; use Friendica\Util\Strings;
use Psr\Log\LoggerInterface;
/** /**
* High-level Session service class * High-level Session service class
@ -37,9 +40,17 @@ class Session
$session_handler = Config::get('system', 'session_handler', 'database'); $session_handler = Config::get('system', 'session_handler', 'database');
if ($session_handler != 'native') { if ($session_handler != 'native') {
if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') { 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 { } else {
$SessionHandler = new DatabaseSessionHandler(); $SessionHandler = new DatabaseSessionHandler(
BaseObject::getClass(Database::class),
BaseObject::getClass(LoggerInterface::class),
$_SERVER
);
} }
session_set_save_handler($SessionHandler); session_set_save_handler($SessionHandler);

View File

@ -2,10 +2,9 @@
namespace Friendica\Core\Session; namespace Friendica\Core\Session;
use Friendica\BaseObject; use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache;
use Friendica\Core\Logger;
use Friendica\Core\Session; use Friendica\Core\Session;
use Psr\Log\LoggerInterface;
use SessionHandlerInterface; use SessionHandlerInterface;
/** /**
@ -13,8 +12,29 @@ use SessionHandlerInterface;
* *
* @author Hypolite Petovan <hypolite@mrpetovan.com> * @author Hypolite Petovan <hypolite@mrpetovan.com>
*/ */
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) public function open($save_path, $session_name)
{ {
return true; return true;
@ -26,13 +46,13 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
return ''; return '';
} }
$data = Cache::get('session:' . $session_id); $data = $this->cache->get('session:' . $session_id);
if (!empty($data)) { if (!empty($data)) {
Session::$exists = true; Session::$exists = true;
return $data; 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 ''; return '';
} }
@ -59,9 +79,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
return true; return true;
} }
$return = Cache::set('session:' . $session_id, $session_data, Session::$expire); return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
return $return;
} }
public function close() public function close()
@ -71,9 +89,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
public function destroy($id) public function destroy($id)
{ {
$return = Cache::delete('session:' . $id); return $this->cache->delete('session:' . $id);
return $return;
} }
public function gc($maxlifetime) public function gc($maxlifetime)

View File

@ -2,10 +2,9 @@
namespace Friendica\Core\Session; namespace Friendica\Core\Session;
use Friendica\BaseObject;
use Friendica\Core\Logger;
use Friendica\Core\Session; use Friendica\Core\Session;
use Friendica\Database\DBA; use Friendica\Database\Database;
use Psr\Log\LoggerInterface;
use SessionHandlerInterface; use SessionHandlerInterface;
/** /**
@ -13,8 +12,29 @@ use SessionHandlerInterface;
* *
* @author Hypolite Petovan <hypolite@mrpetovan.com> * @author Hypolite Petovan <hypolite@mrpetovan.com>
*/ */
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) public function open($save_path, $session_name)
{ {
return true; return true;
@ -26,13 +46,13 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
return ''; return '';
} }
$session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]); $session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
if (DBA::isResult($session)) { if ($this->dba->isResult($session)) {
Session::$exists = true; Session::$exists = true;
return $session['data']; 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 ''; return '';
} }
@ -65,10 +85,10 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
if (Session::$exists) { if (Session::$exists) {
$fields = ['data' => $session_data, 'expire' => $expire]; $fields = ['data' => $session_data, 'expire' => $expire];
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire]; $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
DBA::update('session', $fields, $condition); $this->dba->update('session', $fields, $condition);
} else { } else {
$fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data]; $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
DBA::insert('session', $fields); $this->dba->insert('session', $fields);
} }
return true; return true;
@ -81,13 +101,11 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
public function destroy($id) public function destroy($id)
{ {
DBA::delete('session', ['sid' => $id]); return $this->dba->delete('session', ['sid' => $id]);
return true;
} }
public function gc($maxlifetime) public function gc($maxlifetime)
{ {
DBA::delete('session', ["`expire` < ?", time()]); return $this->dba->delete('session', ["`expire` < ?", time()]);
return true;
} }
} }

View File

@ -18,7 +18,7 @@ class Cookie
const PATH = '/'; const PATH = '/';
/** @var string The domain name of the Friendica cookie */ /** @var string The domain name of the Friendica cookie */
const DOMAIN = ''; 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; const HTTPONLY = true;
/** @var string The remote address of this node */ /** @var string The remote address of this node */
@ -68,10 +68,10 @@ class Cookie
/** /**
* Set the Friendica cookie for a user * Set the Friendica cookie for a user
* *
* @param int $uid The user id * @param int $uid The user id
* @param string $password The user password * @param string $password The user password
* @param string $privateKey The user private key * @param string $privateKey The user private key
* @param int|null $seconds optional the seconds * @param int|null $seconds optional the seconds
* *
* @return bool * @return bool
*/ */
@ -142,9 +142,9 @@ class Cookie
* @link https://php.net/manual/en/function.setcookie.php * @link https://php.net/manual/en/function.setcookie.php
* *
* @param string $name * @param string $name
* @param string $value [optional] * @param string $value [optional]
* @param int $expire [optional] * @param int $expire [optional]
* @param bool $secure [optional] * @param bool $secure [optional]
* *
* @return bool If output exists prior to calling this function, * @return bool If output exists prior to calling this function,
* *