PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/uri.php

https://github.com/joshuarubin/fuel_core
PHP | 295 lines | 163 code | 42 blank | 90 comment | 19 complexity | 2527d8285f474d767378915e82563b92 MD5 | raw file
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Uri Class
  15. *
  16. * @package Fuel
  17. * @category Core
  18. * @author Dan Horrigan
  19. * @link http://fuelphp.com/docs/classes/uri.html
  20. */
  21. class Uri {
  22. protected static $detected_uri = null;
  23. public static function detect()
  24. {
  25. if (static::$detected_uri !== null)
  26. {
  27. return static::$detected_uri;
  28. }
  29. if (\Fuel::$is_cli)
  30. {
  31. if ($uri = \Cli::option('uri') !== null)
  32. {
  33. static::$detected_uri = $uri;
  34. }
  35. else
  36. {
  37. static::$detected_uri = \Cli::option(1);
  38. }
  39. return static::$detected_uri;
  40. }
  41. // We want to use PATH_INFO if we can.
  42. if ( ! empty($_SERVER['PATH_INFO']))
  43. {
  44. $uri = $_SERVER['PATH_INFO'];
  45. }
  46. // Only use ORIG_PATH_INFO if it contains the path
  47. elseif ( ! empty($_SERVER['ORIG_PATH_INFO']) and ($path = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['ORIG_PATH_INFO'])) != '')
  48. {
  49. $uri = $path;
  50. }
  51. else
  52. {
  53. // Fall back to parsing the REQUEST URI
  54. if (isset($_SERVER['REQUEST_URI']))
  55. {
  56. // Some servers require 'index.php?' as the index page
  57. // if we are using mod_rewrite or the server does not require
  58. // the question mark, then parse the url.
  59. if (\Config::get('index_file') != 'index.php?')
  60. {
  61. $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  62. }
  63. else
  64. {
  65. $uri = $_SERVER['REQUEST_URI'];
  66. }
  67. }
  68. else
  69. {
  70. throw new \Fuel_Exception('Unable to detect the URI.');
  71. }
  72. // Remove the base URL from the URI
  73. $base_url = parse_url(\Config::get('base_url'), PHP_URL_PATH);
  74. if ($uri != '' and strncmp($uri, $base_url, strlen($base_url)) === 0)
  75. {
  76. $uri = substr($uri, strlen($base_url));
  77. }
  78. // If we are using an index file (not mod_rewrite) then remove it
  79. $index_file = \Config::get('index_file');
  80. if ($index_file and strncmp($uri, $index_file, strlen($index_file)) === 0)
  81. {
  82. $uri = substr($uri, strlen($index_file));
  83. }
  84. // Lets split the URI up in case it containes a ?. This would
  85. // indecate the server requires 'index.php?' and that mod_rewrite
  86. // is not being used.
  87. preg_match('#(.*?)\?(.*)#i', $uri, $matches);
  88. // If there are matches then lets set set everything correctly
  89. if ( ! empty($matches))
  90. {
  91. $uri = $matches[1];
  92. $_SERVER['QUERY_STRING'] = $matches[2];
  93. parse_str($matches[2], $_GET);
  94. }
  95. }
  96. // Strip the defined url suffix from the uri if needed
  97. $ext = \Config::get('url_suffix');
  98. strrchr($uri, '.') === $ext and $uri = substr($uri,0,-strlen($ext));
  99. // Do some final clean up of the uri
  100. static::$detected_uri = str_replace(array('//', '../'), '/', $uri);
  101. return static::$detected_uri;
  102. }
  103. /**
  104. * Returns the desired segment, or false if it does not exist.
  105. *
  106. * @access public
  107. * @param int The segment number
  108. * @return string
  109. */
  110. public static function segment($segment, $default = null)
  111. {
  112. return \Request::active()->uri->get_segment($segment, $default);
  113. }
  114. /**
  115. * Returns all segments in an array
  116. *
  117. * @return array
  118. */
  119. public static function segments()
  120. {
  121. return \Request::active()->uri->get_segments();
  122. }
  123. /**
  124. * Converts the current URI segments to an associative array. If
  125. * the URI has an odd number of segments, null will be returned.
  126. *
  127. * @return array|null the array or null
  128. */
  129. public static function to_assoc()
  130. {
  131. return \Arr::to_assoc(static::segments());
  132. }
  133. /**
  134. * Returns the full uri as a string
  135. *
  136. * @return string
  137. */
  138. public static function string()
  139. {
  140. return \Request::active()->uri->get();
  141. }
  142. /**
  143. * Creates a url with the given uri, including the base url
  144. *
  145. * @param string the url
  146. * @param array some variables for the url
  147. */
  148. public static function create($uri = null, $variables = array(), $get_variables = array())
  149. {
  150. $url = '';
  151. if(!preg_match("/^(http|https|ftp):\/\//i", $uri))
  152. {
  153. $url .= \Config::get('base_url');
  154. if (\Config::get('index_file'))
  155. {
  156. $url .= \Config::get('index_file').'/';
  157. }
  158. }
  159. $url = $url.ltrim(is_null($uri) ? static::string() : $uri, '/');
  160. substr($url, -1) != '/' and $url .= \Config::get('url_suffix');
  161. if ( ! empty($get_variables))
  162. {
  163. $char = strpos($url, '?') === false ? '?' : '&';
  164. foreach ($get_variables as $key => $val)
  165. {
  166. $url .= $char.$key.'='.$val;
  167. $char = '&';
  168. }
  169. }
  170. foreach($variables as $key => $val)
  171. {
  172. $url = str_replace(':'.$key, $val, $url);
  173. }
  174. return $url;
  175. }
  176. /**
  177. * Gets the current URL, including the BASE_URL
  178. *
  179. * @param string the url
  180. */
  181. public static function main()
  182. {
  183. return static::create(\Request::main()->uri->uri);
  184. }
  185. /**
  186. * Gets the current URL, including the BASE_URL
  187. *
  188. * @param string the url
  189. */
  190. public static function current()
  191. {
  192. return static::create();
  193. }
  194. /**
  195. * Gets the base URL, including the index_file
  196. *
  197. * @return the base uri
  198. */
  199. public static function base($include_index = true)
  200. {
  201. $url = \Config::get('base_url');
  202. if ($include_index and \Config::get('index_file'))
  203. {
  204. $url .= \Config::get('index_file').'/';
  205. }
  206. return $url;
  207. }
  208. /**
  209. * @var string The URI string
  210. */
  211. public $uri = '';
  212. /**
  213. * @var array The URI segements
  214. */
  215. public $segments = '';
  216. /**
  217. * Contruct takes a URI or detects it if none is given and generates
  218. * the segments.
  219. *
  220. * @access public
  221. * @param string The URI
  222. * @return void
  223. */
  224. public function __construct($uri = NULL)
  225. {
  226. if ($uri === NULL)
  227. {
  228. $uri = static::detect();
  229. }
  230. $this->uri = \Security::clean_uri(trim($uri, '/'));
  231. $this->segments = explode('/', $this->uri);
  232. }
  233. public function get()
  234. {
  235. return $this->uri;
  236. }
  237. public function get_segments()
  238. {
  239. return $this->segments;
  240. }
  241. public function get_segment($segment, $default = null)
  242. {
  243. if (isset($this->segments[$segment - 1]))
  244. {
  245. return $this->segments[$segment - 1];
  246. }
  247. return $default;
  248. }
  249. public function __toString()
  250. {
  251. return $this->get();
  252. }
  253. }