Refactor Session Handling (make it more simple & handler are now handler again)
This commit is contained in:
76
src/Core/Session/AbstractSession.php
Normal file
76
src/Core/Session/AbstractSession.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
|
||||
use Friendica\Model\User\Cookie;
|
||||
|
||||
/**
|
||||
* Contains the base methods for $_SESSION interaction
|
||||
*/
|
||||
class AbstractSession
|
||||
{
|
||||
/** @var Cookie */
|
||||
protected $cookie;
|
||||
|
||||
public function __construct( Cookie $cookie)
|
||||
{
|
||||
$this->cookie = $cookie;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}}
|
||||
*/
|
||||
public function exists(string $name)
|
||||
{
|
||||
return isset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get(string $name, $defaults = null)
|
||||
{
|
||||
return $_SESSION[$name] ?? $defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(string $name, $value)
|
||||
{
|
||||
$_SESSION[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function setMultiple(array $values)
|
||||
{
|
||||
$_SESSION = $values + $_SESSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function remove(string $name)
|
||||
{
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$_SESSION = [];
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
namespace Friendica\Core\Session\Handler;
|
||||
|
||||
use Friendica\Core\Cache\ICache;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Session;
|
||||
use Friendica\Model\User\Cookie;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
@@ -14,7 +12,7 @@ use SessionHandlerInterface;
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
final class Cache extends Native implements SessionHandlerInterface
|
||||
final class Cache implements SessionHandlerInterface
|
||||
{
|
||||
/** @var ICache */
|
||||
private $cache;
|
||||
@@ -23,15 +21,11 @@ final class Cache extends Native implements SessionHandlerInterface
|
||||
/** @var array The $_SERVER array */
|
||||
private $server;
|
||||
|
||||
public function __construct(Configuration $config, Cookie $cookie, ICache $cache, LoggerInterface $logger, array $server)
|
||||
public function __construct(ICache $cache, LoggerInterface $logger, array $server)
|
||||
{
|
||||
parent::__construct($config, $cookie);
|
||||
|
||||
$this->cache = $cache;
|
||||
$this->logger = $logger;
|
||||
$this->server = $server;
|
||||
|
||||
session_set_save_handler($this);
|
||||
}
|
||||
|
||||
public function open($save_path, $session_name)
|
||||
@@ -1,11 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
namespace Friendica\Core\Session\Handler;
|
||||
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Core\Session;
|
||||
use Friendica\Database\Database as DBA;
|
||||
use Friendica\Model\User\Cookie;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
@@ -14,7 +12,7 @@ use SessionHandlerInterface;
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
final class Database extends Native implements SessionHandlerInterface
|
||||
final class Database implements SessionHandlerInterface
|
||||
{
|
||||
/** @var DBA */
|
||||
private $dba;
|
||||
@@ -26,19 +24,15 @@ final class Database extends Native implements SessionHandlerInterface
|
||||
/**
|
||||
* DatabaseSessionHandler constructor.
|
||||
*
|
||||
* @param Database $dba
|
||||
* @param DBA $dba
|
||||
* @param LoggerInterface $logger
|
||||
* @param array $server
|
||||
*/
|
||||
public function __construct(Configuration $config, Cookie $cookie, DBA $dba, LoggerInterface $logger, array $server)
|
||||
public function __construct(DBA $dba, LoggerInterface $logger, array $server)
|
||||
{
|
||||
parent::__construct($config, $cookie);
|
||||
|
||||
$this->dba = $dba;
|
||||
$this->logger = $logger;
|
||||
$this->server = $server;
|
||||
|
||||
session_set_save_handler($this);
|
||||
}
|
||||
|
||||
public function open($save_path, $session_name)
|
||||
@@ -29,7 +29,8 @@ interface ISession
|
||||
* Handle the case where session_start() hasn't been called and the super global isn't available.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $defaults
|
||||
* @param mixed $defaults
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name, $defaults = null);
|
||||
@@ -39,7 +40,7 @@ interface ISession
|
||||
* Overrides value of existing key.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set(string $name, $value);
|
||||
|
||||
@@ -63,9 +64,4 @@ interface ISession
|
||||
* Clears the current session array
|
||||
*/
|
||||
public function clear();
|
||||
|
||||
/**
|
||||
* Kills the "Friendica" cookie and all session data
|
||||
*/
|
||||
public function delete();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Model\User\Cookie;
|
||||
|
||||
/**
|
||||
@@ -10,19 +9,14 @@ use Friendica\Model\User\Cookie;
|
||||
*
|
||||
* @todo after replacing the last direct $_SESSION call, use a internal array instead of the global variable
|
||||
*/
|
||||
final class Memory extends Native
|
||||
final class Memory extends AbstractSession implements ISession
|
||||
{
|
||||
public function __construct(Configuration $config, Cookie $cookie)
|
||||
public function __construct(Cookie $cookie)
|
||||
{
|
||||
$this->cookie = $cookie;
|
||||
}
|
||||
parent::__construct($cookie);
|
||||
|
||||
public function start()
|
||||
{
|
||||
// Backward compatibility until all Session variables are replaced
|
||||
// with the Session class
|
||||
$_SESSION = [];
|
||||
$this->clear();
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,29 +2,30 @@
|
||||
|
||||
namespace Friendica\Core\Session;
|
||||
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\App;
|
||||
use Friendica\Model\User\Cookie;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
/**
|
||||
* The native Session class which uses the PHP internal Session function
|
||||
* The native Session class which uses the PHP internal Session functions
|
||||
*/
|
||||
class Native implements ISession
|
||||
final class Native extends AbstractSession implements ISession
|
||||
{
|
||||
/** @var Cookie */
|
||||
protected $cookie;
|
||||
|
||||
public function __construct(Configuration $config, Cookie $cookie)
|
||||
public function __construct(App\BaseURL $baseURL, Cookie $cookie, SessionHandlerInterface $handler = null)
|
||||
{
|
||||
parent::__construct($cookie);
|
||||
|
||||
ini_set('session.gc_probability', 50);
|
||||
ini_set('session.use_only_cookies', 1);
|
||||
ini_set('session.cookie_httponly', 1);
|
||||
ini_set('session.cookie_httponly', (int)Cookie::HTTPONLY);
|
||||
|
||||
if ($config->get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL) {
|
||||
if ($baseURL->getSSLPolicy() == App\BaseURL::SSL_POLICY_FULL) {
|
||||
ini_set('session.cookie_secure', 1);
|
||||
}
|
||||
|
||||
$this->cookie = $cookie;
|
||||
if (isset($handler)) {
|
||||
session_set_save_handler($handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,61 +36,4 @@ class Native implements ISession
|
||||
session_start();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}}
|
||||
*/
|
||||
public function exists(string $name)
|
||||
{
|
||||
return isset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get(string $name, $defaults = null)
|
||||
{
|
||||
return $_SESSION[$name] ?? $defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(string $name, $value)
|
||||
{
|
||||
$_SESSION[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function setMultiple(array $values)
|
||||
{
|
||||
$_SESSION = $values + $_SESSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function remove(string $name)
|
||||
{
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$_SESSION = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Kills the "Friendica" cookie and all session data
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$this->cookie->clear();
|
||||
$_SESSION = [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user