X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FUrl.php;h=af38c4d9155ed4eb856691c941f26f11714b77be;hb=b1eb5d1d31e3ea256501c08a3ed9aa7183b27466;hp=af43b457961729f3a5c2abc82a407b4a402531ec;hpb=ef591e7ee21435da9314c5f7f6ea983c6f423898;p=github%2Fshaarli%2FShaarli.git diff --git a/application/Url.php b/application/Url.php old mode 100755 new mode 100644 index af43b457..af38c4d9 --- a/application/Url.php +++ b/application/Url.php @@ -51,6 +51,18 @@ function get_url_scheme($url) return $obj_url->getScheme(); } +/** + * 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, '/') ? '/' : ''); +} + /** * URL representation and cleanup utilities * @@ -106,6 +118,7 @@ class Url */ public function __construct($url) { + $url = self::cleanupUnparsedUrl(trim($url)); $this->parts = parse_url($url); if (!empty($url) && empty($this->parts['scheme'])) { @@ -113,6 +126,35 @@ class Url } } + /** + * Clean up URL before it's parsed. + * ie. handle urlencode, url prefixes, etc. + * + * @param string $url URL to clean. + * + * @return string cleaned URL. + */ + protected static function cleanupUnparsedUrl($url) + { + return self::removeFirefoxAboutReader($url); + } + + /** + * Remove Firefox Reader prefix if it's present. + * + * @param string $input url + * + * @return string cleaned url + */ + protected static function removeFirefoxAboutReader($input) + { + $firefoxPrefix = 'about://reader?url='; + if (startsWith($input, $firefoxPrefix)) { + return urldecode(ltrim($input, $firefoxPrefix)); + } + return $input; + } + /** * Returns a string representation of this URL */ @@ -189,4 +231,13 @@ class Url } return $this->parts['scheme']; } + + /** + * Test if the Url is an HTTP one. + * + * @return true is HTTP, false otherwise. + */ + public function isHttp() { + return strpos(strtolower($this->parts['scheme']), 'http') !== false; + } }