PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Router.php

https://github.com/cawago/ci_campusync_auth
PHP | 389 lines | 177 code | 57 blank | 155 comment | 26 complexity | 589c2c1cc5b606106f699ffd09c6e279 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Router Class
  18. *
  19. * Parses URIs and determines routing
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @author ExpressionEngine Dev Team
  24. * @category Libraries
  25. * @link http://codeigniter.com/user_guide/general/routing.html
  26. */
  27. class CI_Router {
  28. var $config;
  29. var $routes = array();
  30. var $error_routes = array();
  31. var $class = '';
  32. var $method = 'index';
  33. var $directory = '';
  34. var $uri_protocol = 'auto';
  35. var $default_controller;
  36. var $scaffolding_request = FALSE; // Must be set to FALSE
  37. /**
  38. * Constructor
  39. *
  40. * Runs the route mapping function.
  41. */
  42. function CI_Router()
  43. {
  44. $this->config =& load_class('Config');
  45. $this->uri =& load_class('URI');
  46. $this->_set_routing();
  47. log_message('debug', "Router Class Initialized");
  48. }
  49. // --------------------------------------------------------------------
  50. /**
  51. * Set the route mapping
  52. *
  53. * This function determines what should be served based on the URI request,
  54. * as well as any "routes" that have been set in the routing config file.
  55. *
  56. * @access private
  57. * @return void
  58. */
  59. function _set_routing()
  60. {
  61. // Are query strings enabled in the config file?
  62. // If so, we're done since segment based URIs are not used with query strings.
  63. if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
  64. {
  65. $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
  66. if (isset($_GET[$this->config->item('function_trigger')]))
  67. {
  68. $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
  69. }
  70. return;
  71. }
  72. // Load the routes.php file.
  73. @include(APPPATH.'config/routes'.EXT);
  74. $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
  75. unset($route);
  76. // Set the default controller so we can display it in the event
  77. // the URI doesn't correlated to a valid controller.
  78. $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
  79. // Fetch the complete URI string
  80. $this->uri->_fetch_uri_string();
  81. // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
  82. if ($this->uri->uri_string == '')
  83. {
  84. if ($this->default_controller === FALSE)
  85. {
  86. show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
  87. }
  88. if (strpos($this->default_controller, '/') !== FALSE)
  89. {
  90. $x = explode('/', $this->default_controller);
  91. $this->set_class(end($x));
  92. $this->set_method('index');
  93. $this->_set_request($x);
  94. }
  95. else
  96. {
  97. $this->set_class($this->default_controller);
  98. $this->set_method('index');
  99. $this->_set_request(array($this->default_controller, 'index'));
  100. }
  101. // re-index the routed segments array so it starts with 1 rather than 0
  102. $this->uri->_reindex_segments();
  103. log_message('debug', "No URI present. Default controller set.");
  104. return;
  105. }
  106. unset($this->routes['default_controller']);
  107. // Do we need to remove the URL suffix?
  108. $this->uri->_remove_url_suffix();
  109. // Compile the segments into an array
  110. $this->uri->_explode_segments();
  111. // Parse any custom routing that may exist
  112. $this->_parse_routes();
  113. // Re-index the segment array so that it starts with 1 rather than 0
  114. $this->uri->_reindex_segments();
  115. }
  116. // --------------------------------------------------------------------
  117. /**
  118. * Set the Route
  119. *
  120. * This function takes an array of URI segments as
  121. * input, and sets the current class/method
  122. *
  123. * @access private
  124. * @param array
  125. * @param bool
  126. * @return void
  127. */
  128. function _set_request($segments = array())
  129. {
  130. $segments = $this->_validate_request($segments);
  131. if (count($segments) == 0)
  132. {
  133. return;
  134. }
  135. $this->set_class($segments[0]);
  136. if (isset($segments[1]))
  137. {
  138. // A scaffolding request. No funny business with the URL
  139. if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
  140. {
  141. $this->scaffolding_request = TRUE;
  142. unset($this->routes['scaffolding_trigger']);
  143. }
  144. else
  145. {
  146. // A standard method request
  147. $this->set_method($segments[1]);
  148. }
  149. }
  150. else
  151. {
  152. // This lets the "routed" segment array identify that the default
  153. // index method is being used.
  154. $segments[1] = 'index';
  155. }
  156. // Update our "routed" segment array to contain the segments.
  157. // Note: If there is no custom routing, this array will be
  158. // identical to $this->uri->segments
  159. $this->uri->rsegments = $segments;
  160. }
  161. // --------------------------------------------------------------------
  162. /**
  163. * Validates the supplied segments. Attempts to determine the path to
  164. * the controller.
  165. *
  166. * @access private
  167. * @param array
  168. * @return array
  169. */
  170. function _validate_request($segments)
  171. {
  172. // Does the requested controller exist in the root folder?
  173. if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
  174. {
  175. return $segments;
  176. }
  177. // Is the controller in a sub-folder?
  178. if (is_dir(APPPATH.'controllers/'.$segments[0]))
  179. {
  180. // Set the directory and remove it from the segment array
  181. $this->set_directory($segments[0]);
  182. $segments = array_slice($segments, 1);
  183. if (count($segments) > 0)
  184. {
  185. // Does the requested controller exist in the sub-folder?
  186. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
  187. {
  188. show_404($this->fetch_directory().$segments[0]);
  189. }
  190. }
  191. else
  192. {
  193. $this->set_class($this->default_controller);
  194. $this->set_method('index');
  195. // Does the default controller exist in the sub-folder?
  196. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
  197. {
  198. $this->directory = '';
  199. return array();
  200. }
  201. }
  202. return $segments;
  203. }
  204. // Can't find the requested controller...
  205. show_404($segments[0]);
  206. }
  207. // --------------------------------------------------------------------
  208. /**
  209. * Parse Routes
  210. *
  211. * This function matches any routes that may exist in
  212. * the config/routes.php file against the URI to
  213. * determine if the class/method need to be remapped.
  214. *
  215. * @access private
  216. * @return void
  217. */
  218. function _parse_routes()
  219. {
  220. // Do we even have any custom routing to deal with?
  221. // There is a default scaffolding trigger, so we'll look just for 1
  222. if (count($this->routes) == 1)
  223. {
  224. $this->_set_request($this->uri->segments);
  225. return;
  226. }
  227. // Turn the segment array into a URI string
  228. $uri = implode('/', $this->uri->segments);
  229. // Is there a literal match? If so we're done
  230. if (isset($this->routes[$uri]))
  231. {
  232. $this->_set_request(explode('/', $this->routes[$uri]));
  233. return;
  234. }
  235. // Loop through the route array looking for wild-cards
  236. foreach ($this->routes as $key => $val)
  237. {
  238. // Convert wild-cards to RegEx
  239. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  240. // Does the RegEx match?
  241. if (preg_match('#^'.$key.'$#', $uri))
  242. {
  243. // Do we have a back-reference?
  244. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
  245. {
  246. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  247. }
  248. $this->_set_request(explode('/', $val));
  249. return;
  250. }
  251. }
  252. // If we got this far it means we didn't encounter a
  253. // matching route so we'll set the site default route
  254. $this->_set_request($this->uri->segments);
  255. }
  256. // --------------------------------------------------------------------
  257. /**
  258. * Set the class name
  259. *
  260. * @access public
  261. * @param string
  262. * @return void
  263. */
  264. function set_class($class)
  265. {
  266. $this->class = $class;
  267. }
  268. // --------------------------------------------------------------------
  269. /**
  270. * Fetch the current class
  271. *
  272. * @access public
  273. * @return string
  274. */
  275. function fetch_class()
  276. {
  277. return $this->class;
  278. }
  279. // --------------------------------------------------------------------
  280. /**
  281. * Set the method name
  282. *
  283. * @access public
  284. * @param string
  285. * @return void
  286. */
  287. function set_method($method)
  288. {
  289. $this->method = $method;
  290. }
  291. // --------------------------------------------------------------------
  292. /**
  293. * Fetch the current method
  294. *
  295. * @access public
  296. * @return string
  297. */
  298. function fetch_method()
  299. {
  300. if ($this->method == $this->fetch_class())
  301. {
  302. return 'index';
  303. }
  304. return $this->method;
  305. }
  306. // --------------------------------------------------------------------
  307. /**
  308. * Set the directory name
  309. *
  310. * @access public
  311. * @param string
  312. * @return void
  313. */
  314. function set_directory($dir)
  315. {
  316. $this->directory = $dir.'/';
  317. }
  318. // --------------------------------------------------------------------
  319. /**
  320. * Fetch the sub-directory (if any) that contains the requested controller class
  321. *
  322. * @access public
  323. * @return string
  324. */
  325. function fetch_directory()
  326. {
  327. return $this->directory;
  328. }
  329. }
  330. // END Router Class
  331. /* End of file Router.php */
  332. /* Location: ./system/libraries/Router.php */