blob: aca4fb23c83d408778f6d7dc27f38e72c2ad48c3 [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 */
David Behler050abb82011-08-14 20:30:50 +020036 /**
37 * CodeIgniter Version
38 *
39 * @var string
40 *
41 */
Eric Barnesb183ece2011-08-26 14:42:52 -040042 define('CI_VERSION', '2.1.0-dev');
Derek Allard2067d1a2008-11-13 22:59:24 +000043
Phil Sturgeond8ef6302011-08-14 12:10:55 -060044/**
45 * CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
46 *
47 * @var boolean
48 *
Phil Sturgeonbabfb292011-03-08 21:56:08 +000049 */
David Behler050abb82011-08-14 20:30:50 +020050 /**
51 * CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
52 *
53 * @var string
54 *
55 */
Phil Sturgeon2f8b27e2011-03-08 21:56:08 +000056 define('CI_CORE', FALSE);
Phil Sturgeonbabfb292011-03-08 21:56:08 +000057
58/*
59 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050060 * Load the global functions
Derek Allard2067d1a2008-11-13 22:59:24 +000061 * ------------------------------------------------------
62 */
Greg Aker3a746652011-04-19 10:59:47 -050063 require(BASEPATH.'core/Common.php');
Derek Allard2067d1a2008-11-13 22:59:24 +000064
65/*
66 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050067 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000068 * ------------------------------------------------------
69 */
Greg Aker3a746652011-04-19 10:59:47 -050070 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
Phil Sturgeon35f64912011-03-15 21:01:39 +000071 {
Greg Aker3a746652011-04-19 10:59:47 -050072 require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000073 }
74 else
75 {
Greg Aker3a746652011-04-19 10:59:47 -050076 require(APPPATH.'config/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000077 }
Derek Allard2067d1a2008-11-13 22:59:24 +000078
79/*
80 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050081 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * ------------------------------------------------------
83 */
Derek Jones218876c2010-03-02 13:11:00 -060084 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Jones218876c2010-03-02 13:11:00 -060086 if ( ! is_php('5.3'))
87 {
Barry Mienydd671972010-10-04 16:33:58 +020088 @set_magic_quotes_runtime(0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060089 }
Derek Jones962d2252009-08-05 04:26:11 +000090
Derek Jones218876c2010-03-02 13:11:00 -060091/*
92 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050093 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060094 * ------------------------------------------------------
95 *
Barry Mienydd671972010-10-04 16:33:58 +020096 * Normally the "subclass_prefix" is set in the config file.
97 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060098 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020099 * "libraries" folder. Since CI allows config items to be
100 * overriden via data set in the main index. php file,
101 * before proceeding we need to know if a subclass_prefix
Derek Jones37f4b9c2011-07-01 17:56:50 -0500102 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -0600103 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +0200104 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -0600105 * hurt to load it here.
106 */
Barry Mienydd671972010-10-04 16:33:58 +0200107 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
Derek Jones218876c2010-03-02 13:11:00 -0600108 {
109 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
110 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500111
Pascal Krietef566af52010-11-09 13:03:26 -0500112/*
113 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500114 * Set a liberal script execution time limit
Pascal Krietef566af52010-11-09 13:03:26 -0500115 * ------------------------------------------------------
116 */
117 if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
118 {
119 @set_time_limit(300);
120 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000121
122/*
123 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500124 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * ------------------------------------------------------
126 */
Derek Jones218876c2010-03-02 13:11:00 -0600127 $BM =& load_class('Benchmark', 'core');
128 $BM->mark('total_execution_time_start');
129 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000130
131/*
132 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500133 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * ------------------------------------------------------
135 */
Derek Jones218876c2010-03-02 13:11:00 -0600136 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000137
138/*
139 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500140 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * ------------------------------------------------------
142 */
Derek Jones218876c2010-03-02 13:11:00 -0600143 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000144
145/*
146 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500147 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Derek Jones218876c2010-03-02 13:11:00 -0600150 $CFG =& load_class('Config', 'core');
151
152 // Do we have any manually set config items in the index.php file?
153 if (isset($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200154 {
Derek Jones218876c2010-03-02 13:11:00 -0600155 $CFG->_assign_to_config($assign_to_config);
156 }
157
158/*
159 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500160 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600161 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200162 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500163 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600164 * class needs to be used very early on, but it cannot
Barry Mienydd671972010-10-04 16:33:58 +0200165 * properly determine if UTf-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600166 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200167 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 */
169
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500170 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Jones218876c2010-03-02 13:11:00 -0600172/*
173 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500174 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600175 * ------------------------------------------------------
176 */
177 $URI =& load_class('URI', 'core');
178
179/*
180 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500181 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600182 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200183 */
Derek Jones218876c2010-03-02 13:11:00 -0600184 $RTR =& load_class('Router', 'core');
185 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Jones218876c2010-03-02 13:11:00 -0600187 // Set any routing overrides that may exist in the main index file
188 if (isset($routing))
189 {
190 $RTR->_set_overrides($routing);
191 }
192
193/*
194 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500195 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600196 * ------------------------------------------------------
197 */
198 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
200/*
201 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500202 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * ------------------------------------------------------
204 */
Derek Jones218876c2010-03-02 13:11:00 -0600205 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Derek Jones218876c2010-03-02 13:11:00 -0600207 if ($OUT->_display_cache($CFG, $URI) == TRUE)
208 {
209 exit;
210 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400214 * -----------------------------------------------------
215 * Load the security class for xss and csrf support
216 * -----------------------------------------------------
217 */
218 $SEC =& load_class('Security', 'core');
219
220/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500222 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 * ------------------------------------------------------
224 */
Barry Mienydd671972010-10-04 16:33:58 +0200225 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000226
Derek Jones218876c2010-03-02 13:11:00 -0600227/*
228 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500229 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600230 * ------------------------------------------------------
231 */
232 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000233
234/*
235 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500236 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * ------------------------------------------------------
238 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 */
Derek Jones218876c2010-03-02 13:11:00 -0600240 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500241 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200242
Greg Aker4abfa682010-11-10 14:44:26 -0600243 function &get_instance()
244 {
245 return CI_Controller::get_instance();
246 }
247
248
Greg Aker3a746652011-04-19 10:59:47 -0500249 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500250 {
Greg Aker3a746652011-04-19 10:59:47 -0500251 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Jones218876c2010-03-02 13:11:00 -0600254 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200255 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600256 // 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 -0500257 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600258 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500259 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 -0600260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Greg Aker3a746652011-04-19 10:59:47 -0500262 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Jones218876c2010-03-02 13:11:00 -0600264 // Set a mark point for benchmarking
265 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000266
267/*
268 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500269 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * ------------------------------------------------------
271 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500272 * None of the functions in the app controller or the
273 * loader class can be called via the URI, nor can
274 * controller functions that begin with an underscore
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 */
Derek Jones37f4b9c2011-07-01 17:56:50 -0500276 $class = $RTR->fetch_class();
Derek Jones218876c2010-03-02 13:11:00 -0600277 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Jones218876c2010-03-02 13:11:00 -0600279 if ( ! class_exists($class)
Derek Jones218876c2010-03-02 13:11:00 -0600280 OR strncmp($method, '_', 1) == 0
Greg Aker63277b82010-11-09 13:46:13 -0600281 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
Derek Jones218876c2010-03-02 13:11:00 -0600282 )
283 {
Shane Pearson664a9352011-08-10 16:02:32 -0500284 if ( ! empty($RTR->routes['404_override']))
285 {
286 $x = explode('/', $RTR->routes['404_override']);
287 $class = $x[0];
288 $method = (isset($x[1]) ? $x[1] : 'index');
289 if ( ! class_exists($class))
290 {
291 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
292 {
293 show_404("{$class}/{$method}");
294 }
295
296 include_once(APPPATH.'controllers/'.$class.'.php');
297 }
298 }
299 else
300 {
301 show_404("{$class}/{$method}");
302 }
Derek Jones218876c2010-03-02 13:11:00 -0600303 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000304
305/*
306 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500307 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * ------------------------------------------------------
309 */
Derek Jones218876c2010-03-02 13:11:00 -0600310 $EXT->_call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
312/*
313 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500314 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * ------------------------------------------------------
316 */
Derek Jones218876c2010-03-02 13:11:00 -0600317 // Mark a start point so we can benchmark the controller
318 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Jones218876c2010-03-02 13:11:00 -0600320 $CI = new $class();
321
322/*
323 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500324 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600325 * ------------------------------------------------------
326 */
327 $EXT->_call_hook('post_controller_constructor');
328
329/*
330 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500331 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600332 * ------------------------------------------------------
333 */
334 // Is there a "remap" function? If so, we call it instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 if (method_exists($CI, '_remap'))
336 {
Pascal Kriete3431ae32010-11-09 15:19:50 -0500337 $CI->_remap($method, array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339 else
340 {
341 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
342 // methods, so we'll use this workaround for consistent behavior
343 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
344 {
Eric Barnesc5bf6162011-01-30 21:17:11 -0500345 // Check and see if we are using a 404 override and use it.
346 if ( ! empty($RTR->routes['404_override']))
347 {
348 $x = explode('/', $RTR->routes['404_override']);
349 $class = $x[0];
350 $method = (isset($x[1]) ? $x[1] : 'index');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500351 if ( ! class_exists($class))
352 {
Greg Aker3a746652011-04-19 10:59:47 -0500353 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
Eric Barnes5519e3d2011-02-01 13:07:37 -0500354 {
355 show_404("{$class}/{$method}");
356 }
357
Greg Aker3a746652011-04-19 10:59:47 -0500358 include_once(APPPATH.'controllers/'.$class.'.php');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500359 unset($CI);
360 $CI = new $class();
361 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500362 }
363 else
364 {
365 show_404("{$class}/{$method}");
366 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368
369 // Call the requested method.
Barry Mienydd671972010-10-04 16:33:58 +0200370 // Any URI segments present (besides the class/function) will be passed to the method for convenience
371 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000373
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Jones218876c2010-03-02 13:11:00 -0600375 // Mark a benchmark end point
376 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000377
378/*
379 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500380 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * ------------------------------------------------------
382 */
Derek Jones218876c2010-03-02 13:11:00 -0600383 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000384
385/*
386 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500387 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * ------------------------------------------------------
389 */
Derek Jones218876c2010-03-02 13:11:00 -0600390 if ($EXT->_call_hook('display_override') === FALSE)
391 {
392 $OUT->_display();
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395/*
396 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500397 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * ------------------------------------------------------
399 */
Derek Jones218876c2010-03-02 13:11:00 -0600400 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000401
402/*
403 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500404 * Close the DB connection if one exists
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * ------------------------------------------------------
406 */
Derek Jones218876c2010-03-02 13:11:00 -0600407 if (class_exists('CI_DB') AND isset($CI->db))
408 {
409 $CI->db->close();
410 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000411
412
413/* End of file CodeIgniter.php */
Phil Sturgeon35f64912011-03-15 21:01:39 +0000414/* Location: ./system/core/CodeIgniter.php */