Merge pull request #4382 from MrPetovan/task/3878-move-session-to-src

Move include/session to src/
This commit is contained in:
Michael Vogel 2018-02-03 21:02:09 +01:00 committed by GitHub
commit 3a7dd5e891
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 267 additions and 158 deletions

View file

@ -871,13 +871,10 @@ function get_guid($size = 16, $prefix = "")
/**
* @brief Used to end the current process, after saving session state.
* @deprecated
*/
function killme()
{
if (!get_app()->is_backend()) {
session_write_close();
}
exit();
}

View file

@ -1,134 +0,0 @@
<?php
/**
* Session management functions. These provide database storage of PHP session info.
*/
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Database\DBM;
$session_exists = 0;
$session_expire = 180000;
function ref_session_open()
{
return true;
}
function ref_session_read($id)
{
global $session_exists;
if (!x($id)) {
return '';
}
$memcache = Cache::memcache();
if (is_object($memcache)) {
$data = $memcache->get(get_app()->get_hostname().":session:".$id);
if (!is_bool($data)) {
$session_exists = true;
return $data;
}
logger("no data for session $id", LOGGER_TRACE);
return '';
}
$session = dba::selectFirst('session', ['data'], ['sid' => $id]);
if (DBM::is_result($session)) {
$session_exists = true;
return $session['data'];
} else {
logger("no data for session $id", LOGGER_TRACE);
}
return '';
}
/**
* @brief Standard PHP session write callback
*
* This callback updates the DB-stored session data and/or the expiration depending
* on the case. Uses the $session_expire global for existing session, 5 minutes
* for newly created session.
*
* @global bool $session_exists Whether a session with the given id already exists
* @global int $session_expire Session expiration delay in seconds
* @param string $id Session ID with format: [a-z0-9]{26}
* @param string $data Serialized session data
* @return boolean Returns false if parameters are missing, true otherwise
*/
function ref_session_write($id, $data)
{
global $session_exists, $session_expire;
if (!$id) {
return false;
}
if (!$data) {
return true;
}
$expire = time() + $session_expire;
$default_expire = time() + 300;
$memcache = Cache::memcache();
$a = get_app();
if (is_object($memcache) && is_object($a)) {
$memcache->set($a->get_hostname().":session:".$id, $data, MEMCACHE_COMPRESSED, $expire);
return true;
}
if ($session_exists) {
$fields = ['data' => $data, 'expire' => $expire];
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire];
dba::update('session', $fields, $condition);
} else {
$fields = ['sid' => $id, 'expire' => $default_expire, 'data' => $data];
dba::insert('session', $fields);
}
return true;
}
function ref_session_close()
{
return true;
}
function ref_session_destroy($id)
{
$memcache = Cache::memcache();
if (is_object($memcache)) {
$memcache->delete(get_app()->get_hostname().":session:".$id);
return true;
}
dba::delete('session', ['sid' => $id]);
return true;
}
function ref_session_gc()
{
dba::delete('session', ["`expire` < ?", time()]);
return true;
}
$gc_probability = 50;
ini_set('session.gc_probability', $gc_probability);
ini_set('session.use_only_cookies', 1);
ini_set('session.cookie_httponly', 1);
if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL) {
ini_set('session.cookie_secure', 1);
}
if (!Config::get('system', 'disable_database_session')) {
session_set_save_handler(
'ref_session_open', 'ref_session_close',
'ref_session_read', 'ref_session_write',
'ref_session_destroy', 'ref_session_gc'
);
}

View file

