2015-08-14 01:14:07 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Converts an array-represented URL to a string
|
|
|
|
*
|
|
|
|
* Source: http://php.net/manual/en/function.parse-url.php#106731
|
|
|
|
*
|
|
|
|
* @see http://php.net/manual/en/function.parse-url.php
|
|
|
|
*
|
|
|
|
* @param array $parsedUrl an array-represented URL
|
|
|
|
*
|
|
|
|
* @return string the string representation of the URL
|
|
|
|
*/
|
|
|
|
function unparse_url($parsedUrl)
|
|
|
|
{
|
|
|
|
$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'].'://' : '';
|
|
|
|
$host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
|
|
|
|
$port = isset($parsedUrl['port']) ? ':'.$parsedUrl['port'] : '';
|
|
|
|
$user = isset($parsedUrl['user']) ? $parsedUrl['user'] : '';
|
|
|
|
$pass = isset($parsedUrl['pass']) ? ':'.$parsedUrl['pass'] : '';
|
|
|
|
$pass = ($user || $pass) ? "$pass@" : '';
|
|
|
|
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
|
|
|
|
$query = isset($parsedUrl['query']) ? '?'.$parsedUrl['query'] : '';
|
|
|
|
$fragment = isset($parsedUrl['fragment']) ? '#'.$parsedUrl['fragment'] : '';
|
|
|
|
|
|
|
|
return "$scheme$user$pass$host$port$path$query$fragment";
|
|
|
|
}
|
|
|
|
|
2015-09-02 13:55:39 +02:00
|
|
|
/**
|
|
|
|
* Removes undesired query parameters and fragments
|
|
|
|
*
|
|
|
|
* @param string url Url to be cleaned
|
|
|
|
*
|
|
|
|
* @return string the string representation of this URL after cleanup
|
|
|
|
*/
|
|
|
|
function cleanup_url($url)
|
|
|
|
{
|
2018-12-03 00:23:35 +01:00
|
|
|
$obj_url = new \Shaarli\Http\Url($url);
|
2018-10-13 00:19:03 +02:00
|
|
|
return $obj_url->cleanup();
|
2015-09-02 13:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get URL scheme.
|
|
|
|
*
|
|
|
|
* @param string url Url for which the scheme is requested
|
|
|
|
*
|
|
|
|
* @return mixed the URL scheme or false if none is provided.
|
|
|
|
*/
|
|
|
|
function get_url_scheme($url)
|
|
|
|
{
|
2018-12-03 00:23:35 +01:00
|
|
|
$obj_url = new \Shaarli\Http\Url($url);
|
2018-10-13 00:19:03 +02:00
|
|
|
return $obj_url->getScheme();
|
2015-09-02 13:55:39 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 10:20:27 +01:00
|
|
|
/**
|
|
|
|
* Adds a trailing slash at the end of URL if necessary.
|
|
|
|
*
|
|
|
|
* @param string $url URL to check/edit.
|
|
|
|
*
|
|
|
|
* @return string $url URL with a end trailing slash.
|
|
|
|
*/
|
|
|
|
function add_trailing_slash($url)
|
|
|
|
{
|
|
|
|
return $url . (!endsWith($url, '/') ? '/' : '');
|
|
|
|
}
|
|
|
|
|
2017-05-25 14:52:42 +02:00
|
|
|
/**
|
|
|
|
* Replace not whitelisted protocols by 'http://' from given URL.
|
|
|
|
*
|
|
|
|
* @param string $url URL to clean
|
|
|
|
* @param array $protocols List of allowed protocols (aside from http(s)).
|
|
|
|
*
|
|
|
|
* @return string URL with allowed protocol
|
|
|
|
*/
|
|
|
|
function whitelist_protocols($url, $protocols)
|
|
|
|
{
|
|
|
|
if (startsWith($url, '?') || startsWith($url, '/')) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
$protocols = array_merge(['http', 'https'], $protocols);
|
|
|
|
$protocol = preg_match('#^(\w+):/?/?#', $url, $match);
|
|
|
|
// Protocol not allowed: we remove it and replace it with http
|
|
|
|
if ($protocol === 1 && ! in_array($match[1], $protocols)) {
|
|
|
|
$url = str_replace($match[0], 'http://', $url);
|
2018-02-28 22:34:40 +01:00
|
|
|
} elseif ($protocol !== 1) {
|
2017-05-25 14:52:42 +02:00
|
|
|
$url = 'http://' . $url;
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|