]>
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 * URL representation and cleanup utilities
58 * scheme://[username:password@]host[:port][/path][?query][#fragment]
61 * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
62 * https://host.name.tld
63 * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
65 * @see http://www.faqs.org/rfcs/rfc3986.html
69 private static $annoyingQueryParams = array(
80 // Google Analytics & FeedProxy
87 private static $annoyingFragments = array(
96 * URL parts represented as an array
98 * @see http://php.net/parse_url
103 * Parses a string containing a URL
105 * @param string $url a string containing a URL
107 public function __construct($url)
109 $this->parts = parse_url($url);
111 if (!empty($url) && empty($this->parts['scheme'])) {
112 $this->parts['scheme'] = 'http';
117 * Returns a string representation of this URL
119 public function toString()
121 return unparse_url($this->parts);
125 * Removes undesired query parameters
127 protected function cleanupQuery()
129 if (! isset($this->parts['query'])) {
133 $queryParams = explode('&', $this->parts['query']);
135 foreach (self::$annoyingQueryParams as $annoying) {
136 foreach ($queryParams as $param) {
137 if (startsWith($param, $annoying)) {
138 $queryParams = array_diff($queryParams, array($param));
144 if (count($queryParams) == 0) {
145 unset($this->parts['query']);
149 $this->parts['query'] = implode('&', $queryParams);
153 * Removes undesired fragments
155 protected function cleanupFragment()
157 if (! isset($this->parts['fragment'])) {
161 foreach (self::$annoyingFragments as $annoying) {
162 if (startsWith($this->parts['fragment'], $annoying)) {
163 unset($this->parts['fragment']);
170 * Removes undesired query parameters and fragments
172 * @return string the string representation of this URL after cleanup
174 public function cleanup()
176 $this->cleanupQuery();
177 $this->cleanupFragment();
178 return $this->toString();
184 * @return string the URL scheme or false if none is provided.
186 public function getScheme() {
187 if (!isset($this->parts['scheme'])) {
190 return $this->parts['scheme'];