Url.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. // Scoop.it
  84. '__scoop',
  85. // Google Analytics & FeedProxy
  86. 'utm_',
  87. // ATInternet
  88. 'xtor='
  89. );
  90. private static $annoyingFragments = array(
  91. // ATInternet
  92. 'xtor=RSS-',
  93. // Misc.
  94. 'tk.rss_all'
  95. );
  96. /*
  97. * URL parts represented as an array
  98. *
  99. * @see http://php.net/parse_url
  100. */
  101. protected $parts;
  102. /**
  103. * Parses a string containing a URL
  104. *
  105. * @param string $url a string containing a URL
  106. */
  107. public function __construct($url)
  108. {
  109. $this->parts = parse_url(trim($url));
  110. if (!empty($url) && empty($this->parts['scheme'])) {
  111. $this->parts['scheme'] = 'http';
  112. }
  113. }
  114. /**
  115. * Returns a string representation of this URL
  116. */
  117. public function toString()
  118. {
  119. return unparse_url($this->parts);
  120. }
  121. /**
  122. * Removes undesired query parameters
  123. */
  124. protected function cleanupQuery()
  125. {
  126. if (! isset($this->parts['query'])) {
  127. return;
  128. }
  129. $queryParams = explode('&', $this->parts['query']);
  130. foreach (self::$annoyingQueryParams as $annoying) {
  131. foreach ($queryParams as $param) {
  132. if (startsWith($param, $annoying)) {
  133. $queryParams = array_diff($queryParams, array($param));
  134. continue;
  135. }
  136. }
  137. }
  138. if (count($queryParams) == 0) {
  139. unset($this->parts['query']);
  140. return;
  141. }
  142. $this->parts['query'] = implode('&', $queryParams);
  143. }
  144. /**
  145. * Removes undesired fragments
  146. */
  147. protected function cleanupFragment()
  148. {
  149. if (! isset($this->parts['fragment'])) {
  150. return;
  151. }
  152. foreach (self::$annoyingFragments as $annoying) {
  153. if (startsWith($this->parts['fragment'], $annoying)) {
  154. unset($this->parts['fragment']);
  155. break;
  156. }
  157. }
  158. }
  159. /**
  160. * Removes undesired query parameters and fragments
  161. *
  162. * @return string the string representation of this URL after cleanup
  163. */
  164. public function cleanup()
  165. {
  166. $this->cleanupQuery();
  167. $this->cleanupFragment();
  168. return $this->toString();
  169. }
  170. /**
  171. * Get URL scheme.
  172. *
  173. * @return string the URL scheme or false if none is provided.
  174. */
  175. public function getScheme() {
  176. if (!isset($this->parts['scheme'])) {
  177. return false;
  178. }
  179. return $this->parts['scheme'];
  180. }
  181. /**
  182. * Test if the Url is an HTTP one.
  183. *
  184. * @return true is HTTP, false otherwise.
  185. */
  186. public function isHttp() {
  187. return strpos(strtolower($this->parts['scheme']), 'http') !== false;
  188. }
  189. }