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