]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Url.php
Fixes #531 - Title retrieving is failing with multiple use case
[github/shaarli/Shaarli.git] / application / Url.php
1 <?php
2 /**
3 * Converts an array-represented URL to a string
4 *
5 * Source: http://php.net/manual/en/function.parse-url.php#106731
6 *
7 * @see http://php.net/manual/en/function.parse-url.php
8 *
9 * @param array $parsedUrl an array-represented URL
10 *
11 * @return string the string representation of the URL
12 */
13 function unparse_url($parsedUrl)
14 {
15 $scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'].'://' : '';
16 $host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
17 $port = isset($parsedUrl['port']) ? ':'.$parsedUrl['port'] : '';
18 $user = isset($parsedUrl['user']) ? $parsedUrl['user'] : '';
19 $pass = isset($parsedUrl['pass']) ? ':'.$parsedUrl['pass'] : '';
20 $pass = ($user || $pass) ? "$pass@" : '';
21 $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
22 $query = isset($parsedUrl['query']) ? '?'.$parsedUrl['query'] : '';
23 $fragment = isset($parsedUrl['fragment']) ? '#'.$parsedUrl['fragment'] : '';
24
25 return "$scheme$user$pass$host$port$path$query$fragment";
26 }
27
28 /**
29 * Removes undesired query parameters and fragments
30 *
31 * @param string url Url to be cleaned
32 *
33 * @return string the string representation of this URL after cleanup
34 */
35 function cleanup_url($url)
36 {
37 $obj_url = new Url($url);
38 return $obj_url->cleanup();
39 }
40
41 /**
42 * Get URL scheme.
43 *
44 * @param string url Url for which the scheme is requested
45 *
46 * @return mixed the URL scheme or false if none is provided.
47 */
48 function get_url_scheme($url)
49 {
50 $obj_url = new Url($url);
51 return $obj_url->getScheme();
52 }
53
54 /**
55 * Adds a trailing slash at the end of URL if necessary.
56 *
57 * @param string $url URL to check/edit.
58 *
59 * @return string $url URL with a end trailing slash.
60 */
61 function add_trailing_slash($url)
62 {
63 return $url . (!endsWith($url, '/') ? '/' : '');
64 }
65 /**
66 * Converts an URL with an IDN host to a ASCII one.
67 *
68 * @param string $url Input URL.
69 *
70 * @return string converted URL.
71 */
72 function url_with_idn_to_ascii($url)
73 {
74 $parts = parse_url($url);
75 $parts['host'] = idn_to_ascii($parts['host']);
76
77 $httpUrl = new \http\Url($parts);
78 return $httpUrl->toString();
79 }
80 /**
81 * URL representation and cleanup utilities
82 *
83 * Form
84 * scheme://[username:password@]host[:port][/path][?query][#fragment]
85 *
86 * Examples
87 * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
88 * https://host.name.tld
89 * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
90 *
91 * @see http://www.faqs.org/rfcs/rfc3986.html
92 */
93 class Url
94 {
95 private static $annoyingQueryParams = array(
96 // Facebook
97 'action_object_map=',
98 'action_ref_map=',
99 'action_type_map=',
100 'fb_',
101 'fb=',
102
103 // Scoop.it
104 '__scoop',
105
106 // Google Analytics & FeedProxy
107 'utm_',
108
109 // ATInternet
110 'xtor='
111 );
112
113 private static $annoyingFragments = array(
114 // ATInternet
115 'xtor=RSS-',
116
117 // Misc.
118 'tk.rss_all'
119 );
120
121 /*
122 * URL parts represented as an array
123 *
124 * @see http://php.net/parse_url
125 */
126 protected $parts;
127
128 /**
129 * Parses a string containing a URL
130 *
131 * @param string $url a string containing a URL
132 */
133 public function __construct($url)
134 {
135 $url = self::cleanupUnparsedUrl(trim($url));
136 $this->parts = parse_url($url);
137
138 if (!empty($url) && empty($this->parts['scheme'])) {
139 $this->parts['scheme'] = 'http';
140 }
141 }
142
143 /**
144 * Clean up URL before it's parsed.
145 * ie. handle urlencode, url prefixes, etc.
146 *
147 * @param string $url URL to clean.
148 *
149 * @return string cleaned URL.
150 */
151 protected static function cleanupUnparsedUrl($url)
152 {
153 return self::removeFirefoxAboutReader($url);
154 }
155
156 /**
157 * Remove Firefox Reader prefix if it's present.
158 *
159 * @param string $input url
160 *
161 * @return string cleaned url
162 */
163 protected static function removeFirefoxAboutReader($input)
164 {
165 $firefoxPrefix = 'about://reader?url=';
166 if (startsWith($input, $firefoxPrefix)) {
167 return urldecode(ltrim($input, $firefoxPrefix));
168 }
169 return $input;
170 }
171
172 /**
173 * Returns a string representation of this URL
174 */
175 public function toString()
176 {
177 return unparse_url($this->parts);
178 }
179
180 /**
181 * Removes undesired query parameters
182 */
183 protected function cleanupQuery()
184 {
185 if (! isset($this->parts['query'])) {
186 return;
187 }
188
189 $queryParams = explode('&', $this->parts['query']);
190
191 foreach (self::$annoyingQueryParams as $annoying) {
192 foreach ($queryParams as $param) {
193 if (startsWith($param, $annoying)) {
194 $queryParams = array_diff($queryParams, array($param));
195 continue;
196 }
197 }
198 }
199
200 if (count($queryParams) == 0) {
201 unset($this->parts['query']);
202 return;
203 }
204
205 $this->parts['query'] = implode('&', $queryParams);
206 }
207
208 /**
209 * Removes undesired fragments
210 */
211 protected function cleanupFragment()
212 {
213 if (! isset($this->parts['fragment'])) {
214 return;
215 }
216
217 foreach (self::$annoyingFragments as $annoying) {
218 if (startsWith($this->parts['fragment'], $annoying)) {
219 unset($this->parts['fragment']);
220 break;
221 }
222 }
223 }
224
225 /**
226 * Removes undesired query parameters and fragments
227 *
228 * @return string the string representation of this URL after cleanup
229 */
230 public function cleanup()
231 {
232 $this->cleanupQuery();
233 $this->cleanupFragment();
234 return $this->toString();
235 }
236
237 /**
238 * Converts an URL with an International Domain Name host to a ASCII one.
239 * This requires PHP-intl. If it's not available, just returns this->cleanup().
240 *
241 * @return string converted cleaned up URL.
242 */
243 public function indToAscii()
244 {
245 $out = $this->cleanup();
246 if (! function_exists('idn_to_ascii') || ! isset($this->parts['host'])) {
247 return $out;
248 }
249 $asciiHost = idn_to_ascii($this->parts['host']);
250 return str_replace($this->parts['host'], $asciiHost, $out);
251 }
252
253 /**
254 * Get URL scheme.
255 *
256 * @return string the URL scheme or false if none is provided.
257 */
258 public function getScheme() {
259 if (!isset($this->parts['scheme'])) {
260 return false;
261 }
262 return $this->parts['scheme'];
263 }
264
265 /**
266 * Get URL host.
267 *
268 * @return string the URL host or false if none is provided.
269 */
270 public function getHost() {
271 if (empty($this->parts['host'])) {
272 return false;
273 }
274 return $this->parts['host'];
275 }
276
277 /**
278 * Test if the Url is an HTTP one.
279 *
280 * @return true is HTTP, false otherwise.
281 */
282 public function isHttp() {
283 return strpos(strtolower($this->parts['scheme']), 'http') !== false;
284 }
285 }