blob: 143faec1599115e06d5169e91870a84d5f75f47e [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
Derek Jones218876c2010-03-02 13:11:00 -060019 * System Initialization File
Derek Allard2067d1a2008-11-13 22:59:24 +000020 *
21 * Loads the base classes and executes the request.
22 *
23 * @package CodeIgniter
24 * @subpackage codeigniter
25 * @category Front-controller
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/
28 */
29
Derek Jones218876c2010-03-02 13:11:00 -060030/*
31 * ------------------------------------------------------
32 * Define the CodeIgniter Version
33 * ------------------------------------------------------
34 */
Phil Sturgeon09380592011-03-09 16:42:07 +000035 define('CI_VERSION', '2.0.1');
Derek Allard2067d1a2008-11-13 22:59:24 +000036
37/*
38 * ------------------------------------------------------
Phil Sturgeonbabfb292011-03-08 21:56:08 +000039 * Define the CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
40 * ------------------------------------------------------
41 */
Phil Sturgeon2f8b27e2011-03-08 21:56:08 +000042 define('CI_CORE', FALSE);
Phil Sturgeonbabfb292011-03-08 21:56:08 +000043
44/*
45 * ------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +000046 * Load the global functions
47 * ------------------------------------------------------
48 */
Derek Jones218876c2010-03-02 13:11:00 -060049 require(BASEPATH.'core/Common'.EXT);
Derek Allard2067d1a2008-11-13 22:59:24 +000050
51/*
52 * ------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * Load the framework constants
54 * ------------------------------------------------------
55 */
Phil Sturgeon05fa6112011-04-06 22:57:43 +010056 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants'.EXT))
Phil Sturgeon35f64912011-03-15 21:01:39 +000057 {
58 require(APPPATH.'config/'.ENVIRONMENT.'/constants'.EXT);
59 }
60 else
61 {
62 require(APPPATH.'config/constants'.EXT);
63 }
Derek Allard2067d1a2008-11-13 22:59:24 +000064
65/*
66 * ------------------------------------------------------
67 * Define a custom error handler so we can log PHP errors
68 * ------------------------------------------------------
69 */
Derek Jones218876c2010-03-02 13:11:00 -060070 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020071
Derek Jones218876c2010-03-02 13:11:00 -060072 if ( ! is_php('5.3'))
73 {
Barry Mienydd671972010-10-04 16:33:58 +020074 @set_magic_quotes_runtime(0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060075 }
Derek Jones962d2252009-08-05 04:26:11 +000076
Derek Jones218876c2010-03-02 13:11:00 -060077/*
78 * ------------------------------------------------------
79 * Set the subclass_prefix
80 * ------------------------------------------------------
81 *
Barry Mienydd671972010-10-04 16:33:58 +020082 * Normally the "subclass_prefix" is set in the config file.
83 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060084 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020085 * "libraries" folder. Since CI allows config items to be
86 * overriden via data set in the main index. php file,
87 * before proceeding we need to know if a subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060088 * override exists. If so, we will set this value now,
89 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +020090 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -060091 * hurt to load it here.
92 */
Barry Mienydd671972010-10-04 16:33:58 +020093 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
Derek Jones218876c2010-03-02 13:11:00 -060094 {
95 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
96 }
Eric Barnesc5bf6162011-01-30 21:17:11 -050097
Pascal Krietef566af52010-11-09 13:03:26 -050098/*
99 * ------------------------------------------------------
100 * Set a liberal script execution time limit
101 * ------------------------------------------------------
102 */
103 if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
104 {
105 @set_time_limit(300);
106 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000107
108/*
109 * ------------------------------------------------------
110 * Start the timer... tick tock tick tock...
111 * ------------------------------------------------------
112 */
Derek Jones218876c2010-03-02 13:11:00 -0600113 $BM =& load_class('Benchmark', 'core');
114 $BM->mark('total_execution_time_start');
115 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117/*
118 * ------------------------------------------------------
119 * Instantiate the hooks class
120 * ------------------------------------------------------
121 */
Derek Jones218876c2010-03-02 13:11:00 -0600122 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000123
124/*
125 * ------------------------------------------------------
126 * Is there a "pre_system" hook?
127 * ------------------------------------------------------
128 */
Derek Jones218876c2010-03-02 13:11:00 -0600129 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000130
131/*
132 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600133 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Derek Jones218876c2010-03-02 13:11:00 -0600136 $CFG =& load_class('Config', 'core');
137
138 // Do we have any manually set config items in the index.php file?
139 if (isset($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200140 {
Derek Jones218876c2010-03-02 13:11:00 -0600141 $CFG->_assign_to_config($assign_to_config);
142 }
143
144/*
145 * ------------------------------------------------------
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500146 * Instantiate the UTF-8 class
Derek Jones218876c2010-03-02 13:11:00 -0600147 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200148 *
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500149 * Note: Order here is rather important as the UTF-8
Derek Jones218876c2010-03-02 13:11:00 -0600150 * class needs to be used very early on, but it cannot
Barry Mienydd671972010-10-04 16:33:58 +0200151 * properly determine if UTf-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600152 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200153 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 */
155
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500156 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Jones218876c2010-03-02 13:11:00 -0600158/*
159 * ------------------------------------------------------
160 * Instantiate the URI class
161 * ------------------------------------------------------
162 */
163 $URI =& load_class('URI', 'core');
164
165/*
166 * ------------------------------------------------------
167 * Instantiate the routing class and set the routing
168 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200169 */
Derek Jones218876c2010-03-02 13:11:00 -0600170 $RTR =& load_class('Router', 'core');
171 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Jones218876c2010-03-02 13:11:00 -0600173 // Set any routing overrides that may exist in the main index file
174 if (isset($routing))
175 {
176 $RTR->_set_overrides($routing);
177 }
178
179/*
180 * ------------------------------------------------------
181 * Instantiate the output class
182 * ------------------------------------------------------
183 */
184 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000185
186/*
187 * ------------------------------------------------------
188 * Is there a valid cache file? If so, we're done...
189 * ------------------------------------------------------
190 */
Derek Jones218876c2010-03-02 13:11:00 -0600191 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Derek Jones218876c2010-03-02 13:11:00 -0600193 if ($OUT->_display_cache($CFG, $URI) == TRUE)
194 {
195 exit;
196 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
199/*
200 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600201 * 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 * ------------------------------------------------------
208 * Load the Language class
209 * ------------------------------------------------------
210 */
211 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213/*
214 * ------------------------------------------------------
215 * Load the app controller and local controller
216 * ------------------------------------------------------
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 */
Derek Jones218876c2010-03-02 13:11:00 -0600219 // Load the base controller class
220 require BASEPATH.'core/Controller'.EXT;
Barry Mienydd671972010-10-04 16:33:58 +0200221
Greg Aker4abfa682010-11-10 14:44:26 -0600222 function &get_instance()
223 {
224 return CI_Controller::get_instance();
225 }
226
227
Greg Akerfa281352010-03-22 14:41:27 -0500228 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller'.EXT))
229 {
230 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller'.EXT;
231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Jones218876c2010-03-02 13:11:00 -0600233 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200234 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600235 // If this include fails it means that the default controller in the Routes.php file is not resolving to something valid.
236 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
237 {
Pascal Krieteebb6f4b2010-11-10 17:09:21 -0500238 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 -0600239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Jones218876c2010-03-02 13:11:00 -0600241 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Jones218876c2010-03-02 13:11:00 -0600243 // Set a mark point for benchmarking
244 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000245
246/*
247 * ------------------------------------------------------
248 * Security check
249 * ------------------------------------------------------
250 *
251 * None of the functions in the app controller or the
252 * loader class can be called via the URI, nor can
253 * controller functions that begin with an underscore
254 */
Derek Jones218876c2010-03-02 13:11:00 -0600255 $class = $RTR->fetch_class();
256 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Jones218876c2010-03-02 13:11:00 -0600258 if ( ! class_exists($class)
Derek Jones218876c2010-03-02 13:11:00 -0600259 OR strncmp($method, '_', 1) == 0
Greg Aker63277b82010-11-09 13:46:13 -0600260 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
Derek Jones218876c2010-03-02 13:11:00 -0600261 )
262 {
263 show_404("{$class}/{$method}");
264 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000265
266/*
267 * ------------------------------------------------------
268 * Is there a "pre_controller" hook?
269 * ------------------------------------------------------
270 */
Derek Jones218876c2010-03-02 13:11:00 -0600271 $EXT->_call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000272
273/*
274 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600275 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * ------------------------------------------------------
277 */
Derek Jones218876c2010-03-02 13:11:00 -0600278 // Mark a start point so we can benchmark the controller
279 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Jones218876c2010-03-02 13:11:00 -0600281 $CI = new $class();
282
283/*
284 * ------------------------------------------------------
285 * Is there a "post_controller_constructor" hook?
286 * ------------------------------------------------------
287 */
288 $EXT->_call_hook('post_controller_constructor');
289
290/*
291 * ------------------------------------------------------
292 * Call the requested method
293 * ------------------------------------------------------
294 */
295 // Is there a "remap" function? If so, we call it instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 if (method_exists($CI, '_remap'))
297 {
Pascal Kriete3431ae32010-11-09 15:19:50 -0500298 $CI->_remap($method, array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300 else
301 {
302 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
303 // methods, so we'll use this workaround for consistent behavior
304 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
305 {
Eric Barnesc5bf6162011-01-30 21:17:11 -0500306 // Check and see if we are using a 404 override and use it.
307 if ( ! empty($RTR->routes['404_override']))
308 {
309 $x = explode('/', $RTR->routes['404_override']);
310 $class = $x[0];
311 $method = (isset($x[1]) ? $x[1] : 'index');
Eric Barnes5519e3d2011-02-01 13:07:37 -0500312 if ( ! class_exists($class))
313 {
314 if ( ! file_exists(APPPATH.'controllers/'.$class.EXT))
315 {
316 show_404("{$class}/{$method}");
317 }
318
319 include_once(APPPATH.'controllers/'.$class.EXT);
320 unset($CI);
321 $CI = new $class();
322 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500323 }
324 else
325 {
326 show_404("{$class}/{$method}");
327 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329
330 // Call the requested method.
Barry Mienydd671972010-10-04 16:33:58 +0200331 // Any URI segments present (besides the class/function) will be passed to the method for convenience
332 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000334
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Jones218876c2010-03-02 13:11:00 -0600336 // Mark a benchmark end point
337 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
339/*
340 * ------------------------------------------------------
341 * Is there a "post_controller" hook?
342 * ------------------------------------------------------
343 */
Derek Jones218876c2010-03-02 13:11:00 -0600344 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000345
346/*
347 * ------------------------------------------------------
348 * Send the final rendered output to the browser
349 * ------------------------------------------------------
350 */
Derek Jones218876c2010-03-02 13:11:00 -0600351 if ($EXT->_call_hook('display_override') === FALSE)
352 {
353 $OUT->_display();
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356/*
357 * ------------------------------------------------------
358 * Is there a "post_system" hook?
359 * ------------------------------------------------------
360 */
Derek Jones218876c2010-03-02 13:11:00 -0600361 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000362
363/*
364 * ------------------------------------------------------
365 * Close the DB connection if one exists
366 * ------------------------------------------------------
367 */
Derek Jones218876c2010-03-02 13:11:00 -0600368 if (class_exists('CI_DB') AND isset($CI->db))
369 {
370 $CI->db->close();
371 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000372
373
374/* End of file CodeIgniter.php */
Phil Sturgeon35f64912011-03-15 21:01:39 +0000375/* Location: ./system/core/CodeIgniter.php */