blob: 8b897776fe3539a847374416993454a32f1fa252 [file] [log] [blame]
Andrey Andreev188abaf2012-01-07 19:09:42 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060029 * Common Functions
30 *
31 * Loads the base classes and executes the request.
32 *
33 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030034 * @subpackage CodeIgniter
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060035 * @category Common Functions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060037 * @link http://codeigniter.com/user_guide/
38 */
39
40// ------------------------------------------------------------------------
41
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040042if ( ! function_exists('is_php'))
43{
Timothy Warrenad475052012-04-19 13:21:06 -040044 /**
45 * Determines if the current version of PHP is greater then the supplied value
46 *
47 * Since there are a few places where we conditionally test for PHP > 5
48 * we'll set a static variable.
49 *
50 * @param string
51 * @return bool TRUE if the current version is $version or higher
52 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060053 function is_php($version = '5.0.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000054 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060055 static $_is_php;
Andrey Andreevb7b43962012-02-27 22:45:48 +020056 $version = (string) $version;
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060058 if ( ! isset($_is_php[$version]))
59 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030060 $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060061 }
Derek Jones086ee5a2009-07-28 14:42:12 +000062
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060063 return $_is_php[$version];
64 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040065}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000066
Derek Jones086ee5a2009-07-28 14:42:12 +000067// ------------------------------------------------------------------------
68
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040069if ( ! function_exists('is_really_writable'))
70{
Timothy Warrenad475052012-04-19 13:21:06 -040071 /**
72 * Tests for file writability
73 *
74 * is_writable() returns TRUE on Windows servers when you really can't write to
75 * the file, based on the read-only attribute. is_writable() is also unreliable
76 * on Unix servers if safe_mode is on.
77 *
78 * @param string
79 * @return void
80 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060081 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060083 // If we're on a Unix server with safe_mode off we call is_writable
Andrey Andreevb7b43962012-02-27 22:45:48 +020084 if (DIRECTORY_SEPARATOR === '/' && (bool) @ini_get('safe_mode') === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060085 {
86 return is_writable($file);
87 }
Derek Allard2067d1a2008-11-13 22:59:24 +000088
Andrey Andreev188abaf2012-01-07 19:09:42 +020089 /* For Windows servers and safe_mode "on" installations we'll actually
90 * write a file then read it. Bah...
91 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060092 if (is_dir($file))
93 {
Andrey Andreev536b7712012-01-07 21:31:25 +020094 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060095 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
96 {
97 return FALSE;
98 }
99
100 fclose($fp);
101 @chmod($file, DIR_WRITE_MODE);
102 @unlink($file);
103 return TRUE;
104 }
Eric Barnes15083012011-03-21 22:13:12 -0400105 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
107 return FALSE;
108 }
109
110 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 return TRUE;
112 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400113}
Derek Allard2067d1a2008-11-13 22:59:24 +0000114
115// ------------------------------------------------------------------------
116
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400117if ( ! function_exists('load_class'))
118{
Timothy Warrenad475052012-04-19 13:21:06 -0400119 /**
120 * Class registry
121 *
122 * This function acts as a singleton. If the requested class does not
123 * exist it is instantiated and set to a static variable. If it has
124 * previously been instantiated the variable is returned.
125 *
126 * @param string the class name being requested
127 * @param string the directory where the class should be found
128 * @param string the class name prefix
129 * @return object
130 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600131 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600133 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200134
Andrey Andreev188abaf2012-01-07 19:09:42 +0200135 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600136 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600138 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600140
141 $name = FALSE;
142
Shane Pearsonab57a352011-08-22 16:11:20 -0500143 // Look for the class first in the local application/libraries folder
144 // then in the native system/libraries folder
145 foreach (array(APPPATH, BASEPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200146 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200147 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600148 {
149 $name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600151 if (class_exists($name) === FALSE)
152 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200153 require($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600156 break;
157 }
158 }
159
Andrey Andreev188abaf2012-01-07 19:09:42 +0200160 // Is the request a class extension? If so we load it too
Andrey Andreev536b7712012-01-07 21:31:25 +0200161 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200162 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600163 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600165 if (class_exists($name) === FALSE)
166 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200167 require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600168 }
169 }
170
171 // Did we find the class?
172 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Barry Mienydd671972010-10-04 16:33:58 +0200174 // Note: We use exit() rather then show_error() in order to avoid a
175 // self-referencing loop with the Excptions class
Kevin Cuppd63e4012012-02-05 14:14:32 -0500176 set_status_header(503);
Greg Aker3a746652011-04-19 10:59:47 -0500177 exit('Unable to locate the specified class: '.$class.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600179
180 // Keep track of what we just loaded
181 is_loaded($class);
182
Pascal Kriete58560022010-11-10 16:01:20 -0500183 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600184 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400186}
Derek Allard2067d1a2008-11-13 22:59:24 +0000187
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600188// --------------------------------------------------------------------
189
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400190if ( ! function_exists('is_loaded'))
191{
Timothy Warrenad475052012-04-19 13:21:06 -0400192 /**
193 * Keeps track of which libraries have been loaded. This function is
194 * called by the load_class() function above
195 *
196 * @param string
197 * @return array
198 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200199 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600200 {
201 static $_is_loaded = array();
202
203 if ($class != '')
204 {
205 $_is_loaded[strtolower($class)] = $class;
206 }
207
208 return $_is_loaded;
209 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400210}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600211
212// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000213
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400214if ( ! function_exists('get_config'))
215{
Timothy Warrenad475052012-04-19 13:21:06 -0400216 /**
217 * Loads the main config.php file
218 *
219 * This function lets us grab the config file even if the Config class
220 * hasn't been instantiated yet
221 *
222 * @param array
223 * @return array
224 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600225 function &get_config($replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600227 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600229 if (isset($_config))
230 {
231 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200232 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600233
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100234 // Is the config file in the environment folder?
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100235 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100236 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200237 $file_path = APPPATH.'config/config.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100238 }
joelcox2035fd82011-01-16 16:50:36 +0100239
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600240 // Fetch the config file
joelcox2035fd82011-01-16 16:50:36 +0100241 if ( ! file_exists($file_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500243 set_status_header(503);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100244 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600245 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100246
joelcox2035fd82011-01-16 16:50:36 +0100247 require($file_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600249 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 if ( ! isset($config) OR ! is_array($config))
251 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500252 set_status_header(503);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 exit('Your config file does not appear to be formatted correctly.');
254 }
255
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600256 // Are any values being dynamically replaced?
257 if (count($replace) > 0)
258 {
259 foreach ($replace as $key => $val)
260 {
261 if (isset($config[$key]))
262 {
263 $config[$key] = $val;
264 }
265 }
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600268 return $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400270}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600271
272// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000273
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400274if ( ! function_exists('config_item'))
275{
Timothy Warrenad475052012-04-19 13:21:06 -0400276 /**
277 * Returns the specified config item
278 *
279 * @param string
280 * @return mixed
281 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600282 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600284 static $_config_item = array();
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600286 if ( ! isset($_config_item[$item]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600288 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600290 if ( ! isset($config[$item]))
291 {
292 return FALSE;
293 }
294 $_config_item[$item] = $config[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600297 return $_config_item[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400299}
Derek Allard2067d1a2008-11-13 22:59:24 +0000300
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600301// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400303if ( ! function_exists('show_error'))
304{
Timothy Warrenad475052012-04-19 13:21:06 -0400305 /**
306 * Error Handler
307 *
308 * This function lets us invoke the exception class and
309 * display errors using the standard error template located
310 * in application/errors/errors.php
311 * This function will send the error page directly to the
312 * browser and exit.
313 *
314 * @param string
315 * @param int
316 * @param string
317 * @return void
318 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600319 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
320 {
321 $_error =& load_class('Exceptions', 'core');
322 echo $_error->show_error($heading, $message, 'error_general', $status_code);
323 exit;
324 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400325}
Derek Allard2067d1a2008-11-13 22:59:24 +0000326
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600327// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000328
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400329if ( ! function_exists('show_404'))
330{
Timothy Warrenad475052012-04-19 13:21:06 -0400331 /**
332 * 404 Page Handler
333 *
334 * This function is similar to the show_error() function above
335 * However, instead of the standard error template it displays
336 * 404 errors.
337 *
338 * @param string
339 * @param bool
340 * @return void
341 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400342 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600343 {
344 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400345 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600346 exit;
347 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400348}
Derek Allard2067d1a2008-11-13 22:59:24 +0000349
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600350// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400352if ( ! function_exists('log_message'))
353{
Timothy Warrenad475052012-04-19 13:21:06 -0400354 /**
355 * Error Logging Interface
356 *
357 * We use this as a simple mechanism to access the logging
358 * class and send messages to be logged.
359 *
360 * @param string
361 * @param string
362 * @param bool
363 * @return void
364 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600365 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600367 static $_log;
368
369 if (config_item('log_threshold') == 0)
370 {
371 return;
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600374 $_log =& load_class('Log');
375 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400377}
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600379// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000380
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400381if ( ! function_exists('set_status_header'))
382{
Timothy Warrenad475052012-04-19 13:21:06 -0400383 /**
384 * Set HTTP Status Header
385 *
386 * @param int the status code
387 * @param string
388 * @return void
389 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600390 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000391 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600392 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400393 200 => 'OK',
394 201 => 'Created',
395 202 => 'Accepted',
396 203 => 'Non-Authoritative Information',
397 204 => 'No Content',
398 205 => 'Reset Content',
399 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000400
Timothy Warrenad475052012-04-19 13:21:06 -0400401 300 => 'Multiple Choices',
402 301 => 'Moved Permanently',
403 302 => 'Found',
404 304 => 'Not Modified',
405 305 => 'Use Proxy',
406 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600407
Timothy Warrenad475052012-04-19 13:21:06 -0400408 400 => 'Bad Request',
409 401 => 'Unauthorized',
410 403 => 'Forbidden',
411 404 => 'Not Found',
412 405 => 'Method Not Allowed',
413 406 => 'Not Acceptable',
414 407 => 'Proxy Authentication Required',
415 408 => 'Request Timeout',
416 409 => 'Conflict',
417 410 => 'Gone',
418 411 => 'Length Required',
419 412 => 'Precondition Failed',
420 413 => 'Request Entity Too Large',
421 414 => 'Request-URI Too Long',
422 415 => 'Unsupported Media Type',
423 416 => 'Requested Range Not Satisfiable',
424 417 => 'Expectation Failed',
425 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600426
Timothy Warrenad475052012-04-19 13:21:06 -0400427 500 => 'Internal Server Error',
428 501 => 'Not Implemented',
429 502 => 'Bad Gateway',
430 503 => 'Service Unavailable',
431 504 => 'Gateway Timeout',
432 505 => 'HTTP Version Not Supported'
433 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600434
435 if ($code == '' OR ! is_numeric($code))
436 {
437 show_error('Status codes must be numeric', 500);
438 }
439
Andrey Andreevb7b43962012-02-27 22:45:48 +0200440 if (isset($stati[$code]) && $text == '')
Barry Mienydd671972010-10-04 16:33:58 +0200441 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600442 $text = $stati[$code];
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600445 if ($text == '')
446 {
Andrey Andreev188abaf2012-01-07 19:09:42 +0200447 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Andrey Andreevb7b43962012-02-27 22:45:48 +0200450 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600451
Andrey Andreev188abaf2012-01-07 19:09:42 +0200452 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600453 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200454 header('Status: '.$code.' '.$text, TRUE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600455 }
Andrey Andreevb7b43962012-02-27 22:45:48 +0200456 elseif ($server_protocol === 'HTTP/1.0')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600457 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200458 header('HTTP/1.0 '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600459 }
460 else
461 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200462 header('HTTP/1.1 '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600463 }
Derek Jones817163a2009-07-11 17:05:58 +0000464 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400465}
Barry Mienydd671972010-10-04 16:33:58 +0200466
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('_exception_handler'))
470{
Timothy Warrenad475052012-04-19 13:21:06 -0400471 /**
472 * Exception Handler
473 *
474 * This is the custom exception handler that is declaired at the top
475 * of Codeigniter.php. The main reason we use this is to permit
476 * PHP errors to be logged in our own log files since the user may
477 * not have access to server logs. Since this function
478 * effectively intercepts PHP errors, however, we also need
479 * to display errors based on the current error_reporting level.
480 * We do that with the use of a PHP error template.
481 *
482 * @param int
483 * @param string
484 * @param string
485 * @param int
486 * @return void
487 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600488 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200489 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600490 // We don't bother with "strict" notices since they tend to fill up
491 // the log file with excess information that isn't normally very helpful.
Barry Mienydd671972010-10-04 16:33:58 +0200492 // For example, if you are running PHP 5 and you use version 4 style
493 // class functions (without prefixes like "public", "private", etc.)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600494 // you'll get notices telling you that these have been deprecated.
495 if ($severity == E_STRICT)
496 {
497 return;
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600500 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200501
502 // Should we display the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600503 // level and add its bits with the severity bits to find out.
504 if (($severity & error_reporting()) == $severity)
505 {
506 $_error->show_php_error($severity, $message, $filepath, $line);
507 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000508
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300509 // Should we log the error? No? We're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600510 if (config_item('log_threshold') == 0)
511 {
512 return;
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600515 $_error->log_exception($severity, $message, $filepath, $line);
516 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400517}
Derek Allard2067d1a2008-11-13 22:59:24 +0000518
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400519// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200520
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400521if ( ! function_exists('remove_invisible_characters'))
522{
Timothy Warrenad475052012-04-19 13:21:06 -0400523 /**
524 * Remove Invisible Characters
525 *
526 * This prevents sandwiching null characters
527 * between ascii characters, like Java\0script.
528 *
529 * @param string
530 * @param bool
531 * @return string
532 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400533 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500534 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400535 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200536
537 // every control character except newline (dec 10),
538 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400539 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500540 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400541 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
542 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500543 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200544
Pascal Kriete0ff50262011-04-05 14:52:03 -0400545 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500546
547 do
548 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400549 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500550 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400551 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500552
553 return $str;
554 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400555}
Derek Allard2067d1a2008-11-13 22:59:24 +0000556
kenjisfbac8b42011-08-25 10:51:44 +0900557// ------------------------------------------------------------------------
558
kenjisfbac8b42011-08-25 10:51:44 +0900559if ( ! function_exists('html_escape'))
560{
Timothy Warrenad475052012-04-19 13:21:06 -0400561 /**
562 * Returns HTML escaped variable
563 *
564 * @param mixed
565 * @return mixed
566 */
kenjisfbac8b42011-08-25 10:51:44 +0900567 function html_escape($var)
568 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200569 return is_array($var)
570 ? array_map('html_escape', $var)
571 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600572 }
Greg Akerd96f8822011-12-27 16:23:47 -0600573}
Greg Aker5c1aa632011-12-25 01:24:29 -0600574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575/* End of file Common.php */
Andrey Andreevbb30d792012-03-26 15:49:09 +0300576/* Location: ./system/core/Common.php */