Spaces:
Sleeping
Sleeping
File size: 1,886 Bytes
07c3cdd | 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 | <?php
class AutofixUrl {
function AutofixUrl() {
}
function apply($url) {
$parts = @parse_url($url);
if ($parts === FALSE) {
return null;
};
/*
* Check if path contains only RFC1738 compliant symbols and fix it
* No graphic: 00-1F, 7F, 80-FF
* Unsafe: 'space',<>"#%{}|\^~[]`
* Reserved: ;/?:@=&
*
* Normally, slash is allowed in path part, and % may be a part of encoded character
*/
$no_graphic_found = preg_match('/[\x00-\x1F\x7F\x80-\xFF]/', $parts['path']);
$unsafe_found = preg_match('/[ <>\"#{}\|\^~\[\]`]/', $parts['path']);
$unsafe_percent_found = preg_match('/%[^\dA-F]|%\d[^\dA-F]/i', $parts['path']);
$reserved_found = preg_match('/;\?:@=&/', $parts['path']);
if ($no_graphic_found ||
$unsafe_found ||
$unsafe_percent_found ||
$reserved_found) {
$parts['path'] = join('/', array_map('rawurlencode', explode('/',$parts['path'])));
};
// Build updated URL
$url_fixed = '';
if (isset($parts['scheme'])) {
$url_fixed .= $parts['scheme'];
$url_fixed .= '://';
if (isset($parts['user'])) {
$url_fixed .= $parts['user'];
if (isset($parts['pass'])) {
$url_fixed .= ':';
$url_fixed .= $parts['pass'];
};
$url_fixed .= '@';
};
if (isset($parts['host'])) {
$url_fixed .= $parts['host'];
};
if (isset($parts['port'])) {
$url_fixed .= ':';
$url_fixed .= $parts['port'];
};
};
if (isset($parts['path'])) {
$url_fixed .= $parts['path'];
};
if (isset($parts['query'])) {
$url_fixed .= '?';
$url_fixed .= $parts['query'];
};
if (isset($parts['fragment'])) {
$url_fixed .= '#';
$url_fixed .= $parts['fragment'];
};
return $url_fixed;
}
}
?> |