2018-02-09 03:49:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file src/Core/Session.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2018-03-01 04:48:09 +00:00
|
|
|
use Friendica\Core\Session\CacheSessionHandler;
|
2018-02-09 03:49:49 +00:00
|
|
|
use Friendica\Core\Session\DatabaseSessionHandler;
|
2019-04-08 19:12:10 +00:00
|
|
|
use Friendica\Util\BaseURL;
|
2018-02-09 03:49:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* High-level Session service class
|
|
|
|
*
|
2018-09-15 23:28:38 +00:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-02-09 03:49:49 +00:00
|
|
|
*/
|
|
|
|
class Session
|
|
|
|
{
|
|
|
|
public static $exists = false;
|
|
|
|
public static $expire = 180000;
|
|
|
|
|
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
ini_set('session.gc_probability', 50);
|
|
|
|
ini_set('session.use_only_cookies', 1);
|
|
|
|
ini_set('session.cookie_httponly', 1);
|
|
|
|
|
2019-04-08 19:12:10 +00:00
|
|
|
if (Config::get('system', 'ssl_policy') == BaseUrl::SSL_POLICY_FULL) {
|
2018-02-09 03:49:49 +00:00
|
|
|
ini_set('session.cookie_secure', 1);
|
|
|
|
}
|
|
|
|
|
2018-03-01 04:48:09 +00:00
|
|
|
$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();
|
2018-02-09 03:49:49 +00:00
|
|
|
} else {
|
|
|
|
$SessionHandler = new DatabaseSessionHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
session_set_save_handler($SessionHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exists($name)
|
|
|
|
{
|
|
|
|
return isset($_SESSION[$name]);
|
|
|
|
}
|
|
|
|
|
2018-08-05 13:56:21 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
|
|
|
|
*
|
|
|
|
* Handle the case where session_start() hasn't been called and the super global isn't available.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $defaults
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function get($name, $defaults = null)
|
2018-02-09 03:49:49 +00:00
|
|
|
{
|
2018-08-05 13:56:21 +00:00
|
|
|
if (isset($_SESSION)) {
|
|
|
|
$return = defaults($_SESSION, $name, $defaults);
|
|
|
|
} else {
|
|
|
|
$return = $defaults;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2018-02-09 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function set($name, $value)
|
|
|
|
{
|
|
|
|
$_SESSION[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|