]>
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 * URL representation and cleanup utilities
32 * scheme://[username:password@]host[:port][/path][?query][#fragment]
35 * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
36 * https://host.name.tld
37 * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
39 * @see http://www.faqs.org/rfcs/rfc3986.html
43 private static $annoyingQueryParams = array(
54 // Google Analytics & FeedProxy
61 private static $annoyingFragments = array(
70 * URL parts represented as an array
72 * @see http://php.net/parse_url
77 * Parses a string containing a URL
79 * @param string $url a string containing a URL
81 public function __construct($url)
83 $this->parts = parse_url($url);
87 * Returns a string representation of this URL
89 public function __toString()
91 return unparse_url($this->parts);
95 * Removes undesired query parameters
97 protected function cleanupQuery()
99 if (! isset($this->parts['query'])) {
103 $queryParams = explode('&', $this->parts['query']);
105 foreach (self::$annoyingQueryParams as $annoying) {
106 foreach ($queryParams as $param) {
107 if (startsWith($param, $annoying)) {
108 $queryParams = array_diff($queryParams, array($param));
114 if (count($queryParams) == 0) {
115 unset($this->parts['query']);
119 $this->parts['query'] = implode('&', $queryParams);
123 * Removes undesired fragments
125 protected function cleanupFragment()
127 if (! isset($this->parts['fragment'])) {
131 foreach (self::$annoyingFragments as $annoying) {
132 if (startsWith($this->parts['fragment'], $annoying)) {
133 unset($this->parts['fragment']);
140 * Removes undesired query parameters and fragments
142 * @return string the string representation of this URL after cleanup
144 public function cleanup()
146 $this->cleanupQuery();
147 $this->cleanupFragment();
148 return $this->__toString();