]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Url.php
3 * Converts an array-represented URL to a string
5 * Source: http://php.net/manual/en/function.parse-url.php#106731
7 * @see http://php.net/manual/en/function.parse-url.php
9 * @param array $parsedUrl an array-represented URL
11 * @return string the string representation of the URL
13 function unparse_url($parsedUrl)
15 $scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'].'://' : '';
16 $host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
17 $port = isset($parsedUrl['port']) ? ':'.$parsedUrl['port'] : '';
18 $user = isset($parsedUrl['user']) ? $parsedUrl['user'] : '';
19 $pass = isset($parsedUrl['pass']) ? ':'.$parsedUrl['pass'] : '';
20 $pass = ($user || $pass) ? "$pass@" : '';
21 $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
22 $query = isset($parsedUrl['query']) ? '?'.$parsedUrl['query'] : '';
23 $fragment = isset($parsedUrl['fragment']) ? '#'.$parsedUrl['fragment'] : '';
25 return "$scheme$user$pass$host$port$path$query$fragment";
29 * Removes undesired query parameters and fragments
31 * @param string url Url to be cleaned
33 * @return string the string representation of this URL after cleanup
35 function cleanup_url($url)
37 $obj_url = new Url($url);
38 return $obj_url->cleanup();
44 * @param string url Url for which the scheme is requested
46 * @return mixed the URL scheme or false if none is provided.
48 function get_url_scheme($url)
50 $obj_url = new Url($url);
51 return $obj_url->getScheme();
55 * Adds a trailing slash at the end of URL if necessary.
57 * @param string $url URL to check/edit.
59 * @return string $url URL with a end trailing slash.
61 function add_trailing_slash($url)
63 return $url . (!endsWith($url, '/') ? '/' : '');
67 * Replace not whitelisted protocols by 'http://' from given URL.
69 * @param string $url URL to clean
70 * @param array $protocols List of allowed protocols (aside from http(s)).
72 * @return string URL with allowed protocol
74 function whitelist_protocols($url, $protocols)
76 if (startsWith($url, '?') || startsWith($url, '/')) {
79 $protocols = array_merge(['http', 'https'], $protocols);
80 $protocol = preg_match('#^(\w+):/?/?#', $url, $match);
81 // Protocol not allowed: we remove it and replace it with http
82 if ($protocol === 1 && ! in_array($match[1], $protocols)) {
83 $url = str_replace($match[0], 'http://', $url);
84 } elseif ($protocol !== 1) {
85 $url = 'http://' . $url;
91 * URL representation and cleanup utilities
94 * scheme://[username:password@]host[:port][/path][?query][#fragment]
97 * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
98 * https://host.name.tld
99 * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
101 * @see http://www.faqs.org/rfcs/rfc3986.html
105 private static $annoyingQueryParams = array(
107 'action_object_map=',
117 // Google Analytics & FeedProxy
127 private static $annoyingFragments = array(
136 * URL parts represented as an array
138 * @see http://php.net/parse_url
143 * Parses a string containing a URL
145 * @param string $url a string containing a URL
147 public function __construct($url)
149 $url = self::cleanupUnparsedUrl(trim($url));
150 $this->parts = parse_url($url);
152 if (!empty($url) && empty($this->parts['scheme'])) {
153 $this->parts['scheme'] = 'http';
158 * Clean up URL before it's parsed.
159 * ie. handle urlencode, url prefixes, etc.
161 * @param string $url URL to clean.
163 * @return string cleaned URL.
165 protected static function cleanupUnparsedUrl($url)
167 return self::removeFirefoxAboutReader($url);
171 * Remove Firefox Reader prefix if it's present.
173 * @param string $input url
175 * @return string cleaned url
177 protected static function removeFirefoxAboutReader($input)
179 $firefoxPrefix = 'about://reader?url=';
180 if (startsWith($input, $firefoxPrefix)) {
181 return urldecode(ltrim($input, $firefoxPrefix));
187 * Returns a string representation of this URL
189 public function toString()
191 return unparse_url($this->parts);
195 * Removes undesired query parameters
197 protected function cleanupQuery()
199 if (! isset($this->parts['query'])) {
203 $queryParams = explode('&', $this->parts['query']);
205 foreach (self::$annoyingQueryParams as $annoying) {
206 foreach ($queryParams as $param) {
207 if (startsWith($param, $annoying)) {
208 $queryParams = array_diff($queryParams, array($param));
214 if (count($queryParams) == 0) {
215 unset($this->parts['query']);
219 $this->parts['query'] = implode('&', $queryParams);
223 * Removes undesired fragments
225 protected function cleanupFragment()
227 if (! isset($this->parts['fragment'])) {
231 foreach (self::$annoyingFragments as $annoying) {
232 if (startsWith($this->parts['fragment'], $annoying)) {
233 unset($this->parts['fragment']);
240 * Removes undesired query parameters and fragments
242 * @return string the string representation of this URL after cleanup
244 public function cleanup()
246 $this->cleanupQuery();
247 $this->cleanupFragment();
248 return $this->toString();
252 * Converts an URL with an International Domain Name host to a ASCII one.
253 * This requires PHP-intl. If it's not available, just returns this->cleanup().
255 * @return string converted cleaned up URL.
257 public function idnToAscii()
259 $out = $this->cleanup();
260 if (! function_exists('idn_to_ascii') || ! isset($this->parts['host'])) {
263 $asciiHost = idn_to_ascii($this->parts['host'], 0, INTL_IDNA_VARIANT_UTS46);
264 return str_replace($this->parts['host'], $asciiHost, $out);
270 * @return string the URL scheme or false if none is provided.
272 public function getScheme() {
273 if (!isset($this->parts['scheme'])) {
276 return $this->parts['scheme'];
282 * @return string the URL host or false if none is provided.
284 public function getHost() {
285 if (empty($this->parts['host'])) {
288 return $this->parts['host'];
292 * Test if the Url is an HTTP one.
294 * @return true is HTTP, false otherwise.
296 public function isHttp() {
297 return strpos(strtolower($this->parts['scheme']), 'http') !== false;