1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package core |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Session |
8
|
|
|
*/ |
9
|
|
|
class Session extends Container |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Session handler |
13
|
|
|
* |
14
|
|
|
* @var SessionHandlerInterface |
15
|
|
|
*/ |
16
|
|
|
protected $handler; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Session array |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $session = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Array of settings |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $settings = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Session key |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $key; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param SessionHandlerInterface $handler |
41
|
|
|
* @param array $settings |
42
|
|
|
* @param string $key |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
SessionHandlerInterface $handler = null, |
46
|
|
|
array $settings = array(), |
47
|
|
|
$key = 'symphony' |
48
|
|
|
) { |
49
|
|
|
parent::__construct(); |
50
|
|
|
|
51
|
|
|
$this->handler = $handler; |
52
|
|
|
|
53
|
|
|
$this->settings = array_merge([ |
54
|
|
|
'session_name' => $key, |
55
|
|
|
'session_gc_probability' => '1', |
56
|
|
|
'session_gc_divisor' => '10', |
57
|
|
|
'session_gc_maxlifetime' => '1440', |
58
|
|
|
'session_cookie_lifetime' => '1440', |
59
|
|
|
'session_cookie_path' => '/', |
60
|
|
|
'session_cookie_domain' => '', |
61
|
|
|
'session_cookie_secure' => false, |
62
|
|
|
'session_cookie_httponly' => false |
63
|
|
|
], $settings); |
64
|
|
|
|
65
|
|
|
if (empty($this->settings['session_cookie_domain'])) { |
66
|
|
|
$this->settings['session_cookie_domain'] = $this->getDomain(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->key = $this->settings['session_name']; |
70
|
|
|
$this->store[$this->key()] = []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the current domain for the Session to be saved to, if the installation |
75
|
|
|
* is on localhost, this returns null and just allows PHP to take care of setting |
76
|
|
|
* the valid domain for the Session, otherwise it will return the non-www version |
77
|
|
|
* of the domain host. |
78
|
|
|
* |
79
|
|
|
* @return string|null |
80
|
|
|
* Null if on localhost, or HTTP_HOST is not set, a string of the domain name sans |
81
|
|
|
* www otherwise |
82
|
|
|
*/ |
83
|
|
|
public function getDomain() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
if (isset($_SERVER['HTTP_HOST'])) { |
86
|
|
|
if (preg_match('/(localhost|127\.0\.0\.1)/', |
87
|
|
|
$_SERVER['HTTP_HOST']) || $_SERVER['SERVER_ADDR'] === '127.0.0.1' |
88
|
|
|
) { |
89
|
|
|
return null; // prevent problems on local setups |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return preg_replace('/(^www\.|:\d+$)/i', null, $_SERVER['HTTP_HOST']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the current session save key |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function key() |
104
|
|
|
{ |
105
|
|
|
return $this->key; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Start the session if it is not already started. |
110
|
|
|
* |
111
|
|
|
* @throws Exception when headers have already been sent |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function start() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
if (false === $this->isStarted()) { |
117
|
|
|
// Disable PHP cache headers |
118
|
|
|
session_cache_limiter(''); |
119
|
|
|
|
120
|
|
|
ini_set('session.gc_probability', $this->settings['session_gc_probability']); |
121
|
|
|
ini_set('session.gc_maxlifetime', $this->settings['session_gc_maxlifetime']); |
122
|
|
|
ini_set('session.gc_divisor', $this->settings['session_gc_divisor']); |
123
|
|
|
ini_set('session.use_cookies', '1'); |
124
|
|
|
ini_set('session.hash_bits_per_character', 5); |
125
|
|
|
|
126
|
|
|
if (!is_null($this->handler)) { |
127
|
|
|
session_set_save_handler( |
128
|
|
|
array($this->handler, 'open'), |
129
|
|
|
array($this->handler, 'close'), |
130
|
|
|
array($this->handler, 'read'), |
131
|
|
|
array($this->handler, 'write'), |
132
|
|
|
array($this->handler, 'destroy'), |
133
|
|
|
array($this->handler, 'gc') |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
session_name($this->settings['session_name']); |
138
|
|
|
|
139
|
|
|
session_set_cookie_params( |
140
|
|
|
$this->settings['session_cookie_lifetime'], |
141
|
|
|
$this->settings['session_cookie_path'], |
142
|
|
|
$this->settings['session_cookie_domain'], |
143
|
|
|
$this->settings['session_cookie_secure'], |
144
|
|
|
$this->settings['session_cookie_httponly'] |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
if (headers_sent()) { |
148
|
|
|
throw new Exception('Headers already sent. Cannot start session.'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
register_shutdown_function('session_write_close'); |
152
|
|
|
session_start(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->store =& $_SESSION; |
156
|
|
|
|
157
|
|
|
return session_id(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Is the session started |
162
|
|
|
* |
163
|
|
|
* @return boolean |
164
|
|
|
*/ |
165
|
|
|
protected function isStarted() |
166
|
|
|
{ |
167
|
|
|
if (php_sapi_name() !== 'cli') { |
168
|
|
|
if (version_compare(phpversion(), '5.4.0', '>=')) { |
169
|
|
|
return session_status() === PHP_SESSION_ACTIVE ? true : false; |
170
|
|
|
} else { |
171
|
|
|
return session_id() === '' ? false : true; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Expires the current session by unsetting the Symphony |
180
|
|
|
* namespace (`$this->key`). If `$this->store` |
181
|
|
|
* is empty, the function will destroy the entire `$this->store` |
182
|
|
|
* |
183
|
|
|
* @link http://au2.php.net/manual/en/function.session-destroy.php |
184
|
|
|
*/ |
185
|
|
|
public function expire() |
186
|
|
|
{ |
187
|
|
|
if (isset($this->store[$this->key]) && !empty($this->store[$this->key])) { |
188
|
|
|
unset($this->store[$this->key]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Calling session_destroy triggers the Session::destroy function which removes the entire session |
192
|
|
|
// from the database. To prevent logout issues between functionality that relies on $this->store, such |
193
|
|
|
// as Symphony authentication or the Members extension, only delete the $this->store if it empty! |
194
|
|
|
if (empty($this->store)) { |
195
|
|
|
session_destroy(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set a service or value in this container |
201
|
|
|
* |
202
|
|
|
* @param string $key |
203
|
|
|
* @param mixed $value |
204
|
|
|
*/ |
205
|
|
|
public function offsetSet($key, $value) |
206
|
|
|
{ |
207
|
|
|
$this->store[$this->key][$key] = $value; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get a service or value from this container |
212
|
|
|
* |
213
|
|
|
* @param string $key |
214
|
|
|
* @return mixed |
215
|
|
|
*/ |
216
|
|
|
public function offsetGet($key) |
217
|
|
|
{ |
218
|
|
|
return $this->store[$this->key][$key]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Check if the container contains the given key |
223
|
|
|
* |
224
|
|
|
* @param string $key |
225
|
|
|
* String key to check |
226
|
|
|
* @return boolean |
227
|
|
|
* Whether the key is in the container |
228
|
|
|
*/ |
229
|
|
|
public function offsetExists($key) |
230
|
|
|
{ |
231
|
|
|
return array_key_exists($key, $this->store[$this->key]); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Unset a value from the container |
236
|
|
|
* |
237
|
|
|
* @param string $key |
238
|
|
|
*/ |
239
|
|
|
public function offsetUnset($key) |
240
|
|
|
{ |
241
|
|
|
unset($this->store[$this->key][$key]); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: