Skip to content

Commit

Permalink
Readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
condor2 committed Jun 28, 2024
1 parent c96b59c commit 5b7e196
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 142 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,29 @@ INSERT INTO " . DB_PREFIX . "category
<b>To:</b>
````
INSERT INTO `" . DB_PREFIX . "category`
````
````

# How to enable DB session.

<b>1.</b>Go to <b>system</b>-><b>config</b> and edit <b>catalog.php</b>. After this line: <b>$_['session_autostart'] = false;</b> add these lines:
````
$_['session_engine'] = 'db';
$_['session_name'] = 'OCSESSID';
````

<b>2.</b>Go to <b>system</b>-><b>config</b> and edit <b>default.php</b>. Rename this line:
````
$_['session_name'] = 'PHPSESSID';
````
<b>in to</b>
````
$_['session_name'] = 'OCSESSID';
````
<b>then add this line after OCSESSID line</b>
````
$_['session_engine'] = 'db';
````

<b>3.</b>Go to <b>system</b> and make back-up to <b>framework.php</b>, then rename <b>framework_db.php</b> to <b>framework.php</b>

<b>4.</b>Go to <b>system</b>-><b>library</b> and make back-up to <b>session.php</b>, then rename <b>session_db.php</b> to <b>session.php</b>
125 changes: 47 additions & 78 deletions upload/system/library/session/db.php
Original file line number Diff line number Diff line change
@@ -1,90 +1,59 @@
<?php
/**
* @package OpenCart
* @author Daniel Kerr
* @copyright Copyright (c) 2005 - 2024, OpenCart, Ltd. (https://www.opencart.com/)
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.opencart.com
*/
namespace Session;

