blob: cc12f149f8f5cf58082f31634fe08ef06511f4e2 [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');
Andrey Andreevb6fbcbe2013-11-17 18:06:18 +020048
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 }
Andrey Andreevb6fbcbe2013-11-17 18:06:18 +020058
59 require(APPPATH.'config/constants.php');
Derek Allard2067d1a2008-11-13 22:59:24 +000060
61/*
62 * ------------------------------------------------------
josephok1095d112013-11-16 09:33:37 +080063 * Load the global functions
64 * ------------------------------------------------------
65 */
66 require_once(BASEPATH.'core/Common.php');
67
68/*
69 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050070 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +000071 * ------------------------------------------------------
72 */
Derek Jones218876c2010-03-02 13:11:00 -060073 set_error_handler('_exception_handler');
Kaiwang Chen21fe9da2013-09-11 13:09:41 +080074 register_shutdown_function('_shutdown_handler');
Barry Mienydd671972010-10-04 16:33:58 +020075
Andrey Andreev5932a732013-09-13 14:45:53 +030076 // Kill magic quotes
77 is_php('5.4') OR @ini_set('magic_quotes_runtime', 0);
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
vlakoff8d70c0a2013-08-17 07:31:29 +020088 * overriden via data set in the main index.php file,
Barry Mienydd671972010-10-04 16:33:58 +020089 * before proceeding we need to know if a subclass_prefix
Andrey Andreev9c5c24a2012-01-07 18:51:21 +020090 * 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 */
Andrey Andreev9ba661b2012-06-04 14:44:34 +030095 if ( ! empty($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 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 * ------------------------------------------------------
104 */
Derek Jones218876c2010-03-02 13:11:00 -0600105 $BM =& load_class('Benchmark', 'core');
106 $BM->mark('total_execution_time_start');
107 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
109/*
110 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500111 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 * ------------------------------------------------------
113 */
Derek Jones218876c2010-03-02 13:11:00 -0600114 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
116/*
117 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500118 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * ------------------------------------------------------
120 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300121 $EXT->call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000122
123/*
124 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500125 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Derek Jones218876c2010-03-02 13:11:00 -0600128 $CFG =& load_class('Config', 'core');
129
130 // Do we have any manually set config items in the index.php file?
Andrey Andreev5232ba02012-10-27 15:25:05 +0300131 if (isset($assign_to_config) && is_array($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200132 {
Andrey Andreev5232ba02012-10-27 15:25:05 +0300133 foreach ($assign_to_config as $key => $value)
134 {
135 $CFG->set_item($key, $value);
136 }
Derek Jones218876c2010-03-02 13:11:00 -0600137 }
138
139/*
140 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500141 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600142 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200143 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500144 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600145 * class needs to be used very early on, but it cannot
Chris Berthe827b3f02012-04-27 23:36:54 -0400146 * properly determine if UTF-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600147 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200148 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 */
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500150 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones218876c2010-03-02 13:11:00 -0600152/*
153 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500154 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600155 * ------------------------------------------------------
156 */
157 $URI =& load_class('URI', 'core');
158
159/*
160 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500161 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600162 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163 */
Derek Jones218876c2010-03-02 13:11:00 -0600164 $RTR =& load_class('Router', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jones218876c2010-03-02 13:11:00 -0600166/*
167 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500168 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600169 * ------------------------------------------------------
170 */
171 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000172
173/*
174 * ------------------------------------------------------
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200175 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * ------------------------------------------------------
177 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300178 if ($EXT->call_hook('cache_override') === FALSE
Alex Bilbieed944a32012-06-02 11:07:47 +0100179 && $OUT->_display_cache($CFG, $URI) === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200181 exit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000183
184/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400185 * -----------------------------------------------------
186 * Load the security class for xss and csrf support
187 * -----------------------------------------------------
188 */
189 $SEC =& load_class('Security', 'core');
190
191/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500193 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * ------------------------------------------------------
195 */
Barry Mienydd671972010-10-04 16:33:58 +0200196 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000197
Derek Jones218876c2010-03-02 13:11:00 -0600198/*
199 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500200 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600201 * ------------------------------------------------------
202 */
203 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
205/*
206 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500207 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 * ------------------------------------------------------
209 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 */
Derek Jones218876c2010-03-02 13:11:00 -0600211 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500212 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200213
Timothy Warrenad475052012-04-19 13:21:06 -0400214 /**
215 * Reference to the CI_Controller method.
216 *
217 * Returns current CI instance object
218 *
219 * @return object
220 */
Greg Aker4abfa682010-11-10 14:44:26 -0600221 function &get_instance()
222 {
223 return CI_Controller::get_instance();
224 }
225
Greg Aker3a746652011-04-19 10:59:47 -0500226 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500227 {
Greg Aker3a746652011-04-19 10:59:47 -0500228 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Jones218876c2010-03-02 13:11:00 -0600231 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200232 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600233 // 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 +0300234 $class = ucfirst($RTR->class);
235 if ( ! file_exists(APPPATH.'controllers/'.$RTR->directory.$class.'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600236 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500237 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 -0600238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Andrey Andreev20292312013-07-22 14:29:10 +0300240 include(APPPATH.'controllers/'.$RTR->directory.$class.'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Jones218876c2010-03-02 13:11:00 -0600242 // Set a mark point for benchmarking
243 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000244
245/*
246 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500247 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * ------------------------------------------------------
249 *
Andrey Andreev7d394f82012-11-02 04:37:34 +0200250 * None of the methods in the app controller or the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500251 * loader class can be called via the URI, nor can
Andrey Andreev20292312013-07-22 14:29:10 +0300252 * controller methods that begin with an underscore.
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 */
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300254 $method = $RTR->method;
Barry Mienydd671972010-10-04 16:33:58 +0200255
Andrey Andreev49e68de2013-02-21 16:30:55 +0200256 if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method))
Derek Jones218876c2010-03-02 13:11:00 -0600257 {
Shane Pearson664a9352011-08-10 16:02:32 -0500258 if ( ! empty($RTR->routes['404_override']))
259 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200260 if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
261 {
262 $method = 'index';
263 }
264
Andrey Andreev20292312013-07-22 14:29:10 +0300265 $class = ucfirst($class);
266
Andrey Andreev49e68de2013-02-21 16:30:55 +0200267 if ( ! class_exists($class, FALSE))
Shane Pearson664a9352011-08-10 16:02:32 -0500268 {
269 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
270 {
Andrey Andreevb3f774b2012-04-23 12:57:57 +0300271 show_404($class.'/'.$method);
Shane Pearson664a9352011-08-10 16:02:32 -0500272 }
273
274 include_once(APPPATH.'controllers/'.$class.'.php');
275 }
276 }
277 else
278 {
Andrey Andreevb3f774b2012-04-23 12:57:57 +0300279 show_404($class.'/'.$method);
Shane Pearson664a9352011-08-10 16:02:32 -0500280 }
Derek Jones218876c2010-03-02 13:11:00 -0600281 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000282
Andrey Andreev38e32f62012-11-03 00:16:47 +0200283 if (method_exists($class, '_remap'))
284 {
285 $params = array($method, array_slice($URI->rsegments, 2));
286 $method = '_remap';
287 }
288 else
289 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200290 // WARNING: It appears that there are issues with is_callable() even in PHP 5.2!
291 // Furthermore, there are bug reports and feature/change requests related to it
292 // that make it unreliable to use in this context. Please, DO NOT change this
293 // work-around until a better alternative is available.
294 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($class)), TRUE))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200295 {
296 if (empty($RTR->routes['404_override']))
297 {
298 show_404($class.'/'.$method);
299 }
300 elseif (sscanf($RTR->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
301 {
302 $method = 'index';
303 }
304
Andrey Andreev20292312013-07-22 14:29:10 +0300305 $class = ucfirst($class);
306
Andrey Andreev49e68de2013-02-21 16:30:55 +0200307 if ( ! class_exists($class, FALSE))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200308 {
309 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
310 {
311 show_404($class.'/'.$method);
312 }
313
314 include_once(APPPATH.'controllers/'.$class.'.php');
315 }
316 }
317
318 $params = array_slice($URI->rsegments, 2);
319 }
320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321/*
322 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500323 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * ------------------------------------------------------
325 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300326 $EXT->call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
328/*
329 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500330 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * ------------------------------------------------------
332 */
Derek Jones218876c2010-03-02 13:11:00 -0600333 // Mark a start point so we can benchmark the controller
334 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Jones218876c2010-03-02 13:11:00 -0600336 $CI = new $class();
337
338/*
339 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500340 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600341 * ------------------------------------------------------
342 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300343 $EXT->call_hook('post_controller_constructor');
Derek Jones218876c2010-03-02 13:11:00 -0600344
345/*
346 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500347 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600348 * ------------------------------------------------------
349 */
Andrey Andreev38e32f62012-11-03 00:16:47 +0200350 call_user_func_array(array(&$CI, $method), $params);
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
Derek Jones218876c2010-03-02 13:11:00 -0600352 // Mark a benchmark end point
353 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000354
355/*
356 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500357 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * ------------------------------------------------------
359 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300360 $EXT->call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000361
362/*
363 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500364 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * ------------------------------------------------------
366 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300367 if ($EXT->call_hook('display_override') === FALSE)
Derek Jones218876c2010-03-02 13:11:00 -0600368 {
369 $OUT->_display();
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372/*
373 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500374 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * ------------------------------------------------------
376 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300377 $EXT->call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379/* End of file CodeIgniter.php */
Andrey Andreevb6fbcbe2013-11-17 18:06:18 +0200380/* Location: ./system/core/CodeIgniter.php */