-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSession.php
More file actions
277 lines (235 loc) · 5.66 KB
/
Copy pathSession.php
File metadata and controls
277 lines (235 loc) · 5.66 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace RocketTheme\Toolbox\Session;
use ArrayIterator;
use IteratorAggregate;
use RuntimeException;
/**
* Implements Session handling.
*
* @package RocketTheme\Toolbox\Session
* @author RocketTheme
* @license MIT
*/
class Session implements IteratorAggregate
{
/** @var bool */
protected $started = false;
/** @var Session */
protected static $instance;
/**
* @param int $lifetime Defaults to 1800 seconds.
* @param string $path Cookie path.
* @param string|null $domain Optional, domain for the session
* @throws RuntimeException
*/
public function __construct($lifetime, $path, $domain = null)
{
// Session is a singleton.
if (null !== self::$instance) {
throw new RuntimeException('Session has already been initialized.', 500);
}
// Destroy any existing sessions started with session.auto_start
if ($this->isSessionStarted()) {
session_unset();
session_destroy();
}
// Disable transparent sid support
ini_set('session.use_trans_sid', '0');
// Only allow cookies
ini_set('session.use_cookies', '1');
session_name('msF9kJcW');
session_set_cookie_params($lifetime, $path, $domain);
/** @var callable(): void $callable */
$callable = [$this, 'close'];
register_shutdown_function($callable);
session_cache_limiter('nocache');
self::$instance = $this;
}
/**
* Get current session instance.
*
* @return Session
* @throws RuntimeException
*/
public function instance()
{
if (null === self::$instance) {
throw new RuntimeException("Session hasn't been initialized.", 500);
}
return self::$instance;
}
/**
* Starts the session storage
*
* @return $this
* @throws RuntimeException
*/
public function start()
{
// Protection against invalid session cookie names throwing exception: http://php.net/manual/en/function.session-id.php#116836
if (isset($_COOKIE[session_name()]) && !preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $_COOKIE[session_name()])) {
unset($_COOKIE[session_name()]);
}
if (!session_start()) {
throw new RuntimeException('Failed to start session.', 500);
}
$this->started = true;
return $this;
}
/**
* Get session ID
*
* @return string|null Session ID
*/
public function getId()
{
return session_id() ?: null;
}
/**
* Set session Id
*
* @param string $id Session ID
* @return $this
*/
public function setId($id)
{
session_id($id);
return $this;
}
/**
* Get session name
*
* @return string|null
*/
public function getName()
{
return session_name() ?: null;
}
/**
* Set session name
*
* @param string $name
* @return $this
*/
public function setName($name)
{
session_name($name);
return $this;
}
/**
* Invalidates the current session.
*
* @return $this
*/
public function invalidate()
{
$name = $this->getName();
if (null !== $name) {
$params = session_get_cookie_params();
setcookie($name, '', time() - 42000,
$params['path'], $params['domain'],
$params['secure'], $params['httponly']
);
}
session_unset();
session_destroy();
$this->started = false;
return $this;
}
/**
* Force the session to be saved and closed
*
* @return $this
*/
public function close()
{
if ($this->started) {
session_write_close();
}
$this->started = false;
return $this;
}
/**
* Checks if an attribute is defined.
*
* @param string $name The attribute name
* @return bool True if the attribute is defined, false otherwise
*/
#[\ReturnTypeWillChange]
public function __isset($name)
{
return isset($_SESSION[$name]);
}
/**
* Returns an attribute.
*
* @param string $name The attribute name
* @return mixed
*/
#[\ReturnTypeWillChange]
public function __get($name)
{
return $_SESSION[$name] ?? null;
}
/**
* Sets an attribute.
*
* @param string $name
* @param mixed $value
* @return void
*/
#[\ReturnTypeWillChange]
public function __set($name, $value)
{
$_SESSION[$name] = $value;
}
/**
* Removes an attribute.
*
* @param string $name
* @return void
*/
#[\ReturnTypeWillChange]
public function __unset($name)
{
unset($_SESSION[$name]);
}
/**
* Returns attributes.
*
* @return array Attributes
*/
public function all()
{
return $_SESSION;
}
/**
* Retrieve an external iterator
*
* @return ArrayIterator Return an ArrayIterator of $_SESSION
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($_SESSION);
}
/**
* Checks if the session was started.
*
* @return bool
*/
public function started()
{
return $this->started;
}
/**
* http://php.net/manual/en/function.session-status.php#113468
* Check if session is started nicely.
*
* @return bool
*/
protected function isSessionStarted()
{
return php_sapi_name() !== 'cli' ? session_id() !== '' : false;
}
}