Url.php 6.7 KB

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