PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/system/core/CodeIgniter.php

https://github.com/danmontgomery/CodeIgniter
PHP | 414 lines | 141 code | 50 blank | 223 comment | 27 complexity | cdad0e062b4c0a3850e92ad6204da5a2 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 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. * System Initialization File
  18. *
  19. * Loads the base classes and executes the request.
  20. *
  21. * @package CodeIgniter
  22. * @subpackage codeigniter
  23. * @category Front-controller
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/
  26. */
  27. /**
  28. * CodeIgniter Version
  29. *
  30. * @var string
  31. *
  32. */
  33. /**
  34. * CodeIgniter Version
  35. *
  36. * @var string
  37. *
  38. */
  39. define('CI_VERSION', '2.1.0-dev');
  40. /**
  41. * CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
  42. *
  43. * @var boolean
  44. *
  45. */
  46. /**
  47. * CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
  48. *
  49. * @var string
  50. *
  51. */
  52. define('CI_CORE', FALSE);
  53. /*
  54. * ------------------------------------------------------
  55. * Load the global functions
  56. * ------------------------------------------------------
  57. */
  58. require(BASEPATH.'core/Common.php');
  59. /*
  60. * ------------------------------------------------------
  61. * Load the framework constants
  62. * ------------------------------------------------------
  63. */
  64. if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
  65. {
  66. require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
  67. }
  68. else
  69. {
  70. require(APPPATH.'config/constants.php');
  71. }
  72. /*
  73. * ------------------------------------------------------
  74. * Define a custom error handler so we can log PHP errors
  75. * ------------------------------------------------------
  76. */
  77. set_error_handler('_exception_handler');
  78. if ( ! is_php('5.3'))
  79. {
  80. @set_magic_quotes_runtime(0); // Kill magic quotes
  81. }
  82. /*
  83. * ------------------------------------------------------
  84. * Set the subclass_prefix
  85. * ------------------------------------------------------
  86. *
  87. * Normally the "subclass_prefix" is set in the config file.
  88. * The subclass prefix allows CI to know if a core class is
  89. * being extended via a library in the local application
  90. * "libraries" folder. Since CI allows config items to be
  91. * overriden via data set in the main index. php file,
  92. * before proceeding we need to know if a subclass_prefix
  93. * override exists. If so, we will set this value now,
  94. * before any classes are loaded
  95. * Note: Since the config file data is cached it doesn't
  96. * hurt to load it here.
  97. */
  98. if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
  99. {
  100. get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
  101. }
  102. /*
  103. * ------------------------------------------------------
  104. * Set a liberal script execution time limit
  105. * ------------------------------------------------------
  106. */
  107. if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
  108. {
  109. @set_time_limit(300);
  110. }
  111. /*
  112. * ------------------------------------------------------
  113. * Start the timer... tick tock tick tock...
  114. * ------------------------------------------------------
  115. */
  116. $BM =& load_class('Benchmark', 'core');
  117. $BM->mark('total_execution_time_start');
  118. $BM->mark('loading_time:_base_classes_start');
  119. /*
  120. * ------------------------------------------------------
  121. * Instantiate the hooks class
  122. * ------------------------------------------------------
  123. */
  124. $EXT =& load_class('Hooks', 'core');
  125. /*
  126. * ------------------------------------------------------
  127. * Is there a "pre_system" hook?
  128. * ------------------------------------------------------
  129. */
  130. $EXT->_call_hook('pre_system');
  131. /*
  132. * ------------------------------------------------------
  133. * Instantiate the config class
  134. * ------------------------------------------------------
  135. */
  136. $CFG =& load_class('Config', 'core');
  137. // Do we have any manually set config items in the index.php file?
  138. if (isset($assign_to_config))
  139. {
  140. $CFG->_assign_to_config($assign_to_config);
  141. }
  142. /*
  143. * ------------------------------------------------------
  144. * Instantiate the UTF-8 class
  145. * ------------------------------------------------------
  146. *
  147. * Note: Order here is rather important as the UTF-8
  148. * class needs to be used very early on, but it cannot
  149. * properly determine if UTf-8 can be supported until
  150. * after the Config class is instantiated.
  151. *
  152. */
  153. $UNI =& load_class('Utf8', 'core');
  154. /*
  155. * ------------------------------------------------------
  156. * Instantiate the URI class
  157. * ------------------------------------------------------
  158. */
  159. $URI =& load_class('URI', 'core');
  160. /*
  161. * ------------------------------------------------------
  162. * Instantiate the routing class and set the routing
  163. * ------------------------------------------------------
  164. */
  165. $RTR =& load_class('Router', 'core');
  166. $RTR->_set_routing();
  167. // Set any routing overrides that may exist in the main index file
  168. if (isset($routing))
  169. {
  170. $RTR->_set_overrides($routing);
  171. }
  172. /*
  173. * ------------------------------------------------------
  174. * Instantiate the output class
  175. * ------------------------------------------------------
  176. */
  177. $OUT =& load_class('Output', 'core');
  178. /*
  179. * ------------------------------------------------------
  180. * Is there a valid cache file? If so, we're done...
  181. * ------------------------------------------------------
  182. */
  183. if ($EXT->_call_hook('cache_override') === FALSE)
  184. {
  185. if ($OUT->_display_cache($CFG, $URI) == TRUE)
  186. {
  187. exit;
  188. }
  189. }
  190. /*
  191. * -----------------------------------------------------
  192. * Load the security class for xss and csrf support
  193. * -----------------------------------------------------
  194. */
  195. $SEC =& load_class('Security', 'core');
  196. /*
  197. * ------------------------------------------------------
  198. * Load the Input class and sanitize globals
  199. * ------------------------------------------------------
  200. */
  201. $IN =& load_class('Input', 'core');
  202. /*
  203. * ------------------------------------------------------
  204. * Load the Language class
  205. * ------------------------------------------------------
  206. */
  207. $LANG =& load_class('Lang', 'core');
  208. /*
  209. * ------------------------------------------------------
  210. * Load the app controller and local controller
  211. * ------------------------------------------------------
  212. *
  213. */
  214. // Load the base controller class
  215. require BASEPATH.'core/Controller.php';
  216. function &get_instance()
  217. {
  218. return CI_Controller::get_instance();
  219. }
  220. if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
  221. {
  222. require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
  223. }
  224. // Load the local application controller
  225. // Note: The Router class automatically validates the controller path using the router->_validate_request().
  226. // If this include fails it means that the default controller in the Routes.php file is not resolving to something valid.
  227. if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
  228. {
  229. show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
  230. }
  231. include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
  232. // Set a mark point for benchmarking
  233. $BM->mark('loading_time:_base_classes_end');
  234. /*
  235. * ------------------------------------------------------
  236. * Security check
  237. * ------------------------------------------------------
  238. *
  239. * None of the functions in the app controller or the
  240. * loader class can be called via the URI, nor can
  241. * controller functions that begin with an underscore
  242. */
  243. $class = $RTR->fetch_class();
  244. $method = $RTR->fetch_method();
  245. if ( ! class_exists($class)
  246. OR strncmp($method, '_', 1) == 0
  247. OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
  248. )
  249. {
  250. if ( ! empty($RTR->routes['404_override']))
  251. {
  252. $x = explode('/', $RTR->routes['404_override']);
  253. $class = $x[0];
  254. $method = (isset($x[1]) ? $x[1] : 'index');
  255. if ( ! class_exists($class))
  256. {
  257. if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
  258. {
  259. show_404("{$class}/{$method}");
  260. }
  261. include_once(APPPATH.'controllers/'.$class.'.php');
  262. }
  263. }
  264. else
  265. {
  266. show_404("{$class}/{$method}");
  267. }
  268. }
  269. /*
  270. * ------------------------------------------------------
  271. * Is there a "pre_controller" hook?
  272. * ------------------------------------------------------
  273. */
  274. $EXT->_call_hook('pre_controller');
  275. /*
  276. * ------------------------------------------------------
  277. * Instantiate the requested controller
  278. * ------------------------------------------------------
  279. */
  280. // Mark a start point so we can benchmark the controller
  281. $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
  282. $CI = new $class();
  283. /*
  284. * ------------------------------------------------------
  285. * Is there a "post_controller_constructor" hook?
  286. * ------------------------------------------------------
  287. */
  288. $EXT->_call_hook('post_controller_constructor');
  289. /*
  290. * ------------------------------------------------------
  291. * Call the requested method
  292. * ------------------------------------------------------
  293. */
  294. // Is there a "remap" function? If so, we call it instead
  295. if (method_exists($CI, '_remap'))
  296. {
  297. $CI->_remap($method, array_slice($URI->rsegments, 2));
  298. }
  299. else
  300. {
  301. // is_callable() returns TRUE on some versions of PHP 5 for private and protected
  302. // methods, so we'll use this workaround for consistent behavior
  303. if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
  304. {
  305. // Check and see if we are using a 404 override and use it.
  306. if ( ! empty($RTR->routes['404_override']))
  307. {
  308. $x = explode('/', $RTR->routes['404_override']);
  309. $class = $x[0];
  310. $method = (isset($x[1]) ? $x[1] : 'index');
  311. if ( ! class_exists($class))
  312. {
  313. if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
  314. {
  315. show_404("{$class}/{$method}");
  316. }
  317. include_once(APPPATH.'controllers/'.$class.'.php');
  318. unset($CI);
  319. $CI = new $class();
  320. }
  321. }
  322. else
  323. {
  324. show_404("{$class}/{$method}");
  325. }
  326. }
  327. // Call the requested method.
  328. // Any URI segments present (besides the class/function) will be passed to the method for convenience
  329. call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
  330. }
  331. // Mark a benchmark end point
  332. $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
  333. /*
  334. * ------------------------------------------------------
  335. * Is there a "post_controller" hook?
  336. * ------------------------------------------------------
  337. */
  338. $EXT->_call_hook('post_controller');
  339. /*
  340. * ------------------------------------------------------
  341. * Send the final rendered output to the browser
  342. * ------------------------------------------------------
  343. */
  344. if ($EXT->_call_hook('display_override') === FALSE)
  345. {
  346. $OUT->_display();
  347. }
  348. /*
  349. * ------------------------------------------------------
  350. * Is there a "post_system" hook?
  351. * ------------------------------------------------------
  352. */
  353. $EXT->_call_hook('post_system');
  354. /*
  355. * ------------------------------------------------------
  356. * Close the DB connection if one exists
  357. * ------------------------------------------------------
  358. */
  359. if (class_exists('CI_DB') AND isset($CI->db))
  360. {
  361. $CI->db->close();
  362. }
  363. /* End of file CodeIgniter.php */
  364. /* Location: ./system/core/CodeIgniter.php */