|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Session class. |
|
7
|
|
|
* |
|
8
|
|
|
* @package App |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright YetiForce S.A. |
|
11
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
|
12
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Session |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string Session path |
|
18
|
|
|
*/ |
|
19
|
|
|
const SESSION_PATH = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'cache' . \DIRECTORY_SEPARATOR . 'session'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
1 |
|
* Session handler. |
|
23
|
|
|
* |
|
24
|
1 |
|
* @var \App\Session\Base |
|
25
|
1 |
|
*/ |
|
26
|
|
|
public static $pool; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Initialize session class. |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function init() |
|
32
|
|
|
{ |
|
33
|
|
|
if (PHP_SESSION_ACTIVE === \session_status()) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
if (self::load()) { |
|
37
|
|
|
\session_set_save_handler(self::$pool, true); |
|
38
|
|
|
} |
|
39
|
|
|
\session_start(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Load session driver. |
|
44
|
5 |
|
* |
|
45
|
|
|
* @return bool |
|
46
|
5 |
|
*/ |
|
47
|
|
|
public static function load(): bool |
|
48
|
|
|
{ |
|
49
|
5 |
|
if (empty(self::$pool) && !empty(\Config\Performance::$SESSION_DRIVER)) { |
|
50
|
|
|
$className = '\App\Session\\' . \Config\Performance::$SESSION_DRIVER; |
|
51
|
|
|
self::$pool = new $className(); |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns a session Item representing the specified key. |
|
59
|
4 |
|
* |
|
60
|
|
|
* @param string $key |
|
61
|
4 |
|
* |
|
62
|
|
|
* @return array|string |
|
63
|
|
|
*/ |
|
64
|
4 |
|
public static function get($key) |
|
65
|
|
|
{ |
|
66
|
|
|
if (empty(static::$pool)) { |
|
67
|
|
|
return $_SESSION[$key] ?? null; |
|
68
|
|
|
} |
|
69
|
|
|
return static::$pool->get($key); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Confirms if the session contains specified session item. |
|
74
|
|
|
* |
|
75
|
5 |
|
* @param string $key |
|
76
|
|
|
* |
|
77
|
5 |
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function has($key) |
|
80
|
5 |
|
{ |
|
81
|
|
|
if (empty(static::$pool)) { |
|
82
|
|
|
return isset($_SESSION[$key]); |
|
83
|
|
|
} |
|
84
|
|
|
return static::$pool->has($key); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Session Save. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $key |
|
91
|
|
|
* @param mixed $value |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function set($key, $value = null) |
|
96
|
|
|
{ |
|
97
|
|
|
if (empty(static::$pool)) { |
|
98
|
|
|
return $_SESSION[$key] = $value; |
|
99
|
|
|
} |
|
100
|
|
|
return static::$pool->set($key, $value); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Removes the item from the session. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $key |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function delete($key) |
|
111
|
|
|
{ |
|
112
|
|
|
if (empty(static::$pool)) { |
|
113
|
|
|
unset($_SESSION[$key]); |
|
114
|
|
|
} |
|
115
|
|
|
static::$pool->delete($key); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Update the current session id with a newly generated one. |
|
120
|
|
|
* |
|
121
|
|
|
* @see https://php.net/manual/en/function.session-regenerate-id.php |
|
122
|
|
|
* |
|
123
|
|
|
* @param bool $deleteOldSession |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function regenerateId($deleteOldSession = false) |
|
126
|
|
|
{ |
|
127
|
|
|
if (empty(static::$pool)) { |
|
128
|
|
|
\session_regenerate_id($deleteOldSession); |
|
129
|
|
|
} else { |
|
130
|
|
|
static::$pool->regenerateId($deleteOldSession); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Destroys all data registered to a session. |
|
136
|
|
|
* |
|
137
|
|
|
* @see https://php.net/manual/en/function.session-destroy.php |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function destroy() |
|
140
|
|
|
{ |
|
141
|
|
|
$_SESSION = []; |
|
142
|
|
|
if (\PHP_SAPI !== 'cli' && !headers_sent()) { |
|
143
|
|
|
$params = session_get_cookie_params(); |
|
144
|
|
|
setcookie(session_name(), '', time() - 42000, |
|
145
|
|
|
$params['path'], $params['domain'], |
|
146
|
|
|
$params['secure'], $params['httponly'] |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
\session_destroy(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Function to clean session. Removed old session. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string[] |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function clean() |
|
158
|
|
|
{ |
|
159
|
|
|
if (!empty(static::$pool)) { |
|
160
|
|
|
return static::$pool->clean(); |
|
161
|
|
|
} |
|
162
|
|
|
return []; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Function to clean all session. |
|
167
|
|
|
* |
|
168
|
|
|
* @return int |
|
169
|
|
|
*/ |
|
170
|
|
|
public static function cleanAll(): int |
|
171
|
|
|
{ |
|
172
|
|
|
if (!empty(static::$pool)) { |
|
173
|
|
|
return static::$pool->cleanAll(); |
|
174
|
|
|
} |
|
175
|
|
|
return 0; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Function to get session data by id. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $sessionId |
|
182
|
|
|
* |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function getById(string $sessionId): array |
|
186
|
|
|
{ |
|
187
|
|
|
if (!empty(static::$pool)) { |
|
188
|
|
|
return static::$pool->getById($sessionId); |
|
189
|
|
|
} |
|
190
|
|
|
return []; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|