@ -12,10 +12,11 @@ use Friendica\App;
use Friendica\BaseObject;
use Friendica\Content\Nav;
use Friendica\Core\Addon;
use Friendica\Core\System;
use Friendica\Core\Theme;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Session;
use Friendica\Core\System;
use Friendica\Core\Theme;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Model\Profile;
@ -77,7 +78,7 @@ if (!$install) {
exit();
}
require_once 'include/session.php';
Session::init();
Addon::loadHooks();
Addon::callHooks('init_1');
@ -165,19 +166,10 @@ if (! x($_SESSION, 'authenticated')) {
$a->page['htmlhead'] = '';
$a->page['end'] = '';
$_SESSION['sysmsg'] = defaults($_SESSION, 'sysmsg' , []);
$_SESSION['sysmsg_info'] = defaults($_SESSION, 'sysmsg_info' , []);
$_SESSION['last_updated'] = defaults($_SESSION, 'last_updated', []);
if (! x($_SESSION, 'sysmsg')) {
$_SESSION['sysmsg'] = [];
}
if (! x($_SESSION, 'sysmsg_info')) {
$_SESSION['sysmsg_info'] = [];
}
// Array for informations about last received items
if (! x($_SESSION, 'last_updated')) {
$_SESSION['last_updated'] = [];
}
/*
* check_config() is responsible for running update scripts. These automatically
* update the DB schema whenever we push a new one out. It also checks to see if
@ -474,7 +466,7 @@ if (isset($_GET["mode"]) && (($_GET["mode"] == "raw") || ($_GET["mode"] == "mini
/// @TODO one day, kill those error-surpressing @ stuff, or PHP should ban it
@$doc->loadHTML($content);
$xpath = new DomXPath($doc);
$xpath = new DOMXPath($doc);
$list = $xpath->query("//*[contains(@id,'tread-wrapper-')]"); /* */

View file

@ -18,13 +18,13 @@ require_once 'include/dba.php';
class Cache
{
/**
* @brief Check for memcache and open a connection if configured
* @brief Check for Memcache and open a connection if configured
*
* @return object|boolean The memcache object - or "false" if not successful
* @return Memcache|boolean The Memcache object - or "false" if not successful
*/
public static function memcache()
{
if (!function_exists('memcache_connect')) {
if (!class_exists('Memcache', false)) {
return false;
}
@ -35,7 +35,7 @@ class Cache
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
$memcache_port = Config::get('system', 'memcache_port', 11211);
$memcache = new Memcache;
$memcache = new Memcache();
if (!$memcache->connect($memcache_host, $memcache_port)) {
return false;

57
src/Core/Session.php Normal file
View file

@ -0,0 +1,57 @@
<?php
/**
* @file src/Core/Session.php
*/
namespace Friendica\Core;
use Friendica\Core\Session\DatabaseSessionHandler;
use Friendica\Core\Session\MemcacheSessionHandler;
/**
* High-level Session service class
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
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);
if (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL) {
ini_set('session.cookie_secure', 1);
}
if (!Config::get('system', 'disable_database_session')) {
$memcache = Cache::memcache();
if (is_object($memcache)) {
$SessionHandler = new MemcacheSessionHandler($memcache);
} else {
$SessionHandler = new DatabaseSessionHandler();
}
session_set_save_handler($SessionHandler);
}
}
public static function exists($name)
{
return isset($_SESSION[$name]);
}
public static function get($name)
{
return defaults($_SESSION, $name, null);
}
public static function set($name, $value)
{
$_SESSION[$name] = $value;
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace Friendica\Core\Session;
use Friendica\BaseObject;
use Friendica\Core\Session;
use Friendica\Database\DBM;
use SessionHandlerInterface;
use dba;
require_once 'boot.php';
require_once 'include/dba.php';
require_once 'include/text.php';
/**
* SessionHandler using database
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface
{
public function open($save_path, $session_name)
{
return true;
}
public function read($session_id)
{
if (!x($session_id)) {
return '';
}
$session = dba::selectFirst('session', ['data'], ['sid' => $session_id]);
if (DBM::is_result($session)) {
Session::$exists = true;
return $session['data'];
}
logger("no data for session $session_id", LOGGER_TRACE);
return '';
}
/**
* @brief Standard PHP session write callback
*
* This callback updates the DB-stored session data and/or the expiration depending
* on the case. Uses the Session::expire global for existing session, 5 minutes
* for newly created session.
*
* @param string $session_id Session ID with format: [a-z0-9]{26}
* @param string $session_data Serialized session data
* @return boolean Returns false if parameters are missing, true otherwise
*/
public function write($session_id, $session_data)
{
if (!$session_id) {
return false;
}
if (!$session_data) {
return true;
}
$expire = time() + Session::$expire;
$default_expire = time() + 300;
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);
} else {
$fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
dba::insert('session', $fields);
}
return true;
}
public function close()
{
return true;
}
public function destroy($id)
{
dba::delete('session', ['sid' => $id]);
return true;
}
public function gc($maxlifetime)
{
dba::delete('session', ["`expire` < ?", time()]);
return true;
}
}

View file

@ -0,0 +1,102 @@
<?php
namespace Friendica\Core\Session;
use Friendica\BaseObject;
use Friendica\Core\Session;
use SessionHandlerInterface;
use Memcache;
require_once 'boot.php';
require_once 'include/text.php';
/**
* SessionHandler using Memcache
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MemcacheSessionHandler extends BaseObject implements SessionHandlerInterface
{
/**
* @var Memcache
*/
private $memcache = null;
/**
*
* @param Memcache $memcache
*/
public function __construct(Memcache $memcache)
{
$this->memcache = $memcache;
}
public function open($save_path, $session_name)
{
return true;
}
public function read($session_id)
{
if (!x($session_id)) {
return '';
}
$data = $this->memcache->get(self::getApp()->get_hostname() . ":session:" . $session_id);
if (!is_bool($data)) {
Session::$exists = true;
return $data;
}
logger("no data for session $session_id", LOGGER_TRACE);
return '';
}
/**
* @brief Standard PHP session write callback
*
* This callback updates the stored session data and/or the expiration depending
* on the case. Uses the Session::expire for existing session, 5 minutes
* for newly created session.
*
* @param string $session_id Session ID with format: [a-z0-9]{26}
* @param string $session_data Serialized session data
* @return boolean Returns false if parameters are missing, true otherwise
*/
public function write($session_id, $session_data)
{
if (!$session_id) {
return false;
}
if (!$session_data) {
return true;
}
$expire = time() + Session::$expire;
$this->memcache->set(
self::getApp()->get_hostname() . ":session:" . $session_id,
$session_data,
MEMCACHE_COMPRESSED,
$expire
);
return true;
}
public function close()
{
return true;
}
public function destroy($id)
{
$this->memcache->delete(self::getApp()->get_hostname() . ":session:" . $id);
return true;
}
public function gc($maxlifetime)
{
return true;
}
}