forked from cydrobolt/polr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib-urlparse.php
336 lines (297 loc) · 9.48 KB
/
lib-urlparse.php
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
<?php
class parseurl {
public static function urlencode($url, $encode_special) {
$out = array();
$len = strlen($url);
for ($i = 0; $i < $len; $i++) {
$c = $url[$i];
$ascii = ord($c);
if ($ascii <= 32 || $ascii >= 127) {
$out[] = rawurlencode($c);
} else if ($encode_special && ($ascii == 35 || $ascii == 37)) {
$out[] = rawurlencode($c);
} else {
$out[] = $c;
}
}
return implode('', $out);
}
public static function escape($s) {
$unquoted = rawurldecode($s);
while ($unquoted != $s) {
$s = $unquoted;
$unquoted = rawurldecode($s);
}
$s = self::urlencode($s, TRUE);
return $s;
}
/**
* Canonicalizes a full URL according to Google's definition.
*
* @param string $url
* @return a string array of canonicalized URL parts
*/
public static function getCanonicalizedUrl($url) {
$canurl = self::canonicalize($url);
return $canurl['canonical'];
}
/**
* Canonicalizes a full URL according to Google's definition.
*
* @param string $url
* @return a string array of canonicalized URL parts
*/
public static function canonicalize($url) {
$finalurl = $url;
// Strip off fragment
$pos = strpos($url, '#');
if ($pos !== FALSE) {
$finalurl = substr($url, 0, $pos);
}
// Strip off leading and trailing white space
$finalurl = trim($finalurl);
// Remove line feeds, return carriages, tabs, vertical tabs
$finalurl = str_replace(array("\x09", "\x0A", "\x0D", "\x0B"), '', $finalurl);
$finalurl = self::escape($finalurl);
// Schemeless urls become HTTP
if (! preg_match("/^[a-zA-Z]+:\/\//", $finalurl)) {
$finalurl = 'http://' . $finalurl;
}
// Now extract hostname & path
// parse_url is noisy prior to php 5.3.3. Need to silence with '@'
$parts = @parse_url($finalurl);
$hostname = self::escape($parts['host']);
// Deal with hostname first
// Replace all leading and trailing dots
$hostname = trim($hostname, '.');
// Replace all consecutive dots with one dot
$hostname = preg_replace('/\.{2,}/', '.', $hostname);
// Make it lowercase
$hostname = strtolower($hostname);
if (is_numeric($hostname)) {
// weird case where hostname is one integer.
// some browsers (chrome) actually accept this!
$hostnameip = ip2long(long2ip($hostname));
} else {
// See if its a valid IP
$hostnameip = ip2long($hostname);
}
if ($hostnameip === FALSE) {
$is_ip = false;
} else {
$is_ip = true;
$hostname = long2ip($hostnameip);
}
if (!isset($parts['path'])) {
$path = '/';
} else {
$path = self::escape($parts['path']);
}
$pathparts = explode('/', $path);
foreach ($pathparts as $key => $value) {
if ($value == '..') {
if ($key != 0) {
unset($pathparts[$key - 1]);
unset($pathparts[$key]);
} else {
unset($pathparts[$key]);
}
} elseif ($value == '.' || empty($value)) {
unset($pathparts[$key]);
}
}
if (substr($path, -1, 1) == '/') {
$append = '/';
} else {
$append = '';
}
$path = '/' . implode('/', $pathparts);
if ($append && substr($path, -1, 1) != '/') {
$path .= $append;
}
$canurl = $parts['scheme'] . '://';
if (!empty($parts['userinfo'])) {
$realurl .= $parts['userinfo'] . '@';
}
$canurl .= $hostname;
if (!empty($parts['port']) &&
(($parts['scheme'] == 'http' && $parts['port'] != 80) ||
($parts['scheme'] == 'https' && $parts['port'] != 443))) {
$canurl .= ':' . $parts['port'];
}
$canurl .= $path;
if (isset($parts['query'])) {
$query = $parts['query'];
$canurl .= '?' . $query;
} else if ($finalurl[strlen($finalurl)-1] == '?') {
$query = '';
$canurl .= '?';
} else {
$query = null;
}
return array(
'canonical' => $canurl,
'original' => $url,
'host' => $hostname,
'path' => $path,
'query' => $query,
'is_ip' => $is_ip
);
}
/**
* Hash up a list of values from makePrefixes() (will possibly be
* combined into that function at a later date
*
* @param array() $prefixarray
* @return Ambigous <multitype:, multitype:string unknown >
*/
static function makeHashes($prefixarray) {
$returnprefixes = array();
foreach ($prefixarray as $value) {
$fullhash = self::sha256($value);
$returnprefixes[$fullhash] = array(
'original' => $value,
'prefix' => substr($fullhash, 0, 8),
'hash' => $fullhash);
}
return $returnprefixes;
}
/**
* construct URL paths given the query parameters
*
* @param string $path
* @param string $query
* @return multitype: string
*/
static function makePaths($path, $query) {
$p = array();
if (!is_null($query)) {
array_push($p, $path . '?' . $query);
}
array_push($p, $path);
if ($path == '/') {
return $p;
}
array_push($p, '/');
$parts = explode('/', $path);
$len = count($parts) - 1;
// handle case where path ends in a '/' already
if (empty($parts[$len])) {
$len -= 1;
}
// no more than 3 of these (we already have '/' already, so 4 total)
$len = min($len, 3);
for ($i = 1; $i < $len; $i++) {
array_push($p, '/' . implode('/', array_slice($parts, 1, $i)) . '/');
}
return $p;
}
/**
* Construct host prefixes given the host name, URL path, and
* query strings.
*
* @param string $host
* @param string $path
* @param string $query
* @param boolean $usingip
* @return multitype:
*/
static function makePrefixes($host, $path, $query, $usingip) {
$out = array();
$hosts = self::makeHosts($host, $usingip);
$paths = self::makePaths($path, $query);
foreach ($hosts as $host) {
foreach ($paths as $j => $p) {
array_push($out, $host . $p);
}
}
return $out;
}
/**
* Make URL prefixes for use after a hostkey check
*
* @param string $host
* @param string $path
* @param string $query
* @param boolean $usingip
* @return multitype:string
*/
static function makePrefixesHashes($host, $path, $query, $usingip) {
$prefixes = self::makePrefixes($host, $path, $query, $usingip);
return self::makeHashes($prefixes);
}
/**
* Makes the host keys for initial lookup
*
* maps 1.2.3.4 => ( 1.2.3.4 ) (ip address)
* b.a => ( b.a )
* c.b.a => ( c.b.a, b.a )
* d.c.b.a => ( c.b.a, b.a ) (only 2 dots)
*
*/
static function makeHostList($host, $usingip) {
if ($usingip) {
return array($host);
} else {
$hostparts = explode('.', $host);
$len = count($hostparts);
if ($len <= 2) {
return array($host);
} else {
return array(implode('.', array_slice($hostparts, $len - 3)),
implode('.', array_slice($hostparts, $len - 2)));
}
}
}
/**
*
* Maps IPADDR -> IPADDR (identity)
*
*/
static function makeHosts($host, $usingip) {
// always use the full host.
$hosts = array($host);
if (!$usingip) {
$hostparts = explode('.', $host);
// TRICKY... make sure domain has at least one dot, and no
// more than 4.
$len = count($hostparts) - 1;
for ($i = max(1, $len - 4); $i < $len; ++$i) {
array_push($hosts, implode('.', array_slice($hostparts, $i)));
}
}
return $hosts;
}
/**
* Make Hostkeys for use in a full URL lookup
*
* @param string $host
* @param boolean $usingip
* @return multitype:string
*/
static function makeHostKeyList($host, $usingip) {
// turn 'www.google.com' into ('www.google.com', 'google.com')
$hosts = self::makeHostList($host, $usingip);
// Now make key & key prefix
$returnhosts = array();
foreach ($hosts as $host) {
$host = $host . '/';
$fullhash = self::sha256($host);
$returnhosts[] = array(
'host' => $host,
'host_key' => substr($fullhash, 0, 8),
'hash' => $fullhash
);
}
return $returnhosts;
}
/**
* SHA-256 input
*
* @param string $data
* @return hex-encoded sha256 string
*/
static function sha256($data) {
return hash('sha256', $data);
}
}