blob: c682664085789d41258d2174a1547dd232370ea8 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev9c5c24a2012-01-07 18:51:21 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev9c5c24a2012-01-07 18:51:21 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Derek Jones218876c2010-03-02 13:11:00 -060030 * System Initialization File
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
32 * Loads the base classes and executes the request.
33 *
34 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030035 * @subpackage CodeIgniter
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Front-controller
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/
39 */
40
Phil Sturgeond8ef6302011-08-14 12:10:55 -060041/**
42 * CodeIgniter Version
43 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040044 * @var string
Phil Sturgeond8ef6302011-08-14 12:10:55 -060045 *
Derek Jones218876c2010-03-02 13:11:00 -060046 */
Derek Jonesf4a4bd82011-10-20 12:18:42 -050047 define('CI_VERSION', '3.0-dev');
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Phil Sturgeonbabfb292011-03-08 21:56:08 +000049/*
50 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050051 * Load the global functions
Derek Allard2067d1a2008-11-13 22:59:24 +000052 * ------------------------------------------------------
53 */
Andrey Andreeva52c7752012-10-11 10:54:02 +030054 require_once(BASEPATH.'core/Common.php');
Derek Allard2067d1a2008-11-13 22:59:24 +000055
56/*
57 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050058 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000059 * ------------------------------------------------------
60 */
Andrey Andreevdb529ca2013-01-28 11:00:02 +020061 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
Greg Akerd96f8822011-12-27 16:23:47 -060062 {
63 require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
64 }
65 else
66 {
67 require(APPPATH.'config/constants.php');
68 }
Derek Allard2067d1a2008-11-13 22:59:24 +000069
70/*
71 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050072 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +000073 * ------------------------------------------------------
74 */
Derek Jones218876c2010-03-02 13:11:00 -060075 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020076
Andrey Andreev24bd2302012-06-05 22:29:12 +030077 if ( ! is_php('5.4'))
Derek Jones218876c2010-03-02 13:11:00 -060078 {
Andrey Andreeve6e6eff2012-06-06 00:24:57 +030079 @ini_set('magic_quotes_runtime', 0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060080 }
Derek Jones962d2252009-08-05 04:26:11 +000081
Derek Jones218876c2010-03-02 13:11:00 -060082/*
83 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050084 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060085 * ------------------------------------------------------
86 *
Barry Mienydd671972010-10-04 16:33:58 +020087 * Normally the "subclass_prefix" is set in the config file.
88 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060089 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020090 * "libraries" folder. Since CI allows config items to be
91 * overriden via data set in the main index. php file,
92 * before proceeding we need to know if a subclass_prefix
Andrey Andreev9c5c24a2012-01-07 18:51:21 +020093 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -060094 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +020095 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -060096 * hurt to load it here.
97 */
Andrey Andreev9ba661b2012-06-04 14:44:34 +030098 if ( ! empty($assign_to_config['subclass_prefix']))
Derek Jones218876c2010-03-02 13:11:00 -060099 {
100 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
101 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500102
Pascal Krietef566af52010-11-09 13:03:26 -0500103/*
104 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500105 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * ------------------------------------------------------
107 */
Derek Jones218876c2010-03-02 13:11:00 -0600108 $BM =& load_class('Benchmark', 'core');
109 $BM->mark('total_execution_time_start');
110 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000111
112/*
113 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500114 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * ------------------------------------------------------
116 */
Derek Jones218876c2010-03-02 13:11:00 -0600117 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000118
119/*
120 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500121 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 * ------------------------------------------------------
123 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300124 $EXT->call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000125
126/*
127 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500128 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200130 */
Derek Jones218876c2010-03-02 13:11:00 -0600131 $CFG =& load_class('Config', 'core');
132
133 // Do we have any manually set config items in the index.php file?
Andrey Andreev5232ba02012-10-27 15:25:05 +0300134 if (isset($assign_to_config) && is_array($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200135 {
Andrey Andreev5232ba02012-10-27 15:25:05 +0300136 foreach ($assign_to_config as $key => $value)
137 {
138 $CFG->set_item($key, $value);
139 }
Derek Jones218876c2010-03-02 13:11:00 -0600140 }
141
142/*
143 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500144 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600145 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500147 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600148 * class needs to be used very early on, but it cannot
Chris Berthe827b3f02012-04-27 23:36:54 -0400149 * properly determine if UTF-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600150 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200151 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 */
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500153 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Jones218876c2010-03-02 13:11:00 -0600155/*
156 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500157 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600158 * ------------------------------------------------------
159 */
160 $URI =& load_class('URI', 'core');
161
162/*
163 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500164 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600165 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200166 */
Derek Jones218876c2010-03-02 13:11:00 -0600167 $RTR =& load_class('Router', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Jones218876c2010-03-02 13:11:00 -0600169 // Set any routing overrides that may exist in the main index file
170 if (isset($routing))
171 {
172 $RTR->_set_overrides($routing);
173 }
174
175/*
176 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500177 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600178 * ------------------------------------------------------
179 */
180 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000181
182/*
183 * ------------------------------------------------------
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200184 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * ------------------------------------------------------
186 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300187 if ($EXT->call_hook('cache_override') === FALSE
Alex Bilbieed944a32012-06-02 11:07:47 +0100188 && $OUT->_display_cache($CFG, $URI) === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200190 exit;
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
Timothy Warrenad475052012-04-19 13:21:06 -0400223 /**
224 * Reference to the CI_Controller method.
225 *
226 * Returns current CI instance object
227 *
228 * @return object
229 */
Greg Aker4abfa682010-11-10 14:44:26 -0600230 function &get_instance()
231 {
232 return CI_Controller::get_instance();
233 }
234
Greg Aker3a746652011-04-19 10:59:47 -0500235 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500236 {
Greg Aker3a746652011-04-19 10:59:47 -0500237 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Jones218876c2010-03-02 13:11:00 -0600240 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200241 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600242 // If this include fails it means that the default controller in the Routes.php file is not resolving to something valid.
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300243 if ( ! file_exists(APPPATH.'controllers/'.$RTR->directory.$RTR->class.'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600244 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500245 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 -0600246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300248 include(APPPATH.'controllers/'.$RTR->directory.$RTR->class.'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Jones218876c2010-03-02 13:11:00 -0600250 // Set a mark point for benchmarking
251 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000252
253/*
254 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500255 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * ------------------------------------------------------
257 *
Andrey Andreev7d394f82012-11-02 04:37:34 +0200258 * None of the methods in the app controller or the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500259 * loader class can be called via the URI, nor can
Andrey Andreev7d394f82012-11-02 04:37:34 +0200260 * controller functions that begin with an underscore.
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 */
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300262 $class = $RTR->class;
263 $method = $RTR->method;
Barry Mienydd671972010-10-04 16:33:58 +0200264
Andrey Andreev49e68de2013-02-21 16:30:55 +0200265 if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method))
Derek Jones218876c2010-03-02 13:11:00 -0600266 {
Shane Pearson664a9352011-08-10 16:02:32 -0500267 if ( ! empty($RTR->routes['404_override']))
268 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200269 if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
270 {
271 $method = 'index';
272 }
273
Andrey Andreev49e68de2013-02-21 16:30:55 +0200274 if ( ! class_exists($class, FALSE))
Shane Pearson664a9352011-08-10 16:02:32 -0500275 {
276 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
277 {
Andrey Andreevb3f774b2012-04-23 12:57:57 +0300278 show_404($class.'/'.$method);
Shane Pearson664a9352011-08-10 16:02:32 -0500279 }
280
281 include_once(APPPATH.'controllers/'.$class.'.php');
282 }
283 }
284 else
285 {
Andrey Andreevb3f774b2012-04-23 12:57:57 +0300286 show_404($class.'/'.$method);
Shane Pearson664a9352011-08-10 16:02:32 -0500287 }
Derek Jones218876c2010-03-02 13:11:00 -0600288 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000289
Andrey Andreev38e32f62012-11-03 00:16:47 +0200290 if (method_exists($class, '_remap'))
291 {
292 $params = array($method, array_slice($URI->rsegments, 2));
293 $method = '_remap';
294 }
295 else
296 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200297 // WARNING: It appears that there are issues with is_callable() even in PHP 5.2!
298 // Furthermore, there are bug reports and feature/change requests related to it
299 // that make it unreliable to use in this context. Please, DO NOT change this
300 // work-around until a better alternative is available.
301 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($class)), TRUE))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200302 {
303 if (empty($RTR->routes['404_override']))
304 {
305 show_404($class.'/'.$method);
306 }
307 elseif (sscanf($RTR->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
308 {
309 $method = 'index';
310 }
311
Andrey Andreev49e68de2013-02-21 16:30:55 +0200312 if ( ! class_exists($class, FALSE))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200313 {
314 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
315 {
316 show_404($class.'/'.$method);
317 }
318
319 include_once(APPPATH.'controllers/'.$class.'.php');
320 }
321 }
322
323 $params = array_slice($URI->rsegments, 2);
324 }
325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326/*
327 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500328 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * ------------------------------------------------------
330 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300331 $EXT->call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
333/*
334 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500335 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * ------------------------------------------------------
337 */
Derek Jones218876c2010-03-02 13:11:00 -0600338 // Mark a start point so we can benchmark the controller
339 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Jones218876c2010-03-02 13:11:00 -0600341 $CI = new $class();
342
343/*
344 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500345 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600346 * ------------------------------------------------------
347 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300348 $EXT->call_hook('post_controller_constructor');
Derek Jones218876c2010-03-02 13:11:00 -0600349
350/*
351 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500352 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600353 * ------------------------------------------------------
354 */
Andrey Andreev38e32f62012-11-03 00:16:47 +0200355 call_user_func_array(array(&$CI, $method), $params);
Derek Allard2067d1a2008-11-13 22:59:24 +0000356
Derek Jones218876c2010-03-02 13:11:00 -0600357 // Mark a benchmark end point
358 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000359
360/*
361 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500362 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * ------------------------------------------------------
364 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300365 $EXT->call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000366
367/*
368 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500369 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * ------------------------------------------------------
371 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300372 if ($EXT->call_hook('display_override') === FALSE)
Derek Jones218876c2010-03-02 13:11:00 -0600373 {
374 $OUT->_display();
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377/*
378 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500379 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * ------------------------------------------------------
381 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300382 $EXT->call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384/* End of file CodeIgniter.php */
Timothy Warren31d30bc2012-04-23 08:58:42 -0400385/* Location: ./system/core/CodeIgniter.php */