blob: 16fe97ac35d0b8d37380f2dd43dcc55c951d45cd [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 Allard320f3df2007-02-06 04:06:08 +000010 * @license http://www.codeignitor.com/user_guide/license.html
11 * @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
185$global_data = ( ! isset($global_data)) ? NULL : $global_data;
186
187$CI = new $class($global_data);
Derek Allard320f3df2007-02-06 04:06:08 +0000188
189// Is this a scaffolding request?
190if ($RTR->scaffolding_request === TRUE)
191{
192 if ($EXT->_call_hook('scaffolding_override') === FALSE)
193 {
194 $CI->_ci_scaffolding();
195 }
196}
197else
198{
199 /*
200 * ------------------------------------------------------
201 * Is there a "post_controller_constructor" hook?
202 * ------------------------------------------------------
203 */
204 $EXT->_call_hook('post_controller_constructor');
205
206 // Is there a "remap" function?
207 if (method_exists($CI, '_remap'))
208 {
209 $CI->_remap($method);
210 }
211 else
212 {
213 if ( ! method_exists($CI, $method))
214 {
215 show_404();
216 }
217
218 // Call the requested method.
219 // Any URI segments present (besides the class/function) will be passed to the method for convenience
Rick Ellis30b40152007-07-20 00:01:13 +0000220 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3)));
Derek Allard320f3df2007-02-06 04:06:08 +0000221 }
222}
223
224// Mark a benchmark end point
225$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
226
227/*
228 * ------------------------------------------------------
229 * Is there a "post_controller" hook?
230 * ------------------------------------------------------
231 */
232$EXT->_call_hook('post_controller');
233
234/*
235 * ------------------------------------------------------
236 * Send the final rendered output to the browser
237 * ------------------------------------------------------
238 */
239
240if ($EXT->_call_hook('display_override') === FALSE)
241{
242 $OUT->_display();
243}
244
245/*
246 * ------------------------------------------------------
247 * Is there a "post_system" hook?
248 * ------------------------------------------------------
249 */
250$EXT->_call_hook('post_system');
251
252/*
253 * ------------------------------------------------------
254 * Close the DB connection if one exists
255 * ------------------------------------------------------
256 */
257if (class_exists('CI_DB') AND isset($CI->db))
258{
259 $CI->db->close();
260}
261
262
adminb0dd10f2006-08-25 17:25:49 +0000263?>