blob: 295917c925dcc7806b022deb0300e8a0cf6cc54d [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');
64
65 if ( ! is_php('5.3'))
66 {
67 @set_magic_quotes_runtime(0); // Kill magic quotes
68 }
Derek Jones962d2252009-08-05 04:26:11 +000069
Derek Jones218876c2010-03-02 13:11:00 -060070 // Set a liberal script execution time limit
71 if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
72 {
73 @set_time_limit(300);
74 }
75
76/*
77 * ------------------------------------------------------
78 * Set the subclass_prefix
79 * ------------------------------------------------------
80 *
81 * Normally the "subclass_prefix" is set in the config file.
82 * The subclass prefix allows CI to know if a core class is
83 * being extended via a library in the local application
84 * "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
87 * override exists. If so, we will set this value now,
88 * before any classes are loaded
89 * Note: Since the config file data is cached it doesn't
90 * hurt to load it here.
91 */
92 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
93 {
94 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
95 }
Derek Allard2067d1a2008-11-13 22:59:24 +000096
97/*
98 * ------------------------------------------------------
99 * Start the timer... tick tock tick tock...
100 * ------------------------------------------------------
101 */
Derek Jones218876c2010-03-02 13:11:00 -0600102 $BM =& load_class('Benchmark', 'core');
103 $BM->mark('total_execution_time_start');
104 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
106/*
107 * ------------------------------------------------------
108 * Instantiate the hooks class
109 * ------------------------------------------------------
110 */
Derek Jones218876c2010-03-02 13:11:00 -0600111 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000112
113/*
114 * ------------------------------------------------------
115 * Is there a "pre_system" hook?
116 * ------------------------------------------------------
117 */
Derek Jones218876c2010-03-02 13:11:00 -0600118 $EXT->_call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000119
120/*
121 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600122 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600124 */
125 $CFG =& load_class('Config', 'core');
126
127 // Do we have any manually set config items in the index.php file?
128 if (isset($assign_to_config))
129 {
130 $CFG->_assign_to_config($assign_to_config);
131 }
132
133/*
134 * ------------------------------------------------------
135 * Instantiate the Unicode class
136 * ------------------------------------------------------
137 *
138 * Note: Order here is rather important as the Unicode
139 * class needs to be used very early on, but it cannot
140 * properly determine if UTf-8 can be supported until
141 * after the Config class is instantiated.
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 */
144
Derek Jones218876c2010-03-02 13:11:00 -0600145 $UNI =& load_class('Unicode', 'core');
146
147/*
148 * ------------------------------------------------------
149 * Instantiate the URI class
150 * ------------------------------------------------------
151 */
152 $URI =& load_class('URI', 'core');
153
154/*
155 * ------------------------------------------------------
156 * Instantiate the routing class and set the routing
157 * ------------------------------------------------------
158 */
159 $RTR =& load_class('Router', 'core');
160 $RTR->_set_routing();
161
162 // Set any routing overrides that may exist in the main index file
163 if (isset($routing))
164 {
165 $RTR->_set_overrides($routing);
166 }
167
168/*
169 * ------------------------------------------------------
170 * Instantiate the output class
171 * ------------------------------------------------------
172 */
173 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000174
175/*
176 * ------------------------------------------------------
177 * Is there a valid cache file? If so, we're done...
178 * ------------------------------------------------------
179 */
Derek Jones218876c2010-03-02 13:11:00 -0600180 if ($EXT->_call_hook('cache_override') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Derek Jones218876c2010-03-02 13:11:00 -0600182 if ($OUT->_display_cache($CFG, $URI) == TRUE)
183 {
184 exit;
185 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000187
188/*
189 * ------------------------------------------------------
Derek Jones218876c2010-03-02 13:11:00 -0600190 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * ------------------------------------------------------
192 */
Derek Jones218876c2010-03-02 13:11:00 -0600193 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
Derek Jones218876c2010-03-02 13:11:00 -0600195/*
196 * ------------------------------------------------------
197 * Load the Language class
198 * ------------------------------------------------------
199 */
200 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
202/*
203 * ------------------------------------------------------
204 * Load the app controller and local controller
205 * ------------------------------------------------------
206 *
207 * Note: Due to the poor object handling in PHP 4 we'll
208 * conditionally load different versions of the base
209 * class. Retaining PHP 4 compatibility requires a bit of a hack.
Derek Jones1efda7b2010-03-02 13:30:20 -0600210 * @PHP4
211 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 */
Derek Jones218876c2010-03-02 13:11:00 -0600213 if (is_php('5.0.0') == TRUE)
214 {
215 require(BASEPATH.'core/Base5'.EXT);
216 }
217 else
218 {
219 // The Loader class needs to be included first when running PHP 4.x
220 load_class('Loader', 'core');
221 require(BASEPATH.'core/Base4'.EXT);
222 }
223
224 // Load the base controller class
225 require BASEPATH.'core/Controller'.EXT;
226
Greg Akerfa281352010-03-22 14:41:27 -0500227 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller'.EXT))
228 {
229 require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller'.EXT;
230 }
231
Derek Jones218876c2010-03-02 13:11:00 -0600232 // Load the local application controller
233 // Note: The Router class automatically validates the controller path using the router->_validate_request().
234 // If this include fails it means that the default controller in the Routes.php file is not resolving to something valid.
235 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
236 {
237 show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
238 }
239
240 include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
241
242 // Set a mark point for benchmarking
243 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000244
245/*
246 * ------------------------------------------------------
247 * Security check
248 * ------------------------------------------------------
249 *
250 * None of the functions in the app controller or the
251 * loader class can be called via the URI, nor can
252 * controller functions that begin with an underscore
253 */
Derek Jones218876c2010-03-02 13:11:00 -0600254 $class = $RTR->fetch_class();
255 $method = $RTR->fetch_method();
256
257 if ( ! class_exists($class)
258 OR $method == 'controller'
259 OR strncmp($method, '_', 1) == 0
260 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('Controller')))
261 )
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');
Derek Allard2067d1a2008-11-13 22:59:24 +0000280
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 {
298 $CI->_remap($method);
299 }
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 {
306 show_404("{$class}/{$method}");
307 }
308
309 // Call the requested method.
Derek Jones218876c2010-03-02 13:11:00 -0600310 // Any URI segments present (besides the class/function) will be passed to the method for convenience
311 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000313
Derek Jones218876c2010-03-02 13:11:00 -0600314
315 // Mark a benchmark end point
316 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000317
318/*
319 * ------------------------------------------------------
320 * Is there a "post_controller" hook?
321 * ------------------------------------------------------
322 */
Derek Jones218876c2010-03-02 13:11:00 -0600323 $EXT->_call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000324
325/*
326 * ------------------------------------------------------
327 * Send the final rendered output to the browser
328 * ------------------------------------------------------
329 */
Derek Jones218876c2010-03-02 13:11:00 -0600330 if ($EXT->_call_hook('display_override') === FALSE)
331 {
332 $OUT->_display();
333 }
334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335/*
336 * ------------------------------------------------------
337 * Is there a "post_system" hook?
338 * ------------------------------------------------------
339 */
Derek Jones218876c2010-03-02 13:11:00 -0600340 $EXT->_call_hook('post_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000341
342/*
343 * ------------------------------------------------------
344 * Close the DB connection if one exists
345 * ------------------------------------------------------
346 */
Derek Jones218876c2010-03-02 13:11:00 -0600347 if (class_exists('CI_DB') AND isset($CI->db))
348 {
349 $CI->db->close();
350 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
352
353/* End of file CodeIgniter.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600354/* Location: ./system/core/CodeIgniter.php */