PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/system/application/libraries/SZ_Router.php

https://bitbucket.org/seezoo/seezoo/
PHP | 341 lines | 238 code | 42 blank | 61 comment | 33 complexity | c71fcc15f8e1da8f3c2660d39140f301 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * =========================================================
  4. *Extended CodeIgniter builtin Router Class
  5. *
  6. * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
  7. *
  8. * @additional works:
  9. * load and ignite controller of "3" directory levels.
  10. * this class is not return 404. finally returns default controller.
  11. * =========================================================
  12. */
  13. class SZ_Router extends CI_Router
  14. {
  15. // CI regular routing directories;
  16. var $directory_reg = array();
  17. var $is_packaged_directory = FALSE;
  18. var $directory_array = array();
  19. var $directory_reg_array = array();
  20. var $directory = array();
  21. function __construct()
  22. {
  23. parent::CI_Router();
  24. }
  25. /**
  26. * Set the route mapping (Override)
  27. *
  28. * This function determines what should be served based on the URI request,
  29. * as well as any "routes" that have been set in the routing config file.
  30. *
  31. * Additional:
  32. * Add route mapping for login controller to no more use "login" to security attack
  33. *
  34. * @override CI_Router::_set_routing
  35. * @access private
  36. * @return void
  37. */
  38. function _set_routing()
  39. {
  40. // Are query strings enabled in the config file?
  41. // If so, we're done since segment based URIs are not used with query strings.
  42. if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
  43. {
  44. $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
  45. if (isset($_GET[$this->config->item('function_trigger')]))
  46. {
  47. $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
  48. }
  49. return;
  50. }
  51. // Load the routes.php file.
  52. @include(APPPATH.'config/routes'.EXT);
  53. $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
  54. unset($route);
  55. if ( defined('SEEZOO_SYSTEM_LOGIN_URI') )
  56. {
  57. $this->routes[SEEZOO_SYSTEM_LOGIN_URI . '(.*)'] = 'login$1';
  58. }
  59. // Set the default controller so we can display it in the event
  60. // the URI doesn't correlated to a valid controller.
  61. $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
  62. // Fetch the complete URI string
  63. $this->uri->_fetch_uri_string();
  64. // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
  65. if ($this->uri->uri_string == '')
  66. {
  67. if ($this->default_controller === FALSE)
  68. {
  69. show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
  70. }
  71. if (strpos($this->default_controller, '/') !== FALSE)
  72. {
  73. $x = explode('/', $this->default_controller);
  74. $this->set_class(end($x));
  75. $this->set_method('index');
  76. $this->_set_request($x);
  77. }
  78. else
  79. {
  80. $this->set_class($this->default_controller);
  81. $this->set_method('index');
  82. $this->_set_request(array($this->default_controller, 'index'));
  83. }
  84. // re-index the routed segments array so it starts with 1 rather than 0
  85. $this->uri->_reindex_segments();
  86. log_message('debug', "No URI present. Default controller set.");
  87. return;
  88. }
  89. unset($this->routes['default_controller']);
  90. // Do we need to remove the URL suffix?
  91. $this->uri->_remove_url_suffix();
  92. // Compile the segments into an array
  93. $this->uri->_explode_segments();
  94. // Parse any custom routing that may exist
  95. $this->_parse_routes();
  96. // Re-index the segment array so that it starts with 1 rather than 0
  97. $this->uri->_reindex_segments();
  98. }
  99. function _custom_validate_request($segments)
  100. {
  101. $ext_path = SZ_EXT_PATH . 'controllers/';
  102. if ( file_exists($ext_path . $segments[0].EXT))
  103. {
  104. $this->set_directory_ext();
  105. return $segments;
  106. }
  107. if ( is_dir($ext_path . $segments[0]))
  108. {
  109. $this->set_directory_ext($segments[0]);
  110. //$tmp_dir = $segments[0];
  111. $segments = array_slice($segments, 1);
  112. if (count($segments) > 0)
  113. {
  114. if (is_dir($ext_path . $this->fetch_directory_reg() . $segments[0]))
  115. {
  116. // Set temp directory_name
  117. $tmp_dir = $this->fetch_directory_reg() . $segments[0];
  118. $deep_segments = array_slice($segments, 1);
  119. if (count($deep_segments) > 0)
  120. {
  121. if ( file_exists($ext_path . $tmp_dir . '/' . $deep_segments[0] . EXT))
  122. {
  123. $this->set_directory_ext($tmp_dir);
  124. return $deep_segments;
  125. }
  126. else
  127. {
  128. $this->reset_directory_ext();
  129. return FALSE;
  130. }
  131. }
  132. else
  133. {
  134. // Does the default controller exist in the sub-folder?
  135. if ( file_exists($ext_path.$tmp_dir.'/'.$this->default_controller.EXT))
  136. {
  137. $this->set_directory_ext($tmp_dir);
  138. $this->set_class($this->default_controller);
  139. $this->set_method('index');
  140. //$dir = substr($tmp_dir, 0, strrpos($tmp_dir, '/'));
  141. //$this->set_directory_ext($tmp_dir);
  142. return array();
  143. }
  144. }
  145. }
  146. else
  147. {
  148. if (file_exists($ext_path.$this->fetch_directory_reg().$segments[0].EXT))
  149. {
  150. return $segments;
  151. }
  152. }
  153. }
  154. else
  155. {
  156. if ( file_exists($ext_path . $this->fetch_directory_reg() . $this->default_controller.EXT))
  157. {
  158. $this->set_class($this->default_controller);
  159. $this->set_method('index');
  160. return array();
  161. }
  162. }
  163. }
  164. $this->reset_directory_ext();
  165. return FALSE;
  166. }
  167. function set_directory_ext($dir = FALSE)
  168. {
  169. // @note directory path is relative from system/application/controllers/
  170. //$this->directory = '../../../' . SZ_EXT_DIR . 'controllers';
  171. $this->is_packaged_directory = TRUE;
  172. if ($dir !== FALSE)
  173. {
  174. $exp = explode('/', $dir);
  175. $dir = str_replace(array('/', '.'), '', end($exp));
  176. $this->directory[] = $dir;
  177. $this->directory_reg[] = $dir;
  178. }
  179. }
  180. function set_directory($dir)
  181. {
  182. // CodeIgniter 1.7.3 code is str_replace(array('/', '.'), '', $dir).'/'
  183. // but, when routing 3 levels derectory, that code is not work..
  184. // so that, we replace "." only
  185. $exp = explode('/', $dir);
  186. $dir = str_replace(array('/', '.'), '', end($exp));
  187. $this->directory[] = $dir;
  188. $this->directory_reg[] = $dir;
  189. }
  190. function reset_directory_ext()
  191. {
  192. $this->directory = array();
  193. $this->directory_reg = array();
  194. $this->is_packaged_directory = FALSE;
  195. }
  196. // always returns really routed directory
  197. function fetch_directory_reg()
  198. {
  199. return (count($this->directory_reg) === 0)
  200. ? ''
  201. : (implode('/', $this->directory_reg) . '/');
  202. }
  203. function fetch_directory()
  204. {
  205. $prefix = '';
  206. if ($this->is_packaged_directory === TRUE)
  207. {
  208. $prefix = '../../../' . SZ_EXT_DIR . 'controllers/';
  209. }
  210. return (count($this->directory) === 0)
  211. ? $prefix
  212. : $prefix . (implode('/', $this->directory) . '/');
  213. }
  214. /**
  215. * _validate_request : override method
  216. */
  217. function _validate_request($segments)
  218. {
  219. $customed_segments = $this->_custom_validate_request($segments);
  220. if (is_array($customed_segments))
  221. {
  222. return $customed_segments;
  223. }
  224. // Does the requested controller exist in the root folder?
  225. if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
  226. {
  227. return $segments;
  228. }
  229. // Is the controller in a sub-folder?
  230. if (is_dir(APPPATH.'controllers/'.$segments[0]))
  231. {
  232. // Set the directory and remove it from the segment array
  233. $this->set_directory($segments[0]);
  234. $segments = array_slice($segments, 1);
  235. if (count($segments) > 0)
  236. {
  237. // modified by Yoshiaki Sugimoto : search controller indexof level 3.
  238. if (is_dir(APPPATH . 'controllers/' . $this->fetch_directory() . $segments[0]))
  239. {
  240. // Set temp directory_name
  241. $tmp_dir = $this->fetch_directory() . $segments[0];
  242. $deep_segments = array_slice($segments, 1);
  243. if (count($deep_segments) > 0)
  244. {
  245. if ( file_exists(APPPATH . 'controllers/' . $tmp_dir . '/' . $deep_segments[0] . EXT))
  246. {
  247. $this->set_directory($tmp_dir);
  248. $segments = $deep_segments;
  249. }
  250. else
  251. {
  252. // Does the requested controller exist in the sub-folder?
  253. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
  254. {
  255. show_404($this->fetch_directory() . $segments[0]);
  256. }
  257. }
  258. }
  259. else
  260. {
  261. // Does the default controller exist in the sub-folder?
  262. $this->set_class($this->default_controller);
  263. $this->set_method('index');
  264. $this->set_directory($tmp_dir);
  265. // Does the requested controller exist in the sub-folder?
  266. if ( file_exists(APPPATH.'controllers/'. $tmp_dir . '/' . $this->default_controller.EXT))
  267. {
  268. return array();
  269. }
  270. }
  271. }
  272. else
  273. {
  274. if ( ! file_exists(APPPATH . 'controllers/' . $this->fetch_directory() . $segments[0].EXT))
  275. {
  276. show_404();
  277. }
  278. }
  279. }
  280. else
  281. {
  282. $this->set_class($this->default_controller);
  283. $this->set_method('index');
  284. // Does the default controller exist in the sub-folder?
  285. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
  286. {
  287. $this->directory = '';
  288. return array();
  289. }
  290. }
  291. return $segments;
  292. }
  293. // stop show_404 untill do $CI->_remap
  294. $tmp = $segments[0];
  295. $segments[0] = 'page';
  296. $segments[1] = $tmp;
  297. return $segments;
  298. }
  299. }