Move session stuff in EesyPHP namespace

This commit is contained in:
Benjamin Renard 2023-01-29 22:34:43 +01:00
parent cf6ea5480b
commit 15c2acee08
4 changed files with 92 additions and 48 deletions

View file

@ -5,6 +5,7 @@ use EesyPHP\Log;
use EesyPHP\SentryIntegration; use EesyPHP\SentryIntegration;
use EesyPHP\SentrySpan; use EesyPHP\SentrySpan;
use EesyPHP\SentryTransaction; use EesyPHP\SentryTransaction;
use EesyPHP\Session;
use EesyPHP\Url; use EesyPHP\Url;
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED); error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
@ -59,7 +60,10 @@ Log::init(
isset($log_php_errors_levels)?$log_php_errors_levels:null isset($log_php_errors_levels)?$log_php_errors_levels:null
); );
require_once('functions.php'); require_once('functions.php');
require_once('session.php'); Session :: init(
isset($session_max_duration)?$session_max_duration:null,
isset($session_timeout)?$session_timeout:null
);
// Nomenclatures // Nomenclatures
$status_list = array ( $status_list = array (

View file

@ -2,6 +2,7 @@
use EesyPHP\Check; use EesyPHP\Check;
use EesyPHP\Log; use EesyPHP\Log;
use EesyPHP\Session;
/* /*
* Check values helpers * Check values helpers
@ -313,7 +314,7 @@ function check_ajax_request($session_key=null) {
global $ajax, $debug_ajax; global $ajax, $debug_ajax;
$ajax = true; $ajax = true;
if (check_session_key($session_key)) if (Session :: check_key($session_key))
fatal_error('Invalid request'); fatal_error('Invalid request');
if ($debug_ajax) if ($debug_ajax)

View file

@ -1,46 +0,0 @@
<?php
use EesyPHP\Log;
if (php_sapi_name() == "cli")
return true;
// Define session max duration
if (!isset($session_max_duration))
$session_max_duration = (12*60*60); // Default to 12h
ini_set('session.gc_maxlifetime', $session_max_duration);
ini_set('session.cookie_lifetime', $session_max_duration);
// Start session
session_start();
// Init session key
if (!isset($_SESSION['session_key'])) {
$_SESSION['session_key']=uniqid();
}
// Handle session timeout
if (isset($session_timeout) && $session_timeout) {
if (!isset($_SESSION['session_last_access'])) {
Log :: debug('Set initial session last access');
$_SESSION['session_last_access'] = time();
}
elseif ($_SESSION['session_last_access'] > (time() - $session_timeout)) {
Log :: debug(
'Session timeout not expired, update session last access '.
'(Previous value : '.$_SESSION['session_last_access'].')');
$_SESSION['session_last_access'] = time();
}
else {
Log :: info('Session destroyed due to inactivity');
session_destroy();
}
}
function check_session_key($value=null) {
if (is_null($value) && isset($_REQUEST['session_key']))
$value = $_REQUEST['session_key'];
return ($value && $_SESSION['session_key'] == $value);
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab

85
src/Session.php Normal file
View file

@ -0,0 +1,85 @@
<?php
namespace EesyPHP;
use EesyPHP\Log;
/**
* Session management
*/
class Session {
/**
* Session max duration (in seconds, default: 12h)
* @var int
*/
protected static int $max_duration = 12 * 60 * 60;
/**
* Initialization
* @param int|null $max_duration Session max duration in second
* (optional, default: 12h)
* @param int|null $timeout Session inactivity timeout in second
* (optional, default: no timeout)
* @return void
*/
public static function init($max_duration=null, $timeout=null) {
if (php_sapi_name() == "cli")
return;
// Define session max duration
if (is_int($max_duration))
self :: $max_duration = $max_duration;
ini_set('session.gc_maxlifetime', strval(self :: $max_duration));
ini_set('session.cookie_lifetime', strval(self :: $max_duration));
// Start session
session_start();
// Init session key
if (!isset($_SESSION['session_key'])) {
$_SESSION['session_key'] = uniqid();
}
// Handle session timeout
if ($timeout) {
if (!isset($_SESSION['session_last_access'])) {
Log :: debug('Set initial session last access');
$_SESSION['session_last_access'] = time();
}
elseif ($_SESSION['session_last_access'] > (time() - $timeout)) {
Log :: debug(
'Session timeout not expired, update session last access '.
'(Previous value : %d', $_SESSION['session_last_access']);
$_SESSION['session_last_access'] = time();
}
else {
Log :: info('Session destroyed due to inactivity');
session_destroy();
}
}
}
/**
* Check session key
* @param string|null $value The value of the session key to check
* (optional, default: $_REQUEST['session_key'])
* @return bool
*/
public static function check_key($value=null) {
if (is_null($value) && isset($_REQUEST['session_key']))
$value = $_REQUEST['session_key'];
return ($value && $_SESSION['session_key'] == $value);
}
}
if (php_sapi_name() == "cli")
return true;
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab