]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/Url.php
Refactor and rebase #380: Firefox reader view links
[github/shaarli/Shaarli.git] / application / Url.php
index d80c9c582f32e5e8b8053b291d354a112e21b6c0..af38c4d9155ed4eb856691c941f26f11714b77be 100644 (file)
@@ -118,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'])) {
@@ -125,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
      */
@@ -201,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;
+    }
 }