blob: 2a8957d911c89724d4a2ddd907450a4c7042cc6e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 {
protected $adaptor;
protected $session_id;
public $data = array();
/**
* 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'));
} else {
trigger_error('Error: Could not load cache adaptor ' . $adaptor . ' session!');
exit();
}
}
/**
*
*
* @return string
*/
public function getId() {
return $this->session_id;
}
/**
*
*
* @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);
}
}
if (preg_match('/^[a-zA-Z0-9,\-]{22,52}$/', $session_id)) {
$this->session_id = $session_id;
} else {
exit('Error: Invalid session ID!');
}
$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);
}
}
|