1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Traits; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon; |
5
|
|
|
use PHPDaemon\Core\Debug; |
6
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
7
|
|
|
use PHPDaemon\FS\File; |
8
|
|
|
use PHPDaemon\FS\FileSystem; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Sessions |
12
|
|
|
* @package PHPDaemon\Traits |
13
|
|
|
* @author Zorin Vasily <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
trait Sessions { |
16
|
|
|
/** |
17
|
|
|
* @var string Session ID |
18
|
|
|
*/ |
19
|
|
|
protected $sessionId; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var integer |
23
|
|
|
*/ |
24
|
|
|
protected $sessionStartTimeout = 10; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var boolean |
28
|
|
|
*/ |
29
|
|
|
protected $sessionStarted = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var boolean |
33
|
|
|
*/ |
34
|
|
|
protected $sessionFlushing = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var resource |
38
|
|
|
*/ |
39
|
|
|
protected $sessionFp; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $sessionPrefix = 'sess_'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Is session started? |
48
|
|
|
* @return boolean |
49
|
|
|
*/ |
50
|
|
|
public function sessionStarted() { |
51
|
|
|
return $this->sessionStarted; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Deferred event 'onSessionStart' |
56
|
|
|
* @return callable |
57
|
|
|
*/ |
58
|
|
|
public function onSessionStartEvent() { |
59
|
|
|
return function ($sessionStartEvent) { |
60
|
|
|
/** @var \PHPDaemon\Core\DeferredEvent $sessionStartEvent */ |
61
|
|
|
$name = ini_get('session.name'); |
62
|
|
|
$sid = $this->getCookieStr($name); |
|
|
|
|
63
|
|
|
if ($sid === '') { |
64
|
|
|
$this->sessionStartNew(function ($success) use ($sessionStartEvent) { |
65
|
|
|
$sessionStartEvent->setResult($success); |
66
|
|
|
}); |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
$this->onSessionRead(function ($session) use ($sessionStartEvent) { |
|
|
|
|
70
|
|
|
if ($this->getSessionState() === null) { |
71
|
|
|
$this->sessionStartNew(function ($success) use ($sessionStartEvent) { |
72
|
|
|
$sessionStartEvent->setResult($success); |
73
|
|
|
}); |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
$sessionStartEvent->setResult(true); |
77
|
|
|
}); |
78
|
|
|
}; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Deferred event 'onSessionRead' |
83
|
|
|
* @return callable |
84
|
|
|
*/ |
85
|
|
|
public function onSessionReadEvent() { |
86
|
|
|
return function ($sessionEvent) { |
87
|
|
|
/** @var \PHPDaemon\Core\DeferredEvent $sessionEvent */ |
88
|
|
|
$name = ini_get('session.name'); |
89
|
|
|
$sid = $this->getCookieStr($name); |
|
|
|
|
90
|
|
|
if ($sid === '') { |
91
|
|
|
$sessionEvent->setResult(false); |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
if ($this->getSessionState() !== null) { |
95
|
|
|
$sessionEvent->setResult(true); |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
$this->sessionRead($sid, function ($data) use ($sessionEvent, $sid) { |
99
|
|
|
$canDecode = $data !== false && $this->sessionDecode($data); |
100
|
|
|
$sessionEvent->setResult($canDecode); |
101
|
|
|
}); |
102
|
|
|
}; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Reads session data |
107
|
|
|
* @param string $sid Session ID |
108
|
|
|
* @param callable $cb Callback |
|
|
|
|
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function sessionRead($sid, $cb = null) { |
112
|
|
|
FileSystem::open(FileSystem::genRndTempnamPrefix(session_save_path(), $this->sessionPrefix) . basename($sid), 'r+!', function ($fp) use ($cb) { |
|
|
|
|
113
|
|
|
if (!$fp) { |
114
|
|
|
call_user_func($cb, false); |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
$fp->readAll(function ($fp, $data) use ($cb) { |
118
|
|
|
$this->sessionFp = $fp; |
119
|
|
|
call_user_func($cb, $data); |
120
|
|
|
}); |
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Commmit session data |
126
|
|
|
* @param callable $cb Callback |
|
|
|
|
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function sessionCommit($cb = null) { |
130
|
|
|
if (!$this->sessionFp || $this->sessionFlushing) { |
131
|
|
|
if ($cb) { |
132
|
|
|
call_user_func($cb, false); |
133
|
|
|
} |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
$this->sessionFlushing = true; |
137
|
|
|
$data = $this->sessionEncode(); |
138
|
|
|
$l = strlen($data); |
139
|
|
|
$cb = CallbackWrapper::wrap($cb); |
|
|
|
|
140
|
|
|
$this->sessionFp->write($data, function ($file, $result) use ($l, $cb) { |
|
|
|
|
141
|
|
|
$file->truncate($l, function ($file, $result) use ($cb) { |
|
|
|
|
142
|
|
|
$this->sessionFlushing = false; |
143
|
|
|
if ($cb) { |
144
|
|
|
call_user_func($cb, true); |
145
|
|
|
} |
146
|
|
|
}); |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Session start |
152
|
|
|
* @param boolean $force_start |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
protected function sessionStart($force_start = true) { |
|
|
|
|
156
|
|
|
if ($this->sessionStarted) { |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
$this->sessionStarted = true; |
160
|
|
|
if (!$this instanceof \PHPDaemon\HTTPRequest\Generic) { |
161
|
|
|
Daemon::log('Called ' . get_class($this). '(trait \PHPDaemon\Traits\Sessions)->sessionStart() outside of Request. You should use onSessionStart.'); |
|
|
|
|
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
$f = true; // hack to avoid a sort of "race condition" |
165
|
|
|
$this->onSessionStart(function ($event) use (&$f) { |
|
|
|
|
166
|
|
|
$f = false; |
167
|
|
|
$this->wakeup(); |
|
|
|
|
168
|
|
|
}); |
169
|
|
|
if ($f) { |
170
|
|
|
$this->sleep($this->sessionStartTimeout); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Start new session |
176
|
|
|
* @param callable $cb Callback |
|
|
|
|
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
protected function sessionStartNew($cb = null) { |
180
|
|
|
FileSystem::tempnam(session_save_path(), $this->sessionPrefix, function ($fp) use ($cb) { |
181
|
|
|
if (!$fp) { |
182
|
|
|
call_user_func($cb, false); |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
$this->sessionFp = $fp; |
186
|
|
|
$this->sessionId = substr(basename($fp->path), strlen($this->sessionPrefix)); |
187
|
|
|
$this->setcookie( |
|
|
|
|
188
|
|
|
ini_get('session.name') |
189
|
|
|
, $this->sessionId |
190
|
|
|
, ini_get('session.cookie_lifetime') |
191
|
|
|
, ini_get('session.cookie_path') |
192
|
|
|
, ini_get('session.cookie_domain') |
193
|
|
|
, ini_get('session.cookie_secure') |
194
|
|
|
, ini_get('session.cookie_httponly') |
195
|
|
|
); |
196
|
|
|
call_user_func($cb, true); |
197
|
|
|
}); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Encodes session data |
202
|
|
|
* @return string|false |
203
|
|
|
*/ |
204
|
|
|
protected function sessionEncode() { |
205
|
|
|
$type = ini_get('session.serialize_handler'); |
206
|
|
|
if ($type === 'php') { |
207
|
|
|
return $this->serialize_php($this->getSessionState()); |
208
|
|
|
} |
209
|
|
|
if ($type === 'php_binary') { |
210
|
|
|
return igbinary_serialize($this->getSessionState()); |
211
|
|
|
} |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Set session state |
217
|
|
|
* @param mixed $var |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
protected function setSessionState($var) { |
221
|
|
|
$this->attrs->session = $var; |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get session state |
226
|
|
|
* @return mixed |
227
|
|
|
*/ |
228
|
|
|
protected function getSessionState() { |
229
|
|
|
return $this->attrs->session; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Decodes session data |
234
|
|
|
* @param string $str Data |
235
|
|
|
* @return boolean |
236
|
|
|
*/ |
237
|
|
|
protected function sessionDecode($str) { |
238
|
|
|
$type = ini_get('session.serialize_handler'); |
239
|
|
|
if ($type === 'php') { |
240
|
|
|
$this->setSessionState($this->unserialize_php($str)); |
241
|
|
|
return true; |
242
|
|
|
} |
243
|
|
|
if ($type === 'php_binary') { |
244
|
|
|
$this->setSessionState(igbinary_unserialize($str)); |
245
|
|
|
return true; |
246
|
|
|
} |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* session_encode() - clone, which not require session_start() |
252
|
|
|
* @see http://www.php.net/manual/en/function.session-encode.php |
253
|
|
|
* @param array $array |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
public function serialize_php($array) { |
257
|
|
|
$raw = ''; |
258
|
|
|
$line = 0; |
259
|
|
|
$keys = array_keys($array); |
260
|
|
|
foreach ($keys as $key) { |
261
|
|
|
$value = $array[$key]; |
262
|
|
|
$line++; |
263
|
|
|
$raw .= $key . '|'; |
264
|
|
|
if (is_array($value) && isset($value['huge_recursion_blocker_we_hope'])) { |
265
|
|
|
$raw .= 'R:' . $value['huge_recursion_blocker_we_hope'] . ';'; |
266
|
|
|
} else { |
267
|
|
|
$raw .= serialize($value); |
268
|
|
|
} |
269
|
|
|
$array[$key] = array('huge_recursion_blocker_we_hope' => $line); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $raw; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* session_decode() - clone, which not require session_start() |
277
|
|
|
* @see http://www.php.net/manual/en/function.session-decode.php#108037 |
278
|
|
|
* @param string $session_data |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
protected function unserialize_php($session_data) { |
282
|
|
|
$return_data = array(); |
283
|
|
|
$offset = 0; |
284
|
|
|
while ($offset < strlen($session_data)) { |
285
|
|
|
if (!strstr(substr($session_data, $offset), "|")) { |
286
|
|
|
return $return_data; |
287
|
|
|
//throw new \Exception("invalid session data, remaining: " . substr($session_data, $offset)); |
|
|
|
|
288
|
|
|
} |
289
|
|
|
$pos = strpos($session_data, "|", $offset); |
290
|
|
|
$num = $pos - $offset; |
291
|
|
|
$varname = substr($session_data, $offset, $num); |
292
|
|
|
$offset += $num + 1; |
293
|
|
|
$data = unserialize(substr($session_data, $offset)); |
294
|
|
|
$return_data[$varname] = $data; |
295
|
|
|
$offset += strlen(serialize($data)); |
296
|
|
|
} |
297
|
|
|
return $return_data; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.