Url.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * URL representation and cleanup utilities
  52. *
  53. * Form
  54. * scheme://[username:password@]host[:port][/path][?query][#fragment]
  55. *
  56. * Examples
  57. * http://username:password@hostname:9090/path?arg1=value1&arg2=value2#anchor
  58. * https://host.name.tld
  59. * https://h2.g2/faq/?vendor=hitchhiker&item=guide&dest=galaxy#answer
  60. *
  61. * @see http://www.faqs.org/rfcs/rfc3986.html
  62. */
  63. class Url
  64. {
  65. private static $annoyingQueryParams = array(
  66. // Facebook
  67. 'action_object_map=',
  68. 'action_ref_map=',
  69. 'action_type_map=',
  70. 'fb_',
  71. 'fb=',
  72. // Scoop.it
  73. '__scoop',
  74. // Google Analytics & FeedProxy
  75. 'utm_',
  76. // ATInternet
  77. 'xtor='
  78. );
  79. private static $annoyingFragments = array(
  80. // ATInternet
  81. 'xtor=RSS-',
  82. // Misc.
  83. 'tk.rss_all'
  84. );
  85. /*
  86. * URL parts represented as an array
  87. *
  88. * @see http://php.net/parse_url
  89. */
  90. protected $parts;
  91. /**
  92. * Parses a string containing a URL
  93. *
  94. * @param string $url a string containing a URL
  95. */
  96. public function __construct($url)
  97. {
  98. $this->parts = parse_url($url);
  99. if (!empty($url) && empty($this->parts['scheme'])) {
  100. $this->parts['scheme'] = 'http';
  101. }
  102. }
  103. /**
  104. * Returns a string representation of this URL
  105. */
  106. public function toString()
  107. {
  108. return unparse_url($this->parts);
  109. }
  110. /**
  111. * Removes undesired query parameters
  112. */
  113. protected function cleanupQuery()
  114. {
  115. if (! isset($this->parts['query'])) {
  116. return;
  117. }
  118. $queryParams = explode('&', $this->parts['query']);
  119. foreach (self::$annoyingQueryParams as $annoying) {
  120. foreach ($queryParams as $param) {
  121. if (startsWith($param, $annoying)) {
  122. $queryParams = array_diff($queryParams, array($param));
  123. continue;
  124. }
  125. }
  126. }
  127. if (count($queryParams) == 0) {
  128. unset($this->parts['query']);
  129. return;
  130. }
  131. $this->parts['query'] = implode('&', $queryParams);
  132. }
  133. /**
  134. * Removes undesired fragments
  135. */
  136. protected function cleanupFragment()
  137. {
  138. if (! isset($this->parts['fragment'])) {
  139. return;
  140. }
  141. foreach (self::$annoyingFragments as $annoying) {
  142. if (startsWith($this->parts['fragment'], $annoying)) {
  143. unset($this->parts['fragment']);
  144. break;
  145. }
  146. }
  147. }
  148. /**
  149. * Removes undesired query parameters and fragments
  150. *
  151. * @return string the string representation of this URL after cleanup
  152. */
  153. public function cleanup()
  154. {
  155. $this->cleanupQuery();
  156. $this->cleanupFragment();
  157. return $this->toString();
  158. }
  159. /**
  160. * Get URL scheme.
  161. *
  162. * @return string the URL scheme or false if none is provided.
  163. */
  164. public function getScheme() {
  165. if (!isset($this->parts['scheme'])) {
  166. return false;
  167. }
  168. return $this->parts['scheme'];
  169. }
  170. }