Subversion-Projekte lars-tiefland.ci

Revision

Revision 68 | Revision 2049 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
9
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
10
 *
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:
17
 *
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
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * System Initialization File
42
 *
43
 * Loads the base classes and executes the request.
44
 *
45
 * @package		CodeIgniter
46
 * @subpackage	CodeIgniter
47
 * @category	Front-controller
48
 * @author		EllisLab Dev Team
49
 * @link		https://codeigniter.com/user_guide/
50
 */
51
 
52
/**
53
 * CodeIgniter Version
54
 *
55
 * @var	string
56
 *
57
 */
1257 lars 58
	const CI_VERSION = '3.1.2';
68 lars 59
 
60
/*
61
 * ------------------------------------------------------
62
 *  Load the framework constants
63
 * ------------------------------------------------------
64
 */
65
	if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
66
	{
67
		require_once(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
68
	}
69
 
70
	require_once(APPPATH.'config/constants.php');
71
 
72
/*
73
 * ------------------------------------------------------
74
 *  Load the global functions
75
 * ------------------------------------------------------
76
 */
77
	require_once(BASEPATH.'core/Common.php');
78
 
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',
103
			'system_path',
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
 
130
/*
131
 * ------------------------------------------------------
132
 *  Define a custom error handler so we can log PHP errors
133
 * ------------------------------------------------------
134
 */
135
	set_error_handler('_error_handler');
136
	set_exception_handler('_exception_handler');
137
	register_shutdown_function('_shutdown_handler');
138
 
139
/*
140
 * ------------------------------------------------------
141
 *  Set the subclass_prefix
142
 * ------------------------------------------------------
143
 *
144
 * Normally the "subclass_prefix" is set in the config file.
145
 * The subclass prefix allows CI to know if a core class is
146
 * being extended via a library in the local application
147
 * "libraries" folder. Since CI allows config items to be
148
 * overridden via data set in the main index.php file,
149
 * before proceeding we need to know if a subclass_prefix
150
 * override exists. If so, we will set this value now,
151
 * before any classes are loaded
152
 * Note: Since the config file data is cached it doesn't
153
 * hurt to load it here.
154
 */
155
	if ( ! empty($assign_to_config['subclass_prefix']))
156
	{
157
		get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
158
	}
159
 
160
/*
161
 * ------------------------------------------------------
162
 *  Should we use a Composer autoloader?
163
 * ------------------------------------------------------
164
 */
165
	if ($composer_autoload = config_item('composer_autoload'))
166
	{
167
		if ($composer_autoload === TRUE)
168
		{
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.');
172
		}
173
		elseif (file_exists($composer_autoload))
174
		{
175
			require_once($composer_autoload);
176
		}
177
		else
178
		{
179
			log_message('error', 'Could not find the specified $config[\'composer_autoload\'] path: '.$composer_autoload);
180
		}
181
	}
182
 
183
/*
184
 * ------------------------------------------------------
185
 *  Start the timer... tick tock tick tock...
186
 * ------------------------------------------------------
187
 */
188
	$BM =& load_class('Benchmark', 'core');
189
	$BM->mark('total_execution_time_start');
190
	$BM->mark('loading_time:_base_classes_start');
191
 
192
/*
193
 * ------------------------------------------------------
194
 *  Instantiate the hooks class
195
 * ------------------------------------------------------
196
 */
197
	$EXT =& load_class('Hooks', 'core');
198
 
199
/*
200
 * ------------------------------------------------------
201
 *  Is there a "pre_system" hook?
202
 * ------------------------------------------------------
203
 */
204
	$EXT->call_hook('pre_system');
205
 
206
/*
207
 * ------------------------------------------------------
208
 *  Instantiate the config class
209
 * ------------------------------------------------------
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
 *
215
 */
216
	$CFG =& load_class('Config', 'core');
217
 
218
	// Do we have any manually set config items in the index.php file?
219
	if (isset($assign_to_config) && is_array($assign_to_config))
220
	{
221
		foreach ($assign_to_config as $key => $value)
222
		{
223
			$CFG->set_item($key, $value);
224
		}
225
	}
226
 
227
/*
228
 * ------------------------------------------------------
229
 * Important charset-related stuff
230
 * ------------------------------------------------------
231
 *
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.
236
 *
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'));
242
	ini_set('default_charset', $charset);
243
 
244
	if (extension_loaded('mbstring'))
245
	{
246
		define('MB_ENABLED', TRUE);
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);
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);
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);
267
	}
268
	else
269
	{
270
		define('ICONV_ENABLED', FALSE);
271
	}
272
 
273
	if (is_php('5.6'))
274
	{
275
		ini_set('php.internal_encoding', $charset);
276
	}
277
 
278
/*
279
 * ------------------------------------------------------
280
 *  Load compatibility features
281
 * ------------------------------------------------------
282
 */
283
 
284
	require_once(BASEPATH.'core/compat/mbstring.php');
285
	require_once(BASEPATH.'core/compat/hash.php');
286
	require_once(BASEPATH.'core/compat/password.php');
287
	require_once(BASEPATH.'core/compat/standard.php');
288
 
289
/*
290
 * ------------------------------------------------------
291
 *  Instantiate the UTF-8 class
292
 * ------------------------------------------------------
293
 */
294
	$UNI =& load_class('Utf8', 'core');
295
 
296
/*
297
 * ------------------------------------------------------
298
 *  Instantiate the URI class
299
 * ------------------------------------------------------
300
 */
301
	$URI =& load_class('URI', 'core');
302
 
303
/*
304
 * ------------------------------------------------------
305
 *  Instantiate the routing class and set the routing
306
 * ------------------------------------------------------
307
 */
308
	$RTR =& load_class('Router', 'core', isset($routing) ? $routing : NULL);
309
 
310
/*
311
 * ------------------------------------------------------
312
 *  Instantiate the output class
313
 * ------------------------------------------------------
314
 */
315
	$OUT =& load_class('Output', 'core');
316
 
317
/*
318
 * ------------------------------------------------------
319
 *	Is there a valid cache file? If so, we're done...
320
 * ------------------------------------------------------
321
 */
322
	if ($EXT->call_hook('cache_override') === FALSE && $OUT->_display_cache($CFG, $URI) === TRUE)
323
	{
324
		exit;
325
	}
326
 
327
/*
328
 * -----------------------------------------------------
329
 * Load the security class for xss and csrf support
330
 * -----------------------------------------------------
331
 */
332
	$SEC =& load_class('Security', 'core');
333
 
334
/*
335
 * ------------------------------------------------------
336
 *  Load the Input class and sanitize globals
337
 * ------------------------------------------------------
338
 */
339
	$IN	=& load_class('Input', 'core');
340
 
341
/*
342
 * ------------------------------------------------------
343
 *  Load the Language class
344
 * ------------------------------------------------------
345
 */
346
	$LANG =& load_class('Lang', 'core');
347
 
348
/*
349
 * ------------------------------------------------------
350
 *  Load the app controller and local controller
351
 * ------------------------------------------------------
352
 *
353
 */
354
	// Load the base controller class
355
	require_once BASEPATH.'core/Controller.php';
356
 
357
	/**
358
	 * Reference to the CI_Controller method.
359
	 *
360
	 * Returns current CI instance object
361
	 *
362
	 * @return CI_Controller
363
	 */
364
	function &get_instance()
365
	{
366
		return CI_Controller::get_instance();
367
	}
368
 
369
	if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
370
	{
371
		require_once APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
372
	}
373
 
374
	// Set a mark point for benchmarking
375
	$BM->mark('loading_time:_base_classes_end');
376
 
377
/*
378
 * ------------------------------------------------------
379
 *  Sanity checks
380
 * ------------------------------------------------------
381
 *
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
395
 *  controller methods that begin with an underscore.
396
 */
397
 
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'))
403
	{
404
		$e404 = TRUE;
405
	}
406
	else
407
	{
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
		}
1257 lars 419
		elseif ( ! method_exists($class, $method))
68 lars 420
		{
421
			$e404 = TRUE;
422
		}
1257 lars 423
		/**
424
		 * DO NOT CHANGE THIS, NOTHING ELSE WORKS!
425
		 *
426
		 * - method_exists() returns true for non-public methods, which passes the previous elseif
427
		 * - is_callable() returns false for PHP 4-style constructors, even if there's a __construct()
428
		 * - method_exists($class, '__construct') won't work because CI_Controller::__construct() is inherited
429
		 * - People will only complain if this doesn't work, even though it is documented that it shouldn't.
430
		 *
431
		 * ReflectionMethod::isConstructor() is the ONLY reliable check,
432
		 * knowing which method will be executed as a constructor.
433
		 */
434
		elseif ( ! is_callable(array($class, $method)) && strcasecmp($class, $method) === 0)
435
		{
436
			$reflection = new ReflectionMethod($class, $method);
437
			if ( ! $reflection->isPublic() OR $reflection->isConstructor())
438
			{
439
				$e404 = TRUE;
440
			}
441
		}
68 lars 442
	}
