PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/url.php

https://bitbucket.org/emolina/asesores
PHP | 186 lines | 79 code | 19 blank | 88 comment | 10 complexity | be2af526b717db0f430fcf8e198656ff MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * URL helper class.
  4. *
  5. * @package Kohana
  6. * @category Helpers
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2010 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_URL {
  12. /**
  13. * Gets the base URL to the application. To include the current protocol,
  14. * use TRUE. To specify a protocol, provide the protocol as a string.
  15. * If a protocol is used, a complete URL will be generated using the
  16. * `$_SERVER['HTTP_HOST']` variable.
  17. *
  18. * // Absolute relative, no host or protocol
  19. * echo URL::base();
  20. *
  21. * // Complete relative, with host and protocol
  22. * echo URL::base(TRUE, TRUE);
  23. *
  24. * // Complete relative, with host and "https" protocol
  25. * echo URL::base(TRUE, 'https');
  26. *
  27. * @param boolean add index file to URL?
  28. * @param mixed protocol string or boolean, add protocol and domain?
  29. * @return string
  30. * @uses Kohana::$index_file
  31. * @uses Request::$protocol
  32. */
  33. public static function base($index = FALSE, $protocol = FALSE)
  34. {
  35. // Start with the configured base URL
  36. $base_url = Kohana::$base_url;
  37. if ($protocol === TRUE)
  38. {
  39. // Use the current protocol
  40. $protocol = Request::$protocol;
  41. }
  42. elseif ($protocol === FALSE AND $scheme = parse_url($base_url, PHP_URL_SCHEME))
  43. {
  44. // Use the configured default protocol
  45. $protocol = $scheme;
  46. }
  47. if ($index === TRUE AND ! empty(Kohana::$index_file))
  48. {
  49. // Add the index file to the URL
  50. $base_url .= Kohana::$index_file.'/';
  51. }
  52. if (is_string($protocol))
  53. {
  54. if ($port = parse_url($base_url, PHP_URL_PORT))
  55. {
  56. // Found a port, make it usable for the URL
  57. $port = ':'.$port;
  58. }
  59. if ($domain = parse_url($base_url, PHP_URL_HOST))
  60. {
  61. // Remove everything but the path from the URL
  62. $base_url = parse_url($base_url, PHP_URL_PATH);
  63. }
  64. else
  65. {
  66. // Attempt to use HTTP_HOST and fallback to SERVER_NAME
  67. $domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  68. }
  69. // Add the protocol and domain to the base URL
  70. $base_url = $protocol.'://'.$domain.$port.$base_url;
  71. }
  72. return $base_url;
  73. }
  74. /**
  75. * Fetches an absolute site URL based on a URI segment.
  76. *
  77. * echo URL::site('foo/bar');
  78. *
  79. * @param string site URI to convert
  80. * @param mixed protocol string or boolean, add protocol and domain?
  81. * @return string
  82. * @uses URL::base
  83. */
  84. public static function site($uri = '', $protocol = FALSE)
  85. {
  86. // Chop off possible scheme, host, port, user and pass parts
  87. $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
  88. if ( ! UTF8::is_ascii($path))
  89. {
  90. // Encode all non-ASCII characters, as per RFC 1738
  91. $path = preg_replace('~([^/]+)~e', 'rawurlencode("$1")', $path);
  92. }
  93. // Concat the URL
  94. return URL::base(TRUE, $protocol).$path;
  95. }
  96. /**
  97. * Merges the current GET parameters with an array of new or overloaded
  98. * parameters and returns the resulting query string.
  99. *
  100. * // Returns "?sort=title&limit=10" combined with any existing GET values
  101. * $query = URL::query(array('sort' => 'title', 'limit' => 10));
  102. *
  103. * Typically you would use this when you are sorting query results,
  104. * or something similar.
  105. *
  106. * [!!] Parameters with a NULL value are left out.
  107. *
  108. * @param array array of GET parameters
  109. * @param boolean include current request GET parameters
  110. * @return string
  111. */
  112. public static function query(array $params = NULL, $use_get = TRUE)
  113. {
  114. if ($use_get)
  115. {
  116. if ($params === NULL)
  117. {
  118. // Use only the current parameters
  119. $params = $_GET;
  120. }
  121. else
  122. {
  123. // Merge the current and new parameters
  124. $params = array_merge($_GET, $params);
  125. }
  126. }
  127. if (empty($params))
  128. {
  129. // No query parameters
  130. return '';
  131. }
  132. // Note: http_build_query returns an empty string for a params array with only NULL values
  133. $query = http_build_query($params, '', '&');
  134. // Don't prepend '?' to an empty string
  135. return ($query === '') ? '' : ('?'.$query);
  136. }
  137. /**
  138. * Convert a phrase to a URL-safe title.
  139. *
  140. * echo URL::title('My Blog Post'); // "my-blog-post"
  141. *
  142. * @param string phrase to convert
  143. * @param string word separator (any single character)
  144. * @param boolean transliterate to ASCII?
  145. * @return string
  146. * @uses UTF8::transliterate_to_ascii
  147. */
  148. public static function title($title, $separator = '-', $ascii_only = FALSE)
  149. {
  150. if ($ascii_only === TRUE)
  151. {
  152. // Transliterate non-ASCII characters
  153. $title = UTF8::transliterate_to_ascii($title);
  154. // Remove all characters that are not the separator, a-z, 0-9, or whitespace
  155. $title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
  156. }
  157. else
  158. {
  159. // Remove all characters that are not the separator, letters, numbers, or whitespace
  160. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
  161. }
  162. // Replace all separator characters and whitespace by a single separator
  163. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  164. // Trim separators from the beginning and end
  165. return trim($title, $separator);
  166. }
  167. } // End url