blob: 57870797292e9b31607079272a2cd2dc894a5dde [file] [log] [blame]
Derek Allard320f3df2007-02-06 04:06:08 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard320f3df2007-02-06 04:06:08 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allard320f3df2007-02-06 04:06:08 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * System Front Controller
20 *
21 * Loads the base classes and executes the request.
22 *
23 * @package CodeIgniter
24 * @subpackage codeigniter
25 * @category Front-controller
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/
28 */
29
30// CI Version
Derek Allard60ca9b72007-07-12 19:53:27 +000031define('CI_VERSION', '1.5.4');
Derek Allard320f3df2007-02-06 04:06:08 +000032
33/*
34 * ------------------------------------------------------
35 * Load the global functions
36 * ------------------------------------------------------
37 */
38require(BASEPATH.'codeigniter/Common'.EXT);
39
40/*
41 * ------------------------------------------------------
42 * Define a custom error handler so we can log PHP errors
43 * ------------------------------------------------------
44 */
45set_error_handler('_exception_handler');
46set_magic_quotes_runtime(0); // Kill magic quotes
47
48/*
49 * ------------------------------------------------------
50 * Start the timer... tick tock tick tock...
51 * ------------------------------------------------------
52 */
53
54$BM =& load_class('Benchmark');
55$BM->mark('total_execution_time_start');
56$BM->mark('loading_time_base_classes_start');
57
58/*
59 * ------------------------------------------------------
60 * Instantiate the hooks class
61 * ------------------------------------------------------
62 */
63
64$EXT =& load_class('Hooks');
65
66/*
67 * ------------------------------------------------------
68 * Is there a "pre_system" hook?
69 * ------------------------------------------------------
70 */
71$EXT->_call_hook('pre_system');
72
73/*
74 * ------------------------------------------------------
75 * Instantiate the base classes
76 * ------------------------------------------------------
77 */
78
79$CFG =& load_class('Config');
Rick Ellis30b40152007-07-20 00:01:13 +000080$URI =& load_class('URI');
Derek Allard320f3df2007-02-06 04:06:08 +000081$RTR =& load_class('Router');
82$OUT =& load_class('Output');
83
84/*
85 * ------------------------------------------------------
86 * Is there a valid cache file? If so, we're done...
87 * ------------------------------------------------------
88 */
89
90if ($EXT->_call_hook('cache_override') === FALSE)
91{
92 if ($OUT->_display_cache($CFG, $RTR) == TRUE)
93 {
94 exit;
95 }
96}
97
98/*
99 * ------------------------------------------------------
100 * Load the remaining base classes
101 * ------------------------------------------------------
102 */
103
104$IN =& load_class('Input');
Derek Allard320f3df2007-02-06 04:06:08 +0000105$LANG =& load_class('Language');
106
107/*
108 * ------------------------------------------------------
109 * Load the app controller and local controller
110 * ------------------------------------------------------
111 *
112 * Note: Due to the poor object handling in PHP 4 we'll
113 * conditionally load different versions of the base
114 * class. Retaining PHP 4 compatibility requires a bit of a hack.
115 *
116 * Note: The Loader class needs to be included first
117 *
118 */
119if (floor(phpversion()) < 5)
120{
121 load_class('Loader', FALSE);
122 require(BASEPATH.'codeigniter/Base4'.EXT);
123}
124else
125{
126 require(BASEPATH.'codeigniter/Base5'.EXT);
127}
128
129// Load the base controller class
130load_class('Controller', FALSE);
131
132// Load the local application controller
133// Note: The Router class automatically validates the controller path. If this include fails it
134// means that the default controller in the Routes.php file is not resolving to something valid.
Rick Ellis6a47c112007-07-21 17:42:35 +0000135if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
Derek Allard320f3df2007-02-06 04:06:08 +0000136{
137 show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
138}
139
Rick Ellis6a47c112007-07-21 17:42:35 +0000140include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
141
Derek Allard320f3df2007-02-06 04:06:08 +0000142// Set a mark point for benchmarking
143$BM->mark('loading_time_base_classes_end');
144
145
146/*
147 * ------------------------------------------------------
148 * Security check
149 * ------------------------------------------------------
150 *
151 * None of the functions in the app controller or the
152 * loader class can be called via the URI, nor can
153 * controller functions that begin with an underscore
154 */
155$class = $RTR->fetch_class();
156$method = $RTR->fetch_method();
157
158
159if ( ! class_exists($class)
160 OR $method == 'controller'
161 OR substr($method, 0, 1) == '_'
162 OR in_array($method, get_class_methods('Controller'), TRUE)
163 )
164{
165 show_404();
166}
167
168/*
169 * ------------------------------------------------------
170 * Is there a "pre_controller" hook?
171 * ------------------------------------------------------
172 */
173$EXT->_call_hook('pre_controller');
174
175/*
176 * ------------------------------------------------------
177 * Instantiate the controller and call requested method
178 * ------------------------------------------------------
179 */
180
181// Mark a start point so we can benchmark the controller
182$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
183
Rick Ellise0a4cc02007-07-22 18:14:32 +0000184// Instantiate the Controller and pass any global data that might exist
Rick Ellisf66c71b2007-07-22 18:22:56 +0000185$assign_to_controller = ( ! isset($assign_to_controller)) ? NULL : $assign_to_controller;
Rick Ellise0a4cc02007-07-22 18:14:32 +0000186
Rick Ellisf66c71b2007-07-22 18:22:56 +0000187$CI = new $class($assign_to_controller);
Derek Allard320f3df2007-02-06 04:06:08 +0000188
Rick Ellisd8de26e2007-07-22 18:24:25 +0000189unset($assign_to_controller);
190
Derek Allard320f3df2007-02-06 04:06:08 +0000191// Is this a scaffolding request?
192if ($RTR->scaffolding_request === TRUE)
193{
194 if ($EXT->_call_hook('scaffolding_override') === FALSE)
195 {
196 $CI->_ci_scaffolding();
197 }
198}
199else
200{
201 /*
202 * ------------------------------------------------------
203 * Is there a "post_controller_constructor" hook?
204 * ------------------------------------------------------
205 */
206 $EXT->_call_hook('post_controller_constructor');
207
208 // Is there a "remap" function?
209 if (method_exists($CI, '_remap'))
210 {
211 $CI->_remap($method);
212 }
213 else
214 {
215 if ( ! method_exists($CI, $method))
216 {
217 show_404();
218 }
219
220 // Call the requested method.
221 // Any URI segments present (besides the class/function) will be passed to the method for convenience
Rick Ellis30b40152007-07-20 00:01:13 +0000222 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3)));
Derek Allard320f3df2007-02-06 04:06:08 +0000223 }
224}
225
226// Mark a benchmark end point
227$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
228
229/*
230 * ------------------------------------------------------
231 * Is there a "post_controller" hook?
232 * ------------------------------------------------------
233 */
234$EXT->_call_hook('post_controller');
235
236/*
237 * ------------------------------------------------------
238 * Send the final rendered output to the browser
239 * ------------------------------------------------------
240 */
241
242if ($EXT->_call_hook('display_override') === FALSE)
243{
244 $OUT->_display();
245}
246
247/*
248 * ------------------------------------------------------
249 * Is there a "post_system" hook?
250 * ------------------------------------------------------
251 */
252$EXT->_call_hook('post_system');
253
254/*
255 * ------------------------------------------------------
256 * Close the DB connection if one exists
257 * ------------------------------------------------------
258 */
259if (class_exists('CI_DB') AND isset($CI->db))
260{
261 $CI->db->close();
262}
263
264
adminb0dd10f2006-08-25 17:25:49 +0000265?>