blob: 2f9c4ff43a187431a1ec11a051b6f4bfed7c76c9 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
28// ------------------------------------------------------------------------
29
30/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060031 * Common Functions
32 *
33 * Loads the base classes and executes the request.
34 *
35 * @package CodeIgniter
36 * @subpackage codeigniter
37 * @category Common Functions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060039 * @link http://codeigniter.com/user_guide/
40 */
41
42// ------------------------------------------------------------------------
43
44/**
Derek Jones086ee5a2009-07-28 14:42:12 +000045* 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*
Greg Akerd96f8822011-12-27 16:23:47 -060050* @access public
Derek Jones77b513b2009-08-06 14:39:25 +000051* @param string
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060052* @return bool TRUE if the current version is $version or higher
Derek Jones086ee5a2009-07-28 14:42:12 +000053*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040054if ( ! function_exists('is_php'))
55{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060056 function is_php($version = '5.0.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000057 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060058 static $_is_php;
59 $version = (string)$version;
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060061 if ( ! isset($_is_php[$version]))
62 {
63 $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
64 }
Derek Jones086ee5a2009-07-28 14:42:12 +000065
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060066 return $_is_php[$version];
67 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040068}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000069
Derek Jones086ee5a2009-07-28 14:42:12 +000070// ------------------------------------------------------------------------
71
72/**
Derek Allard2067d1a2008-11-13 22:59:24 +000073 * Tests for file writability
74 *
Barry Mienydd671972010-10-04 16:33:58 +020075 * is_writable() returns TRUE on Windows servers when you really can't write to
Andrey Andreev188abaf2012-01-07 19:09:42 +020076 * the file, based on the read-only attribute. is_writable() is also unreliable
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060077 * on Unix servers if safe_mode is on.
Derek Allard2067d1a2008-11-13 22:59:24 +000078 *
Andrey Andreev188abaf2012-01-07 19:09:42 +020079 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020081 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040082if ( ! function_exists('is_really_writable'))
83{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060084 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060086 // If we're on a Unix server with safe_mode off we call is_writable
Andrey Andreev188abaf2012-01-07 19:09:42 +020087 if (DIRECTORY_SEPARATOR === '/' AND @ini_get('safe_mode') == FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060088 {
89 return is_writable($file);
90 }
Derek Allard2067d1a2008-11-13 22:59:24 +000091
Andrey Andreev188abaf2012-01-07 19:09:42 +020092 /* For Windows servers and safe_mode "on" installations we'll actually
93 * write a file then read it. Bah...
94 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060095 if (is_dir($file))
96 {
Andrey Andreev536b7712012-01-07 21:31:25 +020097 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060098 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
99 {
100 return FALSE;
101 }
102
103 fclose($fp);
104 @chmod($file, DIR_WRITE_MODE);
105 @unlink($file);
106 return TRUE;
107 }
Eric Barnes15083012011-03-21 22:13:12 -0400108 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
110 return FALSE;
111 }
112
113 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 return TRUE;
115 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400116}
Derek Allard2067d1a2008-11-13 22:59:24 +0000117
118// ------------------------------------------------------------------------
119
120/**
Greg Akerd96f8822011-12-27 16:23:47 -0600121* Class registry
122*
123* This function acts as a singleton. If the requested class does not
124* exist it is instantiated and set to a static variable. If it has
125* previously been instantiated the variable is returned.
126*
127* @access public
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*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400133if ( ! function_exists('load_class'))
134{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600135 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600137 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200138
Andrey Andreev188abaf2012-01-07 19:09:42 +0200139 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600140 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600142 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600144
145 $name = FALSE;
146
Shane Pearsonab57a352011-08-22 16:11:20 -0500147 // Look for the class first in the local application/libraries folder
148 // then in the native system/libraries folder
149 foreach (array(APPPATH, BASEPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200150 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200151 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600152 {
153 $name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600155 if (class_exists($name) === FALSE)
156 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200157 require($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600160 break;
161 }
162 }
163
Andrey Andreev188abaf2012-01-07 19:09:42 +0200164 // Is the request a class extension? If so we load it too
Andrey Andreev536b7712012-01-07 21:31:25 +0200165 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200166 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600167 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600169 if (class_exists($name) === FALSE)
170 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200171 require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600172 }
173 }
174
175 // Did we find the class?
176 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Barry Mienydd671972010-10-04 16:33:58 +0200178 // Note: We use exit() rather then show_error() in order to avoid a
179 // self-referencing loop with the Excptions class
Greg Aker3a746652011-04-19 10:59:47 -0500180 exit('Unable to locate the specified class: '.$class.'.php');
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
193/**
Greg Akerd96f8822011-12-27 16:23:47 -0600194* Keeps track of which libraries have been loaded. This function is
195* called by the load_class() function above
196*
197* @access public
198* @return array
199*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400200if ( ! function_exists('is_loaded'))
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
206 if ($class != '')
207 {
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
217/**
Greg Akerd96f8822011-12-27 16:23:47 -0600218* Loads the main config.php file
219*
220* This function lets us grab the config file even if the Config class
221* hasn't been instantiated yet
222*
223* @access private
224* @return array
225*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400226if ( ! function_exists('get_config'))
227{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600228 function &get_config($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
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600232 if (isset($_config))
233 {
234 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200235 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600236
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100237 // Is the config file in the environment folder?
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100238 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100239 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200240 $file_path = APPPATH.'config/config.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100241 }
joelcox2035fd82011-01-16 16:50:36 +0100242
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600243 // Fetch the config file
joelcox2035fd82011-01-16 16:50:36 +0100244 if ( ! file_exists($file_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100246 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600247 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100248
joelcox2035fd82011-01-16 16:50:36 +0100249 require($file_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000250
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600251 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 if ( ! isset($config) OR ! is_array($config))
253 {
254 exit('Your config file does not appear to be formatted correctly.');
255 }
256
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600257 // Are any values being dynamically replaced?
258 if (count($replace) > 0)
259 {
260 foreach ($replace as $key => $val)
261 {
262 if (isset($config[$key]))
263 {
264 $config[$key] = $val;
265 }
266 }
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600269 return $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400271}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600272
273// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
275/**
Greg Akerd96f8822011-12-27 16:23:47 -0600276* Returns the specified config item
277*
278* @access public
279* @return mixed
280*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400281if ( ! function_exists('config_item'))
282{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600283 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600285 static $_config_item = array();
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600287 if ( ! isset($_config_item[$item]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600289 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600291 if ( ! isset($config[$item]))
292 {
293 return FALSE;
294 }
295 $_config_item[$item] = $config[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600298 return $_config_item[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400300}
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600302// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
304/**
Greg Akerd96f8822011-12-27 16:23:47 -0600305* Error Handler
306*
307* This function lets us invoke the exception class and
308* display errors using the standard error template located
309* in application/errors/errors.php
310* This function will send the error page directly to the
311* browser and exit.
312*
313* @access public
314* @return void
315*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400316if ( ! function_exists('show_error'))
317{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600318 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
319 {
320 $_error =& load_class('Exceptions', 'core');
321 echo $_error->show_error($heading, $message, 'error_general', $status_code);
322 exit;
323 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400324}
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600326// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
328/**
Greg Akerd96f8822011-12-27 16:23:47 -0600329* 404 Page Handler
330*
331* This function is similar to the show_error() function above
332* However, instead of the standard error template it displays
333* 404 errors.
334*
335* @access public
336* @return void
337*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400338if ( ! function_exists('show_404'))
339{
Derek Allard2ddc9492010-08-05 10:08:33 -0400340 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600341 {
342 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400343 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600344 exit;
345 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400346}
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600348// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000349
350/**
Greg Akerd96f8822011-12-27 16:23:47 -0600351* Error Logging Interface
352*
353* We use this as a simple mechanism to access the logging
354* class and send messages to be logged.
355*
356* @access public
357* @return void
358*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400359if ( ! function_exists('log_message'))
360{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600361 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600363 static $_log;
364
365 if (config_item('log_threshold') == 0)
366 {
367 return;
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600370 $_log =& load_class('Log');
371 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400373}
Derek Allard2067d1a2008-11-13 22:59:24 +0000374
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600375// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000376
377/**
378 * Set HTTP Status Header
379 *
Greg Akerd96f8822011-12-27 16:23:47 -0600380 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200381 * @param int the status code
382 * @param string
Derek Jones817163a2009-07-11 17:05:58 +0000383 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200384 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400385if ( ! function_exists('set_status_header'))
386{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600387 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000388 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600389 $stati = array(
Andrey Andreev188abaf2012-01-07 19:09:42 +0200390 200 => 'OK',
391 201 => 'Created',
392 202 => 'Accepted',
393 203 => 'Non-Authoritative Information',
394 204 => 'No Content',
395 205 => 'Reset Content',
396 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000397
Andrey Andreev188abaf2012-01-07 19:09:42 +0200398 300 => 'Multiple Choices',
399 301 => 'Moved Permanently',
400 302 => 'Found',
401 304 => 'Not Modified',
402 305 => 'Use Proxy',
403 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600404
Andrey Andreev188abaf2012-01-07 19:09:42 +0200405 400 => 'Bad Request',
406 401 => 'Unauthorized',
407 403 => 'Forbidden',
408 404 => 'Not Found',
409 405 => 'Method Not Allowed',
410 406 => 'Not Acceptable',
411 407 => 'Proxy Authentication Required',
412 408 => 'Request Timeout',
413 409 => 'Conflict',
414 410 => 'Gone',
415 411 => 'Length Required',
416 412 => 'Precondition Failed',
417 413 => 'Request Entity Too Large',
418 414 => 'Request-URI Too Long',
419 415 => 'Unsupported Media Type',
420 416 => 'Requested Range Not Satisfiable',
421 417 => 'Expectation Failed',
422 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600423
Andrey Andreev188abaf2012-01-07 19:09:42 +0200424 500 => 'Internal Server Error',
425 501 => 'Not Implemented',
426 502 => 'Bad Gateway',
427 503 => 'Service Unavailable',
428 504 => 'Gateway Timeout',
429 505 => 'HTTP Version Not Supported'
430 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600431
432 if ($code == '' OR ! is_numeric($code))
433 {
434 show_error('Status codes must be numeric', 500);
435 }
436
437 if (isset($stati[$code]) AND $text == '')
Barry Mienydd671972010-10-04 16:33:58 +0200438 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600439 $text = $stati[$code];
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600442 if ($text == '')
443 {
Andrey Andreev188abaf2012-01-07 19:09:42 +0200444 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 -0600445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600447 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
448
Andrey Andreev188abaf2012-01-07 19:09:42 +0200449 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600450 {
451 header("Status: {$code} {$text}", TRUE);
452 }
453 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
454 {
455 header($server_protocol." {$code} {$text}", TRUE, $code);
456 }
457 else
458 {
459 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
460 }
Derek Jones817163a2009-07-11 17:05:58 +0000461 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400462}
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600464// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466/**
Greg Akerd96f8822011-12-27 16:23:47 -0600467* Exception Handler
468*
469* This is the custom exception handler that is declaired at the top
470* of Codeigniter.php. The main reason we use this is to permit
471* PHP errors to be logged in our own log files since the user may
472* not have access to server logs. Since this function
473* effectively intercepts PHP errors, however, we also need
474* to display errors based on the current error_reporting level.
475* We do that with the use of a PHP error template.
476*
477* @access private
478* @return void
479*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400480if ( ! function_exists('_exception_handler'))
481{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600482 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200483 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600484 // We don't bother with "strict" notices since they tend to fill up
485 // the log file with excess information that isn't normally very helpful.
Barry Mienydd671972010-10-04 16:33:58 +0200486 // For example, if you are running PHP 5 and you use version 4 style
487 // class functions (without prefixes like "public", "private", etc.)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600488 // you'll get notices telling you that these have been deprecated.
489 if ($severity == E_STRICT)
490 {
491 return;
492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600494 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200495
496 // Should we display the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600497 // level and add its bits with the severity bits to find out.
498 if (($severity & error_reporting()) == $severity)
499 {
500 $_error->show_php_error($severity, $message, $filepath, $line);
501 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000502
Derek Jones4b9c6292011-07-01 17:40:48 -0500503 // Should we log the error? No? We're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600504 if (config_item('log_threshold') == 0)
505 {
506 return;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600509 $_error->log_exception($severity, $message, $filepath, $line);
510 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400511}
Derek Allard2067d1a2008-11-13 22:59:24 +0000512
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400513// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200514
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400515/**
516 * Remove Invisible Characters
517 *
518 * This prevents sandwiching null characters
519 * between ascii characters, like Java\0script.
520 *
Greg Akerd96f8822011-12-27 16:23:47 -0600521 * @access public
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400522 * @param string
523 * @return string
524 */
525if ( ! function_exists('remove_invisible_characters'))
526{
Pascal Kriete0ff50262011-04-05 14:52:03 -0400527 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500528 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400529 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200530
531 // every control character except newline (dec 10),
532 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400533 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500534 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400535 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
536 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500537 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200538
Pascal Kriete0ff50262011-04-05 14:52:03 -0400539 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500540
541 do
542 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400543 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500544 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400545 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500546
547 return $str;
548 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400549}
Derek Allard2067d1a2008-11-13 22:59:24 +0000550
kenjisfbac8b42011-08-25 10:51:44 +0900551// ------------------------------------------------------------------------
552
553/**
Greg Akerd96f8822011-12-27 16:23:47 -0600554* Returns HTML escaped variable
555*
556* @access public
557* @param mixed
558* @return mixed
559*/
kenjisfbac8b42011-08-25 10:51:44 +0900560if ( ! function_exists('html_escape'))
561{
562 function html_escape($var)
563 {
564 if (is_array($var))
565 {
566 return array_map('html_escape', $var);
567 }
Greg Aker5c1aa632011-12-25 01:24:29 -0600568 else
569 {
Greg Akerd96f8822011-12-27 16:23:47 -0600570 return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600571 }
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 Andreev188abaf2012-01-07 19:09:42 +0200576/* Location: ./system/core/Common.php */