admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * System Front Controller |
| 20 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 21 | * Loads the base classes and executes the request. |
| 22 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 23 | * @package CodeIgniter |
| 24 | * @subpackage codeigniter |
| 25 | * @category Front-controller |
| 26 | * @author Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/ |
| 28 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 29 | |
admin | 1b90c27 | 2006-10-31 18:22:29 +0000 | [diff] [blame] | 30 | // CI Version |
| 31 | define('APPVER', '1.5.0.1'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * ------------------------------------------------------ |
| 35 | * Load the global functions |
| 36 | * ------------------------------------------------------ |
| 37 | */ |
| 38 | require(BASEPATH.'codeigniter/Common'.EXT); |
| 39 | |
| 40 | /* |
| 41 | * ------------------------------------------------------ |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 42 | * Define a custom error handler so we can log PHP errors |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 43 | * ------------------------------------------------------ |
| 44 | */ |
| 45 | set_error_handler('_exception_handler'); |
| 46 | set_magic_quotes_runtime(0); // Kill magic quotes |
| 47 | |
| 48 | /* |
| 49 | * ------------------------------------------------------ |
| 50 | * Start the timer... tick tock tick tock... |
| 51 | * ------------------------------------------------------ |
| 52 | */ |
| 53 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 54 | $BM =& load_class('Benchmark'); |
admin | e26611f | 2006-10-02 21:59:12 +0000 | [diff] [blame] | 55 | $BM->mark('total_execution_time_start'); |
admin | afe3aaa | 2006-10-20 22:22:55 +0000 | [diff] [blame] | 56 | $BM->mark('loading_time_base_classes_start'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * ------------------------------------------------------ |
admin | afe3aaa | 2006-10-20 22:22:55 +0000 | [diff] [blame] | 60 | * Instantiate the hooks class |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 61 | * ------------------------------------------------------ |
| 62 | */ |
| 63 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 64 | $EXT =& load_class('Hooks'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * ------------------------------------------------------ |
| 68 | * Is there a "pre_system" hook? |
| 69 | * ------------------------------------------------------ |
| 70 | */ |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 71 | $EXT->_call_hook('pre_system'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * ------------------------------------------------------ |
| 75 | * Instantiate the base classes |
| 76 | * ------------------------------------------------------ |
| 77 | */ |
| 78 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 79 | $CFG =& load_class('Config'); |
| 80 | $RTR =& load_class('Router'); |
| 81 | $OUT =& load_class('Output'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * ------------------------------------------------------ |
| 85 | * Is there a valid cache file? If so, we're done... |
| 86 | * ------------------------------------------------------ |
| 87 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 88 | |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 89 | if ($EXT->_call_hook('cache_override') === FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 90 | { |
admin | 06508c4 | 2006-09-18 08:18:08 +0000 | [diff] [blame] | 91 | if ($OUT->_display_cache($CFG, $RTR) == TRUE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 92 | { |
| 93 | exit; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * ------------------------------------------------------ |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 99 | * Load the remaining base classes |
| 100 | * ------------------------------------------------------ |
| 101 | */ |
| 102 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 103 | $IN =& load_class('Input'); |
| 104 | $URI =& load_class('URI'); |
| 105 | $LANG =& load_class('Language'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * ------------------------------------------------------ |
| 109 | * Load the app controller and local controller |
| 110 | * ------------------------------------------------------ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 111 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 112 | * Note: Due to the poor object handling in PHP 4 we'll |
admin | afe3aaa | 2006-10-20 22:22:55 +0000 | [diff] [blame] | 113 | * conditionally load different versions of the base |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 114 | * class. Retaining PHP 4 compatibility requires a bit of a hack. |
| 115 | * |
| 116 | * Note: The Loader class needs to be included first |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 117 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 118 | */ |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 119 | if (floor(phpversion()) < 5) |
| 120 | { |
admin | cef2106 | 2006-10-30 17:13:13 +0000 | [diff] [blame] | 121 | load_class('Loader', FALSE); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 122 | require(BASEPATH.'codeigniter/Base4'.EXT); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | require(BASEPATH.'codeigniter/Base5'.EXT); |
| 127 | } |
| 128 | |
admin | b93464d | 2006-10-31 00:36:32 +0000 | [diff] [blame] | 129 | // Load the base controller class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 130 | load_class('Controller', FALSE); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 131 | |
admin | b93464d | 2006-10-31 00:36:32 +0000 | [diff] [blame] | 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. |
admin | 40d299e | 2006-11-04 05:07:59 +0000 | [diff] [blame^] | 135 | if ( ! include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT)) |
admin | b93464d | 2006-10-31 00:36:32 +0000 | [diff] [blame] | 136 | { |
| 137 | show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.'); |
| 138 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 139 | |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 140 | // Set a mark point for benchmarking |
admin | afe3aaa | 2006-10-20 22:22:55 +0000 | [diff] [blame] | 141 | $BM->mark('loading_time_base_classes_end'); |
admin | f6edc53 | 2006-10-03 05:28:09 +0000 | [diff] [blame] | 142 | |
| 143 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 144 | /* |
| 145 | * ------------------------------------------------------ |
| 146 | * Security check |
| 147 | * ------------------------------------------------------ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 148 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 149 | * None of the functions in the app controller or the |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 150 | * loader class can be called via the URI, nor can |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 151 | * controller functions that begin with an underscore |
| 152 | */ |
| 153 | $class = $RTR->fetch_class(); |
| 154 | $method = $RTR->fetch_method(); |
| 155 | |
admin | f6edc53 | 2006-10-03 05:28:09 +0000 | [diff] [blame] | 156 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 157 | if ( ! class_exists($class) |
| 158 | OR $method == 'controller' |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 159 | OR substr($method, 0, 1) == '_' |
admin | ee54c11 | 2006-09-28 17:13:38 +0000 | [diff] [blame] | 160 | OR in_array($method, get_class_methods('Controller'), TRUE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 161 | ) |
| 162 | { |
| 163 | show_404(); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * ------------------------------------------------------ |
| 168 | * Is there a "pre_controller" hook? |
| 169 | * ------------------------------------------------------ |
| 170 | */ |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 171 | $EXT->_call_hook('pre_controller'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * ------------------------------------------------------ |
| 175 | * Instantiate the controller and call requested method |
| 176 | * ------------------------------------------------------ |
| 177 | */ |
admin | f6edc53 | 2006-10-03 05:28:09 +0000 | [diff] [blame] | 178 | |
| 179 | // Mark a start point so we can benchmark the controller |
| 180 | $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start'); |
| 181 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 182 | // Instantiate the Controller |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 183 | $CI = new $class(); |
| 184 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 185 | // Is this a scaffolding request? |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 186 | if ($RTR->scaffolding_request === TRUE) |
| 187 | { |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 188 | if ($EXT->_call_hook('scaffolding_override') === FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 189 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 190 | $CI->_ci_scaffolding(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | else |
| 194 | { |
admin | eb567c7 | 2006-09-06 02:48:47 +0000 | [diff] [blame] | 195 | /* |
| 196 | * ------------------------------------------------------ |
| 197 | * Is there a "post_controller_constructor" hook? |
| 198 | * ------------------------------------------------------ |
| 199 | */ |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 200 | $EXT->_call_hook('post_controller_constructor'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 201 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 202 | // Is there a "remap" function? |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 203 | if (method_exists($CI, '_remap')) |
| 204 | { |
| 205 | $CI->_remap($method); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | if ( ! method_exists($CI, $method)) |
| 210 | { |
| 211 | show_404(); |
| 212 | } |
admin | 8407cca | 2006-09-23 01:22:56 +0000 | [diff] [blame] | 213 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 214 | // Call the requested method. |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 215 | // Any URI segments present (besides the class/function) will be passed to the method for convenience |
admin | 7d85288 | 2006-09-24 01:33:56 +0000 | [diff] [blame] | 216 | call_user_func_array(array(&$CI, $method), array_slice($RTR->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3))); |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 217 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 218 | } |
| 219 | |
admin | f6edc53 | 2006-10-03 05:28:09 +0000 | [diff] [blame] | 220 | // Mark a benchmark end point |
| 221 | $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end'); |
| 222 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 223 | /* |
| 224 | * ------------------------------------------------------ |
| 225 | * Is there a "post_controller" hook? |
| 226 | * ------------------------------------------------------ |
| 227 | */ |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 228 | $EXT->_call_hook('post_controller'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * ------------------------------------------------------ |
| 232 | * Send the final rendered output to the browser |
| 233 | * ------------------------------------------------------ |
| 234 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 235 | |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 236 | if ($EXT->_call_hook('display_override') === FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 237 | { |
| 238 | $OUT->_display(); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * ------------------------------------------------------ |
| 243 | * Is there a "post_system" hook? |
| 244 | * ------------------------------------------------------ |
| 245 | */ |
admin | af436d7 | 2006-09-15 20:02:14 +0000 | [diff] [blame] | 246 | $EXT->_call_hook('post_system'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * ------------------------------------------------------ |
admin | 606f99c | 2006-10-11 23:48:41 +0000 | [diff] [blame] | 250 | * Close the DB connection if one exists |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 251 | * ------------------------------------------------------ |
| 252 | */ |
admin | 5a14ea1 | 2006-10-12 20:42:55 +0000 | [diff] [blame] | 253 | if (class_exists('CI_DB') AND isset($CI->db)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 254 | { |
| 255 | $CI->db->close(); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | ?> |