first commit
This commit is contained in:
88
system/library/session/db.php
Normal file
88
system/library/session/db.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/*
|
||||
CREATE TABLE IF NOT EXISTS `session` (
|
||||
`session_id` varchar(32) NOT NULL,
|
||||
`data` text NOT NULL,
|
||||
`expire` datetime NOT NULL,
|
||||
PRIMARY KEY (`session_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
|
||||
*/
|
||||
namespace Opencart\System\Library\Session;
|
||||
/**
|
||||
* Class DB
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class DB {
|
||||
private object $db;
|
||||
private object $config;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $registry) {
|
||||
$this->db = $registry->get('db');
|
||||
$this->config = $registry->get('config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Read
|
||||
*
|
||||
* @param string $session_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function read(string $session_id): array {
|
||||
$query = $this->db->query("SELECT `data` FROM `" . DB_PREFIX . "session` WHERE `session_id` = '" . $this->db->escape($session_id) . "' AND `expire` > '" . $this->db->escape(gmdate('Y-m-d H:i:s')) . "'");
|
||||
|
||||
if ($query->num_rows) {
|
||||
return (array)json_decode($query->row['data'], true);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write
|
||||
*
|
||||
* @param string $session_id
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function write(string $session_id, array $data): bool {
|
||||
if ($session_id) {
|
||||
$this->db->query("REPLACE INTO `" . DB_PREFIX . "session` SET `session_id` = '" . $this->db->escape($session_id) . "', `data` = '" . $this->db->escape($data ? json_encode($data) : '') . "', `expire` = '" . $this->db->escape(gmdate('Y-m-d H:i:s', time() + $this->config->get('session_expire'))) . "'");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy
|
||||
*
|
||||
* @param string $session_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(string $session_id): bool {
|
||||
$this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE `session_id` = '" . $this->db->escape($session_id) . "'");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* GC
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function gc(): bool {
|
||||
if (round(rand(1, $this->config->get('session_divisor') / $this->config->get('session_probability'))) == 1) {
|
||||
$this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE `expire` < '" . $this->db->escape(gmdate('Y-m-d H:i:s', time())) . "'");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
83
system/library/session/file.php
Normal file
83
system/library/session/file.php
Normal file
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Session;
|
||||
/**
|
||||
* Class File
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class File {
|
||||
private object $config;
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $registry) {
|
||||
$this->config = $registry->get('config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Read
|
||||
*
|
||||
* @param string $session_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function read(string $session_id): array {
|
||||
$file = DIR_SESSION . 'sess_' . basename($session_id);
|
||||
|
||||
if (is_file($file)) {
|
||||
return json_decode(file_get_contents($file), true);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write
|
||||
*
|
||||
* @param string $session_id
|
||||
* @param string $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function write(string $session_id, array $data): bool {
|
||||
file_put_contents(DIR_SESSION . 'sess_' . basename($session_id), json_encode($data));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy
|
||||
*
|
||||
* @param string $session_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function destroy(string $session_id): void {
|
||||
$file = DIR_SESSION . 'sess_' . basename($session_id);
|
||||
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GC
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function gc(): void {
|
||||
if (round(rand(1, $this->config->get('session_divisor') / $this->config->get('session_probability'))) == 1) {
|
||||
$expire = time() - $this->config->get('session_expire');
|
||||
|
||||
$files = glob(DIR_SESSION . 'sess_*');
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (is_file($file) && filemtime($file) < $expire) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
81
system/library/session/redis.php
Normal file
81
system/library/session/redis.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Session;
|
||||
/**
|
||||
* Class Redis
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Redis {
|
||||
private object $config;
|
||||
private object $redis;
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param object $registry
|
||||
*/
|
||||
public function __construct(\Opencart\System\Engine\Registry $registry) {
|
||||
$this->config = $registry->get('config');
|
||||
|
||||
try {
|
||||
$this->redis = new \Redis();
|
||||
$this->redis->pconnect(CACHE_HOSTNAME, CACHE_PORT);
|
||||
$this->prefix = CACHE_PREFIX . '.session.'; // session prefix to identify session keys
|
||||
} catch (\RedisException $e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read
|
||||
*
|
||||
* @param string $session_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function read(string $session_id): array {
|
||||
$data = $this->redis->get($this->prefix . $session_id);
|
||||
if (is_null($data) || empty($data))
|
||||
return [];
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write
|
||||
*
|
||||
* @param string $session_id
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function write(string $session_id, array $data): bool {
|
||||
if ($session_id) {
|
||||
$this->redis->set($this->prefix . $session_id, $data ? json_encode($data) : '', $this->config->get('session_expire'));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy
|
||||
*
|
||||
* @param string $session_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(string $session_id): bool {
|
||||
$this->redis->unlink($this->prefix . $session_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* GC
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function gc(): bool {
|
||||
// Redis will take care of Garbage Collection itself.
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user