|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Session; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Session\Exception\SessionAlreadyStartedException; |
|
28
|
|
|
use Brickoo\Component\Common\Assert; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* SessionManager |
|
32
|
|
|
* |
|
33
|
|
|
* Object wrapper for the PHP session handling. |
|
34
|
|
|
* @author Celestino Diaz <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class SessionManager { |
|
37
|
|
|
|
|
38
|
|
|
/** @var boolean */ |
|
39
|
|
|
protected static $sessionStarted; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns the current session identifier. |
|
43
|
|
|
* @return string the session identifier |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getId() { |
|
46
|
1 |
|
return session_id(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set the session identifier. |
|
51
|
|
|
* @param string $identifier the session identifier |
|
52
|
|
|
* @throws \InvalidArgumentException if the argument is not valid |
|
53
|
|
|
* @throws \Brickoo\Component\Session\Exception\SessionAlreadyStartedException |
|
54
|
|
|
* @return string the previously used session identifier |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setId($identifier) { |
|
57
|
1 |
|
Assert::isString($identifier); |
|
58
|
1 |
|
$this->checkSessionStart(); |
|
59
|
1 |
|
return session_id($identifier); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Regenerates the session id. |
|
64
|
|
|
* The values of the current session should be kept. |
|
65
|
|
|
* @return \Brickoo\Component\Session\SessionManager |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function regenerateId() { |
|
68
|
1 |
|
session_regenerate_id(false); |
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the current session name. |
|
74
|
|
|
* @return string the current session name |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getName() { |
|
77
|
1 |
|
return session_name(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets the session name. |
|
82
|
|
|
* @param string $name the session name |
|
83
|
|
|
* @throws \InvalidArgumentException if the argument is not valid |
|
84
|
|
|
* @throws \Brickoo\Component\Session\Exception\SessionAlreadyStartedException |
|
85
|
|
|
* @return string the previously used session name |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function setName($name) { |
|
88
|
1 |
|
Assert::isString($name); |
|
89
|
1 |
|
$this->checkSessionStart(); |
|
90
|
1 |
|
return session_name($name); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the current session cookie parameters. |
|
95
|
|
|
* @return array the cookie parameters |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getCookieParams() { |
|
98
|
1 |
|
return session_get_cookie_params(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the session cookie parameters. |
|
103
|
|
|
* @param integer $lifetime the cookie/session lifetime |
|
104
|
|
|
* @param string $path the request path to listen to |
|
105
|
|
|
* @param string $domain the domain to listen to |
|
106
|
|
|
* @param boolean $secure only should be sent while on https mode |
|
107
|
|
|
* @param boolean $httpOnly restriction to the http protocol |
|
108
|
|
|
* @throws \InvalidArgumentException if an argument is not valid |
|
109
|
|
|
* @throws \Brickoo\Component\Session\Exception\SessionAlreadyStartedException |
|
110
|
|
|
* @return \Brickoo\Component\Session\SessionManager |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function setCookieParams($lifetime, $path, $domain, $secure = false, $httpOnly = false) { |
|
113
|
1 |
|
Assert::isInteger($lifetime); |
|
114
|
1 |
|
Assert::isString($path); |
|
115
|
1 |
|
Assert::isString($domain); |
|
116
|
1 |
|
Assert::isBoolean($secure); |
|
117
|
1 |
|
Assert::isBoolean($httpOnly); |
|
118
|
|
|
|
|
119
|
1 |
|
$this->checkSessionStart(); |
|
120
|
1 |
|
session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly); |
|
121
|
1 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns the current session cache limiter. |
|
126
|
|
|
* @return string the current used session cache limiter |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function getCacheLimiter() { |
|
129
|
1 |
|
return session_cache_limiter(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Sets the session cache limiter. |
|
134
|
|
|
* @see session_cache_limiter() for possible values |
|
135
|
|
|
* @param string $limiter the cache limiter |
|
136
|
|
|
* @throws \InvalidArgumentException if the argument is not valid |
|
137
|
|
|
* @return \Brickoo\Component\Session\SessionManager |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function setCacheLimiter($limiter) { |
|
140
|
1 |
|
Assert::isString($limiter); |
|
141
|
1 |
|
session_cache_limiter($limiter); |
|
142
|
1 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Starts the session. |
|
147
|
|
|
* @throws \Brickoo\Component\Session\Exception\SessionAlreadyStartedException |
|
148
|
|
|
* @return \Brickoo\Component\Session\SessionManager |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function start() { |
|
151
|
2 |
|
$this->checkSessionStart(); |
|
152
|
|
|
|
|
153
|
2 |
|
if (session_start()) { |
|
154
|
2 |
|
self::$sessionStarted = true; |
|
155
|
2 |
|
} |
|
156
|
2 |
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Checks if the session has already been started. |
|
161
|
|
|
* @return boolean check result |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function hasStarted() { |
|
164
|
1 |
|
return self::$sessionStarted === true; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Stops the session by writing the current session values. |
|
169
|
|
|
* @return \Brickoo\Component\Session\SessionManager |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function stop() { |
|
172
|
1 |
|
session_write_close(); |
|
173
|
1 |
|
self::$sessionStarted = null; |
|
174
|
1 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Destroys the current session. |
|
179
|
|
|
* To prevent unexpected behaviours |
|
180
|
|
|
* the global $_SESSION variable should be cleared, |
|
181
|
|
|
* also the cookie (if used) should be expired. |
|
182
|
|
|
* This method is commonly used when logging-out an user. |
|
183
|
|
|
* @param null|callable $callback |
|
184
|
|
|
* @return \Brickoo\Component\Session\SessionManager |
|
185
|
|
|
*/ |
|
186
|
1 |
|
public function destroy($callback = null) { |
|
187
|
1 |
|
if ($this->isCookieUsed()) { |
|
188
|
1 |
|
$callback = is_callable($callback) ? $callback : "setcookie"; |
|
189
|
1 |
|
$params = session_get_cookie_params(); |
|
190
|
1 |
|
call_user_func_array($callback, [ |
|
191
|
1 |
|
session_name(), "", time() - (365 * 24 * 60 * 60), |
|
192
|
1 |
|
$params["path"], $params["domain"], |
|
193
|
1 |
|
$params["secure"], $params["httponly"] |
|
194
|
1 |
|
]); |
|
195
|
1 |
|
} |
|
196
|
1 |
|
$_SESSION = []; |
|
197
|
1 |
|
session_destroy(); |
|
198
|
|
|
|
|
199
|
1 |
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Checks if the session has been started. |
|
204
|
|
|
* If the session has been started something |
|
205
|
|
|
* is going wrong, throws an exception |
|
206
|
|
|
* @throws \Brickoo\Component\Session\Exception\SessionAlreadyStartedException |
|
207
|
|
|
* @return void |
|
208
|
|
|
*/ |
|
209
|
5 |
|
private function checkSessionStart() { |
|
210
|
5 |
|
if ($this->hasStarted()) { |
|
211
|
1 |
|
throw new SessionAlreadyStartedException(); |
|
212
|
|
|
} |
|
213
|
5 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Checks if session cookies are used. |
|
217
|
|
|
* @return boolean check result |
|
218
|
|
|
*/ |
|
219
|
1 |
|
private function isCookieUsed() { |
|
220
|
1 |
|
return (boolean)ini_get("session.use_cookies"); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|