]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/http/UrlUtils.php
4 * Converts an array-represented URL to a string
6 * Source: http://php.net/manual/en/function.parse-url.php#106731
8 * @see http://php.net/manual/en/function.parse-url.php
10 * @param array $parsedUrl an array-represented URL
12 * @return string the string representation of the URL
14 function unparse_url($parsedUrl)
16 $scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] . '://' : '';
17 $host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
18 $port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '';
19 $user = isset($parsedUrl['user']) ? $parsedUrl['user'] : '';
20 $pass = isset($parsedUrl['pass']) ? ':' . $parsedUrl['pass'] : '';
21 $pass = ($user || $pass) ? "$pass@" : '';
22 $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
23 $query = isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '';
24 $fragment = isset($parsedUrl['fragment']) ? '#' . $parsedUrl['fragment'] : '';
26 return "$scheme$user$pass$host$port$path$query$fragment";
30 * Removes undesired query parameters and fragments
32 * @param string url UrlUtils to be cleaned
34 * @return string the string representation of this URL after cleanup
36 function cleanup_url($url)
38 $obj_url = new \Shaarli\Http\Url($url);
39 return $obj_url->cleanup();
45 * @param string url UrlUtils for which the scheme is requested
47 * @return mixed the URL scheme or false if none is provided.
49 function get_url_scheme($url)
51 $obj_url = new \Shaarli\Http\Url($url);
52 return $obj_url->getScheme();
56 * Adds a trailing slash at the end of URL if necessary.
58 * @param string $url URL to check/edit.
60 * @return string $url URL with a end trailing slash.
62 function add_trailing_slash($url)
64 return $url . (!endsWith($url, '/') ? '/' : '');
68 * Replace not whitelisted protocols by 'http://' from given URL.
70 * @param string $url URL to clean
71 * @param array $protocols List of allowed protocols (aside from http(s)).
73 * @return string URL with allowed protocol
75 function whitelist_protocols($url, $protocols)
77 if (startsWith($url, '?') || startsWith($url, '/') || startsWith($url, '#')) {
80 $protocols = array_merge(['http', 'https'], $protocols);
81 $protocol = preg_match('#^(\w+):/?/?#', $url, $match);
82 // Protocol not allowed: we remove it and replace it with http
83 if ($protocol === 1 && ! in_array($match[1], $protocols)) {
84 $url = str_replace($match[0], 'http://', $url);
85 } elseif ($protocol !== 1) {
86 $url = 'http://' . $url;