]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Url.php
c166ff6ef03f6023c2bf895279f8eb8940736ba9
[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 'PHPSESSID=',
103
104 // Scoop.it
105 '__scoop',
106
107 // Google Analytics & FeedProxy
108 'utm_',
109
110 // ATInternet
111 'xtor='
112 );
113
114 private static $annoyingFragments = array(
115 // ATInternet
116 'xtor=RSS-',
117
118 // Misc.
119 'tk.rss_all'
120 );
121
122 /*
123 * URL parts represented as an array
124 *
125 * @see http://php.net/parse_url
126 */
127 protected $parts;
128
129 /**
130 * Parses a string containing a URL
131 *
132 * @param string $url a string containing a URL
133 */
134 public function __construct($url)
135 {
136 $url = self::cleanupUnparsedUrl(trim($url));
137 $this->parts = parse_url($url);
138
139 if (!empty($url) && empty($this->parts['scheme'])) {
140 $this->parts['scheme'] = 'http';
141 }
142 }
143
144 /**
145 * Clean up URL before it's parsed.
146 * ie. handle urlencode, url prefixes, etc.
147 *
148 * @param string $url URL to clean.
149 *
150 * @return string cleaned URL.
151 */
152 protected static function cleanupUnparsedUrl($url)
153 {
154 return self::removeFirefoxAboutReader($url);
155 }
156
157 /**
158 * Remove Firefox Reader prefix if it's present.
159 *
160 * @param string $input url
161 *
162 * @return string cleaned url
163 */
164 protected static function removeFirefoxAboutReader($input)
165 {
166 $firefoxPrefix = 'about://reader?url=';
167 if (startsWith($input, $firefoxPrefix)) {
168 return urldecode(ltrim($input, $firefoxPrefix));
169 }
170 return $input;
171 }
172
173 /**
174 * Returns a string representation of this URL
175 */
176 public function toString()
177 {
178 return unparse_url($this->parts);
179 }
180
181 /**
182 * Removes undesired query parameters
183 */
184 protected function cleanupQuery()
185 {
186 if (! isset($this->parts['query'])) {
187 return;
188 }
189
190 $queryParams = explode('&', $this->parts['query']);
191
192 foreach (self::$annoyingQueryParams as $annoying) {
193 foreach ($queryParams as $param) {
194 if (startsWith($param, $annoying)) {
195 $queryParams = array_diff($queryParams, array($param));
196 continue;
197 }
198 }
199 }
200
201 if (count($queryParams) == 0) {
202 unset($this->parts['query']);
203 return;
204 }
205
206 $this->parts['query'] = implode('&', $queryParams);
207 }
208
209 /**
210 * Removes undesired fragments
211 */
212 protected function cleanupFragment()
213 {
214 if (! isset($this->parts['fragment'])) {
215 return;
216 }
217
218 foreach (self::$annoyingFragments as $annoying) {
219 if (startsWith($this->parts['fragment'], $annoying)) {
220 unset($this->parts['fragment']);
221 break;
222 }
223 }
224 }
225
226 /**
227 * Removes undesired query parameters and fragments
228 *
229 * @return string the string representation of this URL after cleanup
230 */
231 public function cleanup()
232 {
233 $this->cleanupQuery();
234 $this->cleanupFragment();
235 return $this->toString();
236 }
237
238 /**
239 * Converts an URL with an International Domain Name host to a ASCII one.
240 * This requires PHP-intl. If it's not available, just returns this->cleanup().
241 *
242 * @return string converted cleaned up URL.
243 */
244 public function idnToAscii()
245 {
246 $out = $this->cleanup();
247 if (! function_exists('idn_to_ascii') || ! isset($this->parts['host'])) {
248 return $out;
249 }
250 $asciiHost = idn_to_ascii($this->parts['host']);
251 return str_replace($this->parts['host'], $asciiHost, $out);
252 }
253
254 /**
255 * Get URL scheme.
256 *
257 * @return string the URL scheme or false if none is provided.
258 */
259 public function getScheme() {
260 if (!isset($this->parts['scheme'])) {
261 return false;
262 }
263 return $this->parts['scheme'];
264 }
265
266 /**
267 * Get URL host.
268 *
269 * @return string the URL host or false if none is provided.
270 */
271 public function getHost() {
272 if (empty($this->parts['host'])) {
273 return false;
274 }
275 return $this->parts['host'];
276 }
277
278 /**
279 * Test if the Url is an HTTP one.
280 *
281 * @return true is HTTP, false otherwise.
282 */
283 public function isHttp() {
284 return strpos(strtolower($this->parts['scheme']), 'http') !== false;
285 }
286 }