blob: 04f346c7cd358a5d6362330aa775d21282cff2c4 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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 */
27
28// ------------------------------------------------------------------------
29
30/**
Derek Jones218876c2010-03-02 13:11:00 -060031 * System Initialization File
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * Loads the base classes and executes the request.
34 *
35 * @package CodeIgniter
36 * @subpackage codeigniter
37 * @category Front-controller
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/
40 */
41
Phil Sturgeond8ef6302011-08-14 12:10:55 -060042/**
43 * CodeIgniter Version
44 *
45 * @var string
46 *
Derek Jones218876c2010-03-02 13:11:00 -060047 */
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 define('CI_VERSION', '3.0-dev');
Derek Allard2067d1a2008-11-13 22:59:24 +000049
Phil Sturgeonbabfb292011-03-08 21:56:08 +000050/*
51 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050052 * Load the global functions
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * ------------------------------------------------------
54 */
Greg Aker3a746652011-04-19 10:59:47 -050055 require(BASEPATH.'core/Common.php');
Derek Allard2067d1a2008-11-13 22:59:24 +000056
57/*
58 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050059 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000060 * ------------------------------------------------------
61 */
Greg Aker5c1aa632011-12-25 01:24:29 -060062 load_environ_config('constants', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000063
64/*
65 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050066 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +000067 * ------------------------------------------------------
68 */
Derek Jones218876c2010-03-02 13:11:00 -060069 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Jones218876c2010-03-02 13:11:00 -060071 if ( ! is_php('5.3'))
72 {
Barry Mienydd671972010-10-04 16:33:58 +020073 @set_magic_quotes_runtime(0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060074 }
Derek Jones962d2252009-08-05 04:26:11 +000075
Derek Jones218876c2010-03-02 13:11:00 -060076/*
77 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050078 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060079 * ------------------------------------------------------
80 *
Barry Mienydd671972010-10-04 16:33:58 +020081 * Normally the "subclass_prefix" is set in the config file.
82 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060083 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020084 * "libraries" folder. Since CI allows config items to be
85 * overriden via data set in the main index. php file,
86 * before proceeding we need to know if a subclass_prefix
Derek Jones37f4b9c2011-07-01 17:56:50 -050087 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -060088 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +020089 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -060090 * hurt to load it here.
91 */
Barry Mienydd671972010-10-04 16:33:58 +020092 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
Derek Jones218876c2010-03-02 13:11:00 -060093 {
94 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
95 }
Eric Barnesc5bf6162011-01-30 21:17:11 -050096
Pascal Krietef566af52010-11-09 13:03:26 -050097/*
98 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050099 * Set a liberal script execution time limit
Pascal Krietef566af52010-11-09 13:03:26 -0500100 * ------------------------------------------------------
101 */
Jack Webb-Heller796b2b72011-12-01 11:56:12 +0000102 if (function_exists("set_time_limit") AND @ini_get("safe_mode") == 0)
Pascal Krietef566af52010-11-09 13:03:26 -0500103 {
Jack Webb-Heller796b2b72011-12-01 11:56:12 +0000104 // Do not override the Time Limit value if running from Command Line
Jack Webb-Hellereea0cbe2011-12-01 13:37:08 +0000105 if(php_sapi_name() != 'cli')
Jack Webb-Heller796b2b72011-12-01 11:56:12 +0000106 {
107 @set_time_limit(300);
108 }
Pascal Krietef566af52010-11-09 13:03:26 -0500109 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000110
111/*
112 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500113 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * ------------------------------------------------------
115 */
Derek Jones218876c2010-03-02 13:11:00 -0600116 $BM =& load_class('Benchmark', 'core');
117 $BM->mark('total_execution_time_start');
118 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000119
120/*
121 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500122 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * ------------------------------------------------------
124 */
Derek Jones218876c2010-03-02 13:11:00 -0600125 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000126
127/*
128 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500129 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * ------------------------------------------------------
131 */
Derek Jones218876c2010-03-02 13:11:00 -0600132 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000133
134/*
135 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500136 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200138 */
Derek Jones218876c2010-03-02 13:11:00 -0600139 $CFG =& load_class('Config', 'core');
140
141 // Do we have any manually set config items in the index.php file?
142 if (isset($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200143 {
Derek Jones218876c2010-03-02 13:11:00 -0600144 $CFG->_assign_to_config($assign_to_config);
145 }
146
147/*
148 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500149 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600150 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200151 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500152 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600153 * class needs to be used very early on, but it cannot
Barry Mienydd671972010-10-04 16:33:58 +0200154 * properly determine if UTf-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600155 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
158
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500159 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Jones218876c2010-03-02 13:11:00 -0600161/*
162 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500163 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600164 * ------------------------------------------------------
165 */
166 $URI =& load_class('URI', 'core');
167
168/*
169 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500170 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600171 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172 */
Derek Jones218876c2010-03-02 13:11:00 -0600173 $RTR =& load_class('Router', 'core');
174 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jones218876c2010-03-02 13:11:00 -0600176 // Set any routing overrides that may exist in the main index file
177 if (isset($routing))
178 {
179 $RTR->_set_overrides($routing);
180 }
181
182/*
183 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500184 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600185 * ------------------------------------------------------
186 */
187 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000188
189/*
190 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500191 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * ------------------------------------------------------
193 */
Derek Jones218876c2010-03-02 13:11:00 -0600194 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Derek Jones218876c2010-03-02 13:11:00 -0600196 if ($OUT->_display_cache($CFG, $URI) == TRUE)
197 {
198 exit;
199 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
202/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400203 * -----------------------------------------------------
204 * Load the security class for xss and csrf support
205 * -----------------------------------------------------
206 */
207 $SEC =& load_class('Security', 'core');
208
209/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500211 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * ------------------------------------------------------
213 */
Barry Mienydd671972010-10-04 16:33:58 +0200214 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Derek Jones218876c2010-03-02 13:11:00 -0600216/*
217 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500218 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600219 * ------------------------------------------------------
220 */
221 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000222
223/*
224 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500225 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * ------------------------------------------------------
227 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 */
Derek Jones218876c2010-03-02 13:11:00 -0600229 // Load the base controller class
Greg Aker3a746652011-04-19 10:59:47 -0500230 require BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200231
Greg Aker4abfa682010-11-10 14:44:26 -0600232 function &get_instance()
233 {
234 return CI_Controller::get_instance();
235 }
236
237
Greg Aker3a746652011-04-19 10:59:47 -0500238 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500239 {
Greg Aker3a746652011-04-19 10:59:47 -0500240 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Jones218876c2010-03-02 13:11:00 -0600243 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200244 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600245 // 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 -0500246 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600247 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500248 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 -0600249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Greg Aker3a746652011-04-19 10:59:47 -0500251 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Jones218876c2010-03-02 13:11:00 -0600253 // Set a mark point for benchmarking
254 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000255
256/*
257 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500258 * Security check
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * ------------------------------------------------------
260 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500261 * None of the functions in the app controller or the
262 * loader class can be called via the URI, nor can
263 * controller functions that begin with an underscore
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 */
Derek Jones37f4b9c2011-07-01 17:56:50 -0500265 $class = $RTR->fetch_class();
Derek Jones218876c2010-03-02 13:11:00 -0600266 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Jones218876c2010-03-02 13:11:00 -0600268 if ( ! class_exists($class)
Derek Jones218876c2010-03-02 13:11:00 -0600269 OR strncmp($method, '_', 1) == 0
Greg Aker63277b82010-11-09 13:46:13 -0600270 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
Derek Jones218876c2010-03-02 13:11:00 -0600271 )
272 {
Shane Pearson664a9352011-08-10 16:02:32 -0500273 if ( ! empty($RTR->routes['404_override']))
274 {
275 $x = explode('/', $RTR->routes['404_override']);
276 $class = $x[0];
277 $method = (isset($x[1]) ? $x[1] : 'index');
278 if ( ! class_exists($class))
279 {
280 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
281 {
282 show_404("{$class}/{$method}");
283 }
284
285 include_once(APPPATH.'controllers/'.$class.'.php');
286 }
287 }
288 else
289 {
290 show_404("{$class}/{$method}");
291 }
Derek Jones218876c2010-03-02 13:11:00 -0600292 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000293
294/*
295 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500296 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * ------------------------------------------------------
298 */
Derek Jones218876c2010-03-02 13:11:00 -0600299 $EXT->_call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000300
301/*
302 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500303 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * ------------------------------------------------------
305 */
Derek Jones218876c2010-03-02 13:11:00 -0600306 // Mark a start point so we can benchmark the controller
307 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Jones218876c2010-03-02 13:11:00 -0600309 $CI = new $class();
310
311/*
312 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500313 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600314 * ------------------------------------------------------
315 */
316 $EXT->_call_hook('post_controller_constructor');
317
318/*
319 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500320 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600321 * ------------------------------------------------------
322 */
323 // Is there a "remap" function? If so, we call it instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 if (method_exists($CI, '_remap'))
325 {
Pascal Kriete3431ae32010-11-09 15:19:50 -0500326 $CI->_remap($method, array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328 else
329 {
330 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
331 // methods, so we'll use this workaround for consistent behavior
332 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
333 {
Eric Barnesc5bf6162011-01-30 21:17:11 -0500334 // Check and see if we are using a 404 override and use it.
335 if ( ! empty($RTR->routes['404_override']))
336 {
337 $x = explode('/', $RTR->routes['404_override']);
338 $class = $x[0];
339 $method = (isset($x[1]) ? $x[1] : 'index');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500340 if ( ! class_exists($class))
341 {
Greg Aker3a746652011-04-19 10:59:47 -0500342 if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
Eric Barnes5519e3d2011-02-01 13:07:37 -0500343 {
344 show_404("{$class}/{$method}");
345 }
346
Greg Aker3a746652011-04-19 10:59:47 -0500347 include_once(APPPATH.'controllers/'.$class.'.php');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500348 unset($CI);
349 $CI = new $class();
350 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500351 }
352 else
353 {
354 show_404("{$class}/{$method}");
355 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357
358 // Call the requested method.
Barry Mienydd671972010-10-04 16:33:58 +0200359 // Any URI segments present (besides the class/function) will be passed to the method for convenience
360 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000362
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Jones218876c2010-03-02 13:11:00 -0600364 // Mark a benchmark end point
365 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000366
367/*
368 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500369 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * ------------------------------------------------------
371 */
Derek Jones218876c2010-03-02 13:11:00 -0600372 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000373
374/*
375 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500376 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * ------------------------------------------------------
378 */
Derek Jones218876c2010-03-02 13:11:00 -0600379 if ($EXT->_call_hook('display_override') === FALSE)
380 {
381 $OUT->_display();
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384/*
385 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500386 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * ------------------------------------------------------
388 */
Derek Jones218876c2010-03-02 13:11:00 -0600389 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000390
391/*
392 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500393 * Close the DB connection if one exists
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * ------------------------------------------------------
395 */
Derek Jones218876c2010-03-02 13:11:00 -0600396 if (class_exists('CI_DB') AND isset($CI->db))
397 {
398 $CI->db->close();
399 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000400
401
402/* End of file CodeIgniter.php */
Phil Sturgeon35f64912011-03-15 21:01:39 +0000403/* Location: ./system/core/CodeIgniter.php */