aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Url.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/Url.php')
-rw-r--r--application/Url.php74
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 */
72function 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.