/**
* Session class
*/
class Session {
protected $adaptor;
protected $session_id;
public $data = array();
final class DB {
public $maxlifetime;
public $db;

/**
* Constructor
*
* @param string $adaptor
* @param object $registry
*/
public function __construct($adaptor, $registry = '') {
$class = 'Session\\' . $adaptor;

if (class_exists($class)) {
if ($registry) {
$this->adaptor = new $class($registry);
} else {
$this->adaptor = new $class();
}

register_shutdown_function(array($this, 'close'));
public function __construct($registry) {
$this->db = $registry->get('db');

$this->maxlifetime = ini_get('session.gc_maxlifetime') !== null ? (int)ini_get('session.gc_maxlifetime') : 1440;

$this->gc();
}

public function read($session_id) {
$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', time())) . "'");

if ($query->num_rows) {
return json_decode($query->row['data'], true);
} else {
trigger_error('Error: Could not load cache adaptor ' . $adaptor . ' session!');
exit();
}
return array();
}
}

public function write($session_id, $data) {
if ($session_id) {
$this->db->query("REPLACE INTO `" . DB_PREFIX . "session` SET `session_id` = '" . $this->db->escape($session_id) . "', `data` = '" . $this->db->escape(json_encode($data)) . "', `expire` = '" . $this->db->escape(gmdate('Y-m-d H:i:s', time() + (int)$this->maxlifetime)) . "'");
}

return true;
}

/**
*
*
* @return string
*/
public function getId() {
return $this->session_id;

public function destroy($session_id) {
$this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE `session_id` = '" . $this->db->escape($session_id) . "'");

return true;
}

/**
*
*
* @param string $session_id
*
* @return string
*/
public function start($session_id = '') {
if (!$session_id) {
if (function_exists('random_bytes')) {
$session_id = substr(bin2hex(random_bytes(26)), 0, 26);
} else {
$session_id = substr(bin2hex(openssl_random_pseudo_bytes(26)), 0, 26);
}
public function gc() {
if (ini_get('session.gc_divisor') && $gc_divisor = (int)ini_get('session.gc_divisor')) {
$gc_divisor = $gc_divisor === 0 ? 100 : $gc_divisor;
} else {
$gc_divisor = 100;
}

if (preg_match('/^[a-zA-Z0-9,\-]{22,52}$/', $session_id)) {
$this->session_id = $session_id;
if (ini_get('session.gc_probability')) {
$gc_probability = (int)ini_get('session.gc_probability');
} else {
exit('Error: Invalid session ID!');
$gc_probability = 1;
}

if (mt_rand() / mt_getrandmax() < $gc_probability / $gc_divisor) {
$this->db->query("DELETE FROM `" . DB_PREFIX . "session` WHERE `expire` < '" . $this->db->escape(gmdate('Y-m-d H:i:s', time())) . "'");

return true;
}

$this->data = $this->adaptor->read($session_id);

return $session_id;
}

/**
*
*/
public function close() {
$this->adaptor->write($this->session_id, $this->data);
}

/**
*
*/
public function destroy() {
$this->adaptor->destroy($this->session_id);
}
}
}
138 changes: 75 additions & 63 deletions upload/system/library/session_db.php
Original file line number Diff line number Diff line change
@@ -1,78 +1,90 @@
<?php
/**
* @package OpenCart
* @author Daniel Kerr
* @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.opencart.com
*/

/**
* Session class
*/
class Session {
public $session_id = '';
public $data = [];
public $adaptor;
protected $adaptor;
protected $session_id;
public $data = array();

public function __construct($adaptor = 'native') {
/**
* Constructor
*
* @param string $adaptor
* @param object $registry
*/
public function __construct($adaptor, $registry = '') {
$class = 'Session\\' . $adaptor;

if (class_exists($class)) {
$this->adaptor = new $class($this);
} else {
throw new \Exception('Error: Could not load session adaptor ' . $adaptor . ' session!');
}

if ($this->adaptor) {
session_set_save_handler($this->adaptor);
}

if ($this->adaptor && !session_id()) {
ini_set('session.use_only_cookies', 'Off');
ini_set('session.use_cookies', 'On');
ini_set('session.use_trans_sid', 'Off');
ini_set('session.cookie_httponly', 'On');

if (isset($_COOKIE[session_name()]) && !preg_match('/^[a-zA-Z0-9,\-]{22,52}$/', $_COOKIE[session_name()])) {
exit('Error: Invalid session ID!');
}

session_set_cookie_params(0, '/');
session_start();
}
}

public function start($key = 'default', $value = '') {
if ($value) {
$this->session_id = $value;
} elseif (isset($_COOKIE[$key])) {
$this->session_id = $_COOKIE[$key];
if ($registry) {
$this->adaptor = new $class($registry);
} else {
$this->adaptor = new $class();
}

register_shutdown_function(array($this, 'close'));
} else {
$this->session_id = $this->createId();
}

if (!isset($_SESSION[$this->session_id])) {
$_SESSION[$this->session_id] = [];
}

$this->data = &$_SESSION[$this->session_id];

if ($key != 'PHPSESSID') {
setcookie($key, $this->session_id, ini_get('session.cookie_lifetime'), ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure'), ini_get('session.cookie_httponly'));
}

return $this->session_id;
trigger_error('Error: Could not load cache adaptor ' . $adaptor . ' session!');
exit();
}
}


/**
*
*
* @return string
*/
public function getId() {
return $this->session_id;
}

public function createId() {
if (version_compare(PHP_VERSION, '5.5.4', '>') == true && method_exists($this->adaptor, 'create_sid')) {
return $this->adaptor->create_sid();
} elseif (function_exists('random_bytes')) {
return substr(bin2hex(random_bytes(26)), 0, 26);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
return substr(bin2hex(openssl_random_pseudo_bytes(26)), 0, 26);
/**
*
*
* @param string $session_id
*
* @return string
*/
public function start($session_id = '') {
if (!$session_id) {
if (function_exists('random_bytes')) {
$session_id = substr(bin2hex(random_bytes(26)), 0, 26);
} else {
$session_id = substr(bin2hex(openssl_random_pseudo_bytes(26)), 0, 26);
}
}
}

public function destroy($key = 'default'): void {
if (isset($_SESSION[$key])) {
unset($_SESSION[$key]);
if (preg_match('/^[a-zA-Z0-9,\-]{22,52}$/', $session_id)) {
$this->session_id = $session_id;
} else {
exit('Error: Invalid session ID!');
}

setcookie($key, '', time() - 42000, ini_get('session.cookie_path'), ini_get('session.cookie_domain'));

$this->data = $this->adaptor->read($session_id);

return $session_id;
}

/**
*
*/
public function close() {
$this->adaptor->write($this->session_id, $this->data);
}

/**
*
*/
public function destroy() {
$this->adaptor->destroy($this->session_id);
}
}
}

0 comments on commit 5b7e196

Please sign in to comment.