blob: cb4b735d5219c1753c334290a2646e8d9deebd82 [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');
168 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Jones218876c2010-03-02 13:11:00 -0600170 // Set any routing overrides that may exist in the main index file
171 if (isset($routing))
172 {
173 $RTR->_set_overrides($routing);
174 }
175
176/*
177 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500178 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600179 * ------------------------------------------------------
180 */
181 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000182
183/*
184 * ------------------------------------------------------
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200185 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * ------------------------------------------------------
187 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300188 if ($EXT->call_hook('cache_override') === FALSE
Alex Bilbieed944a32012-06-02 11:07:47 +0100189 && $OUT->_display_cache($CFG, $URI) === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200191 exit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000193
194/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400195 * -----------------------------------------------------
196 * Load the security class for xss and csrf support
197 * -----------------------------------------------------
198 */
199 $SEC =& load_class('Security', 'core');
200
201/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500203 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * ------------------------------------------------------
205 */
Barry Mienydd671972010-10-04 16:33:58 +0200206 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000207
Derek Jones218876c2010-03-02 13:11:00 -0600208/*
209 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500210 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600211 * ------------------------------------------------------
212 */
213 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
215/*
216 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500217 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * ------------------------------------------------------
219 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 */
Derek Jones218876c2010-03-02 13:11:00 -0600221 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500222 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200223
Timothy Warrenad475052012-04-19 13:21:06 -0400224 /**
225 * Reference to the CI_Controller method.
226 *
227 * Returns current CI instance object
228 *
229 * @return object
230 */
Greg Aker4abfa682010-11-10 14:44:26 -0600231 function &get_instance()
232 {
233 return CI_Controller::get_instance();
234 }
235
Greg Aker3a746652011-04-19 10:59:47 -0500236 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500237 {
Greg Aker3a746652011-04-19 10:59:47 -0500238 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Jones218876c2010-03-02 13:11:00 -0600241 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200242 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600243 // 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 -0500244 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600245 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500246 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 -0600247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Greg Aker3a746652011-04-19 10:59:47 -0500249 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Jones218876c2010-03-02 13:11:00 -0600251 // Set a mark point for benchmarking
252 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
254/*
255 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500256 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * ------------------------------------------------------
258 *
Andrey Andreev7d394f82012-11-02 04:37:34 +0200259 * None of the methods in the app controller or the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500260 * loader class can be called via the URI, nor can
Andrey Andreev7d394f82012-11-02 04:37:34 +0200261 * controller functions that begin with an underscore.
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 */
Derek Jones37f4b9c2011-07-01 17:56:50 -0500263 $class = $RTR->fetch_class();
Derek Jones218876c2010-03-02 13:11:00 -0600264 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200265
Andrey Andreev7d394f82012-11-02 04:37:34 +0200266 if ( ! class_exists($class) OR $method[0] === '_' OR method_exists('CI_Controller', $method))
Derek Jones218876c2010-03-02 13:11:00 -0600267 {
Shane Pearson664a9352011-08-10 16:02:32 -0500268 if ( ! empty($RTR->routes['404_override']))
269 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200270 if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
271 {
272 $method = 'index';
273 }
274
Shane Pearson664a9352011-08-10 16:02:32 -0500275 if ( ! class_exists($class))
276 {
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
313 if ( ! class_exists($class))
314 {
315 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
316 {
317 show_404($class.'/'.$method);
318 }
319
320 include_once(APPPATH.'controllers/'.$class.'.php');
321 }
322 }
323
324 $params = array_slice($URI->rsegments, 2);
325 }
326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327/*
328 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * ------------------------------------------------------
331 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300332 $EXT->call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
334/*
335 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500336 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * ------------------------------------------------------
338 */
Derek Jones218876c2010-03-02 13:11:00 -0600339 // Mark a start point so we can benchmark the controller
340 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Jones218876c2010-03-02 13:11:00 -0600342 $CI = new $class();
343
344/*
345 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500346 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600347 * ------------------------------------------------------
348 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300349 $EXT->call_hook('post_controller_constructor');
Derek Jones218876c2010-03-02 13:11:00 -0600350
351/*
352 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500353 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600354 * ------------------------------------------------------
355 */
Andrey Andreev38e32f62012-11-03 00:16:47 +0200356 call_user_func_array(array(&$CI, $method), $params);
Derek Allard2067d1a2008-11-13 22:59:24 +0000357
Derek Jones218876c2010-03-02 13:11:00 -0600358 // Mark a benchmark end point
359 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000360
361/*
362 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500363 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * ------------------------------------------------------
365 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300366 $EXT->call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000367
368/*
369 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500370 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * ------------------------------------------------------
372 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300373 if ($EXT->call_hook('display_override') === FALSE)
Derek Jones218876c2010-03-02 13:11:00 -0600374 {
375 $OUT->_display();
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378/*
379 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500380 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * ------------------------------------------------------
382 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300383 $EXT->call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385/* End of file CodeIgniter.php */
Timothy Warren31d30bc2012-04-23 08:58:42 -0400386/* Location: ./system/core/CodeIgniter.php */