Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 9c5c24a | 2012-01-07 18:51:21 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, British Columbia Institute of Technology |
Andrey Andreev | 9c5c24a | 2012-01-07 18:51:21 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 41 | * System Initialization File |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * |
| 43 | * Loads the base classes and executes the request. |
| 44 | * |
| 45 | * @package CodeIgniter |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 46 | * @subpackage CodeIgniter |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @category Front-controller |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 52 | /** |
| 53 | * CodeIgniter Version |
| 54 | * |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 55 | * @var string |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 56 | * |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 57 | */ |
Andrey Andreev | 4307bff | 2016-01-13 02:13:10 +0200 | [diff] [blame] | 58 | define('CI_VERSION', '3.0.5-dev'); |
Andrey Andreev | b6fbcbe | 2013-11-17 18:06:18 +0200 | [diff] [blame] | 59 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | /* |
| 61 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 62 | * Load the framework constants |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | * ------------------------------------------------------ |
| 64 | */ |
Andrey Andreev | db529ca | 2013-01-28 11:00:02 +0200 | [diff] [blame] | 65 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 66 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 67 | require_once(APPPATH.'config/'.ENVIRONMENT.'/constants.php'); |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 68 | } |
Andrey Andreev | b6fbcbe | 2013-11-17 18:06:18 +0200 | [diff] [blame] | 69 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 70 | require_once(APPPATH.'config/constants.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * ------------------------------------------------------ |
josephok | 1095d11 | 2013-11-16 09:33:37 +0800 | [diff] [blame] | 74 | * Load the global functions |
| 75 | * ------------------------------------------------------ |
| 76 | */ |
| 77 | require_once(BASEPATH.'core/Common.php'); |
| 78 | |
Andrey Andreev | b78a8c7 | 2014-04-15 17:21:16 +0300 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * ------------------------------------------------------ |
| 82 | * Security procedures |
| 83 | * ------------------------------------------------------ |
| 84 | */ |
| 85 | |
| 86 | if ( ! is_php('5.4')) |
| 87 | { |
| 88 | ini_set('magic_quotes_runtime', 0); |
| 89 | |
| 90 | if ((bool) ini_get('register_globals')) |
| 91 | { |
| 92 | $_protected = array( |
| 93 | '_SERVER', |
| 94 | '_GET', |
| 95 | '_POST', |
| 96 | '_FILES', |
| 97 | '_REQUEST', |
| 98 | '_SESSION', |
| 99 | '_ENV', |
| 100 | '_COOKIE', |
| 101 | 'GLOBALS', |
| 102 | 'HTTP_RAW_POST_DATA', |
vlakoff | de70dbb | 2014-04-15 16:46:03 +0200 | [diff] [blame] | 103 | 'system_path', |
Andrey Andreev | b78a8c7 | 2014-04-15 17:21:16 +0300 | [diff] [blame] | 104 | 'application_folder', |
| 105 | 'view_folder', |
| 106 | '_protected', |
| 107 | '_registered' |
| 108 | ); |
| 109 | |
| 110 | $_registered = ini_get('variables_order'); |
| 111 | foreach (array('E' => '_ENV', 'G' => '_GET', 'P' => '_POST', 'C' => '_COOKIE', 'S' => '_SERVER') as $key => $superglobal) |
| 112 | { |
| 113 | if (strpos($_registered, $key) === FALSE) |
| 114 | { |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | foreach (array_keys($$superglobal) as $var) |
| 119 | { |
| 120 | if (isset($GLOBALS[$var]) && ! in_array($var, $_protected, TRUE)) |
| 121 | { |
| 122 | $GLOBALS[$var] = NULL; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
josephok | 1095d11 | 2013-11-16 09:33:37 +0800 | [diff] [blame] | 130 | /* |
| 131 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 132 | * Define a custom error handler so we can log PHP errors |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | * ------------------------------------------------------ |
| 134 | */ |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 135 | set_error_handler('_error_handler'); |
| 136 | set_exception_handler('_exception_handler'); |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 137 | register_shutdown_function('_shutdown_handler'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 139 | /* |
| 140 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 141 | * Set the subclass_prefix |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 142 | * ------------------------------------------------------ |
| 143 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | * Normally the "subclass_prefix" is set in the config file. |
| 145 | * The subclass prefix allows CI to know if a core class is |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 146 | * being extended via a library in the local application |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | * "libraries" folder. Since CI allows config items to be |
Claudio Galdiolo | 72d63cd | 2014-12-22 15:18:14 -0500 | [diff] [blame] | 148 | * overridden via data set in the main index.php file, |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | * before proceeding we need to know if a subclass_prefix |
Andrey Andreev | 9c5c24a | 2012-01-07 18:51:21 +0200 | [diff] [blame] | 150 | * override exists. If so, we will set this value now, |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 151 | * before any classes are loaded |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | * Note: Since the config file data is cached it doesn't |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 153 | * hurt to load it here. |
| 154 | */ |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 155 | if ( ! empty($assign_to_config['subclass_prefix'])) |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 156 | { |
| 157 | get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix'])); |
| 158 | } |
Eric Barnes | c5bf616 | 2011-01-30 21:17:11 -0500 | [diff] [blame] | 159 | |
Pascal Kriete | f566af5 | 2010-11-09 13:03:26 -0500 | [diff] [blame] | 160 | /* |
| 161 | * ------------------------------------------------------ |
Andrey Andreev | 1725432 | 2015-01-30 15:53:28 +0200 | [diff] [blame] | 162 | * Should we use a Composer autoloader? |
| 163 | * ------------------------------------------------------ |
| 164 | */ |
| 165 | if ($composer_autoload = config_item('composer_autoload')) |
| 166 | { |
Andrey Andreev | 2eb580c | 2015-01-30 16:41:43 +0200 | [diff] [blame] | 167 | if ($composer_autoload === TRUE) |
Andrey Andreev | 1725432 | 2015-01-30 15:53:28 +0200 | [diff] [blame] | 168 | { |
Andrey Andreev | 2eb580c | 2015-01-30 16:41:43 +0200 | [diff] [blame] | 169 | file_exists(APPPATH.'vendor/autoload.php') |
| 170 | ? require_once(APPPATH.'vendor/autoload.php') |
| 171 | : log_message('error', '$config[\'composer_autoload\'] is set to TRUE but '.APPPATH.'vendor/autoload.php was not found.'); |
Andrey Andreev | 1725432 | 2015-01-30 15:53:28 +0200 | [diff] [blame] | 172 | } |
| 173 | elseif (file_exists($composer_autoload)) |
| 174 | { |
| 175 | require_once($composer_autoload); |
| 176 | } |
Andrey Andreev | 2eb580c | 2015-01-30 16:41:43 +0200 | [diff] [blame] | 177 | else |
| 178 | { |
| 179 | log_message('error', 'Could not find the specified $config[\'composer_autoload\'] path: '.$composer_autoload); |
| 180 | } |
Andrey Andreev | 1725432 | 2015-01-30 15:53:28 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
| 184 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 185 | * Start the timer... tick tock tick tock... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | * ------------------------------------------------------ |
| 187 | */ |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 188 | $BM =& load_class('Benchmark', 'core'); |
| 189 | $BM->mark('total_execution_time_start'); |
| 190 | $BM->mark('loading_time:_base_classes_start'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 194 | * Instantiate the hooks class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | * ------------------------------------------------------ |
| 196 | */ |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 197 | $EXT =& load_class('Hooks', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 201 | * Is there a "pre_system" hook? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | * ------------------------------------------------------ |
| 203 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 204 | $EXT->call_hook('pre_system'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 208 | * Instantiate the config class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | * ------------------------------------------------------ |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 210 | * |
| 211 | * Note: It is important that Config is loaded first as |
| 212 | * most other classes depend on it either directly or by |
| 213 | * depending on another class that uses it. |
| 214 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 215 | */ |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 216 | $CFG =& load_class('Config', 'core'); |
| 217 | |
| 218 | // Do we have any manually set config items in the index.php file? |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 219 | if (isset($assign_to_config) && is_array($assign_to_config)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | { |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 221 | foreach ($assign_to_config as $key => $value) |
| 222 | { |
| 223 | $CFG->set_item($key, $value); |
| 224 | } |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * ------------------------------------------------------ |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 229 | * Important charset-related stuff |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 230 | * ------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | * |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 232 | * Configure mbstring and/or iconv if they are enabled |
| 233 | * and set MB_ENABLED and ICONV_ENABLED constants, so |
| 234 | * that we don't repeatedly do extension_loaded() or |
| 235 | * function_exists() calls. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | * |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 237 | * Note: UTF-8 class depends on this. It used to be done |
| 238 | * in it's constructor, but it's _not_ class-specific. |
| 239 | * |
| 240 | */ |
| 241 | $charset = strtoupper(config_item('charset')); |
Andrey Andreev | 51593f4 | 2014-05-07 00:06:31 +0300 | [diff] [blame] | 242 | ini_set('default_charset', $charset); |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 243 | |
| 244 | if (extension_loaded('mbstring')) |
| 245 | { |
| 246 | define('MB_ENABLED', TRUE); |
Andrey Andreev | 1ffa223 | 2014-05-09 12:30:59 +0300 | [diff] [blame] | 247 | // mbstring.internal_encoding is deprecated starting with PHP 5.6 |
| 248 | // and it's usage triggers E_DEPRECATED messages. |
| 249 | @ini_set('mbstring.internal_encoding', $charset); |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 250 | // This is required for mb_convert_encoding() to strip invalid characters. |
| 251 | // That's utilized by CI_Utf8, but it's also done for consistency with iconv. |
| 252 | mb_substitute_character('none'); |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | define('MB_ENABLED', FALSE); |
| 257 | } |
| 258 | |
| 259 | // There's an ICONV_IMPL constant, but the PHP manual says that using |
| 260 | // iconv's predefined constants is "strongly discouraged". |
| 261 | if (extension_loaded('iconv')) |
| 262 | { |
| 263 | define('ICONV_ENABLED', TRUE); |
Andrey Andreev | 51593f4 | 2014-05-07 00:06:31 +0300 | [diff] [blame] | 264 | // iconv.internal_encoding is deprecated starting with PHP 5.6 |
| 265 | // and it's usage triggers E_DEPRECATED messages. |
| 266 | @ini_set('iconv.internal_encoding', $charset); |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 267 | } |
| 268 | else |
| 269 | { |
| 270 | define('ICONV_ENABLED', FALSE); |
| 271 | } |
| 272 | |
Andrey Andreev | 51593f4 | 2014-05-07 00:06:31 +0300 | [diff] [blame] | 273 | if (is_php('5.6')) |
| 274 | { |
| 275 | ini_set('php.internal_encoding', $charset); |
| 276 | } |
| 277 | |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 278 | /* |
| 279 | * ------------------------------------------------------ |
Andrey Andreev | 3fd1b38 | 2014-02-13 03:01:31 +0200 | [diff] [blame] | 280 | * Load compatibility features |
| 281 | * ------------------------------------------------------ |
| 282 | */ |
| 283 | |
Andrey Andreev | 376b215 | 2014-02-13 12:35:52 +0200 | [diff] [blame] | 284 | require_once(BASEPATH.'core/compat/mbstring.php'); |
Andrey Andreev | 9a152a9 | 2014-02-18 16:29:53 +0200 | [diff] [blame] | 285 | require_once(BASEPATH.'core/compat/hash.php'); |
Andrey Andreev | 376b215 | 2014-02-13 12:35:52 +0200 | [diff] [blame] | 286 | require_once(BASEPATH.'core/compat/password.php'); |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 287 | require_once(BASEPATH.'core/compat/standard.php'); |
Andrey Andreev | 3fd1b38 | 2014-02-13 03:01:31 +0200 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | * ------------------------------------------------------ |
Andrey Andreev | eb555ed | 2014-02-12 19:25:01 +0200 | [diff] [blame] | 291 | * Instantiate the UTF-8 class |
| 292 | * ------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | */ |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 294 | $UNI =& load_class('Utf8', 'core'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 296 | /* |
| 297 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 298 | * Instantiate the URI class |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 299 | * ------------------------------------------------------ |
| 300 | */ |
| 301 | $URI =& load_class('URI', 'core'); |
| 302 | |
| 303 | /* |
| 304 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 305 | * Instantiate the routing class and set the routing |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 306 | * ------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | */ |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 308 | $RTR =& load_class('Router', 'core', isset($routing) ? $routing : NULL); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 310 | /* |
| 311 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 312 | * Instantiate the output class |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 313 | * ------------------------------------------------------ |
| 314 | */ |
| 315 | $OUT =& load_class('Output', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * ------------------------------------------------------ |
Andrey Andreev | 9c5c24a | 2012-01-07 18:51:21 +0200 | [diff] [blame] | 319 | * Is there a valid cache file? If so, we're done... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | * ------------------------------------------------------ |
| 321 | */ |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 322 | if ($EXT->call_hook('cache_override') === FALSE && $OUT->_display_cache($CFG, $URI) === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | { |
Andrey Andreev | 9c5c24a | 2012-01-07 18:51:21 +0200 | [diff] [blame] | 324 | exit; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | |
| 327 | /* |
Pascal Kriete | 14a0ac6 | 2011-04-05 14:55:56 -0400 | [diff] [blame] | 328 | * ----------------------------------------------------- |
| 329 | * Load the security class for xss and csrf support |
| 330 | * ----------------------------------------------------- |
| 331 | */ |
| 332 | $SEC =& load_class('Security', 'core'); |
| 333 | |
| 334 | /* |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 336 | * Load the Input class and sanitize globals |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | * ------------------------------------------------------ |
| 338 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | $IN =& load_class('Input', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 341 | /* |
| 342 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 343 | * Load the Language class |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 344 | * ------------------------------------------------------ |
| 345 | */ |
| 346 | $LANG =& load_class('Lang', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 350 | * Load the app controller and local controller |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | * ------------------------------------------------------ |
| 352 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | */ |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 354 | // Load the base controller class |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 355 | require_once BASEPATH.'core/Controller.php'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 357 | /** |
| 358 | * Reference to the CI_Controller method. |
| 359 | * |
| 360 | * Returns current CI instance object |
| 361 | * |
Andrey Andreev | 7e0bde2 | 2016-03-07 17:52:26 +0200 | [diff] [blame^] | 362 | * @return CI_Controller |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 363 | */ |
Greg Aker | 4abfa68 | 2010-11-10 14:44:26 -0600 | [diff] [blame] | 364 | function &get_instance() |
| 365 | { |
| 366 | return CI_Controller::get_instance(); |
| 367 | } |
| 368 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 369 | if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php')) |
Greg Aker | fa28135 | 2010-03-22 14:41:27 -0500 | [diff] [blame] | 370 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 371 | require_once APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'; |
Greg Aker | fa28135 | 2010-03-22 14:41:27 -0500 | [diff] [blame] | 372 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 374 | // Set a mark point for benchmarking |
| 375 | $BM->mark('loading_time:_base_classes_end'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * ------------------------------------------------------ |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 379 | * Sanity checks |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | * ------------------------------------------------------ |
| 381 | * |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 382 | * The Router class has already validated the request, |
| 383 | * leaving us with 3 options here: |
| 384 | * |
| 385 | * 1) an empty class name, if we reached the default |
| 386 | * controller, but it didn't exist; |
| 387 | * 2) a query string which doesn't go through a |
| 388 | * file_exists() check |
| 389 | * 3) a regular request for a non-existing page |
| 390 | * |
| 391 | * We handle all of these as a 404 error. |
| 392 | * |
| 393 | * Furthermore, none of the methods in the app controller |
| 394 | * or the loader class can be called via the URI, nor can |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 395 | * controller methods that begin with an underscore. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 398 | $e404 = FALSE; |
| 399 | $class = ucfirst($RTR->class); |
| 400 | $method = $RTR->method; |
| 401 | |
| 402 | if (empty($class) OR ! file_exists(APPPATH.'controllers/'.$RTR->directory.$class.'.php')) |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 403 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 404 | $e404 = TRUE; |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 405 | } |
| 406 | else |
| 407 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 408 | require_once(APPPATH.'controllers/'.$RTR->directory.$class.'.php'); |
| 409 | |
| 410 | if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method)) |
| 411 | { |
| 412 | $e404 = TRUE; |
| 413 | } |
| 414 | elseif (method_exists($class, '_remap')) |
| 415 | { |
| 416 | $params = array($method, array_slice($URI->rsegments, 2)); |
| 417 | $method = '_remap'; |
| 418 | } |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 419 | // WARNING: It appears that there are issues with is_callable() even in PHP 5.2! |
| 420 | // Furthermore, there are bug reports and feature/change requests related to it |
| 421 | // that make it unreliable to use in this context. Please, DO NOT change this |
| 422 | // work-around until a better alternative is available. |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 423 | elseif ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($class)), TRUE)) |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 424 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 425 | $e404 = TRUE; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if ($e404) |
| 430 | { |
| 431 | if ( ! empty($RTR->routes['404_override'])) |
| 432 | { |
| 433 | if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $error_class, $error_method) !== 2) |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 434 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 435 | $error_method = 'index'; |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 438 | $error_class = ucfirst($error_class); |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 439 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 440 | if ( ! class_exists($error_class, FALSE)) |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 441 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 442 | if (file_exists(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php')) |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 443 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 444 | require_once(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php'); |
| 445 | $e404 = ! class_exists($error_class, FALSE); |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 446 | } |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 447 | // Were we in a directory? If so, check for a global override |
| 448 | elseif ( ! empty($RTR->directory) && file_exists(APPPATH.'controllers/'.$error_class.'.php')) |
| 449 | { |
| 450 | require_once(APPPATH.'controllers/'.$error_class.'.php'); |
| 451 | if (($e404 = ! class_exists($error_class, FALSE)) === FALSE) |
| 452 | { |
| 453 | $RTR->directory = ''; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | $e404 = FALSE; |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 463 | // Did we reset the $e404 flag? If so, set the rsegments, starting from index 1 |
| 464 | if ( ! $e404) |
| 465 | { |
| 466 | $class = $error_class; |
| 467 | $method = $error_method; |
| 468 | |
| 469 | $URI->rsegments = array( |
| 470 | 1 => $class, |
| 471 | 2 => $method |
| 472 | ); |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | show_404($RTR->directory.$class.'/'.$method); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if ($method !== '_remap') |
| 481 | { |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 482 | $params = array_slice($URI->rsegments, 2); |
| 483 | } |
| 484 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | /* |
| 486 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 487 | * Is there a "pre_controller" hook? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | * ------------------------------------------------------ |
| 489 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 490 | $EXT->call_hook('pre_controller'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 494 | * Instantiate the requested controller |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | * ------------------------------------------------------ |
| 496 | */ |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 497 | // Mark a start point so we can benchmark the controller |
| 498 | $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 500 | $CI = new $class(); |
| 501 | |
| 502 | /* |
| 503 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 504 | * Is there a "post_controller_constructor" hook? |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 505 | * ------------------------------------------------------ |
| 506 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 507 | $EXT->call_hook('post_controller_constructor'); |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 508 | |
| 509 | /* |
| 510 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 511 | * Call the requested method |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 512 | * ------------------------------------------------------ |
| 513 | */ |
Andrey Andreev | 38e32f6 | 2012-11-03 00:16:47 +0200 | [diff] [blame] | 514 | call_user_func_array(array(&$CI, $method), $params); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 516 | // Mark a benchmark end point |
| 517 | $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 521 | * Is there a "post_controller" hook? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | * ------------------------------------------------------ |
| 523 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 524 | $EXT->call_hook('post_controller'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 528 | * Send the final rendered output to the browser |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | * ------------------------------------------------------ |
| 530 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 531 | if ($EXT->call_hook('display_override') === FALSE) |
Derek Jones | 218876c | 2010-03-02 13:11:00 -0600 | [diff] [blame] | 532 | { |
| 533 | $OUT->_display(); |
| 534 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 535 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | /* |
| 537 | * ------------------------------------------------------ |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 538 | * Is there a "post_system" hook? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | * ------------------------------------------------------ |
| 540 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 541 | $EXT->call_hook('post_system'); |