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