blob: e701cc3236baf6d83d6cf6cff56c568e89e12469 [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 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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 */
35 define('CI_VERSION', '2.0');
Derek Allard2067d1a2008-11-13 22:59:24 +000036
37/*
38 * ------------------------------------------------------
39 * Load the global functions
40 * ------------------------------------------------------
41 */
Derek Jones218876c2010-03-02 13:11:00 -060042 require(BASEPATH.'core/Common'.EXT);
Derek Allard2067d1a2008-11-13 22:59:24 +000043
44/*
45 * ------------------------------------------------------
46 * Load the compatibility override functions
47 * ------------------------------------------------------
48 */
Derek Jones218876c2010-03-02 13:11:00 -060049 require(BASEPATH.'core/Compat'.EXT);
Derek Allard2067d1a2008-11-13 22:59:24 +000050
51/*
52 * ------------------------------------------------------
53 * Load the framework constants
54 * ------------------------------------------------------
55 */
Derek Jones218876c2010-03-02 13:11:00 -060056 require(APPPATH.'config/constants'.EXT);
Derek Allard2067d1a2008-11-13 22:59:24 +000057
58/*
59 * ------------------------------------------------------
60 * Define a custom error handler so we can log PHP errors
61 * ------------------------------------------------------
62 */
Derek Jones218876c2010-03-02 13:11:00 -060063 set_error_handler('_exception_handler');
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Jones218876c2010-03-02 13:11:00 -060065 if ( ! is_php('5.3'))
66 {
Barry Mienydd671972010-10-04 16:33:58 +020067 @set_magic_quotes_runtime(0); // Kill magic quotes
Derek Jones218876c2010-03-02 13:11:00 -060068 }
Derek Jones962d2252009-08-05 04:26:11 +000069
Derek Jones218876c2010-03-02 13:11:00 -060070/*
71 * ------------------------------------------------------
72 * Set the subclass_prefix
73 * ------------------------------------------------------
74 *
Barry Mienydd671972010-10-04 16:33:58 +020075 * Normally the "subclass_prefix" is set in the config file.
76 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -060077 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +020078 * "libraries" folder. Since CI allows config items to be
79 * overriden via data set in the main index. php file,
80 * before proceeding we need to know if a subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -060081 * override exists. If so, we will set this value now,
82 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +020083 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -060084 * hurt to load it here.
85 */
Barry Mienydd671972010-10-04 16:33:58 +020086 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
Derek Jones218876c2010-03-02 13:11:00 -060087 {
88 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
89 }
Pascal Krietef566af52010-11-09 13:03:26 -050090
91/*
92 * ------------------------------------------------------
93 * Set a liberal script execution time limit
94 * ------------------------------------------------------
95 */
96 if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
97 {
98 @set_time_limit(300);
99 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000100
101/*
102 * ------------------------------------------------------
103 * Start the timer... tick tock tick tock...
104 * ------------------------------------------------------
105 */
Derek Jones218876c2010-03-02 13:11:00 -0600106 $BM =& load_class('Benchmark', 'core');
107 $BM->mark('total_execution_time_start');
108 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
110/*
111 * ------------------------------------------------------
112 * Instantiate the hooks class
113 * ------------------------------------------------------
114 */
Derek Jones218876c2010-03-02 13:11:00 -0600115 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117/*
118 * ------------------------------------------------------
119 * Is there a "pre_system" hook?
120 * ------------------------------------------------------
121 */
Derek Jones218876c2010-03-02 13:11:00 -0600122 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000123
124/*
125 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600126 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200128 */
Derek Jones218876c2010-03-02 13:11:00 -0600129 $CFG =& load_class('Config', 'core');
130
131 // Do we have any manually set config items in the index.php file?
132 if (isset($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200133 {
Derek Jones218876c2010-03-02 13:11:00 -0600134 $CFG->_assign_to_config($assign_to_config);
135 }
136
137/*
138 * ------------------------------------------------------
139 * Instantiate the Unicode class
140 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200141 *
Derek Jones218876c2010-03-02 13:11:00 -0600142 * Note: Order here is rather important as the Unicode
143 * class needs to be used very early on, but it cannot
Barry Mienydd671972010-10-04 16:33:58 +0200144 * properly determine if UTf-8 can be supported until
Derek Jones218876c2010-03-02 13:11:00 -0600145 * after the Config class is instantiated.
Barry Mienydd671972010-10-04 16:33:58 +0200146 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 */
148
Derek Jones218876c2010-03-02 13:11:00 -0600149 $UNI =& load_class('Unicode', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Jones218876c2010-03-02 13:11:00 -0600151/*
152 * ------------------------------------------------------
153 * Instantiate the URI class
154 * ------------------------------------------------------
155 */
156 $URI =& load_class('URI', 'core');
157
158/*
159 * ------------------------------------------------------
160 * Instantiate the routing class and set the routing
161 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200162 */
Derek Jones218876c2010-03-02 13:11:00 -0600163 $RTR =& load_class('Router', 'core');
164 $RTR->_set_routing();
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jones218876c2010-03-02 13:11:00 -0600166 // Set any routing overrides that may exist in the main index file
167 if (isset($routing))
168 {
169 $RTR->_set_overrides($routing);
170 }
171
172/*
173 * ------------------------------------------------------
174 * Instantiate the output class
175 * ------------------------------------------------------
176 */
177 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000178
179/*
180 * ------------------------------------------------------
181 * Is there a valid cache file? If so, we're done...
182 * ------------------------------------------------------
183 */
Derek Jones218876c2010-03-02 13:11:00 -0600184 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Derek Jones218876c2010-03-02 13:11:00 -0600186 if ($OUT->_display_cache($CFG, $URI) == TRUE)
187 {
188 exit;
189 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
192/*
193 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600194 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * ------------------------------------------------------
196 */
Barry Mienydd671972010-10-04 16:33:58 +0200197 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
Derek Jones218876c2010-03-02 13:11:00 -0600199/*
200 * ------------------------------------------------------
201 * Load the Language class
202 * ------------------------------------------------------
203 */
204 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
206/*
207 * ------------------------------------------------------
208 * Load the app controller and local controller
209 * ------------------------------------------------------
210 *
211 * Note: Due to the poor object handling in PHP 4 we'll
212 * conditionally load different versions of the base
213 * class. Retaining PHP 4 compatibility requires a bit of a hack.
Derek Jones1efda7b2010-03-02 13:30:20 -0600214 * @PHP4
Barry Mienydd671972010-10-04 16:33:58 +0200215 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 */
Barry Mienydd671972010-10-04 16:33:58 +0200217 if (is_php('5.0.0') == TRUE)
Derek Jones218876c2010-03-02 13:11:00 -0600218 {
219 require(BASEPATH.'core/Base5'.EXT);
220 }
221 else
222 {
223 // The Loader class needs to be included first when running PHP 4.x
224 load_class('Loader', 'core');
225 require(BASEPATH.'core/Base4'.EXT);
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Jones218876c2010-03-02 13:11:00 -0600228 // Load the base controller class
229 require BASEPATH.'core/Controller'.EXT;
Barry Mienydd671972010-10-04 16:33:58 +0200230
Greg Akerfa281352010-03-22 14:41:27 -0500231 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller'.EXT))
232 {
233 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller'.EXT;
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Jones218876c2010-03-02 13:11:00 -0600236 // Load the local application controller
Barry Mienydd671972010-10-04 16:33:58 +0200237 // Note: The Router class automatically validates the controller path using the router->_validate_request().
Derek Jones218876c2010-03-02 13:11:00 -0600238 // If this include fails it means that the default controller in the Routes.php file is not resolving to something valid.
239 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
240 {
241 show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Jones218876c2010-03-02 13:11:00 -0600244 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Jones218876c2010-03-02 13:11:00 -0600246 // Set a mark point for benchmarking
247 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
249/*
250 * ------------------------------------------------------
251 * Security check
252 * ------------------------------------------------------
253 *
254 * None of the functions in the app controller or the
255 * loader class can be called via the URI, nor can
256 * controller functions that begin with an underscore
257 */
Derek Jones218876c2010-03-02 13:11:00 -0600258 $class = $RTR->fetch_class();
259 $method = $RTR->fetch_method();
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Jones218876c2010-03-02 13:11:00 -0600261 if ( ! class_exists($class)
262 OR $method == 'controller'
263 OR strncmp($method, '_', 1) == 0
264 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('Controller')))
265 )
266 {
267 show_404("{$class}/{$method}");
268 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
270/*
271 * ------------------------------------------------------
272 * Is there a "pre_controller" hook?
273 * ------------------------------------------------------
274 */
Derek Jones218876c2010-03-02 13:11:00 -0600275 $EXT->_call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
277/*
278 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600279 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * ------------------------------------------------------
281 */
Derek Jones218876c2010-03-02 13:11:00 -0600282 // Mark a start point so we can benchmark the controller
283 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Jones218876c2010-03-02 13:11:00 -0600285 $CI = new $class();
286
287/*
288 * ------------------------------------------------------
289 * Is there a "post_controller_constructor" hook?
290 * ------------------------------------------------------
291 */
292 $EXT->_call_hook('post_controller_constructor');
293
294/*
295 * ------------------------------------------------------
296 * Call the requested method
297 * ------------------------------------------------------
298 */
299 // Is there a "remap" function? If so, we call it instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 if (method_exists($CI, '_remap'))
301 {
302 $CI->_remap($method);
303 }
304 else
305 {
306 // is_callable() returns TRUE on some versions of PHP 5 for private and protected
307 // methods, so we'll use this workaround for consistent behavior
308 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
309 {
310 show_404("{$class}/{$method}");
311 }
312
313 // Call the requested method.
Barry Mienydd671972010-10-04 16:33:58 +0200314 // Any URI segments present (besides the class/function) will be passed to the method for convenience
315 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000317
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Jones218876c2010-03-02 13:11:00 -0600319 // Mark a benchmark end point
320 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000321
322/*
323 * ------------------------------------------------------
324 * Is there a "post_controller" hook?
325 * ------------------------------------------------------
326 */
Derek Jones218876c2010-03-02 13:11:00 -0600327 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000328
329/*
330 * ------------------------------------------------------
331 * Send the final rendered output to the browser
332 * ------------------------------------------------------
333 */
Derek Jones218876c2010-03-02 13:11:00 -0600334 if ($EXT->_call_hook('display_override') === FALSE)
335 {
336 $OUT->_display();
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339/*
340 * ------------------------------------------------------
341 * Is there a "post_system" hook?
342 * ------------------------------------------------------
343 */
Derek Jones218876c2010-03-02 13:11:00 -0600344 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000345
346/*
347 * ------------------------------------------------------
348 * Close the DB connection if one exists
349 * ------------------------------------------------------
350 */
Derek Jones218876c2010-03-02 13:11:00 -0600351 if (class_exists('CI_DB') AND isset($CI->db))
352 {
353 $CI->db->close();
354 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000355
356
357/* End of file CodeIgniter.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600358/* Location: ./system/core/CodeIgniter.php */