blob: b4e41c86a2a6cd8d1b341a29e4aac381bfecf130 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev9c5c24a2012-01-07 18:51:21 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev9c5c24a2012-01-07 18:51:21 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
Derek Jones218876c2010-03-02 13:11:00 -060041 * System Initialization File
Derek Allard2067d1a2008-11-13 22:59:24 +000042 *
43 * Loads the base classes and executes the request.
44 *
45 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030046 * @subpackage CodeIgniter
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @category Front-controller
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
51
Phil Sturgeond8ef6302011-08-14 12:10:55 -060052/**
53 * CodeIgniter Version
54 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040055 * @var string
Phil Sturgeond8ef6302011-08-14 12:10:55 -060056 *
Derek Jones218876c2010-03-02 13:11:00 -060057 */
Andrey Andreev44f53fb2018-06-12 16:54:47 +030058 const CI_VERSION = '3.1.10-dev';
Andrey Andreevb6fbcbe2013-11-17 18:06:18 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060/*
61 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050062 * Load the framework constants
Derek Allard2067d1a2008-11-13 22:59:24 +000063 * ------------------------------------------------------
64 */
Andrey Andreevdb529ca2013-01-28 11:00:02 +020065 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
Greg Akerd96f8822011-12-27 16:23:47 -060066 {
Andrey Andreev30d53242014-01-16 14:41:46 +020067 require_once(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
Greg Akerd96f8822011-12-27 16:23:47 -060068 }
Andrey Andreevb6fbcbe2013-11-17 18:06:18 +020069
Andrey Andreev5d6e77b2016-12-01 17:14:35 +020070 if (file_exists(APPPATH.'config/constants.php'))
71 {
72 require_once(APPPATH.'config/constants.php');
73 }
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75/*
76 * ------------------------------------------------------
josephok1095d112013-11-16 09:33:37 +080077 * Load the global functions
78 * ------------------------------------------------------
79 */
80 require_once(BASEPATH.'core/Common.php');
81
Andrey Andreevb78a8c72014-04-15 17:21:16 +030082
83/*
84 * ------------------------------------------------------
85 * Security procedures
86 * ------------------------------------------------------
87 */
88
89if ( ! is_php('5.4'))
90{
91 ini_set('magic_quotes_runtime', 0);
92
93 if ((bool) ini_get('register_globals'))
94 {
95 $_protected = array(
96 '_SERVER',
97 '_GET',
98 '_POST',
99 '_FILES',
100 '_REQUEST',
101 '_SESSION',
102 '_ENV',
103 '_COOKIE',
104 'GLOBALS',
105 'HTTP_RAW_POST_DATA',
vlakoffde70dbb2014-04-15 16:46:03 +0200106 'system_path',
Andrey Andreevb78a8c72014-04-15 17:21:16 +0300107 'application_folder',
108 'view_folder',
109 '_protected',
110 '_registered'
111 );
112
113 $_registered = ini_get('variables_order');
114 foreach (array('E' => '_ENV', 'G' => '_GET', 'P' => '_POST', 'C' => '_COOKIE', 'S' => '_SERVER') as $key => $superglobal)
115 {
116 if (strpos($_registered, $key) === FALSE)
117 {
118 continue;
119 }
120
121 foreach (array_keys($$superglobal) as $var)
122 {
123 if (isset($GLOBALS[$var]) && ! in_array($var, $_protected, TRUE))
124 {
125 $GLOBALS[$var] = NULL;
126 }
127 }
128 }
129 }
130}
131
132
josephok1095d112013-11-16 09:33:37 +0800133/*
134 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500135 * Define a custom error handler so we can log PHP errors
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * ------------------------------------------------------
137 */
Andrey Andreev4b838af2014-10-28 23:46:45 +0200138 set_error_handler('_error_handler');
139 set_exception_handler('_exception_handler');
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800140 register_shutdown_function('_shutdown_handler');
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Jones218876c2010-03-02 13:11:00 -0600142/*
143 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500144 * Set the subclass_prefix
Derek Jones218876c2010-03-02 13:11:00 -0600145 * ------------------------------------------------------
146 *
Barry Mienydd671972010-10-04 16:33:58 +0200147 * Normally the "subclass_prefix" is set in the config file.
148 * The subclass prefix allows CI to know if a core class is
Derek Jones218876c2010-03-02 13:11:00 -0600149 * being extended via a library in the local application
Barry Mienydd671972010-10-04 16:33:58 +0200150 * "libraries" folder. Since CI allows config items to be
Claudio Galdiolo72d63cd2014-12-22 15:18:14 -0500151 * overridden via data set in the main index.php file,
Barry Mienydd671972010-10-04 16:33:58 +0200152 * before proceeding we need to know if a subclass_prefix
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200153 * override exists. If so, we will set this value now,
Derek Jones218876c2010-03-02 13:11:00 -0600154 * before any classes are loaded
Barry Mienydd671972010-10-04 16:33:58 +0200155 * Note: Since the config file data is cached it doesn't
Derek Jones218876c2010-03-02 13:11:00 -0600156 * hurt to load it here.
157 */
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300158 if ( ! empty($assign_to_config['subclass_prefix']))
Derek Jones218876c2010-03-02 13:11:00 -0600159 {
160 get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
161 }
Eric Barnesc5bf6162011-01-30 21:17:11 -0500162
Pascal Krietef566af52010-11-09 13:03:26 -0500163/*
164 * ------------------------------------------------------
Andrey Andreev17254322015-01-30 15:53:28 +0200165 * Should we use a Composer autoloader?
166 * ------------------------------------------------------
167 */
168 if ($composer_autoload = config_item('composer_autoload'))
169 {
Andrey Andreev2eb580c2015-01-30 16:41:43 +0200170 if ($composer_autoload === TRUE)
Andrey Andreev17254322015-01-30 15:53:28 +0200171 {
Andrey Andreev2eb580c2015-01-30 16:41:43 +0200172 file_exists(APPPATH.'vendor/autoload.php')
173 ? require_once(APPPATH.'vendor/autoload.php')
174 : log_message('error', '$config[\'composer_autoload\'] is set to TRUE but '.APPPATH.'vendor/autoload.php was not found.');
Andrey Andreev17254322015-01-30 15:53:28 +0200175 }
176 elseif (file_exists($composer_autoload))
177 {
178 require_once($composer_autoload);
179 }
Andrey Andreev2eb580c2015-01-30 16:41:43 +0200180 else
181 {
182 log_message('error', 'Could not find the specified $config[\'composer_autoload\'] path: '.$composer_autoload);
183 }
Andrey Andreev17254322015-01-30 15:53:28 +0200184 }
185
186/*
187 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500188 * Start the timer... tick tock tick tock...
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * ------------------------------------------------------
190 */
Derek Jones218876c2010-03-02 13:11:00 -0600191 $BM =& load_class('Benchmark', 'core');
192 $BM->mark('total_execution_time_start');
193 $BM->mark('loading_time:_base_classes_start');
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
195/*
196 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500197 * Instantiate the hooks class
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * ------------------------------------------------------
199 */
Derek Jones218876c2010-03-02 13:11:00 -0600200 $EXT =& load_class('Hooks', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
202/*
203 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500204 * Is there a "pre_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * ------------------------------------------------------
206 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300207 $EXT->call_hook('pre_system');
Derek Allard2067d1a2008-11-13 22:59:24 +0000208
209/*
210 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500211 * Instantiate the config class
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * ------------------------------------------------------
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200213 *
214 * Note: It is important that Config is loaded first as
215 * most other classes depend on it either directly or by
216 * depending on another class that uses it.
217 *
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Derek Jones218876c2010-03-02 13:11:00 -0600219 $CFG =& load_class('Config', 'core');
220
221 // Do we have any manually set config items in the index.php file?
Andrey Andreev5232ba02012-10-27 15:25:05 +0300222 if (isset($assign_to_config) && is_array($assign_to_config))
Barry Mienydd671972010-10-04 16:33:58 +0200223 {
Andrey Andreev5232ba02012-10-27 15:25:05 +0300224 foreach ($assign_to_config as $key => $value)
225 {
226 $CFG->set_item($key, $value);
227 }
Derek Jones218876c2010-03-02 13:11:00 -0600228 }
229
230/*
231 * ------------------------------------------------------
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200232 * Important charset-related stuff
Derek Jones218876c2010-03-02 13:11:00 -0600233 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200234 *
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200235 * Configure mbstring and/or iconv if they are enabled
236 * and set MB_ENABLED and ICONV_ENABLED constants, so
237 * that we don't repeatedly do extension_loaded() or
238 * function_exists() calls.
Barry Mienydd671972010-10-04 16:33:58 +0200239 *
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200240 * Note: UTF-8 class depends on this. It used to be done
241 * in it's constructor, but it's _not_ class-specific.
242 *
243 */
244 $charset = strtoupper(config_item('charset'));
Andrey Andreev51593f42014-05-07 00:06:31 +0300245 ini_set('default_charset', $charset);
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200246
247 if (extension_loaded('mbstring'))
248 {
249 define('MB_ENABLED', TRUE);
Andrey Andreev1ffa2232014-05-09 12:30:59 +0300250 // mbstring.internal_encoding is deprecated starting with PHP 5.6
251 // and it's usage triggers E_DEPRECATED messages.
252 @ini_set('mbstring.internal_encoding', $charset);
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200253 // This is required for mb_convert_encoding() to strip invalid characters.
254 // That's utilized by CI_Utf8, but it's also done for consistency with iconv.
255 mb_substitute_character('none');
256 }
257 else
258 {
259 define('MB_ENABLED', FALSE);
260 }
261
262 // There's an ICONV_IMPL constant, but the PHP manual says that using
263 // iconv's predefined constants is "strongly discouraged".
264 if (extension_loaded('iconv'))
265 {
266 define('ICONV_ENABLED', TRUE);
Andrey Andreev51593f42014-05-07 00:06:31 +0300267 // iconv.internal_encoding is deprecated starting with PHP 5.6
268 // and it's usage triggers E_DEPRECATED messages.
269 @ini_set('iconv.internal_encoding', $charset);
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200270 }
271 else
272 {
273 define('ICONV_ENABLED', FALSE);
274 }
275
Andrey Andreev51593f42014-05-07 00:06:31 +0300276 if (is_php('5.6'))
277 {
278 ini_set('php.internal_encoding', $charset);
279 }
280
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200281/*
282 * ------------------------------------------------------
Andrey Andreev3fd1b382014-02-13 03:01:31 +0200283 * Load compatibility features
284 * ------------------------------------------------------
285 */
286
Andrey Andreev376b2152014-02-13 12:35:52 +0200287 require_once(BASEPATH.'core/compat/mbstring.php');
Andrey Andreev9a152a92014-02-18 16:29:53 +0200288 require_once(BASEPATH.'core/compat/hash.php');
Andrey Andreev376b2152014-02-13 12:35:52 +0200289 require_once(BASEPATH.'core/compat/password.php');
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +0300290 require_once(BASEPATH.'core/compat/standard.php');
Andrey Andreev3fd1b382014-02-13 03:01:31 +0200291
292/*
293 * ------------------------------------------------------
Andrey Andreeveb555ed2014-02-12 19:25:01 +0200294 * Instantiate the UTF-8 class
295 * ------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 */
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500297 $UNI =& load_class('Utf8', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Jones218876c2010-03-02 13:11:00 -0600299/*
300 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500301 * Instantiate the URI class
Derek Jones218876c2010-03-02 13:11:00 -0600302 * ------------------------------------------------------
303 */
304 $URI =& load_class('URI', 'core');
305
306/*
307 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500308 * Instantiate the routing class and set the routing
Derek Jones218876c2010-03-02 13:11:00 -0600309 * ------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200311 $RTR =& load_class('Router', 'core', isset($routing) ? $routing : NULL);
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Jones218876c2010-03-02 13:11:00 -0600313/*
314 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500315 * Instantiate the output class
Derek Jones218876c2010-03-02 13:11:00 -0600316 * ------------------------------------------------------
317 */
318 $OUT =& load_class('Output', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000319
320/*
321 * ------------------------------------------------------
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200322 * Is there a valid cache file? If so, we're done...
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * ------------------------------------------------------
324 */
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200325 if ($EXT->call_hook('cache_override') === FALSE && $OUT->_display_cache($CFG, $URI) === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreev9c5c24a2012-01-07 18:51:21 +0200327 exit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
330/*
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400331 * -----------------------------------------------------
332 * Load the security class for xss and csrf support
333 * -----------------------------------------------------
334 */
335 $SEC =& load_class('Security', 'core');
336
337/*
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500339 * Load the Input class and sanitize globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 * ------------------------------------------------------
341 */
Barry Mienydd671972010-10-04 16:33:58 +0200342 $IN =& load_class('Input', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000343
Derek Jones218876c2010-03-02 13:11:00 -0600344/*
345 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500346 * Load the Language class
Derek Jones218876c2010-03-02 13:11:00 -0600347 * ------------------------------------------------------
348 */
349 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
351/*
352 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500353 * Load the app controller and local controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * ------------------------------------------------------
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 */
Derek Jones218876c2010-03-02 13:11:00 -0600357 // Load the base controller class
Andrey Andreev30d53242014-01-16 14:41:46 +0200358 require_once BASEPATH.'core/Controller.php';
Barry Mienydd671972010-10-04 16:33:58 +0200359
Timothy Warrenad475052012-04-19 13:21:06 -0400360 /**
361 * Reference to the CI_Controller method.
362 *
363 * Returns current CI instance object
364 *
Andrey Andreev7e0bde22016-03-07 17:52:26 +0200365 * @return CI_Controller
Timothy Warrenad475052012-04-19 13:21:06 -0400366 */
Greg Aker4abfa682010-11-10 14:44:26 -0600367 function &get_instance()
368 {
369 return CI_Controller::get_instance();
370 }
371
Greg Aker3a746652011-04-19 10:59:47 -0500372 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
Greg Akerfa281352010-03-22 14:41:27 -0500373 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200374 require_once APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
Greg Akerfa281352010-03-22 14:41:27 -0500375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Jones218876c2010-03-02 13:11:00 -0600377 // Set a mark point for benchmarking
378 $BM->mark('loading_time:_base_classes_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000379
380/*
381 * ------------------------------------------------------
Andrey Andreev30d53242014-01-16 14:41:46 +0200382 * Sanity checks
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * ------------------------------------------------------
384 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200385 * The Router class has already validated the request,
386 * leaving us with 3 options here:
387 *
388 * 1) an empty class name, if we reached the default
389 * controller, but it didn't exist;
390 * 2) a query string which doesn't go through a
391 * file_exists() check
392 * 3) a regular request for a non-existing page
393 *
394 * We handle all of these as a 404 error.
395 *
396 * Furthermore, none of the methods in the app controller
397 * or the loader class can be called via the URI, nor can
Andrey Andreev20292312013-07-22 14:29:10 +0300398 * controller methods that begin with an underscore.
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 */
Barry Mienydd671972010-10-04 16:33:58 +0200400
Andrey Andreev30d53242014-01-16 14:41:46 +0200401 $e404 = FALSE;
402 $class = ucfirst($RTR->class);
403 $method = $RTR->method;
404
405 if (empty($class) OR ! file_exists(APPPATH.'controllers/'.$RTR->directory.$class.'.php'))
Derek Jones218876c2010-03-02 13:11:00 -0600406 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200407 $e404 = TRUE;
Andrey Andreev38e32f62012-11-03 00:16:47 +0200408 }
409 else
410 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200411 require_once(APPPATH.'controllers/'.$RTR->directory.$class.'.php');
412
413 if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method))
414 {
415 $e404 = TRUE;
416 }
417 elseif (method_exists($class, '_remap'))
418 {
419 $params = array($method, array_slice($URI->rsegments, 2));
420 $method = '_remap';
421 }
Andrey Andreev7bc88232016-10-27 15:41:23 +0300422 elseif ( ! method_exists($class, $method))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200423 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200424 $e404 = TRUE;
425 }
Andrey Andreev7bc88232016-10-27 15:41:23 +0300426 /**
427 * DO NOT CHANGE THIS, NOTHING ELSE WORKS!
428 *
429 * - method_exists() returns true for non-public methods, which passes the previous elseif
430 * - is_callable() returns false for PHP 4-style constructors, even if there's a __construct()
431 * - method_exists($class, '__construct') won't work because CI_Controller::__construct() is inherited
432 * - People will only complain if this doesn't work, even though it is documented that it shouldn't.
433 *
434 * ReflectionMethod::isConstructor() is the ONLY reliable check,
435 * knowing which method will be executed as a constructor.
436 */
Andrey Andreevab0010f2017-05-25 12:14:41 +0300437 elseif ( ! is_callable(array($class, $method)))
Andrey Andreev7bc88232016-10-27 15:41:23 +0300438 {
439 $reflection = new ReflectionMethod($class, $method);
440 if ( ! $reflection->isPublic() OR $reflection->isConstructor())
441 {
442 $e404 = TRUE;
443 }
444 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200445 }
446
447 if ($e404)
448 {
449 if ( ! empty($RTR->routes['404_override']))
450 {
451 if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $error_class, $error_method) !== 2)
Andrey Andreev38e32f62012-11-03 00:16:47 +0200452 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200453 $error_method = 'index';
Andrey Andreev38e32f62012-11-03 00:16:47 +0200454 }
455
Andrey Andreev30d53242014-01-16 14:41:46 +0200456 $error_class = ucfirst($error_class);
Andrey Andreev20292312013-07-22 14:29:10 +0300457
Andrey Andreev30d53242014-01-16 14:41:46 +0200458 if ( ! class_exists($error_class, FALSE))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200459 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200460 if (file_exists(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php'))
Andrey Andreev38e32f62012-11-03 00:16:47 +0200461 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200462 require_once(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php');
463 $e404 = ! class_exists($error_class, FALSE);
Andrey Andreev38e32f62012-11-03 00:16:47 +0200464 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200465 // Were we in a directory? If so, check for a global override
466 elseif ( ! empty($RTR->directory) && file_exists(APPPATH.'controllers/'.$error_class.'.php'))
467 {
468 require_once(APPPATH.'controllers/'.$error_class.'.php');
469 if (($e404 = ! class_exists($error_class, FALSE)) === FALSE)
470 {
471 $RTR->directory = '';
472 }
473 }
474 }
475 else
476 {
477 $e404 = FALSE;
Andrey Andreev38e32f62012-11-03 00:16:47 +0200478 }
479 }
480
Andrey Andreev30d53242014-01-16 14:41:46 +0200481 // Did we reset the $e404 flag? If so, set the rsegments, starting from index 1
482 if ( ! $e404)
483 {
484 $class = $error_class;
485 $method = $error_method;
486
487 $URI->rsegments = array(
488 1 => $class,
489 2 => $method
490 );
491 }
492 else
493 {
494 show_404($RTR->directory.$class.'/'.$method);
495 }
496 }
497
498 if ($method !== '_remap')
499 {
Andrey Andreev38e32f62012-11-03 00:16:47 +0200500 $params = array_slice($URI->rsegments, 2);
501 }
502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503/*
504 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500505 * Is there a "pre_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * ------------------------------------------------------
507 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300508 $EXT->call_hook('pre_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000509
510/*
511 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500512 * Instantiate the requested controller
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * ------------------------------------------------------
514 */
Derek Jones218876c2010-03-02 13:11:00 -0600515 // Mark a start point so we can benchmark the controller
516 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Jones218876c2010-03-02 13:11:00 -0600518 $CI = new $class();
519
520/*
521 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500522 * Is there a "post_controller_constructor" hook?
Derek Jones218876c2010-03-02 13:11:00 -0600523 * ------------------------------------------------------
524 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300525 $EXT->call_hook('post_controller_constructor');
Derek Jones218876c2010-03-02 13:11:00 -0600526
527/*
528 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500529 * Call the requested method
Derek Jones218876c2010-03-02 13:11:00 -0600530 * ------------------------------------------------------
531 */
Andrey Andreev38e32f62012-11-03 00:16:47 +0200532 call_user_func_array(array(&$CI, $method), $params);
Derek Allard2067d1a2008-11-13 22:59:24 +0000533
Derek Jones218876c2010-03-02 13:11:00 -0600534 // Mark a benchmark end point
535 $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
Derek Allard2067d1a2008-11-13 22:59:24 +0000536
537/*
538 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500539 * Is there a "post_controller" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * ------------------------------------------------------
541 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300542 $EXT->call_hook('post_controller');
Derek Allard2067d1a2008-11-13 22:59:24 +0000543
544/*
545 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500546 * Send the final rendered output to the browser
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * ------------------------------------------------------
548 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300549 if ($EXT->call_hook('display_override') === FALSE)
Derek Jones218876c2010-03-02 13:11:00 -0600550 {
551 $OUT->_display();
552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554/*
555 * ------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500556 * Is there a "post_system" hook?
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * ------------------------------------------------------
558 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300559 $EXT->call_hook('post_system');