]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Url.php
e7c3a4cc4ae50291bd9e1cccf3dc55bd47899940
[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
89 // Scoop.it
90 '__scoop',
91
92 // Google Analytics & FeedProxy
93 'utm_',
94
95 // ATInternet
96 'xtor='
97 );
98
99 private static $annoyingFragments = array(
100 // ATInternet
101 'xtor=RSS-',
102
103 // Misc.
104 'tk.rss_all'
105 );
106
107 /*
108 * URL parts represented as an array
109 *
110 * @see http://php.net/parse_url
111 */
112 protected $parts;
113
114 /**
115 * Parses a string containing a URL
116 *
117 * @param string $url a string containing a URL
118 */
119 public function __construct($url)
120 {
121 $this->parts = parse_url(trim($url));
122
123 if (!empty($url) && empty($this->parts['scheme'])) {
124 $this->parts['scheme'] = 'http';
125 }
126 }
127
128
129 private function removeFirefoxAboutReader($input){
130 $output_array = [];
131 preg_match("%^about://reader\?url=(.*)%", $input, $output_array);
132 if(!empty($output_array)){
133 $extractedUrl = preg_replace("%^about://reader\?url=(.*)%", "$1", $input);
134 $url = urldecode($extractedUrl);
135 }else{
136 $url = $input;
137 }
138 return $url;
139 }
140
141 /**
142 * Returns a string representation of this URL
143 */
144 public function toString()
145 {
146 return unparse_url($this->parts);
147 }
148
149 /**
150 * Removes undesired query parameters
151 */
152 protected function cleanupQuery()
153 {
154 if (! isset($this->parts['query'])) {
155 return;
156 }
157
158 $queryParams = explode('&', $this->parts['query']);
159
160 foreach (self::$annoyingQueryParams as $annoying) {
161 foreach ($queryParams as $param) {
162 if (startsWith($param, $annoying)) {
163 $queryParams = array_diff($queryParams, array($param));
164 continue;
165 }
166 }
167 }
168
169 if (count($queryParams) == 0) {
170 unset($this->parts['query']);
171 return;
172 }
173
174 $this->parts['query'] = implode('&', $queryParams);
175 }
176
177 /**
178 * Removes undesired fragments
179 */
180 protected function cleanupFragment()
181 {
182 if (! isset($this->parts['fragment'])) {
183 return;
184 }
185
186 foreach (self::$annoyingFragments as $annoying) {
187 if (startsWith($this->parts['fragment'], $annoying)) {
188 unset($this->parts['fragment']);
189 break;
190 }
191 }
192 }
193
194 /**
195 * Removes undesired query parameters and fragments
196 *
197 * @return string the string representation of this URL after cleanup
198 */
199 public function cleanup()
200 {
201 $this->cleanupQuery();
202 $this->cleanupFragment();
203 $url = $this->toString();
204 return $this->removeFirefoxAboutReader($url);
205 }
206
207 /**
208 * Get URL scheme.
209 *
210 * @return string the URL scheme or false if none is provided.
211 */
212 public function getScheme() {
213 if (!isset($this->parts['scheme'])) {
214 return false;
215 }
216 return $this->parts['scheme'];
217 }
218
219 /**
220 * Test if the Url is an HTTP one.
221 *
222 * @return true is HTTP, false otherwise.
223 */
224 public function isHttp() {
225 return strpos(strtolower($this->parts['scheme']), 'http') !== false;
226 }
227 }