]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/http/Url.php
3 namespace Shaarli\Http
;
6 * URL representation and cleanup utilities
9 * scheme://[username:password@]host[:port][/path][?query][#fragment]
12 * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
13 * https://host.name.tld
14 * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
16 * @see http://www.faqs.org/rfcs/rfc3986.html
20 private static $annoyingQueryParams = array(
32 // Google Analytics & FeedProxy
42 private static $annoyingFragments = array(
51 * URL parts represented as an array
53 * @see http://php.net/parse_url
58 * Parses a string containing a URL
60 * @param string $url a string containing a URL
62 public function __construct($url)
64 $url = self
::cleanupUnparsedUrl(trim($url));
65 $this->parts
= parse_url($url);
67 if (!empty($url) && empty($this->parts
['scheme'])) {
68 $this->parts
['scheme'] = 'http';
73 * Clean up URL before it's parsed.
74 * ie. handle urlencode, url prefixes, etc.
76 * @param string $url URL to clean.
78 * @return string cleaned URL.
80 protected static function cleanupUnparsedUrl($url)
82 return self
::removeFirefoxAboutReader($url);
86 * Remove Firefox Reader prefix if it's present.
88 * @param string $input url
90 * @return string cleaned url
92 protected static function removeFirefoxAboutReader($input)
94 $firefoxPrefix = 'about://reader?url=';
95 if (startsWith($input, $firefoxPrefix)) {
96 return urldecode(ltrim($input, $firefoxPrefix));
102 * Returns a string representation of this URL
104 public function toString()
106 return unparse_url($this->parts
);
110 * Removes undesired query parameters
112 protected function cleanupQuery()
114 if (!isset($this->parts
['query'])) {
118 $queryParams = explode('&', $this->parts
['query']);
120 foreach (self
::$annoyingQueryParams as $annoying) {
121 foreach ($queryParams as $param) {
122 if (startsWith($param, $annoying)) {
123 $queryParams = array_diff($queryParams, array($param));
129 if (count($queryParams) == 0) {
130 unset($this->parts
['query']);
134 $this->parts
['query'] = implode('&', $queryParams);
138 * Removes undesired fragments
140 protected function cleanupFragment()
142 if (!isset($this->parts
['fragment'])) {
146 foreach (self
::$annoyingFragments as $annoying) {
147 if (startsWith($this->parts
['fragment'], $annoying)) {
148 unset($this->parts
['fragment']);
155 * Removes undesired query parameters and fragments
157 * @return string the string representation of this URL after cleanup
159 public function cleanup()
161 $this->cleanupQuery();
162 $this->cleanupFragment();
163 return $this->toString();
167 * Converts an URL with an International Domain Name host to a ASCII one.
168 * This requires PHP-intl. If it's not available, just returns this->cleanup().
170 * @return string converted cleaned up URL.
172 public function idnToAscii()
174 $out = $this->cleanup();
175 if (!function_exists('idn_to_ascii') || !isset($this->parts
['host'])) {
178 $asciiHost = idn_to_ascii($this->parts
['host'], 0, INTL_IDNA_VARIANT_UTS46
);
179 return str_replace($this->parts
['host'], $asciiHost, $out);
185 * @return string the URL scheme or false if none is provided.
187 public function getScheme()
189 if (!isset($this->parts
['scheme'])) {
192 return $this->parts
['scheme'];
198 * @return string the URL host or false if none is provided.
200 public function getHost()
202 if (empty($this->parts
['host'])) {
205 return $this->parts
['host'];
209 * Test if the UrlUtils is an HTTP one.
211 * @return true is HTTP, false otherwise.
213 public function isHttp()
215 return strpos(strtolower($this->parts
['scheme']), 'http') !== false;