blob: 5f795514f8e85a7c26ff67e46703635aaced0f24 [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 */
Greg Akerbfbcf742011-04-07 18:33:29 -050036 define('CI_VERSION', '2.0.2');
Derek Allard2067d1a2008-11-13 22:59:24 +000037
Phil Sturgeond8ef6302011-08-14 12:10:55 -060038/**
39 * CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
40 *
41 * @var boolean
42 *
Phil Sturgeonbabfb292011-03-08 21:56:08 +000043 */
Phil Sturgeon2f8b27e2011-03-08 21:56:08 +000044 define('CI_CORE', FALSE);
Phil Sturgeonbabfb292011-03-08 21:56:08 +000045
46/*
47 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050048 * Load the global functions
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * ------------------------------------------------------
50 */
Greg Aker3a746652011-04-19 10:59:47 -050051 require(BASEPATH.'core/Common.php');
Derek Allard2067d1a2008-11-13 22:59:24 +000052
53/*
54 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050055 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000056 * ------------------------------------------------------
57 */
Greg Aker3a746652011-04-19 10:59:47 -050058 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
Phil Sturgeon35f64912011-03-15 21:01:39 +000059 {
Greg Aker3a746652011-04-19 10:59:47 -050060 require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000061 }
62 else
63 {
Greg Aker3a746652011-04-19 10:59:47 -050064 require(APPPATH.'config/constants.php');
Phil Sturgeon35f64912011-03-15 21:01:39 +000065 }
Derek Allard2067d1a2008-11-13 22:59:24 +000066
67/*
68 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050069 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +000070 * ------------------------------------------------------
71 */
Derek Jones218876c2010-03-02 13:11:00 -060072 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Jones218876c2010-03-02 13:11:00 -060074 if ( ! is_php('5.3'))
75 {
Barry Mienydd671972010-10-04 16:33:58 +020076 @set_magic_quotes_runtime(0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060077 }
Derek Jones962d2252009-08-05 04:26:11 +000078
Derek Jones218876c2010-03-02 13:11:00 -060079/*
80 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050081 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060082 * ------------------------------------------------------
83 *
Barry Mienydd671972010-10-04 16:33:58 +020084 * Normally the "subclass_prefix" is set in the config file.
85 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060086 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020087 * "libraries" folder. Since CI allows config items to be
88 * overriden via data set in the main index. php file,
89 * before proceeding we need to know if a subclass_prefix
Derek Jones37f4b9c2011-07-01 17:56:50 -050090 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -060091 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +020092 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -060093 * hurt to load it here.
94 */
Barry Mienydd671972010-10-04 16:33:58 +020095 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
Derek Jones218876c2010-03-02 13:11:00 -060096 {
97 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
98 }
Eric Barnesc5bf6162011-01-30 21:17:11 -050099
Pascal Krietef566af52010-11-09 13:03:26 -0500100/*
101 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500102 * Set a liberal script execution time limit
Pascal Krietef566af52010-11-09 13:03:26 -0500103 * ------------------------------------------------------
104 */
105 if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
106 {
107 @set_time_limit(300);
108 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
110/*
111 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500112 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * ------------------------------------------------------
114 */
Derek Jones218876c2010-03-02 13:11:00 -0600115 $BM =& load_class('Benchmark', 'core');
116 $BM->mark('total_execution_time_start');
117 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000118
119/*
120 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500121 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 * ------------------------------------------------------
123 */
Derek Jones218876c2010-03-02 13:11:00 -0600124 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000125
126/*
127 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500128 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * ------------------------------------------------------
130 */
Derek Jones218876c2010-03-02 13:11:00 -0600131 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000132
133/*
134 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500135 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200137 */
Derek Jones218876c2010-03-02 13:11:00 -0600138 $CFG =& load_class('Config', 'core');
139
140 // Do we have any manually set config items in the index.php file?
141 if (isset($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200142 {
Derek Jones218876c2010-03-02 13:11:00 -0600143 $CFG->_assign_to_config($assign_to_config);
144 }
145
146/*
147 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500148 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600149 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200150 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500151 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600152 * class needs to be used very early on, but it cannot
Barry Mienydd671972010-10-04 16:33:58 +0200153 * properly determine if UTf-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600154 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 */
157
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500158 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Jones218876c2010-03-02 13:11:00 -0600160/*
161 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500162 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600163 * ------------------------------------------------------
164 */
165 $URI =& load_class('URI', 'core');
166
167/*
168 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500169 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600170 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200171 */
Derek Jones218876c2010-03-02 13:11:00 -0600172 $RTR =& load_class('Router', 'core');
173 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Jones218876c2010-03-02 13:11:00 -0600175 // Set any routing overrides that may exist in the main index file
176 if (isset($routing))
177 {
178 $RTR->_set_overrides($routing);
179 }
180
181/*
182 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500183 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600184 * ------------------------------------------------------
185 */
186 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000187
188/*
189 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500190 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * ------------------------------------------------------
192 */
Derek Jones218876c2010-03-02 13:11:00 -0600193 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Derek Jones218876c2010-03-02 13:11:00 -0600195 if ($OUT->_display_cache($CFG, $URI) == TRUE)
196 {
197 exit;
198 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
201/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400202 * -----------------------------------------------------
203 * Load the security class for xss and csrf support
204 * -----------------------------------------------------
205 */
206 $SEC =& load_class('Security', 'core');
207
208/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500210 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 * ------------------------------------------------------
212 */
Barry Mienydd671972010-10-04 16:33:58 +0200213 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
Derek Jones218876c2010-03-02 13:11:00 -0600215/*
216 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500217 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600218 * ------------------------------------------------------
219 */
220 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000221
222/*
223 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500224 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 * ------------------------------------------------------
226 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 */
Derek Jones218876c2010-03-02 13:11:00 -0600228 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500229 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200230
Greg Aker4abfa682010-11-10 14:44:26 -0600231 function &get_instance()
232 {
233 return CI_Controller::get_instance();
234 }
235
236
Greg Aker3a746652011-04-19 10:59:47 -0500237 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500238 {
Greg Aker3a746652011-04-19 10:59:47 -0500239 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Jones218876c2010-03-02 13:11:00 -0600242 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200243 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600244 // 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 -0500245 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600246 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500247 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 -0600248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Greg Aker3a746652011-04-19 10:59:47 -0500250 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Jones218876c2010-03-02 13:11:00 -0600252 // Set a mark point for benchmarking
253 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000254
255/*
256 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500257 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * ------------------------------------------------------
259 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500260 * None of the functions in the app controller or the
261 * loader class can be called via the URI, nor can
262 * controller functions that begin with an underscore
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 */
Derek Jones37f4b9c2011-07-01 17:56:50 -0500264 $class = $RTR->fetch_class();
Derek Jones218876c2010-03-02 13:11:00 -0600265 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Jones218876c2010-03-02 13:11:00 -0600267 if ( ! class_exists($class)
Derek Jones218876c2010-03-02 13:11:00 -0600268 OR strncmp($method, '_', 1) == 0
Greg Aker63277b82010-11-09 13:46:13 -0600269 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
Derek Jones218876c2010-03-02 13:11:00 -0600270 )
271 {
Shane Pearson664a9352011-08-10 16:02:32 -0500272 if ( ! empty($RTR->routes['404_override']))
273 {
274 $x = explode('/', $RTR->routes['404_override']);
275 $class = $x[0];
276 $method = (isset($x[1]) ? $x[1] : 'index');
277 if ( ! class_exists($class))
278 {
279 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
280 {
281 show_404("{$class}/{$method}");
282 }
283
284 include_once(APPPATH.'controllers/'.$class.'.php');
285 }
286 }
287 else
288 {
289 show_404("{$class}/{$method}");
290 }
Derek Jones218876c2010-03-02 13:11:00 -0600291 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000292
293/*
294 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500295 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * ------------------------------------------------------
297 */
Derek Jones218876c2010-03-02 13:11:00 -0600298 $EXT->_call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000299
300/*
301 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500302 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * ------------------------------------------------------
304 */
Derek Jones218876c2010-03-02 13:11:00 -0600305 // Mark a start point so we can benchmark the controller
306 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Jones218876c2010-03-02 13:11:00 -0600308 $CI = new $class();
309
310/*
311 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500312 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600313 * ------------------------------------------------------
314 */
315 $EXT->_call_hook('post_controller_constructor');
316
317/*
318 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500319 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600320 * ------------------------------------------------------
321 */
322 // Is there a "remap" function? If so, we call it instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 if (method_exists($CI, '_remap'))
324 {
Pascal Kriete3431ae32010-11-09 15:19:50 -0500325 $CI->_remap($method, array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327 else
328 {
329 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
330 // methods, so we'll use this workaround for consistent behavior
331 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
332 {
Eric Barnesc5bf6162011-01-30 21:17:11 -0500333 // Check and see if we are using a 404 override and use it.
334 if ( ! empty($RTR->routes['404_override']))
335 {
336 $x = explode('/', $RTR->routes['404_override']);
337 $class = $x[0];
338 $method = (isset($x[1]) ? $x[1] : 'index');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500339 if ( ! class_exists($class))
340 {
Greg Aker3a746652011-04-19 10:59:47 -0500341 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
Eric Barnes5519e3d2011-02-01 13:07:37 -0500342 {
343 show_404("{$class}/{$method}");
344 }
345
Greg Aker3a746652011-04-19 10:59:47 -0500346 include_once(APPPATH.'controllers/'.$class.'.php');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500347 unset($CI);
348 $CI = new $class();
349 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500350 }
351 else
352 {
353 show_404("{$class}/{$method}");
354 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
357 // Call the requested method.
Barry Mienydd671972010-10-04 16:33:58 +0200358 // Any URI segments present (besides the class/function) will be passed to the method for convenience
359 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000361
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Jones218876c2010-03-02 13:11:00 -0600363 // Mark a benchmark end point
364 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000365
366/*
367 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500368 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * ------------------------------------------------------
370 */
Derek Jones218876c2010-03-02 13:11:00 -0600371 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000372
373/*
374 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500375 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * ------------------------------------------------------
377 */
Derek Jones218876c2010-03-02 13:11:00 -0600378 if ($EXT->_call_hook('display_override') === FALSE)
379 {
380 $OUT->_display();
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383/*
384 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500385 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * ------------------------------------------------------
387 */
Derek Jones218876c2010-03-02 13:11:00 -0600388 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000389
390/*
391 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500392 * Close the DB connection if one exists
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * ------------------------------------------------------
394 */
Derek Jones218876c2010-03-02 13:11:00 -0600395 if (class_exists('CI_DB') AND isset($CI->db))
396 {
397 $CI->db->close();
398 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000399
400
401/* End of file CodeIgniter.php */
Phil Sturgeon35f64912011-03-15 21:01:39 +0000402/* Location: ./system/core/CodeIgniter.php */