forked from ezyang/simpletest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookies.php
More file actions
382 lines (347 loc) · 10.1 KB
/
Copy pathcookies.php
File metadata and controls
382 lines (347 loc) · 10.1 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
require_once dirname(__FILE__) . '/url.php';
/**
* Cookie data holder.
* Cookie rules are full of pretty arbitary stuff
* I have used: http://www.cookiecentral.com/faq/
*/
class SimpleCookie
{
private $expiry;
private $host;
private $is_secure;
private $name;
private $path;
private $value;
/**
* Constructor. Sets the stored values.
*
* @param string $name Cookie key.
* @param string $value Value of cookie.
* @param string $path Cookie path if not host wide.
* @param string $expiry Expiry date as string.
* @param bool $is_secure Currently ignored.
*/
public function __construct($name, $value = false, $path = false, $expiry = false, $is_secure = false)
{
$this->host = false;
$this->name = $name;
$this->value = $value;
$this->path = ($path ? $this->fixPath($path) : '/');
$this->expiry = false;
if (is_string($expiry)) {
$this->expiry = strtotime($expiry);
} elseif (is_integer($expiry)) {
$this->expiry = $expiry;
}
$this->is_secure = $is_secure;
}
/**
* Sets the host. The cookie rules determine that
* the first two parts are taken for certain TLDs and three for others.
* If the new host does not match these rules then the call will fail.
*
* @param string $host New hostname.
*
* @return bool True if hostname is valid.
*/
public function setHost($host)
{
if ($host = $this->truncateHost($host)) {
$this->host = $host;
return true;
}
return false;
}
/**
* Accessor for the truncated host to which this cookie applies.
*
* @return string Truncated hostname.
*/
public function getHost()
{
return $this->host;
}
/**
* Test for a cookie being valid for a host name.
*
* @param string $host Host to test against.
*
* @return bool True if the cookie would be valid here.
*/
public function isValidHost($host)
{
return ($this->truncateHost($host) === $this->getHost());
}
/**
* Extracts just the domain part that determines a cookie's host validity.
*
* @param string $host Host name to truncate.
*
* @return string Domain or false on a bad host.
*/
protected function truncateHost($host)
{
$tlds = SimpleUrl::getAllTopLevelDomains();
if (preg_match('/[a-z\-]+\.(' . $tlds . ')$/i', $host, $matches)) {
return $matches[0];
} elseif (preg_match('/[a-z\-]+\.[a-z\-]+\.[a-z\-]+$/i', $host, $matches)) {
return $matches[0];
}
return false;
}
/**
* Accessor for name.
*
* @return string Cookie key.
*/
public function getName()
{
return $this->name;
}
/**
* Accessor for value. A deleted cookie will have an empty string for this.
*
* @return string Cookie value.
*/
public function getValue()
{
return $this->value;
}
/**
* Accessor for path.
*
* @return string Valid cookie path.
*/
public function getPath()
{
return $this->path;
}
/**
* Tests a path to see if the cookie applies there.
* The test path must be longer or equal to the cookie path.
*
* @param string $path Path to test against.
*
* @return bool True if cookie valid here.
*/
public function isValidPath($path)
{
return (strncmp($this->fixPath($path), $this->getPath(), strlen($this->getPath())) == 0);
}
/**
* Accessor for expiry.
*
* @return string Expiry string.
*/
public function getExpiry()
{
if (! $this->expiry) {
return false;
}
return gmdate('D, d M Y H:i:s', $this->expiry) . ' GMT';
}
/**
* Test to see if cookie is expired against the cookie format time or timestamp.
* Will give true for a session cookie.
*
* @param integer/string $now Time to test against. Result will be false if this time
* is later than the cookie expiry. Can be either a timestamp
* integer or a cookie format date.
*/
public function isExpired($now)
{
if (! $this->expiry) {
return true;
}
if (is_string($now)) {
$now = strtotime($now);
}
return ($this->expiry < $now);
}
/**
* Ages the cookie by the specified number of seconds.
*
* @param int $interval In seconds.
*/
public function agePrematurely($interval)
{
if ($this->expiry) {
$this->expiry -= $interval;
}
}
/**
* Accessor for the secure flag.
*
* @return bool True if cookie needs SSL.
*/
public function isSecure()
{
return $this->is_secure;
}
/**
* Adds a trailing and leading slash to the path if missing.
*
* @param string $path Path to fix.
*/
protected function fixPath($path)
{
if (substr($path, 0, 1) != '/') {
$path = '/' . $path;
}
if (substr($path, -1, 1) != '/') {
$path .= '/';
}
return $path;
}
}
/**
* Repository for cookies. This stuff is a tiny bit browser dependent.
*/
class SimpleCookieJar
{
private $cookies;
/**
* Constructor. Jar starts empty.
*/
public function __construct()
{
$this->cookies = array();
}
/**
* Removes expired and temporary cookies as if the browser was closed and re-opened.
*
* @param string/integer $date Time to test expiry against.
*/
public function restartSession($date = false)
{
$surviving_cookies = array();
for ($i = 0; $i < count($this->cookies); $i++) {
if (! $this->cookies[$i]->getValue()) {
continue;
}
if (! $this->cookies[$i]->getExpiry()) {
continue;
}
if ($date && $this->cookies[$i]->isExpired($date)) {
continue;
}
$surviving_cookies[] = $this->cookies[$i];
}
$this->cookies = $surviving_cookies;
}
/**
* Ages all cookies in the cookie jar.
*
* @param int $interval The old session is moved into the past by this number
* of seconds. Cookies now over age will be removed.
*/
public function agePrematurely($interval)
{
for ($i = 0; $i < count($this->cookies); $i++) {
$this->cookies[$i]->agePrematurely($interval);
}
}
/**
* Sets an additional cookie. If a cookie has the same name and path it is replaced.
*
* @param string $name Cookie key.
* @param string $value Value of cookie.
* @param string $host Host upon which the cookie is valid.
* @param string $path Cookie path if not host wide.
* @param string $expiry Expiry date.
*/
public function setCookie($name, $value, $host = false, $path = '/', $expiry = false)
{
$cookie = new SimpleCookie($name, $value, $path, $expiry);
if ($host) {
$cookie->setHost($host);
}
$this->cookies[$this->findFirstMatch($cookie)] = $cookie;
}
/**
* Finds a matching cookie to write over or the first empty slot if none.
*
* @param SimpleCookie $cookie Cookie to write into jar.
*
* @return int Available slot.
*/
protected function findFirstMatch($cookie)
{
for ($i = 0; $i < count($this->cookies); $i++) {
$is_match = $this->isMatch(
$cookie,
$this->cookies[$i]->getHost(),
$this->cookies[$i]->getPath(),
$this->cookies[$i]->getName());
if ($is_match) {
return $i;
}
}
return count($this->cookies);
}
/**
* Reads the most specific cookie value from the browser cookies.
* Looks for the longest path that matches.
*
* @param string $host Host to search.
* @param string $path Applicable path.
* @param string $name Name of cookie to read.
*
* @return string False if not present, else the value as a string.
*/
public function getCookieValue($host, $path, $name)
{
$longest_path = '';
foreach ($this->cookies as $cookie) {
if ($this->isMatch($cookie, $host, $path, $name)) {
if (strlen($cookie->getPath()) > strlen($longest_path)) {
$value = $cookie->getValue();
$longest_path = $cookie->getPath();
}
}
}
return (isset($value) ? $value : false);
}
/**
* Tests cookie for matching against search criteria.
*
* @param SimpleTest $cookie Cookie to test.
* @param string $host Host must match.
* @param string $path Cookie path must be shorter than this path.
* @param string $name Name must match.
*
* @return bool True if matched.
*/
protected function isMatch($cookie, $host, $path, $name)
{
if ($cookie->getName() != $name) {
return false;
}
if ($host && $cookie->getHost() && ! $cookie->isValidHost($host)) {
return false;
}
if (! $cookie->isValidPath($path)) {
return false;
}
return true;
}
/**
* Uses a URL to sift relevant cookies by host and path.
* Results are list of strings of form "name=value".
*
* @param SimpleUrl $url Url to select by.
*
* @return array Valid name and value pairs.
*/
public function selectAsPairs($url)
{
$pairs = array();
foreach ($this->cookies as $cookie) {
if ($this->isMatch($cookie, $url->getHost(), $url->getPath(), $cookie->getName())) {
$pairs[] = $cookie->getName() . '=' . $cookie->getValue();
}
}
return $pairs;
}
}