]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Url.php
3 * Converts an array-represented URL to a string
5 * Source: http://php.net/manual/en/function.parse-url.php#106731
7 * @see http://php.net/manual/en/function.parse-url.php
9 * @param array $parsedUrl an array-represented URL
11 * @return string the string representation of the URL
13 function unparse_url($parsedUrl)
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'] : '';
25 return "$scheme$user$pass$host$port$path$query$fragment";
29 * Removes undesired query parameters and fragments
31 * @param string url Url to be cleaned
33 * @return string the string representation of this URL after cleanup
35 function cleanup_url($url)
37 $obj_url = new Url($url);
38 return $obj_url->cleanup();
44 * @param string url Url for which the scheme is requested
46 * @return mixed the URL scheme or false if none is provided.
48 function get_url_scheme($url)
50 $obj_url = new Url($url);
51 return $obj_url->getScheme();
55 * Adds a trailing slash at the end of URL if necessary.
57 * @param string $url URL to check/edit.
59 * @return string $url URL with a end trailing slash.
61 function add_trailing_slash($url)
63 return $url . (!endsWith($url, '/') ? '/' : '');
67 * URL representation and cleanup utilities
70 * scheme://[username:password@]host[:port][/path][?query][#fragment]
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
77 * @see http://www.faqs.org/rfcs/rfc3986.html
81 private static $annoyingQueryParams = array(
92 // Google Analytics & FeedProxy
99 private static $annoyingFragments = array(
108 * URL parts represented as an array
110 * @see http://php.net/parse_url
115 * Parses a string containing a URL
117 * @param string $url a string containing a URL
119 public function __construct($url)
121 $this->parts = parse_url(trim($url));
123 if (!empty($url) && empty($this->parts['scheme'])) {
124 $this->parts['scheme'] = 'http';
129 * Returns a string representation of this URL
131 public function toString()
133 return unparse_url($this->parts);
137 * Removes undesired query parameters
139 protected function cleanupQuery()
141 if (! isset($this->parts['query'])) {
145 $queryParams = explode('&', $this->parts['query']);
147 foreach (self::$annoyingQueryParams as $annoying) {
148 foreach ($queryParams as $param) {
149 if (startsWith($param, $annoying)) {
150 $queryParams = array_diff($queryParams, array($param));
156 if (count($queryParams) == 0) {
157 unset($this->parts['query']);
161 $this->parts['query'] = implode('&', $queryParams);
165 * Removes undesired fragments
167 protected function cleanupFragment()
169 if (! isset($this->parts['fragment'])) {
173 foreach (self::$annoyingFragments as $annoying) {
174 if (startsWith($this->parts['fragment'], $annoying)) {
175 unset($this->parts['fragment']);
182 * Removes undesired query parameters and fragments
184 * @return string the string representation of this URL after cleanup
186 public function cleanup()
188 $this->cleanupQuery();
189 $this->cleanupFragment();
190 return $this->toString();
196 * @return string the URL scheme or false if none is provided.
198 public function getScheme() {
199 if (!isset($this->parts['scheme'])) {
202 return $this->parts['scheme'];
206 * Test if the Url is an HTTP one.
208 * @return true is HTTP, false otherwise.
210 public function isHttp() {
211 return strpos(strtolower($this->parts['scheme']), 'http') !== false;