443
 
444
	if ($e404)
445
	{
446
		if ( ! empty($RTR->routes['404_override']))
447
		{
448
			if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $error_class, $error_method) !== 2)
449
			{
450
				$error_method = 'index';
451
			}
452
 
453
			$error_class = ucfirst($error_class);
454
 
455
			if ( ! class_exists($error_class, FALSE))
456
			{
457
				if (file_exists(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php'))
458
				{
459
					require_once(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php');
460
					$e404 = ! class_exists($error_class, FALSE);
461
				}
462
				// Were we in a directory? If so, check for a global override
463
				elseif ( ! empty($RTR->directory) && file_exists(APPPATH.'controllers/'.$error_class.'.php'))
464
				{
465
					require_once(APPPATH.'controllers/'.$error_class.'.php');
466
					if (($e404 = ! class_exists($error_class, FALSE)) === FALSE)
467
					{
468
						$RTR->directory = '';
469
					}
470
				}
471
			}
472
			else
473
			{
474
				$e404 = FALSE;
475
			}
476
		}
477
 
478
		// Did we reset the $e404 flag? If so, set the rsegments, starting from index 1
479
		if ( ! $e404)
480
		{
481
			$class = $error_class;
482
			$method = $error_method;
483
 
484
			$URI->rsegments = array(
485
				1 => $class,
486
				2 => $method
487
			);
488
		}
489
		else
490
		{
491
			show_404($RTR->directory.$class.'/'.$method);
492
		}
493
	}
494
 
495
	if ($method !== '_remap')
496
	{
497
		$params = array_slice($URI->rsegments, 2);
498
	}
499
 
500
/*
501
 * ------------------------------------------------------
502
 *  Is there a "pre_controller" hook?
503
 * ------------------------------------------------------
504
 */
505
	$EXT->call_hook('pre_controller');
506
 
507
/*
508
 * ------------------------------------------------------
509
 *  Instantiate the requested controller
510
 * ------------------------------------------------------
511
 */
512
	// Mark a start point so we can benchmark the controller
513
	$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
514
 
515
	$CI = new $class();
516
 
517
/*
518
 * ------------------------------------------------------
519
 *  Is there a "post_controller_constructor" hook?
520
 * ------------------------------------------------------
521
 */
522
	$EXT->call_hook('post_controller_constructor');
523
 
524
/*
525
 * ------------------------------------------------------
526
 *  Call the requested method
527
 * ------------------------------------------------------
528
 */
529
	call_user_func_array(array(&$CI, $method), $params);
530
 
531
	// Mark a benchmark end point
532
	$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
533
 
534
/*
535
 * ------------------------------------------------------
536
 *  Is there a "post_controller" hook?
537
 * ------------------------------------------------------
538
 */
539
	$EXT->call_hook('post_controller');
540
 
541
/*
542
 * ------------------------------------------------------
543
 *  Send the final rendered output to the browser
544
 * ------------------------------------------------------
545
 */
546
	if ($EXT->call_hook('display_override') === FALSE)
547
	{
548
		$OUT->_display();
549
	}
550
 
551
/*
552
 * ------------------------------------------------------
553
 *  Is there a "post_system" hook?
554
 * ------------------------------------------------------
555
 */
556
	$EXT->call_hook('post_system');