PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/system/pyrocms/core/MY_Router.php

https://github.com/ikhattab/pyrocms
PHP | 306 lines | 189 code | 53 blank | 64 comment | 38 complexity | a4ddae45a0dd9af45428a85aee8c7db6 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /* PHP5 spl_autoload */
  3. spl_autoload_register('Modules::autoload');
  4. /* define the module locations and offset */
  5. Modules::$locations = array(
  6. APPPATH . 'modules/' => '../modules/',
  7. ADDONPATH . 'modules/' => '../../../addons/modules/'
  8. );
  9. /**
  10. * Modular Separation - PHP5
  11. *
  12. * Adapted from the CodeIgniter Core Classes
  13. * @copyright Copyright (c) 2006, EllisLab, Inc.
  14. * @link http://codeigniter.com
  15. *
  16. * Description:
  17. * This library extends the CodeIgniter router class.
  18. *
  19. * Install this file as application/libraries/MY_Router.php
  20. *
  21. * @copyright Copyright (c) Wiredesignz 2010-03-01
  22. * @version 2.3
  23. *
  24. * Permission is hereby granted, free of charge, to any person obtaining a copy
  25. * of this software and associated documentation files (the "Software"), to deal
  26. * in the Software without restriction, including without limitation the rights
  27. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  28. * copies of the Software, and to permit persons to whom the Software is
  29. * furnished to do so, subject to the following conditions:
  30. *
  31. * The above copyright notice and this permission notice shall be included in
  32. * all copies or substantial portions of the Software.
  33. *
  34. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  40. * THE SOFTWARE.
  41. * */
  42. class MY_Router extends CI_Router {
  43. private $module;
  44. public function fetch_module()
  45. {
  46. return $this->module;
  47. }
  48. public function _validate_request($segments)
  49. {
  50. /* locate module controller */
  51. if ($located = $this->locate($segments))
  52. {
  53. return $located;
  54. }
  55. // route 404 is now deprecated, use 404_override
  56. if(isset($this->routes['404']))
  57. {
  58. $this->routes['404_override'] = $this->routes['404'];
  59. unset($this->routes['404']);
  60. }
  61. /* use a default 404 controller */
  62. if (isset($this->routes['404_override']) AND $segments = explode('/', $this->routes['404_override']))
  63. {
  64. if ($located = $this->locate($segments))
  65. {
  66. return $located;
  67. }
  68. }
  69. /* no controller found */
  70. show_404();
  71. }
  72. /** Locate the controller * */
  73. public function locate($segments)
  74. {
  75. $this->module = '';
  76. $this->directory = '';
  77. $ext = $this->config->item('controller_suffix') . EXT;
  78. /* use module route if available */
  79. if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments)))
  80. {
  81. $segments = $routes;
  82. }
  83. /* get the segments array elements */
  84. list($module, $directory, $controller) = array_pad($segments, 3, NULL);
  85. foreach (Modules::$locations as $location => $offset)
  86. {
  87. /* module exists? */
  88. if (is_dir($source = $location . $module . '/controllers/'))
  89. {
  90. $this->module = $module;
  91. $this->directory = $offset . $module . '/controllers/';
  92. /* module sub-controller exists? */
  93. if ($directory AND is_file($source . $directory . $ext))
  94. {
  95. return array_slice($segments, 1);
  96. }
  97. /* module sub-directory exists? */
  98. if ($directory AND is_dir($module_subdir = $source . $directory . '/'))
  99. {
  100. $this->directory .= $directory . '/';
  101. /* module sub-directory controller exists? */
  102. if (is_file($module_subdir . $directory . $ext))
  103. {
  104. return array_slice($segments, 1);
  105. }
  106. /* module sub-directory sub-controller exists? */
  107. if ($controller AND is_file($module_subdir . $controller . $ext))
  108. {
  109. return array_slice($segments, 2);
  110. }
  111. }
  112. /* module controller exists? */
  113. if (is_file($source . $module . $ext))
  114. {
  115. return $segments;
  116. }
  117. }
  118. }
  119. /* application controller exists? */
  120. if (is_file(APPPATH . 'controllers/' . $module . $ext))
  121. {
  122. return $segments;
  123. }
  124. /* application sub-directory controller exists? */
  125. if (is_file(APPPATH . 'controllers/' . $module . '/' . $directory . $ext))
  126. {
  127. $this->directory = $module . '/';
  128. return array_slice($segments, 1);
  129. }
  130. }
  131. public function set_class($class)
  132. {
  133. $this->class = $class . $this->config->item('controller_suffix');
  134. }
  135. }
  136. class Modules {
  137. public static $routes, $locations;
  138. /** Library base class autoload * */
  139. public static function autoload($class)
  140. {
  141. /* don't autoload CI_ or MY_ prefixed classes */
  142. if (strstr($class, 'CI_') OR strstr($class, 'MY_'))
  143. {
  144. return;
  145. }
  146. if (is_file($location = APPPATH . 'core/' . $class . EXT))
  147. {
  148. include_once $location;
  149. }
  150. else if (is_file($location = APPPATH . 'libraries/' . $class . EXT))
  151. {
  152. include_once $location;
  153. }
  154. }
  155. /** Load a module file * */
  156. public static function load_file($file, $path, $type = 'other', $result = TRUE)
  157. {
  158. $file = str_replace(EXT, '', $file);
  159. $location = $path . $file . EXT;
  160. if ($type === 'other')
  161. {
  162. if (class_exists($file, FALSE))
  163. {
  164. log_message('debug', "File already loaded: {$location}");
  165. return $result;
  166. }
  167. include_once $location;
  168. }
  169. else
  170. {
  171. /* load config or language array */
  172. include $location;
  173. if (!isset($$type) OR !is_array($$type))
  174. show_error("{$location} does not contain a valid {$type} array");
  175. $result = $$type;
  176. }
  177. log_message('debug', "File loaded: {$location}");
  178. return $result;
  179. }
  180. /**
  181. * Find a file
  182. * Scans for files located within modules directories.
  183. * Also scans application directories for models and views.
  184. * Generates fatal error if file not found.
  185. * */
  186. public static function find($file, $module, $base, $lang = '')
  187. {
  188. $segments = explode('/', $file);
  189. $file = array_pop($segments);
  190. if ($base == 'core/' OR $base == 'libraries/')
  191. {
  192. $file = ucfirst($file);
  193. }
  194. else if ($base == 'models/')
  195. {
  196. $file = strtolower($file);
  197. }
  198. $file_ext = strpos($file, '.') ? $file : $file . EXT;
  199. $lang && $lang .= '/';
  200. $path = ltrim(implode('/', $segments) . '/', '/');
  201. $module ? $modules[$module] = $path : $modules = array();
  202. if (!empty($segments))
  203. {
  204. $modules[array_shift($segments)] = ltrim(implode('/', $segments) . '/', '/');
  205. }
  206. foreach (Modules::$locations as $location => $offset)
  207. {
  208. foreach ($modules as $module => $subpath)
  209. {
  210. $fullpath = $location . $module . '/' . $base . $lang . $subpath;
  211. if (is_file($fullpath . $file_ext))
  212. {
  213. return array($fullpath, $file);
  214. }
  215. }
  216. }
  217. /* is the file in an application directory? */
  218. if ($base == 'views/' OR $base == 'models/')
  219. {
  220. if (is_file(APPPATH . $base . $path . $file_ext))
  221. return array(APPPATH . $base . $path, $file);
  222. show_error("Unable to locate the file: {$path}{$file_ext}");
  223. }
  224. return array(FALSE, $file);
  225. }
  226. /** Parse module routes * */
  227. public static function parse_routes($module, $uri)
  228. {
  229. /* load the route file */
  230. if (!isset(self::$routes[$module]))
  231. {
  232. if (list($path) = self::find('routes', $module, 'config/') AND $path)
  233. self::$routes[$module] = self::load_file('routes', $path, 'route');
  234. }
  235. if (!isset(self::$routes[$module]))
  236. {
  237. return;
  238. }
  239. /* parse module routes */
  240. foreach (self::$routes[$module] as $key => $val)
  241. {
  242. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  243. if (preg_match('#^' . $key . '$#', $uri))
  244. {
  245. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
  246. {
  247. $val = preg_replace('#^' . $key . '$#', $val, $uri);
  248. }
  249. return explode('/', $module . '/' . $val);
  250. }
  251. }
  252. }
  253. }