diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-03-24 19:01:40 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-03-26 16:26:23 +0100 |
commit | c9da01e749e5319d0c0f400cde06e71c0e7312d5 (patch) | |
tree | 5150c9af94115efd5908b4e8d7e6f4a3c6a71cc8 | |
parent | ecd051905f0c8aa97590eb453d553dc678125a50 (diff) | |
download | Shaarli-c9da01e749e5319d0c0f400cde06e71c0e7312d5.tar.gz Shaarli-c9da01e749e5319d0c0f400cde06e71c0e7312d5.tar.zst Shaarli-c9da01e749e5319d0c0f400cde06e71c0e7312d5.zip |
Refactor and rebase #380: Firefox reader view links
Fixes #366
Closes #380
-rw-r--r-- | application/Url.php | 42 | ||||
-rw-r--r-- | tests/Url/UrlTest.php | 7 |
2 files changed, 36 insertions, 13 deletions
diff --git a/application/Url.php b/application/Url.php index e7c3a4cc..af38c4d9 100644 --- a/application/Url.php +++ b/application/Url.php | |||
@@ -118,24 +118,41 @@ class Url | |||
118 | */ | 118 | */ |
119 | public function __construct($url) | 119 | public function __construct($url) |
120 | { | 120 | { |
121 | $this->parts = parse_url(trim($url)); | 121 | $url = self::cleanupUnparsedUrl(trim($url)); |
122 | $this->parts = parse_url($url); | ||
122 | 123 | ||
123 | if (!empty($url) && empty($this->parts['scheme'])) { | 124 | if (!empty($url) && empty($this->parts['scheme'])) { |
124 | $this->parts['scheme'] = 'http'; | 125 | $this->parts['scheme'] = 'http'; |
125 | } | 126 | } |
126 | } | 127 | } |
127 | 128 | ||
129 | /** | ||
130 | * Clean up URL before it's parsed. | ||
131 | * ie. handle urlencode, url prefixes, etc. | ||
132 | * | ||
133 | * @param string $url URL to clean. | ||
134 | * | ||
135 | * @return string cleaned URL. | ||
136 | */ | ||
137 | protected static function cleanupUnparsedUrl($url) | ||
138 | { | ||
139 | return self::removeFirefoxAboutReader($url); | ||
140 | } | ||
128 | 141 | ||
129 | private function removeFirefoxAboutReader($input){ | 142 | /** |
130 | $output_array = []; | 143 | * Remove Firefox Reader prefix if it's present. |
131 | preg_match("%^about://reader\?url=(.*)%", $input, $output_array); | 144 | * |
132 | if(!empty($output_array)){ | 145 | * @param string $input url |
133 | $extractedUrl = preg_replace("%^about://reader\?url=(.*)%", "$1", $input); | 146 | * |
134 | $url = urldecode($extractedUrl); | 147 | * @return string cleaned url |
135 | }else{ | 148 | */ |
136 | $url = $input; | 149 | protected static function removeFirefoxAboutReader($input) |
137 | } | 150 | { |
138 | return $url; | 151 | $firefoxPrefix = 'about://reader?url='; |
152 | if (startsWith($input, $firefoxPrefix)) { | ||
153 | return urldecode(ltrim($input, $firefoxPrefix)); | ||
154 | } | ||
155 | return $input; | ||
139 | } | 156 | } |
140 | 157 | ||
141 | /** | 158 | /** |
@@ -200,8 +217,7 @@ class Url | |||
200 | { | 217 | { |
201 | $this->cleanupQuery(); | 218 | $this->cleanupQuery(); |
202 | $this->cleanupFragment(); | 219 | $this->cleanupFragment(); |
203 | $url = $this->toString(); | 220 | return $this->toString(); |
204 | return $this->removeFirefoxAboutReader($url); | ||
205 | } | 221 | } |
206 | 222 | ||
207 | /** | 223 | /** |
diff --git a/tests/Url/UrlTest.php b/tests/Url/UrlTest.php index 425327ed..a64a73ea 100644 --- a/tests/Url/UrlTest.php +++ b/tests/Url/UrlTest.php | |||
@@ -128,6 +128,13 @@ class UrlTest extends PHPUnit_Framework_TestCase | |||
128 | self::$baseUrl.'?my=stuff&is=kept#again', | 128 | self::$baseUrl.'?my=stuff&is=kept#again', |
129 | $url->cleanup() | 129 | $url->cleanup() |
130 | ); | 130 | ); |
131 | |||
132 | // test firefox reader url | ||
133 | $url = new Url( | ||
134 | 'about://reader?url=' . urlencode(self::$baseUrl .'?my=stuff&is=kept') | ||
135 | ); | ||
136 | $this->assertEquals(self::$baseUrl.'?my=stuff&is=kept', $url->cleanup()); | ||
137 | |||
131 | } | 138 | } |
132 | 139 | ||
133 | /** | 140 | /** |