blob: 7591cd794181d00c9c0a43589d737238ed7427bd [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev188abaf2012-01-07 19:09:42 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev188abaf2012-01-07 19:09:42 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060030 * Common Functions
31 *
32 * Loads the base classes and executes the request.
33 *
34 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030035 * @subpackage CodeIgniter
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060036 * @category Common Functions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060038 * @link http://codeigniter.com/user_guide/
39 */
40
41// ------------------------------------------------------------------------
42
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040043if ( ! function_exists('is_php'))
44{
Timothy Warrenad475052012-04-19 13:21:06 -040045 /**
46 * Determines if the current version of PHP is greater then the supplied value
47 *
Andrey Andreev24bd2302012-06-05 22:29:12 +030048 * Since there are a few places where we conditionally test for PHP > 5.3
Timothy Warrenad475052012-04-19 13:21:06 -040049 * we'll set a static variable.
50 *
51 * @param string
52 * @return bool TRUE if the current version is $version or higher
53 */
Andrey Andreev24bd2302012-06-05 22:29:12 +030054 function is_php($version = '5.3.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000055 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060056 static $_is_php;
Andrey Andreevb7b43962012-02-27 22:45:48 +020057 $version = (string) $version;
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060059 if ( ! isset($_is_php[$version]))
60 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061 $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060062 }
Derek Jones086ee5a2009-07-28 14:42:12 +000063
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060064 return $_is_php[$version];
65 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040066}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000067
Derek Jones086ee5a2009-07-28 14:42:12 +000068// ------------------------------------------------------------------------
69
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040070if ( ! function_exists('is_really_writable'))
71{
Timothy Warrenad475052012-04-19 13:21:06 -040072 /**
73 * Tests for file writability
74 *
75 * is_writable() returns TRUE on Windows servers when you really can't write to
76 * the file, based on the read-only attribute. is_writable() is also unreliable
77 * on Unix servers if safe_mode is on.
78 *
Andrey Andreev2b284f92014-01-25 00:25:56 +020079 * @link https://bugs.php.net/bug.php?id=54709
Timothy Warrenad475052012-04-19 13:21:06 -040080 * @param string
81 * @return void
82 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060083 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060085 // If we're on a Unix server with safe_mode off we call is_writable
Andrey Andreev5932a732013-09-13 14:45:53 +030086 if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR (bool) @ini_get('safe_mode') === FALSE))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060087 {
88 return is_writable($file);
89 }
Derek Allard2067d1a2008-11-13 22:59:24 +000090
Andrey Andreev188abaf2012-01-07 19:09:42 +020091 /* For Windows servers and safe_mode "on" installations we'll actually
92 * write a file then read it. Bah...
93 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060094 if (is_dir($file))
95 {
vlakoff06127562013-03-30 00:06:39 +010096 $file = rtrim($file, '/').'/'.md5(mt_rand());
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060097 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
98 {
99 return FALSE;
100 }
101
102 fclose($fp);
103 @chmod($file, DIR_WRITE_MODE);
104 @unlink($file);
105 return TRUE;
106 }
Eric Barnes15083012011-03-21 22:13:12 -0400107 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
109 return FALSE;
110 }
111
112 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return TRUE;
114 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400115}
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117// ------------------------------------------------------------------------
118
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400119if ( ! function_exists('load_class'))
120{
Timothy Warrenad475052012-04-19 13:21:06 -0400121 /**
122 * Class registry
123 *
124 * This function acts as a singleton. If the requested class does not
125 * exist it is instantiated and set to a static variable. If it has
126 * previously been instantiated the variable is returned.
127 *
128 * @param string the class name being requested
129 * @param string the directory where the class should be found
130 * @param string the class name prefix
131 * @return object
132 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600133 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600135 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200136
Andrey Andreev188abaf2012-01-07 19:09:42 +0200137 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600138 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600140 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600142
143 $name = FALSE;
144
Shane Pearsonab57a352011-08-22 16:11:20 -0500145 // Look for the class first in the local application/libraries folder
146 // then in the native system/libraries folder
147 foreach (array(APPPATH, BASEPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200148 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200149 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600150 {
151 $name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200152
Andrey Andreev49e68de2013-02-21 16:30:55 +0200153 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600154 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300155 require_once($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600158 break;
159 }
160 }
161
Andrey Andreev188abaf2012-01-07 19:09:42 +0200162 // Is the request a class extension? If so we load it too
Andrey Andreev536b7712012-01-07 21:31:25 +0200163 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600165 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200166
Andrey Andreev49e68de2013-02-21 16:30:55 +0200167 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600168 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300169 require_once(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600170 }
171 }
172
173 // Did we find the class?
174 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Barry Mienydd671972010-10-04 16:33:58 +0200176 // Note: We use exit() rather then show_error() in order to avoid a
vlakofffe8fe452012-07-11 19:56:50 +0200177 // self-referencing loop with the Exceptions class
Kevin Cuppd63e4012012-02-05 14:14:32 -0500178 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700179 echo 'Unable to locate the specified class: '.$class.'.php';
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700180 exit(EXIT_UNKNOWN_CLASS);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600182
183 // Keep track of what we just loaded
184 is_loaded($class);
185
Pascal Kriete58560022010-11-10 16:01:20 -0500186 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600187 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400189}
Derek Allard2067d1a2008-11-13 22:59:24 +0000190
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600191// --------------------------------------------------------------------
192
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400193if ( ! function_exists('is_loaded'))
194{
Timothy Warrenad475052012-04-19 13:21:06 -0400195 /**
196 * Keeps track of which libraries have been loaded. This function is
197 * called by the load_class() function above
198 *
199 * @param string
200 * @return array
201 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200202 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600203 {
204 static $_is_loaded = array();
205
Alex Bilbieed944a32012-06-02 11:07:47 +0100206 if ($class !== '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600207 {
208 $_is_loaded[strtolower($class)] = $class;
209 }
210
211 return $_is_loaded;
212 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400213}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600214
215// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000216
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400217if ( ! function_exists('get_config'))
218{
Timothy Warrenad475052012-04-19 13:21:06 -0400219 /**
220 * Loads the main config.php file
221 *
222 * This function lets us grab the config file even if the Config class
223 * hasn't been instantiated yet
224 *
225 * @param array
226 * @return array
227 */
Andrey Andreev49890a92013-08-19 19:56:18 +0300228 function &get_config(Array $replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600230 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200231
vlakoff05d043b2013-08-19 04:55:34 +0200232 if (empty($_config))
vlakoff8d70c0a2013-08-17 07:31:29 +0200233 {
234 $file_path = APPPATH.'config/config.php';
235 $found = FALSE;
236 if (file_exists($file_path))
237 {
238 $found = TRUE;
239 require($file_path);
240 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600241
vlakoff8d70c0a2013-08-17 07:31:29 +0200242 // Is the config file in the environment folder?
243 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
244 {
245 require($file_path);
246 }
247 elseif ( ! $found)
248 {
249 set_status_header(503);
250 echo 'The configuration file does not exist.';
251 exit(EXIT_CONFIG);
252 }
joelcox2035fd82011-01-16 16:50:36 +0100253
vlakoff8d70c0a2013-08-17 07:31:29 +0200254 // Does the $config array exist in the file?
255 if ( ! isset($config) OR ! is_array($config))
256 {
257 set_status_header(503);
258 echo 'Your config file does not appear to be formatted correctly.';
259 exit(EXIT_CONFIG);
260 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100261
vlakoff05d043b2013-08-19 04:55:34 +0200262 // references cannot be directly assigned to static variables, so we use an array
vlakoff8d70c0a2013-08-17 07:31:29 +0200263 $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265
vlakoff67e5ca62013-08-19 04:52:00 +0200266 // Are any values being dynamically added or replaced?
vlakoff2f7810a2013-08-19 04:46:26 +0200267 foreach ($replace as $key => $val)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600268 {
vlakoff05d043b2013-08-19 04:55:34 +0200269 $_config[0][$key] = $val;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
vlakoff05d043b2013-08-19 04:55:34 +0200272 return $_config[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400274}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600275
276// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000277
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400278if ( ! function_exists('config_item'))
279{
Timothy Warrenad475052012-04-19 13:21:06 -0400280 /**
281 * Returns the specified config item
282 *
283 * @param string
284 * @return mixed
285 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600286 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
vlakoffaf431ce2013-07-19 02:06:41 +0200288 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200289
vlakoffaf431ce2013-07-19 02:06:41 +0200290 if (empty($_config))
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
vlakoffaf431ce2013-07-19 02:06:41 +0200292 // references cannot be directly assigned to static variables, so we use an array
293 $_config[0] =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
vlakoffaf431ce2013-07-19 02:06:41 +0200296 return isset($_config[0][$item]) ? $_config[0][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400298}
Derek Allard2067d1a2008-11-13 22:59:24 +0000299
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600300// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300302if ( ! function_exists('get_mimes'))
303{
304 /**
305 * Returns the MIME types array from config/mimes.php
306 *
307 * @return array
308 */
309 function &get_mimes()
310 {
311 static $_mimes = array();
312
Andrey Andreev06879112013-01-29 15:05:02 +0200313 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300314 {
315 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
316 }
Andrey Andreev06879112013-01-29 15:05:02 +0200317 elseif (file_exists(APPPATH.'config/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300318 {
319 $_mimes = include(APPPATH.'config/mimes.php');
320 }
321
322 return $_mimes;
323 }
324}
325
326// ------------------------------------------------------------------------
327
Andrey Andreev3fb02672012-10-22 16:48:01 +0300328if ( ! function_exists('is_https'))
329{
330 /**
331 * Is HTTPS?
332 *
333 * Determines if the application is accessed via an encrypted
334 * (HTTPS) connection.
335 *
336 * @return bool
337 */
338 function is_https()
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200339 {
Andrey Andreev333b80e2013-07-01 16:21:54 +0300340 if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200341 {
342 return TRUE;
343 }
344 elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
345 {
346 return TRUE;
347 }
Andrey Andreev333b80e2013-07-01 16:21:54 +0300348 elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200349 {
350 return TRUE;
351 }
Richard Deurwaarder (Xeli)98999972013-06-24 15:19:30 +0200352
Richard Deurwaarder (Xeli)7cc29452013-06-24 15:06:19 +0200353 return FALSE;
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200354 }
Andrey Andreev3fb02672012-10-22 16:48:01 +0300355}
356
357// ------------------------------------------------------------------------
358
Andrey Andreevf964b162013-11-12 17:04:55 +0200359if ( ! function_exists('is_cli'))
360{
361
362 /**
363 * Is CLI?
364 *
365 * Test to see if a request was made from the command line.
366 *
367 * @return bool
368 */
369 function is_cli()
370 {
371 return (PHP_SAPI === 'cli' OR defined('STDIN'));
372 }
373}
374
375// ------------------------------------------------------------------------
376
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400377if ( ! function_exists('show_error'))
378{
Timothy Warrenad475052012-04-19 13:21:06 -0400379 /**
380 * Error Handler
381 *
382 * This function lets us invoke the exception class and
383 * display errors using the standard error template located
vlakoff52301c72013-03-29 14:23:34 +0100384 * in application/views/errors/error_general.php
Timothy Warrenad475052012-04-19 13:21:06 -0400385 * This function will send the error page directly to the
386 * browser and exit.
387 *
388 * @param string
389 * @param int
390 * @param string
391 * @return void
392 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600393 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
394 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700395 $status_code = abs($status_code);
396 if ($status_code < 100)
397 {
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700398 $exit_status = $status_code + EXIT__AUTO_MIN;
399 if ($exit_status > EXIT__AUTO_MAX)
400 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700401 $exit_status = EXIT_ERROR;
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700402 }
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700403 $status_code = 500;
404 }
405 else
406 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700407 $exit_status = EXIT_ERROR;
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700408 }
Andrey Andreev5a6814e2013-03-04 15:44:12 +0200409
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600410 $_error =& load_class('Exceptions', 'core');
411 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700412 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600413 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400414}
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600416// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000417
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400418if ( ! function_exists('show_404'))
419{
Timothy Warrenad475052012-04-19 13:21:06 -0400420 /**
421 * 404 Page Handler
422 *
423 * This function is similar to the show_error() function above
424 * However, instead of the standard error template it displays
425 * 404 errors.
426 *
427 * @param string
428 * @param bool
429 * @return void
430 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400431 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600432 {
433 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400434 $_error->show_404($page, $log_error);
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700435 exit(EXIT_UNKNOWN_FILE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600436 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400437}
Derek Allard2067d1a2008-11-13 22:59:24 +0000438
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600439// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000440
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400441if ( ! function_exists('log_message'))
442{
Timothy Warrenad475052012-04-19 13:21:06 -0400443 /**
444 * Error Logging Interface
445 *
446 * We use this as a simple mechanism to access the logging
447 * class and send messages to be logged.
448 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200449 * @param string the error level: 'error', 'debug' or 'info'
450 * @param string the error message
Timothy Warrenad475052012-04-19 13:21:06 -0400451 * @return void
452 */
Andrey Andreev838c9a92013-09-13 14:05:13 +0300453 function log_message($level, $message)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreev2f8d2d32013-08-07 15:54:47 +0300455 static $_log;
Barry Mienydd671972010-10-04 16:33:58 +0200456
Andrey Andreev49890a92013-08-19 19:56:18 +0300457 if ($_log === NULL)
Ted Woodb19a2032013-01-05 16:02:43 -0800458 {
vlakoff61f1aa02013-08-07 11:29:17 +0200459 // references cannot be directly assigned to static variables, so we use an array
460 $_log[0] =& load_class('Log', 'core');
Ted Woodb19a2032013-01-05 16:02:43 -0800461 }
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200462
Andrey Andreev838c9a92013-09-13 14:05:13 +0300463 $_log[0]->write_log($level, $message);
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400465}
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600467// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000468
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400469if ( ! function_exists('set_status_header'))
470{
Timothy Warrenad475052012-04-19 13:21:06 -0400471 /**
472 * Set HTTP Status Header
473 *
474 * @param int the status code
475 * @param string
476 * @return void
477 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600478 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000479 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600480 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400481 200 => 'OK',
482 201 => 'Created',
483 202 => 'Accepted',
484 203 => 'Non-Authoritative Information',
485 204 => 'No Content',
486 205 => 'Reset Content',
487 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000488
Timothy Warrenad475052012-04-19 13:21:06 -0400489 300 => 'Multiple Choices',
490 301 => 'Moved Permanently',
491 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300492 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400493 304 => 'Not Modified',
494 305 => 'Use Proxy',
495 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600496
Timothy Warrenad475052012-04-19 13:21:06 -0400497 400 => 'Bad Request',
498 401 => 'Unauthorized',
499 403 => 'Forbidden',
500 404 => 'Not Found',
501 405 => 'Method Not Allowed',
502 406 => 'Not Acceptable',
503 407 => 'Proxy Authentication Required',
504 408 => 'Request Timeout',
505 409 => 'Conflict',
506 410 => 'Gone',
507 411 => 'Length Required',
508 412 => 'Precondition Failed',
509 413 => 'Request Entity Too Large',
510 414 => 'Request-URI Too Long',
511 415 => 'Unsupported Media Type',
512 416 => 'Requested Range Not Satisfiable',
513 417 => 'Expectation Failed',
514 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600515
Timothy Warrenad475052012-04-19 13:21:06 -0400516 500 => 'Internal Server Error',
517 501 => 'Not Implemented',
518 502 => 'Bad Gateway',
519 503 => 'Service Unavailable',
520 504 => 'Gateway Timeout',
521 505 => 'HTTP Version Not Supported'
522 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600523
Andrey Andreev51d6d842012-06-15 16:41:09 +0300524 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600525 {
526 show_error('Status codes must be numeric', 500);
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Andrey Andreev51d6d842012-06-15 16:41:09 +0300529 is_int($code) OR $code = (int) $code;
530
531 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600532 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300533 if (isset($stati[$code]))
534 {
535 $text = $stati[$code];
536 }
537 else
538 {
539 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
540 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Andrey Andreevb7b43962012-02-27 22:45:48 +0200543 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600544
vlakoff40d12492013-08-06 14:46:00 +0200545 if (strpos(PHP_SAPI, 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600546 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700547 header('Status: '.$code.' '.$text, TRUE);
548 }
549 else
550 {
551 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600552 }
Derek Jones817163a2009-07-11 17:05:58 +0000553 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400554}
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600556// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000557
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400558if ( ! function_exists('_exception_handler'))
559{
Timothy Warrenad475052012-04-19 13:21:06 -0400560 /**
561 * Exception Handler
562 *
vlakoffc941d852013-08-06 14:44:40 +0200563 * This is the custom exception handler that is declared at the top
564 * of CodeIgniter.php. The main reason we use this is to permit
Timothy Warrenad475052012-04-19 13:21:06 -0400565 * PHP errors to be logged in our own log files since the user may
566 * not have access to server logs. Since this function
567 * effectively intercepts PHP errors, however, we also need
568 * to display errors based on the current error_reporting level.
569 * We do that with the use of a PHP error template.
570 *
Andrey Andreev0850a282013-10-21 14:26:18 +0300571 * @param int $severity
572 * @param string $message
573 * @param string $filepath
574 * @param int $line
Timothy Warrenad475052012-04-19 13:21:06 -0400575 * @return void
576 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600577 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200578 {
Andrey Andreev0850a282013-10-21 14:26:18 +0300579 $is_error = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) === $severity);
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200580
581 // When an error occurred, set the status header to '500 Internal Server Error'
582 // to indicate to the client something went wrong.
583 // This can't be done within the $_error->show_php_error method because
584 // it is only called when the display_errors flag is set (which isn't usually
585 // the case in a production environment) or when errors are ignored because
586 // they are above the error_reporting threshold.
587 if ($is_error)
588 {
589 set_status_header(500);
Andrey Andreev02545892014-02-19 23:49:31 +0200590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300592 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600593 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300594 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600595 {
596 return;
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Cristian Kocza2be27442014-02-19 21:52:38 +0200599 $_error =& load_class('Exceptions', 'core');
Andrey Andreev76160bc2014-01-30 22:26:14 +0200600 $_error->log_exception($severity, $message, $filepath, $line);
601
Francesco Negri312bdc52012-08-04 07:32:19 -0400602 // Should we display the error?
603 if ((bool) ini_get('display_errors') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 $_error->show_php_error($severity, $message, $filepath, $line);
606 }
607
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200608 // If the error is fatal, the execution of the script should be stopped because
609 // errors can't be recovered from. Halting the script conforms with PHP's
610 // default error handling. See http://www.php.net/manual/en/errorfunc.constants.php
611 if ($is_error)
612 {
Jesse van Assencf60fa72013-09-27 11:58:44 +0200613 exit(EXIT_ERROR);
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200614 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600615 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400616}
Derek Allard2067d1a2008-11-13 22:59:24 +0000617
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800618// ------------------------------------------------------------------------
619
620if ( ! function_exists('_shutdown_handler'))
621{
622 /**
623 * Shutdown Handler
624 *
625 * This is the shutdown handler that is declared at the top
626 * of CodeIgniter.php. The main reason we use this is to simulate
627 * a complete custom exception handler.
628 *
vkeranovd6f3d312013-09-14 21:39:49 +0300629 * E_STRICT is purposivly neglected because such events may have
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800630 * been caught. Duplication or none? None is preferred for now.
631 *
632 * @link http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a
633 * @return void
634 */
635 function _shutdown_handler()
636 {
Andrey Andreevafca3522013-11-14 15:26:59 +0200637 $last_error = error_get_last();
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800638 if (isset($last_error) &&
639 ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING)))
640 {
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800641 _exception_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']);
642 }
643 }
644}
645
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400646// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200647
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400648if ( ! function_exists('remove_invisible_characters'))
649{
Timothy Warrenad475052012-04-19 13:21:06 -0400650 /**
651 * Remove Invisible Characters
652 *
653 * This prevents sandwiching null characters
654 * between ascii characters, like Java\0script.
655 *
656 * @param string
657 * @param bool
658 * @return string
659 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400660 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500661 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400662 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200663
664 // every control character except newline (dec 10),
665 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400666 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500667 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400668 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
669 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500670 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200671
Pascal Kriete0ff50262011-04-05 14:52:03 -0400672 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500673
674 do
675 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400676 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500677 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400678 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500679
680 return $str;
681 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400682}
Derek Allard2067d1a2008-11-13 22:59:24 +0000683
kenjisfbac8b42011-08-25 10:51:44 +0900684// ------------------------------------------------------------------------
685
kenjisfbac8b42011-08-25 10:51:44 +0900686if ( ! function_exists('html_escape'))
687{
Timothy Warrenad475052012-04-19 13:21:06 -0400688 /**
689 * Returns HTML escaped variable
690 *
691 * @param mixed
692 * @return mixed
693 */
kenjisfbac8b42011-08-25 10:51:44 +0900694 function html_escape($var)
695 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200696 return is_array($var)
697 ? array_map('html_escape', $var)
698 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600699 }
Greg Akerd96f8822011-12-27 16:23:47 -0600700}
Greg Aker5c1aa632011-12-25 01:24:29 -0600701
Chad Furmana1abada2012-07-29 01:03:50 -0400702// ------------------------------------------------------------------------
703
704if ( ! function_exists('_stringify_attributes'))
705{
706 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300707 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400708 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300709 * Helper function used to convert a string, array, or object
710 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400711 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300712 * @param mixed string, array, object
713 * @param bool
714 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400715 */
716 function _stringify_attributes($attributes, $js = FALSE)
717 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300718 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400719
720 if (empty($attributes))
721 {
722 return $atts;
723 }
724
725 if (is_string($attributes))
726 {
727 return ' '.$attributes;
728 }
729
730 $attributes = (array) $attributes;
731
732 foreach ($attributes as $key => $val)
733 {
734 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
735 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300736
Chad Furmana1abada2012-07-29 01:03:50 -0400737 return rtrim($atts, ',');
738 }
739}
740
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200741// ------------------------------------------------------------------------
742
743if ( ! function_exists('function_usable'))
744{
745 /**
746 * Function usable
747 *
748 * Executes a function_exists() check, and if the Suhosin PHP
749 * extension is loaded - checks whether the function that is
750 * checked might be disabled in there as well.
751 *
752 * This is useful as function_exists() will return FALSE for
753 * functions disabled via the *disable_functions* php.ini
754 * setting, but not for *suhosin.executor.func.blacklist* and
755 * *suhosin.executor.disable_eval*. These settings will just
756 * terminate script execution if a disabled function is executed.
757 *
Andrey Andreevaaa8ddb2014-02-03 14:10:44 +0200758 * The above described behavior turned out to be a bug in Suhosin,
759 * but even though a fix was commited for 0.9.34 on 2012-02-12,
760 * that version is yet to be released. This function will therefore
761 * be just temporary, but would probably be kept for a few years.
762 *
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200763 * @link http://www.hardened-php.net/suhosin/
764 * @param string $function_name Function to check for
765 * @return bool TRUE if the function exists and is safe to call,
766 * FALSE otherwise.
767 */
768 function function_usable($function_name)
769 {
770 static $_suhosin_func_blacklist;
771
772 if (function_exists($function_name))
773 {
774 if ( ! isset($_suhosin_func_blacklist))
775 {
Andrey Andreevae634622012-12-17 10:30:18 +0200776 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200777 {
Andrey Andreevae634622012-12-17 10:30:18 +0200778 $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist')));
779
780 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval'))
781 {
782 $_suhosin_func_blacklist[] = 'eval';
783 }
784 }
785 else
786 {
787 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200788 }
789 }
790
Andrey Andreevae634622012-12-17 10:30:18 +0200791 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200792 }
793
794 return FALSE;
795 }
796}
Richard Deurwaarder (Xeli)668d0092013-06-24 13:50:52 +0200797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798/* End of file Common.php */
Andrey Andreev13c818e2013-09-14 21:44:36 +0300799/* Location: ./system/core/Common.php */