|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
|
|
|
|
* GPLv3 General License.
|
|
|
|
* https://exacti.com.br
|
|
|
|
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Phacil\Framework;
|
|
|
|
|
|
|
|
use Phacil\Framework\Config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The session manipulation class
|
|
|
|
*
|
|
|
|
* You can activate the Redis session instead use the default PHP session manipulation.
|
|
|
|
*
|
|
|
|
* @param bool $redis Active or not the Redis session
|
|
|
|
*
|
|
|
|
* @since 1.0.0
|
|
|
|
* @package Phacil\Framework
|
|
|
|
*/
|
|
|
|
class Session
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
public $data = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of session
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redis prefix
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $redisPrefix = "sess_";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redis Key
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $redisKey;
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var \Phacil\Framework\Session\Redis\Handler
|
|
|
|
*/
|
|
|
|
private $saveHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var \Phacil\Framework\Registry
|
|
|
|
*/
|
|
|
|
private $registry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var \Phacil\Framework\Cookies\SameSite
|
|
|
|
*/
|
|
|
|
private $sameSite;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param bool $redis
|
|
|
|
* @param string|null $redisDSN
|
|
|
|
* @param int|null $redisPort
|
|
|
|
* @param string|null $redisPass
|
|
|
|
* @param int|null $redis_expire
|
|
|
|
* @param string $redis_prefix
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
\Phacil\Framework\Registry $registry,
|
|
|
|
Config $config,
|
|
|
|
\Phacil\Framework\Cookies\SameSite $sameSite
|
|
|
|
) {
|
|
|
|
$this->registry = $registry;
|
|
|
|
|
|
|
|
$this->sameSite = $sameSite;
|
|
|
|
|
|
|
|
$this->name = (Config::SESSION_PREFIX() ?: 'SESS') . (isset($_SERVER['REMOTE_ADDR']) ? md5($_SERVER['REMOTE_ADDR']) : md5(date("dmY")));
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
|
|
|
|
//define('SESSION_PREFIX_INTERNAL_REDIS', Config::REDIS_SESSION_PREFIX() ?: 'phacil_');
|
|
|
|
|
|
|
|
$this->redis($config->get('session_redis'));
|
|
|
|
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
if (!session_id()) {
|
|
|
|
$this->openSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session_name() === $this->name) {
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
$this->data =& $_SESSION;
|
|
|
|
} else {
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
$this->openSession();
|
|
|
|
$this->data =& $_SESSION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the PHP session
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function openSession()
|
|
|
|
{
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
|
|
|
|
$this->closeSession();
|
|
|
|
|
|
|
|
ini_set('session.use_cookies', 'On');
|
|
|
|
ini_set('session.use_trans_sid', 'Off');
|
|
|
|
ini_set('session.cookie_httponly', 1);
|
|
|
|
if ($this->isSecure())
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
ini_set('session.cookie_secure', 1);
|
|
|
|
|
|
|
|
if (version_compare(phpversion(), "7.3.0", "<")) {
|
|
|
|
session_set_cookie_params(0, '/; samesite=' . $this->sameSite->getValue());
|
|
|
|
} else {
|
|
|
|
session_set_cookie_params([
|
|
|
|
'lifetime' => 0,
|
|
|
|
'path' => '/',
|
|
|
|
'samesite' => $this->sameSite->getValue()
|
|
|
|
]);
|
|
|
|
}
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
//session_id(md5());
|
|
|
|
session_name($this->name);
|
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check and iniciate the Redis connection
|
|
|
|
*
|
|
|
|
* @param bool $redis
|
|
|
|
* @param string|null $redisDSN
|
|
|
|
* @param string|null $redisPort
|
|
|
|
* @param string|null $redisPass
|
|
|
|
* @param int|null $redis_expire
|
|
|
|
* @param string $redis_prefix
|
|
|
|
*
|
|
|
|
* @since 2.0.0
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function redis($redis = false)
|
|
|
|
{
|
|
|
|
if (!$redis)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$this->saveHandler = $this->registry->getInstance(\Phacil\Framework\Session\Redis\Handler::class);
|
|
|
|
|
|
|
|
$this->saveHandler->setName($this->name);
|
|
|
|
|
|
|
|
return $this->registerSaveHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register save handler
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function registerSaveHandler()
|
|
|
|
{
|
|
|
|
return session_set_save_handler(
|
|
|
|
//$this->saveHandler
|
|
|
|
[$this->saveHandler, 'open'],
|
|
|
|
[$this->saveHandler, 'close'],
|
|
|
|
[$this->saveHandler, 'read'],
|
|
|
|
[$this->saveHandler, 'write'],
|
|
|
|
[$this->saveHandler, 'destroy'],
|
|
|
|
[$this->saveHandler, 'gc']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close sessions
|
|
|
|
*
|
|
|
|
* @param bool $force
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function closeSession($force = false)
|
|
|
|
{
|
|
|
|
//return ;
|
|
|
|
if (session_status() == PHP_SESSION_ACTIVE || $force) {
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
session_unset();
|
|
|
|
session_destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if is secure (SSL) connection
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isSecure()
|
|
|
|
{
|
PHP SESSION Prefix and IP check, engine constants and user_constants as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
5 years ago
|
|
|
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush all session data
|
|
|
|
* @return void
|
|
|
|
* @since 2.0.0
|
|
|
|
*/
|
|
|
|
public function flushAll()
|
|
|
|
{
|
|
|
|
$this->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush current session data
|
|
|
|
* @return void
|
|
|
|
* @since 2.0.0
|
|
|
|
*/
|
|
|
|
public function flush()
|
|
|
|
{
|
|
|
|
$this->data = [];
|
|
|
|
$this->closeSession(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current session ID
|
|
|
|
*
|
|
|
|
* @since 2.0.0
|
|
|
|
* @return string|false
|
|
|
|
*/
|
|
|
|
public function getSessionId()
|
|
|
|
{
|
|
|
|
return session_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function getData($key) {
|
|
|
|
return isset($this->data[$key]) ? $this->data[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setData($key, $value) {
|
|
|
|
$this->data[$key] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|