]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/Url.php
Merge pull request #868 from ArthurHoaro/theme/default-as-default
[github/shaarli/Shaarli.git] / application / Url.php
CommitLineData
d9d776af
V
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 */
13function 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
ef591e7e
GV
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 */
35function 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 */
48function get_url_scheme($url)
49{
50 $obj_url = new Url($url);
51 return $obj_url->getScheme();
52}
53
938d9cce
A
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 */
61function add_trailing_slash($url)
62{
63 return $url . (!endsWith($url, '/') ? '/' : '');
64}
65
d9d776af
V
66/**
67 * URL representation and cleanup utilities
68 *
69 * Form
70 * scheme://[username:password@]host[:port][/path][?query][#fragment]
71 *
72 * Examples
73 * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
74 * https://host.name.tld
75 * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
76 *
77 * @see http://www.faqs.org/rfcs/rfc3986.html
78 */
79class Url
80{
81 private static $annoyingQueryParams = array(
82 // Facebook
83 'action_object_map=',
84 'action_ref_map=',
85 'action_type_map=',
86 'fb_',
87 'fb=',
725ca094 88 'PHPSESSID=',
d9d776af
V
89
90 // Scoop.it
91 '__scoop',
92
93 // Google Analytics & FeedProxy
94 'utm_',
95
96 // ATInternet
eaf25248
V
97 'xtor=',
98
99 // Other
100 'campaign_'
d9d776af
V
101 );
102
103 private static $annoyingFragments = array(
104 // ATInternet
105 'xtor=RSS-',
106
107 // Misc.
108 'tk.rss_all'
109 );
110
111 /*
112 * URL parts represented as an array
113 *
114 * @see http://php.net/parse_url
115 */
116 protected $parts;
117
118 /**
119 * Parses a string containing a URL
120 *
121 * @param string $url a string containing a URL
122 */
123 public function __construct($url)
124 {
c9da01e7
A
125 $url = self::cleanupUnparsedUrl(trim($url));
126 $this->parts = parse_url($url);
9e1724f1
A
127
128 if (!empty($url) && empty($this->parts['scheme'])) {
129 $this->parts['scheme'] = 'http';
130 }
d9d776af
V
131 }
132
c9da01e7
A
133 /**
134 * Clean up URL before it's parsed.
135 * ie. handle urlencode, url prefixes, etc.
136 *
137 * @param string $url URL to clean.
138 *
139 * @return string cleaned URL.
140 */
141 protected static function cleanupUnparsedUrl($url)
142 {
143 return self::removeFirefoxAboutReader($url);
144 }
ecd05190 145
c9da01e7
A
146 /**
147 * Remove Firefox Reader prefix if it's present.
148 *
149 * @param string $input url
150 *
151 * @return string cleaned url
152 */
153 protected static function removeFirefoxAboutReader($input)
154 {
155 $firefoxPrefix = 'about://reader?url=';
156 if (startsWith($input, $firefoxPrefix)) {
157 return urldecode(ltrim($input, $firefoxPrefix));
158 }
159 return $input;
ecd05190
KC
160 }
161
d9d776af
V
162 /**
163 * Returns a string representation of this URL
164 */
ef591e7e 165 public function toString()
d9d776af
V
166 {
167 return unparse_url($this->parts);
168 }
169
170 /**
171 * Removes undesired query parameters
172 */
173 protected function cleanupQuery()
174 {
175 if (! isset($this->parts['query'])) {
176 return;
177 }
178
179 $queryParams = explode('&', $this->parts['query']);
180
181 foreach (self::$annoyingQueryParams as $annoying) {
182 foreach ($queryParams as $param) {
183 if (startsWith($param, $annoying)) {
184 $queryParams = array_diff($queryParams, array($param));
185 continue;
186 }
187 }
188 }
189
190 if (count($queryParams) == 0) {
191 unset($this->parts['query']);
192 return;
193 }
194
195 $this->parts['query'] = implode('&', $queryParams);
196 }
197
198 /**
199 * Removes undesired fragments
200 */
201 protected function cleanupFragment()
202 {
203 if (! isset($this->parts['fragment'])) {
204 return;
205 }
206
207 foreach (self::$annoyingFragments as $annoying) {
208 if (startsWith($this->parts['fragment'], $annoying)) {
209 unset($this->parts['fragment']);
210 break;
211 }
212 }
213 }
214
215 /**
216 * Removes undesired query parameters and fragments
217 *
218 * @return string the string representation of this URL after cleanup
219 */
220 public function cleanup()
221 {
222 $this->cleanupQuery();
223 $this->cleanupFragment();
c9da01e7 224 return $this->toString();
d9d776af 225 }
9e1724f1 226
ce7b0b64
A
227 /**
228 * Converts an URL with an International Domain Name host to a ASCII one.
229 * This requires PHP-intl. If it's not available, just returns this->cleanup().
230 *
231 * @return string converted cleaned up URL.
232 */
caa69b58 233 public function idnToAscii()
ce7b0b64
A
234 {
235 $out = $this->cleanup();
236 if (! function_exists('idn_to_ascii') || ! isset($this->parts['host'])) {
237 return $out;
238 }
239 $asciiHost = idn_to_ascii($this->parts['host']);
240 return str_replace($this->parts['host'], $asciiHost, $out);
241 }
242
9e1724f1
A
243 /**
244 * Get URL scheme.
245 *
246 * @return string the URL scheme or false if none is provided.
247 */
248 public function getScheme() {
249 if (!isset($this->parts['scheme'])) {
250 return false;
251 }
252 return $this->parts['scheme'];
253 }
1557cefb 254
ce7b0b64
A
255 /**
256 * Get URL host.
257 *
258 * @return string the URL host or false if none is provided.
259 */
260 public function getHost() {
261 if (empty($this->parts['host'])) {
262 return false;
263 }
264 return $this->parts['host'];
265 }
266
1557cefb
A
267 /**
268 * Test if the Url is an HTTP one.
269 *
270 * @return true is HTTP, false otherwise.
271 */
272 public function isHttp() {
273 return strpos(strtolower($this->parts['scheme']), 'http') !== false;
274 }
d9d776af 275}