From 67dd808341dcca7b0d3a147d592a775902275914 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sat, 14 Aug 2021 00:48:04 -0300 Subject: [PATCH] Support to DNS Redis connection and flush all session method Remove the nullStatement default driver loader for Database, Credis changed args constructor order --- system/credis/autoload.php | 4 +- system/login/autoload.php | 2 +- system/session/autoload.php | 77 ++++++++++++++++++++++++++++++++----- system/system.php | 11 ++++-- 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/system/credis/autoload.php b/system/credis/autoload.php index 29c9062..52c80b6 100644 --- a/system/credis/autoload.php +++ b/system/credis/autoload.php @@ -328,12 +328,12 @@ class Credis_Client { * * @param string $host The hostname of the Redis server * @param integer $port The port number of the Redis server + * @param string $password The authentication password of the Redis server * @param float $timeout Timeout period in seconds * @param string $persistent Flag to establish persistent connection * @param int $db The selected datbase of the Redis server - * @param string $password The authentication password of the Redis server */ - public function __construct($host = '127.0.0.1', $port = 6379, $timeout = null, $persistent = '', $db = 0, $password = null) + public function __construct($host = '127.0.0.1', $port = 6379, $password = null, $timeout = null, $persistent = '', $db = 0) { $this->host = (string) $host; $this->port = (int) $port; diff --git a/system/login/autoload.php b/system/login/autoload.php index b05950c..450bf10 100644 --- a/system/login/autoload.php +++ b/system/login/autoload.php @@ -37,7 +37,7 @@ class login { private $session; /** - * @param arrray $authorizedUsers + * @param array $authorizedUsers * @return void */ public function __construct($authorizedUsers){ diff --git a/system/session/autoload.php b/system/session/autoload.php index fdbb3ad..74f7721 100644 --- a/system/session/autoload.php +++ b/system/session/autoload.php @@ -1,5 +1,4 @@ closeSession(); @@ -73,6 +91,12 @@ final class Session { } + /** + * Check and iniciate the Redis connection + * + * @since 2.0.0 + * @return false|Credis + */ private function redis(){ global $engine; @@ -82,26 +106,49 @@ final class Session { $this->redisExpire = ($engine->config->get('session_redis_expire')) ?: session_cache_expire()*60; $this->redisPrefix = ($engine->config->get('session_redis_prefix')) ?: 'phacil_'; $this->redisKey = $this->redisPrefix.session_name().session_id(); - $this->redis = new Credis(); + + /** + * Instanciate the Credis object + * + * @var \Phacil\Framework\Credis + */ + $this->redis = new Credis((($engine->config->get('session_redis_dsn')) ?: '127.0.0.1'), (($engine->config->get('session_redis_port')) ?: '6379'), (($engine->config->get('session_redis_password')) ?: null)); + $_SESSION = json_decode($this->redis->get($this->redisKey), true); return $this->redis; } - /** @return void */ - private function closeSession() { - if (session_status() == PHP_SESSION_ACTIVE) { + /** + * Close sessions + * + * @param bool $force + * @return void + */ + private function closeSession($force = false) { + if (session_status() == PHP_SESSION_ACTIVE || $force) { session_unset(); session_destroy(); } + if($this->redis && $force){ + $this->redis->close(); + unset($this->redis); + } } - /** @return bool */ + /** + * Check if is secure (SSL) connection + * @return bool + */ private function isSecure() { return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443; } - + /** + * Set the Redis session data + * @return void + * @since 2.0.0 + */ public function __destruct() { if($this->redis){ @@ -110,4 +157,16 @@ final class Session { $this->redis->expire($this->redisKey, ($this->redisExpire)); } } + + /** + * Flush all session data + * @return void + * @since 2.0.0 + */ + public function flushAll(){ + if($this->redis){ + ($this->redis->flushAll()); + } + $this->closeSession(true); + } } diff --git a/system/system.php b/system/system.php index 02931d0..d198d5f 100644 --- a/system/system.php +++ b/system/system.php @@ -120,13 +120,13 @@ final class startEngineExacTI { /** @return bool */ private function checkConstantsRequired () { - $dbConsts = ['DB_DRIVER' => 'nullStatement', 'DB_HOSTNAME' => NULL, 'DB_USERNAME' => NULL, 'DB_PASSWORD' => NULL, 'DB_DATABASE' => NULL]; + /* $dbConsts = ['DB_DRIVER' => 'nullStatement', 'DB_HOSTNAME' => NULL, 'DB_USERNAME' => NULL, 'DB_PASSWORD' => NULL, 'DB_DATABASE' => NULL]; - foreach ($dbConsts as $constDB => $value) { + foreach ($dbConsts as $constDB => $value) { if (!defined($constDB)) { define($constDB, $value); } - } + } */ if (!defined('DIR_APPLICATION') || !defined('DIR_SYSTEM') || !defined('DIR_PUBLIC') || !defined('DIR_TEMPLATE') || !defined('USE_DB_CONFIG')) { return(false); @@ -244,6 +244,11 @@ final class startEngineExacTI { $this->translate = new Translate(); } + // Session + if(!isset($this->registry->$key) && $key == 'session'){ + $this->session = new Session(); + } + return (isset($this->registry->$key)) ? $this->registry->$key : NULL; }