Advertisement
pisio

/* End of file Router.php */

Feb 3rd, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.24 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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.  
  18. /**
  19. * Router Class
  20. *
  21. * Parses URIs and determines routing
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Libraries
  25. * @author ExpressionEngine Dev Team
  26. * @category Libraries
  27. * @link http://codeigniter.com/user_guide/general/routing.html
  28. */
  29. class CI_Router {
  30.  
  31. /**
  32. * Config class
  33. *
  34. * @var object
  35. * @access public
  36. */
  37. var $config;
  38. /**
  39. * List of routes
  40. *
  41. * @var array
  42. * @access public
  43. */
  44. var $routes = array();
  45. /**
  46. * List of error routes
  47. *
  48. * @var array
  49. * @access public
  50. */
  51. var $error_routes = array();
  52. /**
  53. * Current class name
  54. *
  55. * @var string
  56. * @access public
  57. */
  58. var $class = '';
  59. /**
  60. * Current method name
  61. *
  62. * @var string
  63. * @access public
  64. */
  65. var $method = 'index';
  66. /**
  67. * Sub-directory that contains the requested controller class
  68. *
  69. * @var string
  70. * @access public
  71. */
  72. var $directory = '';
  73. /**
  74. * Default controller (and method if specific)
  75. *
  76. * @var string
  77. * @access public
  78. */
  79. var $default_controller;
  80.  
  81. /**
  82. * Constructor
  83. *
  84. * Runs the route mapping function.
  85. */
  86. function __construct()
  87. {
  88. $this->config =& load_class('Config', 'core');
  89. $this->uri =& load_class('URI', 'core');
  90. log_message('debug', "Router Class Initialized");
  91. }
  92.  
  93. // --------------------------------------------------------------------
  94.  
  95. /**
  96. * Set the route mapping
  97. *
  98. * This function determines what should be served based on the URI request,
  99. * as well as any "routes" that have been set in the routing config file.
  100. *
  101. * @access private
  102. * @return void
  103. */
  104. function _set_routing()
  105. {
  106. // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
  107. // since URI segments are more search-engine friendly, but they can optionally be used.
  108. // If this feature is enabled, we will gather the directory/class/method a little differently
  109. $segments = array();
  110. if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
  111. {
  112. if (isset($_GET[$this->config->item('directory_trigger')]))
  113. {
  114. $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
  115. $segments[] = $this->fetch_directory();
  116. }
  117.  
  118. if (isset($_GET[$this->config->item('controller_trigger')]))
  119. {
  120. $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
  121. $segments[] = $this->fetch_class();
  122. }
  123.  
  124. if (isset($_GET[$this->config->item('function_trigger')]))
  125. {
  126. $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
  127. $segments[] = $this->fetch_method();
  128. }
  129. }
  130.  
  131. // Load the routes.php file.
  132. if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
  133. {
  134. include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
  135. }
  136. elseif (is_file(APPPATH.'config/routes.php'))
  137. {
  138. include(APPPATH.'config/routes.php');
  139. }
  140.  
  141. $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
  142. unset($route);
  143.  
  144. // Set the default controller so we can display it in the event
  145. // the URI doesn't correlated to a valid controller.
  146. $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
  147.  
  148. // Were there any query string segments? If so, we'll validate them and bail out since we're done.
  149. if (count($segments) > 0)
  150. {
  151. return $this->_validate_request($segments);
  152. }
  153.  
  154. // Fetch the complete URI string
  155. $this->uri->_fetch_uri_string();
  156.  
  157. // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
  158. if ($this->uri->uri_string == '')
  159. {
  160. return $this->_set_default_controller();
  161. }
  162.  
  163. // Do we need to remove the URL suffix?
  164. $this->uri->_remove_url_suffix();
  165.  
  166. // Compile the segments into an array
  167. $this->uri->_explode_segments();
  168.  
  169. // Parse any custom routing that may exist
  170. $this->_parse_routes();
  171.  
  172. // Re-index the segment array so that it starts with 1 rather than 0
  173. $this->uri->_reindex_segments();
  174. }
  175.  
  176. // --------------------------------------------------------------------
  177.  
  178. /**
  179. * Set the default controller
  180. *
  181. * @access private
  182. * @return void
  183. */
  184. function _set_default_controller()
  185. {
  186. if ($this->default_controller === FALSE)
  187. {
  188. show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
  189. }
  190. // Is the method being specified?
  191. if (strpos($this->default_controller, '/') !== FALSE)
  192. {
  193. $x = explode('/', $this->default_controller);
  194.  
  195. $this->set_class($x[0]);
  196. $this->set_method($x[1]);
  197. $this->_set_request($x);
  198. }
  199. else
  200. {
  201. $this->set_class($this->default_controller);
  202. $this->set_method('index');
  203. $this->_set_request(array($this->default_controller, 'index'));
  204. }
  205.  
  206. // re-index the routed segments array so it starts with 1 rather than 0
  207. $this->uri->_reindex_segments();
  208.  
  209. log_message('debug', "No URI present. Default controller set.");
  210. }
  211.  
  212. // --------------------------------------------------------------------
  213.  
  214. /**
  215. * Set the Route
  216. *
  217. * This function takes an array of URI segments as
  218. * input, and sets the current class/method
  219. *
  220. * @access private
  221. * @param array
  222. * @param bool
  223. * @return void
  224. */
  225. function _set_request($segments = array())
  226. {
  227. $segments = $this->_validate_request($segments);
  228.  
  229. if (count($segments) == 0)
  230. {
  231. return $this->_set_default_controller();
  232. }
  233.  
  234. $this->set_class($segments[0]);
  235.  
  236. if (isset($segments[1]))
  237. {
  238. // A standard method request
  239. $this->set_method($segments[1]);
  240. }
  241. else
  242. {
  243. // This lets the "routed" segment array identify that the default
  244. // index method is being used.
  245. $segments[1] = 'index';
  246. }
  247.  
  248. // Update our "routed" segment array to contain the segments.
  249. // Note: If there is no custom routing, this array will be
  250. // identical to $this->uri->segments
  251. $this->uri->rsegments = $segments;
  252. }
  253.  
  254. // --------------------------------------------------------------------
  255.  
  256. /**
  257. * Validates the supplied segments. Attempts to determine the path to
  258. * the controller.
  259. *
  260. * @access private
  261. * @param array
  262. * @return array
  263. */
  264. function _validate_request($segments)
  265. {
  266. if (count($segments) == 0)
  267. {
  268. return $segments;
  269. }
  270.  
  271. // Does the requested controller exist in the root folder?
  272. if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
  273. {
  274. return $segments;
  275. }
  276. if (file_exists(APPPATH.'../protected/vortexcms/'.$segments[0].'.php'))
  277. {
  278. return $segments;
  279. }
  280.  
  281. // Is the controller in a sub-folder?
  282. if (is_dir(APPPATH.'controllers/'.$segments[0]))
  283. {
  284. // Set the directory and remove it from the segment array
  285. $this->set_directory($segments[0]);
  286. $segments = array_slice($segments, 1);
  287.  
  288. if (count($segments) > 0)
  289. {
  290. // Does the requested controller exist in the sub-folder?
  291. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
  292. {
  293. if ( ! empty($this->routes['404_override']))
  294. {
  295. $x = explode('/', $this->routes['404_override']);
  296.  
  297. $this->set_directory('');
  298. $this->set_class($x[0]);
  299. $this->set_method(isset($x[1]) ? $x[1] : 'index');
  300.  
  301. return $x;
  302. }
  303. else
  304. {
  305. show_404($this->fetch_directory().$segments[0]);
  306. }
  307. }
  308. }
  309. else
  310. {
  311. // Is the method being specified in the route?
  312. if (strpos($this->default_controller, '/') !== FALSE)
  313. {
  314. $x = explode('/', $this->default_controller);
  315.  
  316. $this->set_class($x[0]);
  317. $this->set_method($x[1]);
  318. }
  319. else
  320. {
  321. $this->set_class($this->default_controller);
  322. $this->set_method('index');
  323. }
  324.  
  325. // Does the default controller exist in the sub-folder?
  326. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
  327. {
  328. $this->directory = '';
  329. return array();
  330. }
  331.  
  332. }
  333.  
  334. return $segments;
  335. }
  336. /* VORTEX CMS FOLDER ADDON */
  337. // Is the controller in a sub-folder?
  338. if (is_dir(APPPATH.'../protected/vortexcms/'.$segments[0]))
  339. {
  340. // Set the directory and remove it from the segment array
  341. $this->set_directory($segments[0]);
  342. $segments = array_slice($segments, 1);
  343.  
  344. if (count($segments) > 0)
  345. {
  346. // Does the requested controller exist in the sub-folder?
  347. if ( ! file_exists(APPPATH.'../protected/vortexcms/'.$this->fetch_directory().$segments[0].'.php'))
  348. {
  349. if ( ! empty($this->routes['404_override']))
  350. {
  351. $x = explode('/', $this->routes['404_override']);
  352.  
  353. $this->set_directory('');
  354. $this->set_class($x[0]);
  355. $this->set_method(isset($x[1]) ? $x[1] : 'index');
  356.  
  357. return $x;
  358. }
  359. else
  360. {
  361. show_404($this->fetch_directory().$segments[0]);
  362. }
  363. }
  364. }
  365. else
  366. {
  367. // Is the method being specified in the route?
  368. if (strpos($this->default_controller, '/') !== FALSE)
  369. {
  370. $x = explode('/', $this->default_controller);
  371.  
  372. $this->set_class($x[0]);
  373. $this->set_method($x[1]);
  374. }
  375. else
  376. {
  377. $this->set_class($this->default_controller);
  378. $this->set_method('index');
  379. }
  380.  
  381. // Does the default controller exist in the sub-folder?
  382. if ( ! file_exists(APPPATH.'../protected/vortexcms/'.$this->fetch_directory().$this->default_controller.'.php'))
  383. {
  384. $this->directory = '';
  385. return array();
  386. }
  387.  
  388. }
  389.  
  390. return $segments;
  391. }
  392.  
  393.  
  394. // If we've gotten this far it means that the URI does not correlate to a valid
  395. // controller class. We will now see if there is an override
  396. if ( ! empty($this->routes['404_override']))
  397. {
  398. $x = explode('/', $this->routes['404_override']);
  399.  
  400. $this->set_class($x[0]);
  401. $this->set_method(isset($x[1]) ? $x[1] : 'index');
  402.  
  403. return $x;
  404. }
  405.  
  406.  
  407. // Nothing else to do at this point but show a 404
  408. show_404($segments[0]);
  409. }
  410.  
  411. // --------------------------------------------------------------------
  412.  
  413. /**
  414. * Parse Routes
  415. *
  416. * This function matches any routes that may exist in
  417. * the config/routes.php file against the URI to
  418. * determine if the class/method need to be remapped.
  419. *
  420. * @access private
  421. * @return void
  422. */
  423. function _parse_routes()
  424. {
  425. // Turn the segment array into a URI string
  426. $uri = implode('/', $this->uri->segments);
  427.  
  428. // Is there a literal match? If so we're done
  429. if (isset($this->routes[$uri]))
  430. {
  431. return $this->_set_request(explode('/', $this->routes[$uri]));
  432. }
  433.  
  434. // Loop through the route array looking for wild-cards
  435. foreach ($this->routes as $key => $val)
  436. {
  437. // Convert wild-cards to RegEx
  438. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  439.  
  440. // Does the RegEx match?
  441. if (preg_match('#^'.$key.'$#', $uri))
  442. {
  443. // Do we have a back-reference?
  444. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
  445. {
  446. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  447. }
  448.  
  449. return $this->_set_request(explode('/', $val));
  450. }
  451. }
  452.  
  453. // If we got this far it means we didn't encounter a
  454. // matching route so we'll set the site default route
  455. $this->_set_request($this->uri->segments);
  456. }
  457.  
  458. // --------------------------------------------------------------------
  459.  
  460. /**
  461. * Set the class name
  462. *
  463. * @access public
  464. * @param string
  465. * @return void
  466. */
  467. function set_class($class)
  468. {
  469. $this->class = str_replace(array('/', '.'), '', $class);
  470. }
  471.  
  472. // --------------------------------------------------------------------
  473.  
  474. /**
  475. * Fetch the current class
  476. *
  477. * @access public
  478. * @return string
  479. */
  480. function fetch_class()
  481. {
  482. return $this->class;
  483. }
  484.  
  485. // --------------------------------------------------------------------
  486.  
  487. /**
  488. * Set the method name
  489. *
  490. * @access public
  491. * @param string
  492. * @return void
  493. */
  494. function set_method($method)
  495. {
  496. $this->method = $method;
  497. }
  498.  
  499. // --------------------------------------------------------------------
  500.  
  501. /**
  502. * Fetch the current method
  503. *
  504. * @access public
  505. * @return string
  506. */
  507. function fetch_method()
  508. {
  509. if ($this->method == $this->fetch_class())
  510. {
  511. return 'index';
  512. }
  513.  
  514. return $this->method;
  515. }
  516.  
  517. // --------------------------------------------------------------------
  518.  
  519. /**
  520. * Set the directory name
  521. *
  522. * @access public
  523. * @param string
  524. * @return void
  525. */
  526. function set_directory($dir)
  527. {
  528. $this->directory = str_replace(array('/', '.'), '', $dir).'/';
  529. }
  530.  
  531. // --------------------------------------------------------------------
  532.  
  533. /**
  534. * Fetch the sub-directory (if any) that contains the requested controller class
  535. *
  536. * @access public
  537. * @return string
  538. */
  539. function fetch_directory()
  540. {
  541. return $this->directory;
  542. }
  543.  
  544. // --------------------------------------------------------------------
  545.  
  546. /**
  547. * Set the controller overrides
  548. *
  549. * @access public
  550. * @param array
  551. * @return null
  552. */
  553. function _set_overrides($routing)
  554. {
  555. if ( ! is_array($routing))
  556. {
  557. return;
  558. }
  559.  
  560. if (isset($routing['directory']))
  561. {
  562. $this->set_directory($routing['directory']);
  563. }
  564.  
  565. if (isset($routing['controller']) AND $routing['controller'] != '')
  566. {
  567. $this->set_class($routing['controller']);
  568. }
  569.  
  570. if (isset($routing['function']))
  571. {
  572. $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
  573. $this->set_method($routing['function']);
  574. }
  575. }
  576.  
  577.  
  578. }
  579. // END Router Class
  580.  
  581. /* End of file Router.php */
  582. /* Location: ./system/core/Router.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement