blob: 1981c2649f0461849c5c30293eb28bbdaccfb6d8 [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');
josephok1095d112013-11-16 09:33:37 +080048
Derek Allard2067d1a2008-11-13 22:59:24 +000049/*
50 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050051 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000052 * ------------------------------------------------------
53 */
Andrey Andreevdb529ca2013-01-28 11:00:02 +020054 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
Greg Akerd96f8822011-12-27 16:23:47 -060055 {
56 require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
57 }
58 else
59 {
60 require(APPPATH.'config/constants.php');
61 }
Derek Allard2067d1a2008-11-13 22:59:24 +000062
63/*
64 * ------------------------------------------------------
josephok1095d112013-11-16 09:33:37 +080065 * Load the global functions
66 * ------------------------------------------------------
67 */
68 require_once(BASEPATH.'core/Common.php');
69
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');
Kaiwang Chen21fe9da2013-09-11 13:09:41 +080076 register_shutdown_function('_shutdown_handler');
Barry Mienydd671972010-10-04 16:33:58 +020077
Andrey Andreev5932a732013-09-13 14:45:53 +030078 // Kill magic quotes
79 is_php('5.4') OR @ini_set('magic_quotes_runtime', 0);
Derek Jones962d2252009-08-05 04:26:11 +000080
Derek Jones218876c2010-03-02 13:11:00 -060081/*
82 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050083 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060084 * ------------------------------------------------------
85 *
Barry Mienydd671972010-10-04 16:33:58 +020086 * Normally the "subclass_prefix" is set in the config file.
87 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060088 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020089 * "libraries" folder. Since CI allows config items to be
vlakoff8d70c0a2013-08-17 07:31:29 +020090 * overriden via data set in the main index.php file,
Barry Mienydd671972010-10-04 16:33:58 +020091 * before proceeding we need to know if a subclass_prefix
Andrey Andreev9c5c24a2012-01-07 18:51:21 +020092 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -060093 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +020094 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -060095 * hurt to load it here.
96 */
Andrey Andreev9ba661b2012-06-04 14:44:34 +030097 if ( ! empty($assign_to_config['subclass_prefix']))
Derek Jones218876c2010-03-02 13:11:00 -060098 {
99 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
100 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500101
Pascal Krietef566af52010-11-09 13:03:26 -0500102/*
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 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300123 $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?
Andrey Andreev5232ba02012-10-27 15:25:05 +0300133 if (isset($assign_to_config) && is_array($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200134 {
Andrey Andreev5232ba02012-10-27 15:25:05 +0300135 foreach ($assign_to_config as $key => $value)
136 {
137 $CFG->set_item($key, $value);
138 }
Derek Jones218876c2010-03-02 13:11:00 -0600139 }
140
141/*
142 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500143 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600144 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200145 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500146 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600147 * class needs to be used very early on, but it cannot
Chris Berthe827b3f02012-04-27 23:36:54 -0400148 * properly determine if UTF-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600149 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200150 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 */
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500152 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Jones218876c2010-03-02 13:11:00 -0600154/*
155 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500156 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600157 * ------------------------------------------------------
158 */
159 $URI =& load_class('URI', 'core');
160
161/*
162 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500163 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600164 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200165 */
Derek Jones218876c2010-03-02 13:11:00 -0600166 $RTR =& load_class('Router', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Jones218876c2010-03-02 13:11:00 -0600168 // Set any routing overrides that may exist in the main index file
169 if (isset($routing))
170 {
171 $RTR->_set_overrides($routing);
172 }
173
174/*
175 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500176 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600177 * ------------------------------------------------------
178 */
179 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000180
181/*
182 * ------------------------------------------------------
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200183 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * ------------------------------------------------------
185 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300186 if ($EXT->call_hook('cache_override') === FALSE
Alex Bilbieed944a32012-06-02 11:07:47 +0100187 && $OUT->_display_cache($CFG, $URI) === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200189 exit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
192/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400193 * -----------------------------------------------------
194 * Load the security class for xss and csrf support
195 * -----------------------------------------------------
196 */
197 $SEC =& load_class('Security', 'core');
198
199/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500201 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * ------------------------------------------------------
203 */
Barry Mienydd671972010-10-04 16:33:58 +0200204 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
Derek Jones218876c2010-03-02 13:11:00 -0600206/*
207 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500208 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600209 * ------------------------------------------------------
210 */
211 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213/*
214 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500215 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 * ------------------------------------------------------
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 */
Derek Jones218876c2010-03-02 13:11:00 -0600219 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500220 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200221
Timothy Warrenad475052012-04-19 13:21:06 -0400222 /**
223 * Reference to the CI_Controller method.
224 *
225 * Returns current CI instance object
226 *
227 * @return object
228 */
Greg Aker4abfa682010-11-10 14:44:26 -0600229 function &get_instance()
230 {
231 return CI_Controller::get_instance();
232 }
233
Greg Aker3a746652011-04-19 10:59:47 -0500234 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500235 {
Greg Aker3a746652011-04-19 10:59:47 -0500236 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Jones218876c2010-03-02 13:11:00 -0600239 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200240 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600241 // If this include fails it means that the default controller in the Routes.php file is not resolving to something valid.
Andrey Andreev20292312013-07-22 14:29:10 +0300242 $class = ucfirst($RTR->class);
243 if ( ! file_exists(APPPATH.'controllers/'.$RTR->directory.$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 Andreev20292312013-07-22 14:29:10 +0300248 include(APPPATH.'controllers/'.$RTR->directory.$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 Andreev20292312013-07-22 14:29:10 +0300260 * controller methods that begin with an underscore.
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 */
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300262 $method = $RTR->method;
Barry Mienydd671972010-10-04 16:33:58 +0200263
Andrey Andreev49e68de2013-02-21 16:30:55 +0200264 if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method))
Derek Jones218876c2010-03-02 13:11:00 -0600265 {
Shane Pearson664a9352011-08-10 16:02:32 -0500266 if ( ! empty($RTR->routes['404_override']))
267 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200268 if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
269 {
270 $method = 'index';
271 }
272
Andrey Andreev20292312013-07-22 14:29:10 +0300273 $class = ucfirst($class);
274
Andrey Andreev49e68de2013-02-21 16:30:55 +0200275 if ( ! class_exists($class, FALSE))
Shane Pearson664a9352011-08-10 16:02:32 -0500276 {
277 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
278 {
Andrey Andreevb3f774b2012-04-23 12:57:57 +0300279 show_404($class.'/'.$method);
Shane Pearson664a9352011-08-10 16:02:32 -0500280 }
281
282 include_once(APPPATH.'controllers/'.$class.'.php');
283 }
284 }
285 else
286 {
Andrey Andreevb3f774b2012-04-23 12:57:57 +0300287 show_404($class.'/'.$method);
Shane Pearson664a9352011-08-10 16:02:32 -0500288 }
Derek Jones218876c2010-03-02 13:11:00 -0600289 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000290
Andrey Andreev38e32f62012-11-03 00:16:47 +0200291 if (method_exists($class, '_remap'))
292 {
293 $params = array($method, array_slice($URI->rsegments, 2));
294 $method = '_remap';
295 }
296 else
297 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200298 // WARNING: It appears that there are issues with is_callable() even in PHP 5.2!
299 // Furthermore, there are bug reports and feature/change requests related to it
300 // that make it unreliable to use in this context. Please, DO NOT change this
301 // work-around until a better alternative is available.
302 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($class)), TRUE))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200303 {
304 if (empty($RTR->routes['404_override']))
305 {
306 show_404($class.'/'.$method);
307 }
308 elseif (sscanf($RTR->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
309 {
310 $method = 'index';
311 }
312
Andrey Andreev20292312013-07-22 14:29:10 +0300313 $class = ucfirst($class);
314
Andrey Andreev49e68de2013-02-21 16:30:55 +0200315 if ( ! class_exists($class, FALSE))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200316 {
317 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
318 {
319 show_404($class.'/'.$method);
320 }
321
322 include_once(APPPATH.'controllers/'.$class.'.php');
323 }
324 }
325
326 $params = array_slice($URI->rsegments, 2);
327 }
328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329/*
330 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500331 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * ------------------------------------------------------
333 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300334 $EXT->call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000335
336/*
337 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500338 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * ------------------------------------------------------
340 */
Derek Jones218876c2010-03-02 13:11:00 -0600341 // Mark a start point so we can benchmark the controller
342 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Jones218876c2010-03-02 13:11:00 -0600344 $CI = new $class();
345
346/*
347 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500348 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600349 * ------------------------------------------------------
350 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300351 $EXT->call_hook('post_controller_constructor');
Derek Jones218876c2010-03-02 13:11:00 -0600352
353/*
354 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500355 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600356 * ------------------------------------------------------
357 */
Andrey Andreev38e32f62012-11-03 00:16:47 +0200358 call_user_func_array(array(&$CI, $method), $params);
Derek Allard2067d1a2008-11-13 22:59:24 +0000359
Derek Jones218876c2010-03-02 13:11:00 -0600360 // Mark a benchmark end point
361 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000362
363/*
364 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500365 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * ------------------------------------------------------
367 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300368 $EXT->call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000369
370/*
371 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500372 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * ------------------------------------------------------
374 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300375 if ($EXT->call_hook('display_override') === FALSE)
Derek Jones218876c2010-03-02 13:11:00 -0600376 {
377 $OUT->_display();
378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380/*
381 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500382 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * ------------------------------------------------------
384 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300385 $EXT->call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387/* End of file CodeIgniter.php */
josephok1095d112013-11-16 09:33:37 +0800388/* Location: ./system/core/CodeIgniter.php */