]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/Url.php
Merge pull request #472 from ArthurHoaro/wallabag-version-parameter
[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=',
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 {
1557cefb 121 $this->parts = parse_url(trim($url));
9e1724f1
A
122
123 if (!empty($url) && empty($this->parts['scheme'])) {
124 $this->parts['scheme'] = 'http';
125 }
d9d776af
V
126 }
127
128 /**
129 * Returns a string representation of this URL
130 */
ef591e7e 131 public function toString()
d9d776af
V
132 {
133 return unparse_url($this->parts);
134 }
135
136 /**
137 * Removes undesired query parameters
138 */
139 protected function cleanupQuery()
140 {
141 if (! isset($this->parts['query'])) {
142 return;
143 }
144
145 $queryParams = explode('&', $this->parts['query']);
146
147 foreach (self::$annoyingQueryParams as $annoying) {
148 foreach ($queryParams as $param) {
149 if (startsWith($param, $annoying)) {
150 $queryParams = array_diff($queryParams, array($param));
151 continue;
152 }
153 }
154 }
155
156 if (count($queryParams) == 0) {
157 unset($this->parts['query']);
158 return;
159 }
160
161 $this->parts['query'] = implode('&', $queryParams);
162 }
163
164 /**
165 * Removes undesired fragments
166 */
167 protected function cleanupFragment()
168 {
169 if (! isset($this->parts['fragment'])) {
170 return;
171 }
172
173 foreach (self::$annoyingFragments as $annoying) {
174 if (startsWith($this->parts['fragment'], $annoying)) {
175 unset($this->parts['fragment']);
176 break;
177 }
178 }
179 }
180
181 /**
182 * Removes undesired query parameters and fragments
183 *
184 * @return string the string representation of this URL after cleanup
185 */
186 public function cleanup()
187 {
188 $this->cleanupQuery();
189 $this->cleanupFragment();
ef591e7e 190 return $this->toString();
d9d776af 191 }
9e1724f1
A
192
193 /**
194 * Get URL scheme.
195 *
196 * @return string the URL scheme or false if none is provided.
197 */
198 public function getScheme() {
199 if (!isset($this->parts['scheme'])) {
200 return false;
201 }
202 return $this->parts['scheme'];
203 }
1557cefb
A
204
205 /**
206 * Test if the Url is an HTTP one.
207 *
208 * @return true is HTTP, false otherwise.
209 */
210 public function isHttp() {
211 return strpos(strtolower($this->parts['scheme']), 'http') !== false;
212 }
d9d776af 213}