CodeIgniter.php 11 KB

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