]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/Url.php
Merge pull request #406 from ArthurHoaro/qrcode-style
[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
d9d776af
V
54/**
55 * URL representation and cleanup utilities
56 *
57 * Form
58 * scheme://[username:password@]host[:port][/path][?query][#fragment]
59 *
60 * Examples
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
64 *
65 * @see http://www.faqs.org/rfcs/rfc3986.html
66 */
67class Url
68{
69 private static $annoyingQueryParams = array(
70 // Facebook
71 'action_object_map=',
72 'action_ref_map=',
73 'action_type_map=',
74 'fb_',
75 'fb=',
76
77 // Scoop.it
78 '__scoop',
79
80 // Google Analytics & FeedProxy
81 'utm_',
82
83 // ATInternet
84 'xtor='
85 );
86
87 private static $annoyingFragments = array(
88 // ATInternet
89 'xtor=RSS-',
90
91 // Misc.
92 'tk.rss_all'
93 );
94
95 /*
96 * URL parts represented as an array
97 *
98 * @see http://php.net/parse_url
99 */
100 protected $parts;
101
102 /**
103 * Parses a string containing a URL
104 *
105 * @param string $url a string containing a URL
106 */
107 public function __construct($url)
108 {
109 $this->parts = parse_url($url);
9e1724f1
A
110
111 if (!empty($url) && empty($this->parts['scheme'])) {
112 $this->parts['scheme'] = 'http';
113 }
d9d776af
V
114 }
115
116 /**
117 * Returns a string representation of this URL
118 */
ef591e7e 119 public function toString()
d9d776af
V
120 {
121 return unparse_url($this->parts);
122 }
123
124 /**
125 * Removes undesired query parameters
126 */
127 protected function cleanupQuery()
128 {
129 if (! isset($this->parts['query'])) {
130 return;
131 }
132
133 $queryParams = explode('&', $this->parts['query']);
134
135 foreach (self::$annoyingQueryParams as $annoying) {
136 foreach ($queryParams as $param) {
137 if (startsWith($param, $annoying)) {
138 $queryParams = array_diff($queryParams, array($param));
139 continue;
140 }
141 }
142 }
143
144 if (count($queryParams) == 0) {
145 unset($this->parts['query']);
146 return;
147 }
148
149 $this->parts['query'] = implode('&', $queryParams);
150 }
151
152 /**
153 * Removes undesired fragments
154 */
155 protected function cleanupFragment()
156 {
157 if (! isset($this->parts['fragment'])) {
158 return;
159 }
160
161 foreach (self::$annoyingFragments as $annoying) {
162 if (startsWith($this->parts['fragment'], $annoying)) {
163 unset($this->parts['fragment']);
164 break;
165 }
166 }
167 }
168
169 /**
170 * Removes undesired query parameters and fragments
171 *
172 * @return string the string representation of this URL after cleanup
173 */
174 public function cleanup()
175 {
176 $this->cleanupQuery();
177 $this->cleanupFragment();
ef591e7e 178 return $this->toString();
d9d776af 179 }
9e1724f1
A
180
181 /**
182 * Get URL scheme.
183 *
184 * @return string the URL scheme or false if none is provided.
185 */
186 public function getScheme() {
187 if (!isset($this->parts['scheme'])) {
188 return false;
189 }
190 return $this->parts['scheme'];
191 }
d9d776af 192}