blob: 7b92ddf11189d5a2d07986c5222a9e966f307e86 [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
Derek Jones218876c2010-03-02 13:11:00 -060030/*
31 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050032 * Define the CodeIgniter Version
Derek Jones218876c2010-03-02 13:11:00 -060033 * ------------------------------------------------------
34 */
David Behler050abb82011-08-14 20:30:50 +020035 /**
36 * CodeIgniter Version
37 *
38 * @var string
39 *
40 */
Greg Akerbfbcf742011-04-07 18:33:29 -050041 define('CI_VERSION', '2.0.2');
Derek Allard2067d1a2008-11-13 22:59:24 +000042
43/*
44 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050045 * Define the CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
Phil Sturgeonbabfb292011-03-08 21:56:08 +000046 * ------------------------------------------------------
47 */
David Behler050abb82011-08-14 20:30:50 +020048 /**
49 * CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
50 *
51 * @var string
52 *
53 */
Phil Sturgeon2f8b27e2011-03-08 21:56:08 +000054 define('CI_CORE', FALSE);
Phil Sturgeonbabfb292011-03-08 21:56:08 +000055
56/*
57 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050058 * Load the global functions
Derek Allard2067d1a2008-11-13 22:59:24 +000059 * ------------------------------------------------------
60 */
Greg Aker3a746652011-04-19 10:59:47 -050061 require(BASEPATH.'core/Common.php');
Derek Allard2067d1a2008-11-13 22:59:24 +000062
63/*
64 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050065 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * ------------------------------------------------------
67 */
Greg Aker3a746652011-04-19 10:59:47 -050068 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
Phil Sturgeon35f64912011-03-15 21:01:39 +000069 {
Greg Aker3a746652011-04-19 10:59:47 -050070 require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000071 }
72 else
73 {
Greg Aker3a746652011-04-19 10:59:47 -050074 require(APPPATH.'config/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000075 }
Derek Allard2067d1a2008-11-13 22:59:24 +000076
77/*
78 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050079 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * ------------------------------------------------------
81 */
Derek Jones218876c2010-03-02 13:11:00 -060082 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Jones218876c2010-03-02 13:11:00 -060084 if ( ! is_php('5.3'))
85 {
Barry Mienydd671972010-10-04 16:33:58 +020086 @set_magic_quotes_runtime(0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060087 }
Derek Jones962d2252009-08-05 04:26:11 +000088
Derek Jones218876c2010-03-02 13:11:00 -060089/*
90 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050091 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060092 * ------------------------------------------------------
93 *
Barry Mienydd671972010-10-04 16:33:58 +020094 * Normally the "subclass_prefix" is set in the config file.
95 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060096 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020097 * "libraries" folder. Since CI allows config items to be
98 * overriden via data set in the main index. php file,
99 * before proceeding we need to know if a subclass_prefix
Derek Jones37f4b9c2011-07-01 17:56:50 -0500100 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -0600101 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +0200102 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -0600103 * hurt to load it here.
104 */
Barry Mienydd671972010-10-04 16:33:58 +0200105 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
Derek Jones218876c2010-03-02 13:11:00 -0600106 {
107 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
108 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500109
Pascal Krietef566af52010-11-09 13:03:26 -0500110/*
111 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500112 * Set a liberal script execution time limit
Pascal Krietef566af52010-11-09 13:03:26 -0500113 * ------------------------------------------------------
114 */
115 if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
116 {
117 @set_time_limit(300);
118 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000119
120/*
121 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500122 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * ------------------------------------------------------
124 */
Derek Jones218876c2010-03-02 13:11:00 -0600125 $BM =& load_class('Benchmark', 'core');
126 $BM->mark('total_execution_time_start');
127 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000128
129/*
130 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500131 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * ------------------------------------------------------
133 */
Derek Jones218876c2010-03-02 13:11:00 -0600134 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
136/*
137 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500138 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * ------------------------------------------------------
140 */
Derek Jones218876c2010-03-02 13:11:00 -0600141 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143/*
144 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500145 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147 */
Derek Jones218876c2010-03-02 13:11:00 -0600148 $CFG =& load_class('Config', 'core');
149
150 // Do we have any manually set config items in the index.php file?
151 if (isset($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200152 {
Derek Jones218876c2010-03-02 13:11:00 -0600153 $CFG->_assign_to_config($assign_to_config);
154 }
155
156/*
157 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500158 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600159 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200160 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500161 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600162 * class needs to be used very early on, but it cannot
Barry Mienydd671972010-10-04 16:33:58 +0200163 * properly determine if UTf-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600164 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 */
167
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500168 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Jones218876c2010-03-02 13:11:00 -0600170/*
171 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500172 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600173 * ------------------------------------------------------
174 */
175 $URI =& load_class('URI', 'core');
176
177/*
178 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500179 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600180 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181 */
Derek Jones218876c2010-03-02 13:11:00 -0600182 $RTR =& load_class('Router', 'core');
183 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Jones218876c2010-03-02 13:11:00 -0600185 // Set any routing overrides that may exist in the main index file
186 if (isset($routing))
187 {
188 $RTR->_set_overrides($routing);
189 }
190
191/*
192 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500193 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600194 * ------------------------------------------------------
195 */
196 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000197
198/*
199 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500200 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * ------------------------------------------------------
202 */
Derek Jones218876c2010-03-02 13:11:00 -0600203 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Derek Jones218876c2010-03-02 13:11:00 -0600205 if ($OUT->_display_cache($CFG, $URI) == TRUE)
206 {
207 exit;
208 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
211/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400212 * -----------------------------------------------------
213 * Load the security class for xss and csrf support
214 * -----------------------------------------------------
215 */
216 $SEC =& load_class('Security', 'core');
217
218/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500220 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 * ------------------------------------------------------
222 */
Barry Mienydd671972010-10-04 16:33:58 +0200223 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000224
Derek Jones218876c2010-03-02 13:11:00 -0600225/*
226 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500227 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600228 * ------------------------------------------------------
229 */
230 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000231
232/*
233 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500234 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * ------------------------------------------------------
236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 */
Derek Jones218876c2010-03-02 13:11:00 -0600238 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500239 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200240
Greg Aker4abfa682010-11-10 14:44:26 -0600241 function &get_instance()
242 {
243 return CI_Controller::get_instance();
244 }
245
246
Greg Aker3a746652011-04-19 10:59:47 -0500247 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500248 {
Greg Aker3a746652011-04-19 10:59:47 -0500249 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Jones218876c2010-03-02 13:11:00 -0600252 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200253 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600254 // 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 -0500255 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600256 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500257 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 -0600258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Greg Aker3a746652011-04-19 10:59:47 -0500260 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Jones218876c2010-03-02 13:11:00 -0600262 // Set a mark point for benchmarking
263 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000264
265/*
266 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500267 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * ------------------------------------------------------
269 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500270 * None of the functions in the app controller or the
271 * loader class can be called via the URI, nor can
272 * controller functions that begin with an underscore
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 */
Derek Jones37f4b9c2011-07-01 17:56:50 -0500274 $class = $RTR->fetch_class();
Derek Jones218876c2010-03-02 13:11:00 -0600275 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Jones218876c2010-03-02 13:11:00 -0600277 if ( ! class_exists($class)
Derek Jones218876c2010-03-02 13:11:00 -0600278 OR strncmp($method, '_', 1) == 0
Greg Aker63277b82010-11-09 13:46:13 -0600279 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
Derek Jones218876c2010-03-02 13:11:00 -0600280 )
281 {
Shane Pearson664a9352011-08-10 16:02:32 -0500282 if ( ! empty($RTR->routes['404_override']))
283 {
284 $x = explode('/', $RTR->routes['404_override']);
285 $class = $x[0];
286 $method = (isset($x[1]) ? $x[1] : 'index');
287 if ( ! class_exists($class))
288 {
289 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
290 {
291 show_404("{$class}/{$method}");
292 }
293
294 include_once(APPPATH.'controllers/'.$class.'.php');
295 }
296 }
297 else
298 {
299 show_404("{$class}/{$method}");
300 }
Derek Jones218876c2010-03-02 13:11:00 -0600301 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
303/*
304 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500305 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 * ------------------------------------------------------
307 */
Derek Jones218876c2010-03-02 13:11:00 -0600308 $EXT->_call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000309
310/*
311 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500312 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * ------------------------------------------------------
314 */
Derek Jones218876c2010-03-02 13:11:00 -0600315 // Mark a start point so we can benchmark the controller
316 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Jones218876c2010-03-02 13:11:00 -0600318 $CI = new $class();
319
320/*
321 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500322 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600323 * ------------------------------------------------------
324 */
325 $EXT->_call_hook('post_controller_constructor');
326
327/*
328 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600330 * ------------------------------------------------------
331 */
332 // Is there a "remap" function? If so, we call it instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 if (method_exists($CI, '_remap'))
334 {
Pascal Kriete3431ae32010-11-09 15:19:50 -0500335 $CI->_remap($method, array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337 else
338 {
339 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
340 // methods, so we'll use this workaround for consistent behavior
341 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
342 {
Eric Barnesc5bf6162011-01-30 21:17:11 -0500343 // Check and see if we are using a 404 override and use it.
344 if ( ! empty($RTR->routes['404_override']))
345 {
346 $x = explode('/', $RTR->routes['404_override']);
347 $class = $x[0];
348 $method = (isset($x[1]) ? $x[1] : 'index');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500349 if ( ! class_exists($class))
350 {
Greg Aker3a746652011-04-19 10:59:47 -0500351 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
Eric Barnes5519e3d2011-02-01 13:07:37 -0500352 {
353 show_404("{$class}/{$method}");
354 }
355
Greg Aker3a746652011-04-19 10:59:47 -0500356 include_once(APPPATH.'controllers/'.$class.'.php');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500357 unset($CI);
358 $CI = new $class();
359 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500360 }
361 else
362 {
363 show_404("{$class}/{$method}");
364 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
367 // Call the requested method.
Barry Mienydd671972010-10-04 16:33:58 +0200368 // Any URI segments present (besides the class/function) will be passed to the method for convenience
369 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000371
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Jones218876c2010-03-02 13:11:00 -0600373 // Mark a benchmark end point
374 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000375
376/*
377 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500378 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * ------------------------------------------------------
380 */
Derek Jones218876c2010-03-02 13:11:00 -0600381 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000382
383/*
384 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500385 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * ------------------------------------------------------
387 */
Derek Jones218876c2010-03-02 13:11:00 -0600388 if ($EXT->_call_hook('display_override') === FALSE)
389 {
390 $OUT->_display();
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393/*
394 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500395 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * ------------------------------------------------------
397 */
Derek Jones218876c2010-03-02 13:11:00 -0600398 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000399
400/*
401 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500402 * Close the DB connection if one exists
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * ------------------------------------------------------
404 */
Derek Jones218876c2010-03-02 13:11:00 -0600405 if (class_exists('CI_DB') AND isset($CI->db))
406 {
407 $CI->db->close();
408 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000409
410
411/* End of file CodeIgniter.php */
Phil Sturgeon35f64912011-03-15 21:01:39 +0000412/* Location: ./system/core/CodeIgniter.php */