Url.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. */
  13. function 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. return "$scheme$user$pass$host$port$path$query$fragment";
  25. }
  26. /**
  27. * Removes undesired query parameters and fragments
  28. *
  29. * @param string url Url to be cleaned
  30. *
  31. * @return string the string representation of this URL after cleanup
  32. */
  33. function cleanup_url($url)
  34. {
  35. $obj_url = new Url($url);
  36. return $obj_url->cleanup();
  37. }
  38. /**
  39. * Get URL scheme.
  40. *
  41. * @param string url Url for which the scheme is requested
  42. *
  43. * @return mixed the URL scheme or false if none is provided.
  44. */
  45. function get_url_scheme($url)
  46. {
  47. $obj_url = new Url($url);
  48. return $obj_url->getScheme();
  49. }
  50. /**
  51. * Adds a trailing slash at the end of URL if necessary.
  52. *
  53. * @param string $url URL to check/edit.
  54. *
  55. * @return string $url URL with a end trailing slash.
  56. */
  57. function add_trailing_slash($url)
  58. {
  59. return $url . (!endsWith($url, '/') ? '/' : '');
  60. }
  61. /**
  62. * Converts an URL with an IDN host to a ASCII one.
  63. *
  64. * @param string $url Input URL.
  65. *
  66. * @return string converted URL.
  67. */
  68. function url_with_idn_to_ascii($url)
  69. {
  70. $parts = parse_url($url);
  71. $parts['host'] = idn_to_ascii($parts['host']);
  72. $httpUrl = new \http\Url($parts);
  73. return $httpUrl->toString();
  74. }
  75. /**
  76. * URL representation and cleanup utilities
  77. *
  78. * Form
  79. * scheme://[username:password@]host[:port][/path][?query][#fragment]
  80. *
  81. * Examples
  82. * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
  83. * https://host.name.tld
  84. * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
  85. *
  86. * @see http://www.faqs.org/rfcs/rfc3986.html
  87. */
  88. class Url
  89. {
  90. private static $annoyingQueryParams = array(
  91. // Facebook
  92. 'action_object_map=',
  93. 'action_ref_map=',
  94. 'action_type_map=',
  95. 'fb_',
  96. 'fb=',
  97. 'PHPSESSID=',
  98. // Scoop.it
  99. '__scoop',
  100. // Google Analytics & FeedProxy
  101. 'utm_',
  102. // ATInternet
  103. 'xtor='
  104. );
  105. private static $annoyingFragments = array(
  106. // ATInternet
  107. 'xtor=RSS-',
  108. // Misc.
  109. 'tk.rss_all'
  110. );
  111. /*
  112. * URL parts represented as an array
  113. *
  114. * @see http://php.net/parse_url
  115. */
  116. protected $parts;
  117. /**
  118. * Parses a string containing a URL
  119. *
  120. * @param string $url a string containing a URL
  121. */
  122. public function __construct($url)
  123. {
  124. $url = self::cleanupUnparsedUrl(trim($url));
  125. $this->parts = parse_url($url);
  126. if (!empty($url) && empty($this->parts['scheme'])) {
  127. $this->parts['scheme'] = 'http';
  128. }
  129. }
  130. /**
  131. * Clean up URL before it's parsed.
  132. * ie. handle urlencode, url prefixes, etc.
  133. *
  134. * @param string $url URL to clean.
  135. *
  136. * @return string cleaned URL.
  137. */
  138. protected static function cleanupUnparsedUrl($url)
  139. {
  140. return self::removeFirefoxAboutReader($url);
  141. }
  142. /**
  143. * Remove Firefox Reader prefix if it's present.
  144. *
  145. * @param string $input url
  146. *
  147. * @return string cleaned url
  148. */
  149. protected static function removeFirefoxAboutReader($input)
  150. {
  151. $firefoxPrefix = 'about://reader?url=';
  152. if (startsWith($input, $firefoxPrefix)) {
  153. return urldecode(ltrim($input, $firefoxPrefix));
  154. }
  155. return $input;
  156. }
  157. /**
  158. * Returns a string representation of this URL
  159. */
  160. public function toString()
  161. {
  162. return unparse_url($this->parts);
  163. }
  164. /**
  165. * Removes undesired query parameters
  166. */
  167. protected function cleanupQuery()
  168. {
  169. if (! isset($this->parts['query'])) {
  170. return;
  171. }
  172. $queryParams = explode('&', $this->parts['query']);
  173. foreach (self::$annoyingQueryParams as $annoying) {
  174. foreach ($queryParams as $param) {
  175. if (startsWith($param, $annoying)) {
  176. $queryParams = array_diff($queryParams, array($param));
  177. continue;
  178. }
  179. }
  180. }
  181. if (count($queryParams) == 0) {
  182. unset($this->parts['query']);
  183. return;
  184. }
  185. $this->parts['query'] = implode('&', $queryParams);
  186. }
  187. /**
  188. * Removes undesired fragments
  189. */
  190. protected function cleanupFragment()
  191. {
  192. if (! isset($this->parts['fragment'])) {
  193. return;
  194. }
  195. foreach (self::$annoyingFragments as $annoying) {
  196. if (startsWith($this->parts['fragment'], $annoying)) {
  197. unset($this->parts['fragment']);
  198. break;
  199. }
  200. }
  201. }
  202. /**
  203. * Removes undesired query parameters and fragments
  204. *
  205. * @return string the string representation of this URL after cleanup
  206. */
  207. public function cleanup()
  208. {
  209. $this->cleanupQuery();
  210. $this->cleanupFragment();
  211. return $this->toString();
  212. }
  213. /**
  214. * Converts an URL with an International Domain Name host to a ASCII one.
  215. * This requires PHP-intl. If it's not available, just returns this->cleanup().
  216. *
  217. * @return string converted cleaned up URL.
  218. */
  219. public function idnToAscii()
  220. {
  221. $out = $this->cleanup();
  222. if (! function_exists('idn_to_ascii') || ! isset($this->parts['host'])) {
  223. return $out;
  224. }
  225. $asciiHost = idn_to_ascii($this->parts['host']);
  226. return str_replace($this->parts['host'], $asciiHost, $out);
  227. }
  228. /**
  229. * Get URL scheme.
  230. *
  231. * @return string the URL scheme or false if none is provided.
  232. */
  233. public function getScheme() {
  234. if (!isset($this->parts['scheme'])) {
  235. return false;
  236. }
  237. return $this->parts['scheme'];
  238. }
  239. /**
  240. * Get URL host.
  241. *
  242. * @return string the URL host or false if none is provided.
  243. */
  244. public function getHost() {
  245. if (empty($this->parts['host'])) {
  246. return false;
  247. }
  248. return $this->parts['host'];
  249. }
  250. /**
  251. * Test if the Url is an HTTP one.
  252. *
  253. * @return true is HTTP, false otherwise.
  254. */
  255. public function isHttp() {
  256. return strpos(strtolower($this->parts['scheme']), 'http') !== false;
  257. }
  258. }