forked from ezyang/simpletest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.php
More file actions
590 lines (525 loc) · 16.2 KB
/
Copy pathurl.php
File metadata and controls
590 lines (525 loc) · 16.2 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
require_once dirname(__FILE__) . '/encoding.php';
/**
* URL parser to replace parse_url() PHP function which got broken in PHP 4.3.0.
* Adds some browser specific functionality such as expandomatics.
* Guesses a bit trying to separate the host from the path and
* tries to keep a raw, possibly unparsable, request string as long as possible.
*
* @todo Check PHP version compatibility and if possible, leverage native PHP functionality.
*/
class SimpleUrl
{
public $path;
private $fragment;
private $host;
private $password;
private $port;
private $raw = false;
private $request;
private $scheme;
private $target;
private $username;
private $x;
private $y;
/**
* Constructor. Parses URL into sections.
*
* @param string $url Incoming URL.
*/
public function __construct($url = '')
{
list($x, $y) = $this->chompCoordinates($url);
$this->setCoordinates($x, $y);
$this->scheme = $this->chompScheme($url);
if ($this->scheme === 'file') {
// Unescaped backslashes not used in directory separator context
// will get caught by this, but they should have been urlencoded
// anyway so we don't care. If this ends up being a problem, the
// host regexp must be modified to match for backslashes when
// the scheme is file.
$url = str_replace('\\', '/', $url);
}
list($this->username, $this->password) = $this->chompLogin($url);
$this->host = $this->chompHost($url);
$this->port = false;
if (preg_match('/(.*?):(.*)/', $this->host, $host_parts)) {
if ($this->scheme === 'file' && strlen($this->host) === 2) {
// DOS drive was placed in authority; promote it to path.
$url = '/' . $this->host . $url;
$this->host = false;
} else {
$this->host = $host_parts[1];
$this->port = (int) $host_parts[2];
}
}
$this->path = $this->chompPath($url);
$this->request = $this->parseRequest($this->chompRequest($url));
$this->fragment = (strncmp($url, '#', 1) == 0 ? substr($url, 1) : false);
$this->target = false;
}
/**
* Extracts the X, Y coordinate pair from an image map.
*
* @param string $url URL so far. The coordinates will be removed.
*
* @return array X, Y as a pair of integers.
*/
protected function chompCoordinates(&$url)
{
if (preg_match('/(.*)\?(\d+),(\d+)$/', $url, $matches)) {
$url = $matches[1];
return array((int) $matches[2], (int) $matches[3]);
}
return array(false, false);
}
/**
* Extracts the scheme part of an incoming URL.
*
* @param string $url URL so far. The scheme will be removed.
*
* @return string Scheme part or false.
*/
protected function chompScheme(&$url)
{
if (preg_match('#^([^/:]*):(//)(.*)#', $url, $matches)) {
$url = $matches[2] . $matches[3];
return $matches[1];
}
return false;
}
/**
* Extracts the username and password from the incoming URL. The // prefix will be reattached to
* the URL after the doublet is extracted.
*
* @param string $url URL so far. The username and password are removed.
*
* @return array Two item list of username and password. Will urldecode() them.
*/
protected function chompLogin(&$url)
{
$prefix = '';
if (preg_match('#^(//)(.*)#', $url, $matches)) {
$prefix = $matches[1];
$url = $matches[2];
}
if (preg_match('#^([^/]*)@(.*)#', $url, $matches)) {
$url = $prefix . $matches[2];
$parts = explode(':', $matches[1]);
return array(
urldecode($parts[0]),
isset($parts[1]) ? urldecode($parts[1]) : false);
}
$url = $prefix . $url;
return array(false, false);
}
/**
* Extracts the host part of an incoming URL. Includes the port number part. Will extract the
* host if it starts with // or it has a top level domain or it has at least two dots.
*
* @param string $url URL so far. The host will be removed.
*
* @return string Host part guess or false.
*/
protected function chompHost(&$url)
{
if (preg_match('!^(//)(.*?)(/.*|\?.*|#.*|$)!', $url, $matches)) {
$url = $matches[3];
return $matches[2];
}
if (preg_match('!(.*?)(\.\./|\./|/|\?|#|$)(.*)!', $url, $matches)) {
$tlds = self::getAllTopLevelDomains();
if (preg_match('/[a-z0-9\-]+\.(' . $tlds . ')/i', $matches[1])) {
$url = $matches[2] . $matches[3];
return $matches[1];
} elseif (preg_match('/[a-z0-9\-]+\.[a-z0-9\-]+\.[a-z0-9\-]+/i', $matches[1])) {
$url = $matches[2] . $matches[3];
return $matches[1];
}
}
return false;
}
/**
* Extracts the path information from the incoming URL. Strips this path from the URL.
*
* @param string $url URL so far. The host will be removed.
*
* @return string Path part or '/'.
*/
protected function chompPath(&$url)
{
if (preg_match('/(.*?)(\?|#|$)(.*)/', $url, $matches)) {
$url = $matches[2] . $matches[3];
return ($matches[1] ? $matches[1] : '');
}
return '';
}
/**
* Strips off the request data.
*
* @param string $url URL so far. The request will be removed.
*
* @return string Raw request part.
*/
protected function chompRequest(&$url)
{
if (preg_match('/\?(.*?)(#|$)(.*)/', $url, $matches)) {
$url = $matches[2] . $matches[3];
return $matches[1];
}
return '';
}
/**
* Breaks the request down into an object.
*
* @param string $raw Raw request.
*
* @return SimpleFormEncoding Parsed data.
*/
protected function parseRequest($raw)
{
$this->raw = $raw;
$request = new SimpleGetEncoding();
foreach (explode('&', $raw) as $pair) {
if (preg_match('/(.*?)=(.*)/', $pair, $matches)) {
$request->add(urldecode($matches[1]), urldecode($matches[2]));
} elseif ($pair) {
$request->add(urldecode($pair), '');
}
}
return $request;
}
/**
* Accessor for protocol part.
*
* @param string $default Value to use if not present.
*
* @return string Scheme name, e.g "http".
*/
public function getScheme($default = false)
{
return $this->scheme ? $this->scheme : $default;
}
/**
* Accessor for user name.
*
* @return string Username preceding host.
*/
public function getUsername()
{
return $this->username;
}
/**
* Accessor for password.
*
* @return string Password preceding host.
*/
public function getPassword()
{
return $this->password;
}
/**
* Accessor for hostname and port.
*
* @param string $default Value to use if not present.
*
* @return string Hostname only.
*/
public function getHost($default = false)
{
return $this->host ? $this->host : $default;
}
/**
* Accessor for top level domain.
*
* @return string Last part of host.
*/
public function getTld()
{
$path_parts = pathinfo($this->getHost());
return (isset($path_parts['extension']) ? $path_parts['extension'] : false);
}
/**
* Accessor for port number.
*
* @return int TCP/IP port number.
*/
public function getPort()
{
return $this->port;
}
/**
* Accessor for path.
*
* @return string Full path including leading slash if implied.
*/
public function getPath()
{
if (! $this->path && $this->host) {
return '/';
}
return $this->path;
}
/**
* Accessor for page if any. This may be a directory name if ambiguious.
*
* @return Page name.
*/
public function getPage()
{
if (! preg_match('/([^\/]*?)$/', $this->getPath(), $matches)) {
return false;
}
return $matches[1];
}
/**
* Gets the path to the page.
*
* @return string Path less the page.
*/
public function getBasePath()
{
if (! preg_match('/(.*\/)[^\/]*?$/', $this->getPath(), $matches)) {
return false;
}
return $matches[1];
}
/**
* Accessor for fragment at end of URL after the "#".
*
* @return string Part after "#".
*/
public function getFragment()
{
return $this->fragment;
}
/**
* Sets image coordinates. Set to false to clear them.
*
* @param int $x Horizontal position.
* @param int $y Vertical position.
*/
public function setCoordinates($x = false, $y = false)
{
if (($x === false) || ($y === false)) {
$this->x = $this->y = false;
return;
}
$this->x = (int) $x;
$this->y = (int) $y;
}
/**
* Accessor for horizontal image coordinate.
*
* @return int X value.
*/
public function getX()
{
return $this->x;
}
/**
* Accessor for vertical image coordinate.
*
* @return int Y value.
*/
public function getY()
{
return $this->y;
}
/**
* Accessor for current request parameters in URL string form.
* Will return teh original request
* if at all possible even if it doesn't make much sense.
*
* @return string Form is string "?a=1&b=2", etc.
*/
public function getEncodedRequest()
{
if ($this->raw) {
$encoded = $this->raw;
} else {
$encoded = $this->request->asUrlRequest();
}
if ($encoded) {
return '?' . preg_replace('/^\?/', '', $encoded);
}
return '';
}
/**
* Adds an additional parameter to the request.
*
* @param string $key Name of parameter.
* @param string $value Value as string.
*/
public function addRequestParameter($key, $value)
{
$this->raw = false;
$this->request->add($key, $value);
}
/**
* Adds additional parameters to the request.
*
* @param hash/SimpleFormEncoding $parameters Additional parameters.
*/
public function addRequestParameters($parameters)
{
$this->raw = false;
$this->request->merge($parameters);
}
/**
* Clears down all parameters.
*/
public function clearRequest()
{
$this->raw = false;
$this->request = new SimpleGetEncoding();
}
/**
* Gets the frame target if present.
* Although not strictly part of the URL specification it acts
* as similarily to the browser.
*
* @return boolean/string Frame name or false if none.
*/
public function getTarget()
{
return $this->target;
}
/**
* Attaches a frame target.
*
* @param string $frame Name of frame.
*/
public function setTarget($frame)
{
$this->raw = false;
$this->target = $frame;
}
/**
* Renders the URL back into a string.
*
* @return string URL in canonical form.
*/
public function asString()
{
$path = $this->path;
$scheme = $identity = $host = $port = $encoded = $fragment = '';
if ($this->username && $this->password) {
$identity = $this->username . ':' . $this->password . '@';
}
if ($this->getHost()) {
$scheme = $this->getScheme() ? $this->getScheme() : 'http';
$scheme .= '://';
$host = $this->getHost();
} elseif ($this->getScheme() === 'file') {
// Safest way; otherwise, file URLs on Windows have an extra
// leading slash. It might be possible to convert file://
// URIs to local file paths, but that requires more research.
$scheme = 'file://';
}
if ($this->getPort() && $this->getPort() != 80) {
$port = ':' . $this->getPort();
}
if (substr($this->path, 0, 1) == '/') {
$path = $this->normalisePath($this->path);
}
$encoded = $this->getEncodedRequest();
$fragment = $this->getFragment() ? '#' . $this->getFragment() : '';
$coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY();
return "$scheme$identity$host$port$path$encoded$fragment$coords";
}
/**
* Replaces unknown sections to turn a relative URL into an absolute one.
* The base URL can be either a string or a SimpleUrl object.
*
* @param string/SimpleUrl $base Base URL.
*/
public function makeAbsolute($base)
{
if (! is_object($base)) {
$base = new self($base);
}
if ($this->getHost()) {
$scheme = $this->getScheme();
$host = $this->getHost();
$port = $this->getPort() ? ':' . $this->getPort() : '';
$identity = $this->getIdentity() ? $this->getIdentity() . '@' : '';
if (! $identity) {
$identity = $base->getIdentity() ? $base->getIdentity() . '@' : '';
}
} else {
$scheme = $base->getScheme();
$host = $base->getHost();
$port = $base->getPort() ? ':' . $base->getPort() : '';
$identity = $base->getIdentity() ? $base->getIdentity() . '@' : '';
}
$path = $this->normalisePath($this->extractAbsolutePath($base));
$encoded = $this->getEncodedRequest();
$fragment = $this->getFragment() ? '#' . $this->getFragment() : '';
$coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY();
return new self("$scheme://$identity$host$port$path$encoded$fragment$coords");
}
/**
* Replaces unknown sections of the path with base parts to return a complete absolute one.
*
* @param string/SimpleUrl $base Base URL.
* @param string Absolute path.
*/
protected function extractAbsolutePath($base)
{
if ($this->getHost()) {
return $this->path;
}
if (! $this->isRelativePath($this->path)) {
return $this->path;
}
if ($this->path) {
return $base->getBasePath() . $this->path;
}
return $base->getPath();
}
/**
* Simple test to see if a path part is relative.
*
* @param string $path Path to test.
*
* @return bool True if starts with a "/".
*/
protected function isRelativePath($path)
{
return (substr($path, 0, 1) != '/');
}
/**
* Extracts the username and password for use in rendering a URL.
*
* @return string/boolean Form of username:password or false.
*/
public function getIdentity()
{
if ($this->username && $this->password) {
return $this->username . ':' . $this->password;
}
return false;
}
/**
* Replaces . and .. sections of the path.
*
* @param string $path Unoptimised path.
*
* @return string Path with dots removed if possible.
*/
public function normalisePath($path)
{
$path = preg_replace('|/\./|', '/', $path);
return preg_replace('|/[^/]+/\.\./|', '/', $path);
}
/**
* A pipe seperated list of all TLDs that result in two part domain names.
*
* @return string Pipe separated list.
*/
public static function getAllTopLevelDomains()
{
return 'com|edu|net|org|gov|mil|int|biz|info|name|pro|aero|coop|museum';
}
}