|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Curl; |
| 4 | + |
| 5 | +use Curl\StrUtil; |
| 6 | + |
| 7 | +class Url |
| 8 | +{ |
| 9 | + private $baseUrl = null; |
| 10 | + private $relativeUrl = null; |
| 11 | + |
| 12 | + public function __construct($base_url, $relative_url = null) |
| 13 | + { |
| 14 | + $this->baseUrl = $base_url; |
| 15 | + $this->relativeUrl = $relative_url; |
| 16 | + } |
| 17 | + |
| 18 | + public function __toString() |
| 19 | + { |
| 20 | + return $this->absolutizeUrl(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Remove dot segments. |
| 25 | + * |
| 26 | + * Interpret and remove the special "." and ".." path segments from a referenced path. |
| 27 | + */ |
| 28 | + public static function removeDotSegments($input) |
| 29 | + { |
| 30 | + // 1. The input buffer is initialized with the now-appended path |
| 31 | + // components and the output buffer is initialized to the empty |
| 32 | + // string. |
| 33 | + $output = ''; |
| 34 | + |
| 35 | + // 2. While the input buffer is not empty, loop as follows: |
| 36 | + while (!empty($input)) { |
| 37 | + // A. If the input buffer begins with a prefix of "../" or "./", |
| 38 | + // then remove that prefix from the input buffer; otherwise, |
| 39 | + if (StrUtil::startsWith($input, '../')) { |
| 40 | + $input = substr($input, 3); |
| 41 | + } elseif (StrUtil::startsWith($input, './')) { |
| 42 | + $input = substr($input, 2); |
| 43 | + |
| 44 | + // B. if the input buffer begins with a prefix of "/./" or "/.", |
| 45 | + // where "." is a complete path segment, then replace that |
| 46 | + // prefix with "/" in the input buffer; otherwise, |
| 47 | + } elseif (StrUtil::startsWith($input, '/./')) { |
| 48 | + $input = substr($input, 2); |
| 49 | + } elseif ($input === '/.') { |
| 50 | + $input = '/'; |
| 51 | + |
| 52 | + // C. if the input buffer begins with a prefix of "/../" or "/..", |
| 53 | + // where ".." is a complete path segment, then replace that |
| 54 | + // prefix with "/" in the input buffer and remove the last |
| 55 | + // segment and its preceding "/" (if any) from the output |
| 56 | + // buffer; otherwise, |
| 57 | + } elseif (StrUtil::startsWith($input, '/../')) { |
| 58 | + $input = substr($input, 3); |
| 59 | + $output = substr_replace($output, '', mb_strrpos($output, '/')); |
| 60 | + } elseif ($input === '/..') { |
| 61 | + $input = '/'; |
| 62 | + $output = substr_replace($output, '', mb_strrpos($output, '/')); |
| 63 | + |
| 64 | + // D. if the input buffer consists only of "." or "..", then remove |
| 65 | + // that from the input buffer; otherwise, |
| 66 | + } elseif ($input === '.' || $input === '..') { |
| 67 | + $input = ''; |
| 68 | + |
| 69 | + // E. move the first path segment in the input buffer to the end of |
| 70 | + // the output buffer, including the initial "/" character (if |
| 71 | + // any) and any subsequent characters up to, but not including, |
| 72 | + // the next "/" character or the end of the input buffer. |
| 73 | + } elseif (!(($pos = mb_strpos($input, '/', 1)) === false)) { |
| 74 | + $output .= substr($input, 0, $pos); |
| 75 | + $input = substr_replace($input, '', 0, $pos); |
| 76 | + } else { |
| 77 | + $output .= $input; |
| 78 | + $input = ''; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // 3. Finally, the output buffer is returned as the result of |
| 83 | + // remove_dot_segments. |
| 84 | + return $output . $input; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Absolutize url. |
| 89 | + * |
| 90 | + * Combine the base and relative url into an absolute url. |
| 91 | + */ |
| 92 | + private function absolutizeUrl() |
| 93 | + { |
| 94 | + $b = $this->parseUrl($this->baseUrl); |
| 95 | + |
| 96 | + if (!($this->relativeUrl === null)) { |
| 97 | + $r = $this->parseUrl($this->relativeUrl); |
| 98 | + |
| 99 | + // Copy relative parts to base url. |
| 100 | + if (isset($r['scheme'])) { |
| 101 | + $b['scheme'] = $r['scheme']; |
| 102 | + } |
| 103 | + if (isset($r['host'])) { |
| 104 | + $b['host'] = $r['host']; |
| 105 | + } |
| 106 | + if (isset($r['port'])) { |
| 107 | + $b['port'] = $r['port']; |
| 108 | + } |
| 109 | + if (isset($r['user'])) { |
| 110 | + $b['user'] = $r['user']; |
| 111 | + } |
| 112 | + if (isset($r['pass'])) { |
| 113 | + $b['pass'] = $r['pass']; |
| 114 | + } |
| 115 | + |
| 116 | + if (!isset($r['path']) || $r['path'] === '') { |
| 117 | + $r['path'] = '/'; |
| 118 | + } |
| 119 | + // Merge relative url with base when relative url's path doesn't start with a slash. |
| 120 | + if (!(StrUtil::startsWith($r['path'], '/'))) { |
| 121 | + $base = mb_strrchr($b['path'], '/', true); |
| 122 | + if ($base === false) { |
| 123 | + $base = ''; |
| 124 | + } |
| 125 | + $r['path'] = $base . '/' . $r['path']; |
| 126 | + } |
| 127 | + $b['path'] = $r['path']; |
| 128 | + $b['path'] = $this->removeDotSegments($b['path']); |
| 129 | + |
| 130 | + if (isset($r['query'])) { |
| 131 | + $b['query'] = $r['query']; |
| 132 | + } |
| 133 | + if (isset($r['fragment'])) { |
| 134 | + $b['fragment'] = $r['fragment']; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (!isset($b['path'])) { |
| 139 | + $b['path'] = '/'; |
| 140 | + } |
| 141 | + |
| 142 | + $absolutized_url = $this->unparseUrl($b); |
| 143 | + return $absolutized_url; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Parse url. |
| 148 | + * |
| 149 | + * Parse url into components of a URI as specified by RFC 3986. |
| 150 | + */ |
| 151 | + private function parseUrl($url) |
| 152 | + { |
| 153 | + return parse_url($url); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Unparse url. |
| 158 | + * |
| 159 | + * Combine url components into a url. |
| 160 | + */ |
| 161 | + private function unparseUrl($parsed_url) { |
| 162 | + $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; |
| 163 | + $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
| 164 | + $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; |
| 165 | + $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
| 166 | + $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; |
| 167 | + $pass = ($user || $pass) ? $pass . '@' : ''; |
| 168 | + $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
| 169 | + $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; |
| 170 | + $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; |
| 171 | + $unparsed_url = $scheme . $user . $pass . $host . $port . $path . $query . $fragment; |
| 172 | + return $unparsed_url; |
| 173 | + } |
| 174 | +} |
0 commit comments