Url.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // Other
  91. 'campaign_'
  92. );
  93. private static $annoyingFragments = array(
  94. // ATInternet
  95. 'xtor=RSS-',
  96. // Misc.
  97. 'tk.rss_all'
  98. );
  99. /*
  100. * URL parts represented as an array
  101. *
  102. * @see http://php.net/parse_url
  103. */
  104. protected $parts;
  105. /**
  106. * Parses a string containing a URL
  107. *
  108. * @param string $url a string containing a URL
  109. */
  110. public function __construct($url)
  111. {
  112. $url = self::cleanupUnparsedUrl(trim($url));
  113. $this->parts = parse_url($url);
  114. if (!empty($url) && empty($this->parts['scheme'])) {
  115. $this->parts['scheme'] = 'http';
  116. }
  117. }
  118. /**
  119. * Clean up URL before it's parsed.
  120. * ie. handle urlencode, url prefixes, etc.
  121. *
  122. * @param string $url URL to clean.
  123. *
  124. * @return string cleaned URL.
  125. */
  126. protected static function cleanupUnparsedUrl($url)
  127. {
  128. return self::removeFirefoxAboutReader($url);
  129. }
  130. /**
  131. * Remove Firefox Reader prefix if it's present.
  132. *
  133. * @param string $input url
  134. *
  135. * @return string cleaned url
  136. */
  137. protected static function removeFirefoxAboutReader($input)
  138. {
  139. $firefoxPrefix = 'about://reader?url=';
  140. if (startsWith($input, $firefoxPrefix)) {
  141. return urldecode(ltrim($input, $firefoxPrefix));
  142. }
  143. return $input;
  144. }
  145. /**
  146. * Returns a string representation of this URL
  147. */
  148. public function toString()
  149. {
  150. return unparse_url($this->parts);
  151. }
  152. /**
  153. * Removes undesired query parameters
  154. */
  155. protected function cleanupQuery()
  156. {
  157. if (! isset($this->parts['query'])) {
  158. return;
  159. }
  160. $queryParams = explode('&', $this->parts['query']);
  161. foreach (self::$annoyingQueryParams as $annoying) {
  162. foreach ($queryParams as $param) {
  163. if (startsWith($param, $annoying)) {
  164. $queryParams = array_diff($queryParams, array($param));
  165. continue;
  166. }
  167. }
  168. }
  169. if (count($queryParams) == 0) {
  170. unset($this->parts['query']);
  171. return;
  172. }
  173. $this->parts['query'] = implode('&', $queryParams);
  174. }
  175. /**
  176. * Removes undesired fragments
  177. */
  178. protected function cleanupFragment()
  179. {
  180. if (! isset($this->parts['fragment'])) {
  181. return;
  182. }
  183. foreach (self::$annoyingFragments as $annoying) {
  184. if (startsWith($this->parts['fragment'], $annoying)) {
  185. unset($this->parts['fragment']);
  186. break;
  187. }
  188. }
  189. }
  190. /**
  191. * Removes undesired query parameters and fragments
  192. *
  193. * @return string the string representation of this URL after cleanup
  194. */
  195. public function cleanup()
  196. {
  197. $this->cleanupQuery();
  198. $this->cleanupFragment();
  199. return $this->toString();
  200. }
  201. /**
  202. * Converts an URL with an International Domain Name host to a ASCII one.
  203. * This requires PHP-intl. If it's not available, just returns this->cleanup().
  204. *
  205. * @return string converted cleaned up URL.
  206. */
  207. public function idnToAscii()
  208. {
  209. $out = $this->cleanup();
  210. if (! function_exists('idn_to_ascii') || ! isset($this->parts['host'])) {
  211. return $out;
  212. }
  213. $asciiHost = idn_to_ascii($this->parts['host']);
  214. return str_replace($this->parts['host'], $asciiHost, $out);
  215. }
  216. /**
  217. * Get URL scheme.
  218. *
  219. * @return string the URL scheme or false if none is provided.
  220. */
  221. public function getScheme() {
  222. if (!isset($this->parts['scheme'])) {
  223. return false;
  224. }
  225. return $this->parts['scheme'];
  226. }
  227. /**
  228. * Get URL host.
  229. *
  230. * @return string the URL host or false if none is provided.
  231. */
  232. public function getHost() {
  233. if (empty($this->parts['host'])) {
  234. return false;
  235. }
  236. return $this->parts['host'];
  237. }
  238. /**
  239. * Test if the Url is an HTTP one.
  240. *
  241. * @return true is HTTP, false otherwise.
  242. */
  243. public function isHttp() {
  244. return strpos(strtolower($this->parts['scheme']), 'http') !== false;
  245. }
  246. }