blob: 225227d178b847453acadab175d34d0307425285 [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
Kevin Cuppd63e4012012-02-05 14:14:32 -0500180 set_status_header(503);
Greg Aker3a746652011-04-19 10:59:47 -0500181 exit('Unable to locate the specified class: '.$class.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600183
184 // Keep track of what we just loaded
185 is_loaded($class);
186
Pascal Kriete58560022010-11-10 16:01:20 -0500187 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600188 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400190}
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600192// --------------------------------------------------------------------
193
194/**
Greg Akerd96f8822011-12-27 16:23:47 -0600195* Keeps track of which libraries have been loaded. This function is
196* called by the load_class() function above
197*
198* @access public
199* @return array
200*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400201if ( ! function_exists('is_loaded'))
202{
Andrey Andreevd47baab2012-01-09 16:56:46 +0200203 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600204 {
205 static $_is_loaded = array();
206
207 if ($class != '')
208 {
209 $_is_loaded[strtolower($class)] = $class;
210 }
211
212 return $_is_loaded;
213 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400214}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600215
216// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000217
218/**
Greg Akerd96f8822011-12-27 16:23:47 -0600219* Loads the main config.php file
220*
221* This function lets us grab the config file even if the Config class
222* hasn't been instantiated yet
223*
224* @access private
225* @return array
226*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400227if ( ! function_exists('get_config'))
228{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600229 function &get_config($replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600231 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600233 if (isset($_config))
234 {
235 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200236 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600237
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100238 // Is the config file in the environment folder?
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100239 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100240 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200241 $file_path = APPPATH.'config/config.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100242 }
joelcox2035fd82011-01-16 16:50:36 +0100243
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600244 // Fetch the config file
joelcox2035fd82011-01-16 16:50:36 +0100245 if ( ! file_exists($file_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500247 set_status_header(503);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100248 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600249 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100250
joelcox2035fd82011-01-16 16:50:36 +0100251 require($file_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000252
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600253 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 if ( ! isset($config) OR ! is_array($config))
255 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500256 set_status_header(503);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 exit('Your config file does not appear to be formatted correctly.');
258 }
259
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600260 // Are any values being dynamically replaced?
261 if (count($replace) > 0)
262 {
263 foreach ($replace as $key => $val)
264 {
265 if (isset($config[$key]))
266 {
267 $config[$key] = $val;
268 }
269 }
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600272 return $_config[0] =& $config;
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
278/**
Greg Akerd96f8822011-12-27 16:23:47 -0600279* Returns the specified config item
280*
281* @access public
282* @return mixed
283*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400284if ( ! function_exists('config_item'))
285{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600286 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600288 static $_config_item = array();
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600290 if ( ! isset($_config_item[$item]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600292 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600294 if ( ! isset($config[$item]))
295 {
296 return FALSE;
297 }
298 $_config_item[$item] = $config[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600301 return $_config_item[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400303}
Derek Allard2067d1a2008-11-13 22:59:24 +0000304
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600305// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000306
307/**
Greg Akerd96f8822011-12-27 16:23:47 -0600308* Error Handler
309*
310* This function lets us invoke the exception class and
311* display errors using the standard error template located
312* in application/errors/errors.php
313* This function will send the error page directly to the
314* browser and exit.
315*
316* @access public
317* @return void
318*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400319if ( ! function_exists('show_error'))
320{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600321 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
322 {
323 $_error =& load_class('Exceptions', 'core');
324 echo $_error->show_error($heading, $message, 'error_general', $status_code);
325 exit;
326 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400327}
Derek Allard2067d1a2008-11-13 22:59:24 +0000328
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600329// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000330
331/**
Greg Akerd96f8822011-12-27 16:23:47 -0600332* 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* @access public
339* @return void
340*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400341if ( ! function_exists('show_404'))
342{
Derek Allard2ddc9492010-08-05 10:08:33 -0400343 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600344 {
345 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400346 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600347 exit;
348 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400349}
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600351// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353/**
Greg Akerd96f8822011-12-27 16:23:47 -0600354* Error Logging Interface
355*
356* We use this as a simple mechanism to access the logging
357* class and send messages to be logged.
358*
359* @access public
360* @return void
361*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400362if ( ! function_exists('log_message'))
363{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600364 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600366 static $_log;
367
368 if (config_item('log_threshold') == 0)
369 {
370 return;
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600373 $_log =& load_class('Log');
374 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400376}
Derek Allard2067d1a2008-11-13 22:59:24 +0000377
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600378// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000379
380/**
381 * Set HTTP Status Header
382 *
Greg Akerd96f8822011-12-27 16:23:47 -0600383 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200384 * @param int the status code
385 * @param string
Derek Jones817163a2009-07-11 17:05:58 +0000386 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200387 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400388if ( ! function_exists('set_status_header'))
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(
Andrey Andreev188abaf2012-01-07 19:09:42 +0200393 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
Andrey Andreev188abaf2012-01-07 19:09:42 +0200401 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
Andrey Andreev188abaf2012-01-07 19:09:42 +0200408 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
Andrey Andreev188abaf2012-01-07 19:09:42 +0200427 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
440 if (isset($stati[$code]) AND $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
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600450 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
451
Andrey Andreev188abaf2012-01-07 19:09:42 +0200452 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600453 {
454 header("Status: {$code} {$text}", TRUE);
455 }
456 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
457 {
458 header($server_protocol." {$code} {$text}", TRUE, $code);
459 }
460 else
461 {
462 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
463 }
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000469/**
Greg Akerd96f8822011-12-27 16:23:47 -0600470* Exception Handler
471*
472* This is the custom exception handler that is declaired at the top
473* of Codeigniter.php. The main reason we use this is to permit
474* PHP errors to be logged in our own log files since the user may
475* not have access to server logs. Since this function
476* effectively intercepts PHP errors, however, we also need
477* to display errors based on the current error_reporting level.
478* We do that with the use of a PHP error template.
479*
480* @access private
481* @return void
482*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400483if ( ! function_exists('_exception_handler'))
484{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600485 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200486 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600487 // We don't bother with "strict" notices since they tend to fill up
488 // the log file with excess information that isn't normally very helpful.
Barry Mienydd671972010-10-04 16:33:58 +0200489 // For example, if you are running PHP 5 and you use version 4 style
490 // class functions (without prefixes like "public", "private", etc.)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600491 // you'll get notices telling you that these have been deprecated.
492 if ($severity == E_STRICT)
493 {
494 return;
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600497 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200498
499 // Should we display the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600500 // level and add its bits with the severity bits to find out.
501 if (($severity & error_reporting()) == $severity)
502 {
503 $_error->show_php_error($severity, $message, $filepath, $line);
504 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000505
Derek Jones4b9c6292011-07-01 17:40:48 -0500506 // Should we log the error? No? We're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600507 if (config_item('log_threshold') == 0)
508 {
509 return;
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600512 $_error->log_exception($severity, $message, $filepath, $line);
513 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400514}
Derek Allard2067d1a2008-11-13 22:59:24 +0000515
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400516// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200517
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400518/**
519 * Remove Invisible Characters
520 *
521 * This prevents sandwiching null characters
522 * between ascii characters, like Java\0script.
523 *
Greg Akerd96f8822011-12-27 16:23:47 -0600524 * @access public
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400525 * @param string
526 * @return string
527 */
528if ( ! function_exists('remove_invisible_characters'))
529{
Pascal Kriete0ff50262011-04-05 14:52:03 -0400530 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500531 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400532 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200533
534 // every control character except newline (dec 10),
535 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400536 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500537 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400538 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
539 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500540 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200541
Pascal Kriete0ff50262011-04-05 14:52:03 -0400542 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500543
544 do
545 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400546 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500547 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400548 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500549
550 return $str;
551 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400552}
Derek Allard2067d1a2008-11-13 22:59:24 +0000553
kenjisfbac8b42011-08-25 10:51:44 +0900554// ------------------------------------------------------------------------
555
556/**
Greg Akerd96f8822011-12-27 16:23:47 -0600557* Returns HTML escaped variable
558*
559* @access public
560* @param mixed
561* @return mixed
562*/
kenjisfbac8b42011-08-25 10:51:44 +0900563if ( ! function_exists('html_escape'))
564{
565 function html_escape($var)
566 {
567 if (is_array($var))
568 {
569 return array_map('html_escape', $var);
570 }
Greg Aker5c1aa632011-12-25 01:24:29 -0600571 else
572 {
Greg Akerd96f8822011-12-27 16:23:47 -0600573 return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600574 }
Greg Aker5c1aa632011-12-25 01:24:29 -0600575 }
Greg Akerd96f8822011-12-27 16:23:47 -0600576}
Greg Aker5c1aa632011-12-25 01:24:29 -0600577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578/* End of file Common.php */
Andrey Andreev188abaf2012-01-07 19:09:42 +0200579/* Location: ./system/core/Common.php */