diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-10-12 13:58:35 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-10-12 13:58:35 +0200 |
commit | bc22c9a0acb095970e9494cbe8954f0612e05dc0 (patch) | |
tree | 4e3a94b7469f5b2e3eaf946756235730429bf9d4 /application/Url.php | |
parent | 890afc32f744859d11b97eb26ed5c030af9b4145 (diff) | |
parent | ebd67c6e1b40aebdd3a52285ce9ff9412b2a3038 (diff) | |
download | Shaarli-bc22c9a0acb095970e9494cbe8954f0612e05dc0.tar.gz Shaarli-bc22c9a0acb095970e9494cbe8954f0612e05dc0.tar.zst Shaarli-bc22c9a0acb095970e9494cbe8954f0612e05dc0.zip |
Merge tag 'v0.7.0' of github.com:shaarli/Shaarli into stable
Release v0.7.0
Diffstat (limited to 'application/Url.php')
-rw-r--r-- | application/Url.php | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/application/Url.php b/application/Url.php index a4ac2e73..77447c8d 100644 --- a/application/Url.php +++ b/application/Url.php | |||
@@ -62,7 +62,21 @@ function add_trailing_slash($url) | |||
62 | { | 62 | { |
63 | return $url . (!endsWith($url, '/') ? '/' : ''); | 63 | return $url . (!endsWith($url, '/') ? '/' : ''); |
64 | } | 64 | } |
65 | /** | ||
66 | * Converts an URL with an IDN host to a ASCII one. | ||
67 | * | ||
68 | * @param string $url Input URL. | ||
69 | * | ||
70 | * @return string converted URL. | ||
71 | */ | ||
72 | function url_with_idn_to_ascii($url) | ||
73 | { | ||
74 | $parts = parse_url($url); | ||
75 | $parts['host'] = idn_to_ascii($parts['host']); | ||
65 | 76 | ||
77 | $httpUrl = new \http\Url($parts); | ||
78 | return $httpUrl->toString(); | ||
79 | } | ||
66 | /** | 80 | /** |
67 | * URL representation and cleanup utilities | 81 | * URL representation and cleanup utilities |
68 | * | 82 | * |
@@ -118,7 +132,8 @@ class Url | |||
118 | */ | 132 | */ |
119 | public function __construct($url) | 133 | public function __construct($url) |
120 | { | 134 | { |
121 | $this->parts = parse_url(trim($url)); | 135 | $url = self::cleanupUnparsedUrl(trim($url)); |
136 | $this->parts = parse_url($url); | ||
122 | 137 | ||
123 | if (!empty($url) && empty($this->parts['scheme'])) { | 138 | if (!empty($url) && empty($this->parts['scheme'])) { |
124 | $this->parts['scheme'] = 'http'; | 139 | $this->parts['scheme'] = 'http'; |
@@ -126,6 +141,35 @@ class Url | |||
126 | } | 141 | } |
127 | 142 | ||
128 | /** | 143 | /** |
144 | * Clean up URL before it's parsed. | ||
145 | * ie. handle urlencode, url prefixes, etc. | ||
146 | * | ||
147 | * @param string $url URL to clean. | ||
148 | * | ||
149 | * @return string cleaned URL. | ||
150 | */ | ||
151 | protected static function cleanupUnparsedUrl($url) | ||
152 | { | ||
153 | return self::removeFirefoxAboutReader($url); | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * Remove Firefox Reader prefix if it's present. | ||
158 | * | ||
159 | * @param string $input url | ||
160 | * | ||
161 | * @return string cleaned url | ||
162 | */ | ||
163 | protected static function removeFirefoxAboutReader($input) | ||
164 | { | ||
165 | $firefoxPrefix = 'about://reader?url='; | ||
166 | if (startsWith($input, $firefoxPrefix)) { | ||
167 | return urldecode(ltrim($input, $firefoxPrefix)); | ||
168 | } | ||
169 | return $input; | ||
170 | } | ||
171 | |||
172 | /** | ||
129 | * Returns a string representation of this URL | 173 | * Returns a string representation of this URL |
130 | */ | 174 | */ |
131 | public function toString() | 175 | public function toString() |
@@ -191,6 +235,22 @@ class Url | |||
191 | } | 235 | } |
192 | 236 | ||
193 | /** | 237 | /** |
238 | * Converts an URL with an International Domain Name host to a ASCII one. | ||
239 | * This requires PHP-intl. If it's not available, just returns this->cleanup(). | ||
240 | * | ||
241 | * @return string converted cleaned up URL. | ||
242 | */ | ||
243 | public function idnToAscii() | ||
244 | { | ||
245 | $out = $this->cleanup(); | ||
246 | if (! function_exists('idn_to_ascii') || ! isset($this->parts['host'])) { | ||
247 | return $out; | ||
248 | } | ||
249 | $asciiHost = idn_to_ascii($this->parts['host']); | ||
250 | return str_replace($this->parts['host'], $asciiHost, $out); | ||
251 | } | ||
252 | |||
253 | /** | ||
194 | * Get URL scheme. | 254 | * Get URL scheme. |
195 | * | 255 | * |
196 | * @return string the URL scheme or false if none is provided. | 256 | * @return string the URL scheme or false if none is provided. |
@@ -203,6 +263,18 @@ class Url | |||
203 | } | 263 | } |
204 | 264 | ||
205 | /** | 265 | /** |
266 | * Get URL host. | ||
267 | * | ||
268 | * @return string the URL host or false if none is provided. | ||
269 | */ | ||
270 | public function getHost() { | ||
271 | if (empty($this->parts['host'])) { | ||
272 | return false; | ||
273 | } | ||
274 | return $this->parts['host']; | ||
275 | } | ||
276 | |||
277 | /** | ||
206 | * Test if the Url is an HTTP one. | 278 | * Test if the Url is an HTTP one. |
207 | * | 279 | * |
208 | * @return true is HTTP, false otherwise. | 280 | * @return true is HTTP, false otherwise. |