2018-02-08 22:49:49 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Session;
|
|
|
|
|
|
|
|
use Friendica\BaseObject;
|
2018-10-29 17:20:46 -04:00
|
|
|
use Friendica\Core\Logger;
|
2018-02-08 22:49:49 -05:00
|
|
|
use Friendica\Core\Session;
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-02-08 22:49:49 -05:00
|
|
|
use SessionHandlerInterface;
|
|
|
|
|
|
|
|
require_once 'boot.php';
|
|
|
|
require_once 'include/dba.php';
|
|
|
|
require_once 'include/text.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SessionHandler using database
|
|
|
|
*
|
2018-09-15 19:28:38 -04:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-02-08 22:49:49 -05:00
|
|
|
*/
|
|
|
|
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 '';
|
|
|
|
}
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
$session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]);
|
2018-07-21 08:46:04 -04:00
|
|
|
if (DBA::isResult($session)) {
|
2018-02-08 22:49:49 -05:00
|
|
|
Session::$exists = true;
|
|
|
|
return $session['data'];
|
|
|
|
}
|
2018-10-29 17:20:46 -04:00
|
|
|
Logger::log("no data for session $session_id", LOGGER_TRACE);
|
2018-02-08 22:49:49 -05:00
|
|
|
|
|
|
|
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];
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::update('session', $fields, $condition);
|
2018-02-08 22:49:49 -05:00
|
|
|
} else {
|
|
|
|
$fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::insert('session', $fields);
|
2018-02-08 22:49:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::delete('session', ['sid' => $id]);
|
2018-02-08 22:49:49 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function gc($maxlifetime)
|
|
|
|
{
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::delete('session', ["`expire` < ?", time()]);
|
2018-02-08 22:49:49 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|