blob: 9f88384b1976fcf601db20ce3ef68b3a2d8e0d45 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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/**
Derek Jones218876c2010-03-02 13:11:00 -060019 * System Initialization File
Derek Allard2067d1a2008-11-13 22:59:24 +000020 *
21 * Loads the base classes and executes the request.
22 *
23 * @package CodeIgniter
24 * @subpackage codeigniter
25 * @category Front-controller
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/
28 */
29
Phil Sturgeond8ef6302011-08-14 12:10:55 -060030/**
31 * CodeIgniter Version
32 *
33 * @var string
34 *
Derek Jones218876c2010-03-02 13:11:00 -060035 */
Eric Barnesb183ece2011-08-26 14:42:52 -040036 define('CI_VERSION', '2.1.0-dev');
Derek Allard2067d1a2008-11-13 22:59:24 +000037
Phil Sturgeonbabfb292011-03-08 21:56:08 +000038/*
39 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050040 * Load the global functions
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * ------------------------------------------------------
42 */
Greg Aker3a746652011-04-19 10:59:47 -050043 require(BASEPATH.'core/Common.php');
Derek Allard2067d1a2008-11-13 22:59:24 +000044
45/*
46 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050047 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000048 * ------------------------------------------------------
49 */
Greg Aker3a746652011-04-19 10:59:47 -050050 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
Phil Sturgeon35f64912011-03-15 21:01:39 +000051 {
Greg Aker3a746652011-04-19 10:59:47 -050052 require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000053 }
54 else
55 {
Greg Aker3a746652011-04-19 10:59:47 -050056 require(APPPATH.'config/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000057 }
Derek Allard2067d1a2008-11-13 22:59:24 +000058
59/*
60 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050061 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +000062 * ------------------------------------------------------
63 */
Derek Jones218876c2010-03-02 13:11:00 -060064 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Jones218876c2010-03-02 13:11:00 -060066 if ( ! is_php('5.3'))
67 {
Barry Mienydd671972010-10-04 16:33:58 +020068 @set_magic_quotes_runtime(0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060069 }
Derek Jones962d2252009-08-05 04:26:11 +000070
Derek Jones218876c2010-03-02 13:11:00 -060071/*
72 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050073 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060074 * ------------------------------------------------------
75 *
Barry Mienydd671972010-10-04 16:33:58 +020076 * Normally the "subclass_prefix" is set in the config file.
77 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060078 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020079 * "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
Derek Jones37f4b9c2011-07-01 17:56:50 -050082 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -060083 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +020084 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -060085 * hurt to load it here.
86 */
Barry Mienydd671972010-10-04 16:33:58 +020087 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
Derek Jones218876c2010-03-02 13:11:00 -060088 {
89 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
90 }
Eric Barnesc5bf6162011-01-30 21:17:11 -050091
Pascal Krietef566af52010-11-09 13:03:26 -050092/*
93 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050094 * Set a liberal script execution time limit
Pascal Krietef566af52010-11-09 13:03:26 -050095 * ------------------------------------------------------
96 */
97 if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
98 {
99 @set_time_limit(300);
100 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000101
102/*
103 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500104 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * ------------------------------------------------------
106 */
Derek Jones218876c2010-03-02 13:11:00 -0600107 $BM =& load_class('Benchmark', 'core');
108 $BM->mark('total_execution_time_start');
109 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000110
111/*
112 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500113 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * ------------------------------------------------------
115 */
Derek Jones218876c2010-03-02 13:11:00 -0600116 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000117
118/*
119 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500120 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 * ------------------------------------------------------
122 */
Derek Jones218876c2010-03-02 13:11:00 -0600123 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000124
125/*
126 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500127 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200129 */
Derek Jones218876c2010-03-02 13:11:00 -0600130 $CFG =& load_class('Config', 'core');
131
132 // Do we have any manually set config items in the index.php file?
133 if (isset($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200134 {
Derek Jones218876c2010-03-02 13:11:00 -0600135 $CFG->_assign_to_config($assign_to_config);
136 }
137
138/*
139 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500140 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600141 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200142 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500143 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600144 * class needs to be used very early on, but it cannot
Barry Mienydd671972010-10-04 16:33:58 +0200145 * properly determine if UTf-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600146 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 */
149
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500150 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones218876c2010-03-02 13:11:00 -0600152/*
153 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500154 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600155 * ------------------------------------------------------
156 */
157 $URI =& load_class('URI', 'core');
158
159/*
160 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500161 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600162 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163 */
Derek Jones218876c2010-03-02 13:11:00 -0600164 $RTR =& load_class('Router', 'core');
165 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Jones218876c2010-03-02 13:11:00 -0600167 // 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 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500175 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600176 * ------------------------------------------------------
177 */
178 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000179
180/*
181 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500182 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * ------------------------------------------------------
184 */
Derek Jones218876c2010-03-02 13:11:00 -0600185 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Derek Jones218876c2010-03-02 13:11:00 -0600187 if ($OUT->_display_cache($CFG, $URI) == TRUE)
188 {
189 exit;
190 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000192
193/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400194 * -----------------------------------------------------
195 * Load the security class for xss and csrf support
196 * -----------------------------------------------------
197 */
198 $SEC =& load_class('Security', 'core');
199
200/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500202 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * ------------------------------------------------------
204 */
Barry Mienydd671972010-10-04 16:33:58 +0200205 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
Derek Jones218876c2010-03-02 13:11:00 -0600207/*
208 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500209 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600210 * ------------------------------------------------------
211 */
212 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000213
214/*
215 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500216 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * ------------------------------------------------------
218 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 */
Derek Jones218876c2010-03-02 13:11:00 -0600220 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500221 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200222
Greg Aker4abfa682010-11-10 14:44:26 -0600223 function &get_instance()
224 {
225 return CI_Controller::get_instance();
226 }
227
228
Greg Aker3a746652011-04-19 10:59:47 -0500229 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500230 {
Greg Aker3a746652011-04-19 10:59:47 -0500231 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Jones218876c2010-03-02 13:11:00 -0600234 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200235 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600236 // If this include fails it means that the default controller in the Routes.php file is not resolving to something valid.
Greg Aker3a746652011-04-19 10:59:47 -0500237 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600238 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500239 show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
Derek Jones218876c2010-03-02 13:11:00 -0600240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Greg Aker3a746652011-04-19 10:59:47 -0500242 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Jones218876c2010-03-02 13:11:00 -0600244 // Set a mark point for benchmarking
245 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000246
247/*
248 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500249 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * ------------------------------------------------------
251 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500252 * None of the functions in the app controller or the
253 * loader class can be called via the URI, nor can
254 * controller functions that begin with an underscore
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 */
Derek Jones37f4b9c2011-07-01 17:56:50 -0500256 $class = $RTR->fetch_class();
Derek Jones218876c2010-03-02 13:11:00 -0600257 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Jones218876c2010-03-02 13:11:00 -0600259 if ( ! class_exists($class)
Derek Jones218876c2010-03-02 13:11:00 -0600260 OR strncmp($method, '_', 1) == 0
Greg Aker63277b82010-11-09 13:46:13 -0600261 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
Derek Jones218876c2010-03-02 13:11:00 -0600262 )
263 {
Shane Pearson664a9352011-08-10 16:02:32 -0500264 if ( ! empty($RTR->routes['404_override']))
265 {
266 $x = explode('/', $RTR->routes['404_override']);
267 $class = $x[0];
268 $method = (isset($x[1]) ? $x[1] : 'index');
269 if ( ! class_exists($class))
270 {
271 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
272 {
273 show_404("{$class}/{$method}");
274 }
275
276 include_once(APPPATH.'controllers/'.$class.'.php');
277 }
278 }
279 else
280 {
281 show_404("{$class}/{$method}");
282 }
Derek Jones218876c2010-03-02 13:11:00 -0600283 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
285/*
286 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500287 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * ------------------------------------------------------
289 */
Derek Jones218876c2010-03-02 13:11:00 -0600290 $EXT->_call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000291
292/*
293 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500294 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * ------------------------------------------------------
296 */
Derek Jones218876c2010-03-02 13:11:00 -0600297 // Mark a start point so we can benchmark the controller
298 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Jones218876c2010-03-02 13:11:00 -0600300 $CI = new $class();
301
302/*
303 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500304 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600305 * ------------------------------------------------------
306 */
307 $EXT->_call_hook('post_controller_constructor');
308
309/*
310 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500311 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600312 * ------------------------------------------------------
313 */
314 // Is there a "remap" function? If so, we call it instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 if (method_exists($CI, '_remap'))
316 {
Pascal Kriete3431ae32010-11-09 15:19:50 -0500317 $CI->_remap($method, array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
319 else
320 {
321 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
322 // methods, so we'll use this workaround for consistent behavior
323 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
324 {
Eric Barnesc5bf6162011-01-30 21:17:11 -0500325 // Check and see if we are using a 404 override and use it.
326 if ( ! empty($RTR->routes['404_override']))
327 {
328 $x = explode('/', $RTR->routes['404_override']);
329 $class = $x[0];
330 $method = (isset($x[1]) ? $x[1] : 'index');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500331 if ( ! class_exists($class))
332 {
Greg Aker3a746652011-04-19 10:59:47 -0500333 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
Eric Barnes5519e3d2011-02-01 13:07:37 -0500334 {
335 show_404("{$class}/{$method}");
336 }
337
Greg Aker3a746652011-04-19 10:59:47 -0500338 include_once(APPPATH.'controllers/'.$class.'.php');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500339 unset($CI);
340 $CI = new $class();
341 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500342 }
343 else
344 {
345 show_404("{$class}/{$method}");
346 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348
349 // Call the requested method.
Barry Mienydd671972010-10-04 16:33:58 +0200350 // Any URI segments present (besides the class/function) will be passed to the method for convenience
351 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000353
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Jones218876c2010-03-02 13:11:00 -0600355 // Mark a benchmark end point
356 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000357
358/*
359 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500360 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * ------------------------------------------------------
362 */
Derek Jones218876c2010-03-02 13:11:00 -0600363 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
365/*
366 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500367 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * ------------------------------------------------------
369 */
Derek Jones218876c2010-03-02 13:11:00 -0600370 if ($EXT->_call_hook('display_override') === FALSE)
371 {
372 $OUT->_display();
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375/*
376 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500377 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * ------------------------------------------------------
379 */
Derek Jones218876c2010-03-02 13:11:00 -0600380 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000381
382/*
383 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500384 * Close the DB connection if one exists
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 * ------------------------------------------------------
386 */
Derek Jones218876c2010-03-02 13:11:00 -0600387 if (class_exists('CI_DB') AND isset($CI->db))
388 {
389 $CI->db->close();
390 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000391
392
393/* End of file CodeIgniter.php */
Phil Sturgeon35f64912011-03-15 21:01:39 +0000394/* Location: ./system/core/CodeIgniter.php */