blob: 06b1622640d7934d032f62a42296ad9b6fb22975 [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 *
Andrey Andreev24bd2302012-06-05 22:29:12 +030047 * Since there are a few places where we conditionally test for PHP > 5.3
Timothy Warrenad475052012-04-19 13:21:06 -040048 * we'll set a static variable.
49 *
50 * @param string
51 * @return bool TRUE if the current version is $version or higher
52 */
Andrey Andreev24bd2302012-06-05 22:29:12 +030053 function is_php($version = '5.3.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
vlakofffe8fe452012-07-11 19:56:50 +0200175 // self-referencing loop with the Exceptions 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
Alex Bilbieed944a32012-06-02 11:07:47 +0100203 if ($class !== '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600204 {
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
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300234 $file_path = APPPATH.'config/config.php';
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300235 $found = FALSE;
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300236 if (file_exists($file_path))
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300237 {
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300238 $found = TRUE;
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300239 require($file_path);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100240 }
joelcox2035fd82011-01-16 16:50:36 +0100241
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300242 // Is the config file in the environment folder?
Andrey Andreev92aa67c2012-06-09 23:37:17 +0300243 if (defined('ENVIRONMENT') && file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300245 require($file_path);
246 }
247 elseif ( ! $found)
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300248 {
249 set_status_header(503);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100250 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600251 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100252
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
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 {
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
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300307if ( ! function_exists('get_mimes'))
308{
309 /**
310 * Returns the MIME types array from config/mimes.php
311 *
312 * @return array
313 */
314 function &get_mimes()
315 {
316 static $_mimes = array();
317
318 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
319 {
320 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
321 }
322 elseif (is_file(APPPATH.'config/mimes.php'))
323 {
324 $_mimes = include(APPPATH.'config/mimes.php');
325 }
326
327 return $_mimes;
328 }
329}
330
331// ------------------------------------------------------------------------
332
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400333if ( ! function_exists('show_error'))
334{
Timothy Warrenad475052012-04-19 13:21:06 -0400335 /**
336 * Error Handler
337 *
338 * This function lets us invoke the exception class and
339 * display errors using the standard error template located
340 * in application/errors/errors.php
341 * This function will send the error page directly to the
342 * browser and exit.
343 *
344 * @param string
345 * @param int
346 * @param string
347 * @return void
348 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600349 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
350 {
351 $_error =& load_class('Exceptions', 'core');
352 echo $_error->show_error($heading, $message, 'error_general', $status_code);
353 exit;
354 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400355}
Derek Allard2067d1a2008-11-13 22:59:24 +0000356
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600357// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000358
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400359if ( ! function_exists('show_404'))
360{
Timothy Warrenad475052012-04-19 13:21:06 -0400361 /**
362 * 404 Page Handler
363 *
364 * This function is similar to the show_error() function above
365 * However, instead of the standard error template it displays
366 * 404 errors.
367 *
368 * @param string
369 * @param bool
370 * @return void
371 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400372 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600373 {
374 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400375 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600376 exit;
377 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400378}
Derek Allard2067d1a2008-11-13 22:59:24 +0000379
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600380// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000381
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400382if ( ! function_exists('log_message'))
383{
Timothy Warrenad475052012-04-19 13:21:06 -0400384 /**
385 * Error Logging Interface
386 *
387 * We use this as a simple mechanism to access the logging
388 * class and send messages to be logged.
389 *
390 * @param string
391 * @param string
392 * @param bool
393 * @return void
394 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600395 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600397 static $_log;
398
Alex Bilbieed944a32012-06-02 11:07:47 +0100399 if (config_item('log_threshold') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600400 {
401 return;
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600404 $_log =& load_class('Log');
405 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400407}
Derek Allard2067d1a2008-11-13 22:59:24 +0000408
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600409// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000410
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400411if ( ! function_exists('set_status_header'))
412{
Timothy Warrenad475052012-04-19 13:21:06 -0400413 /**
414 * Set HTTP Status Header
415 *
416 * @param int the status code
417 * @param string
418 * @return void
419 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600420 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000421 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600422 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400423 200 => 'OK',
424 201 => 'Created',
425 202 => 'Accepted',
426 203 => 'Non-Authoritative Information',
427 204 => 'No Content',
428 205 => 'Reset Content',
429 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000430
Timothy Warrenad475052012-04-19 13:21:06 -0400431 300 => 'Multiple Choices',
432 301 => 'Moved Permanently',
433 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300434 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400435 304 => 'Not Modified',
436 305 => 'Use Proxy',
437 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600438
Timothy Warrenad475052012-04-19 13:21:06 -0400439 400 => 'Bad Request',
440 401 => 'Unauthorized',
441 403 => 'Forbidden',
442 404 => 'Not Found',
443 405 => 'Method Not Allowed',
444 406 => 'Not Acceptable',
445 407 => 'Proxy Authentication Required',
446 408 => 'Request Timeout',
447 409 => 'Conflict',
448 410 => 'Gone',
449 411 => 'Length Required',
450 412 => 'Precondition Failed',
451 413 => 'Request Entity Too Large',
452 414 => 'Request-URI Too Long',
453 415 => 'Unsupported Media Type',
454 416 => 'Requested Range Not Satisfiable',
455 417 => 'Expectation Failed',
456 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600457
Timothy Warrenad475052012-04-19 13:21:06 -0400458 500 => 'Internal Server Error',
459 501 => 'Not Implemented',
460 502 => 'Bad Gateway',
461 503 => 'Service Unavailable',
462 504 => 'Gateway Timeout',
463 505 => 'HTTP Version Not Supported'
464 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600465
Andrey Andreev51d6d842012-06-15 16:41:09 +0300466 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600467 {
468 show_error('Status codes must be numeric', 500);
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Andrey Andreev51d6d842012-06-15 16:41:09 +0300471 is_int($code) OR $code = (int) $code;
472
473 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600474 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300475 if (isset($stati[$code]))
476 {
477 $text = $stati[$code];
478 }
479 else
480 {
481 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
482 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Andrey Andreevb7b43962012-02-27 22:45:48 +0200485 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600486
Andrey Andreev188abaf2012-01-07 19:09:42 +0200487 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600488 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200489 header('Status: '.$code.' '.$text, TRUE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600490 }
Andrey Andreevb7b43962012-02-27 22:45:48 +0200491 elseif ($server_protocol === 'HTTP/1.0')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600492 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200493 header('HTTP/1.0 '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600494 }
495 else
496 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200497 header('HTTP/1.1 '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600498 }
Derek Jones817163a2009-07-11 17:05:58 +0000499 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400500}
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600502// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000503
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400504if ( ! function_exists('_exception_handler'))
505{
Timothy Warrenad475052012-04-19 13:21:06 -0400506 /**
507 * Exception Handler
508 *
509 * This is the custom exception handler that is declaired at the top
510 * of Codeigniter.php. The main reason we use this is to permit
511 * PHP errors to be logged in our own log files since the user may
512 * not have access to server logs. Since this function
513 * effectively intercepts PHP errors, however, we also need
514 * to display errors based on the current error_reporting level.
515 * We do that with the use of a PHP error template.
516 *
517 * @param int
518 * @param string
519 * @param string
520 * @param int
521 * @return void
522 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600523 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200524 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600525 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200526
527 // Should we display the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600528 // level and add its bits with the severity bits to find out.
Christopher Guineyf3b7fa22012-07-04 12:30:37 -0700529 // And respect display_errors
Christopher Guiney94e36fd2012-06-28 21:34:21 -0700530 if (($severity & error_reporting()) === $severity && (bool) ini_get('display_errors') === TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600531 {
532 $_error->show_php_error($severity, $message, $filepath, $line);
533 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000534
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300535 // Should we log the error? No? We're done...
Alex Bilbieed944a32012-06-02 11:07:47 +0100536 if (config_item('log_threshold') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600537 {
538 return;
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600541 $_error->log_exception($severity, $message, $filepath, $line);
542 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400543}
Derek Allard2067d1a2008-11-13 22:59:24 +0000544
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400545// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200546
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400547if ( ! function_exists('remove_invisible_characters'))
548{
Timothy Warrenad475052012-04-19 13:21:06 -0400549 /**
550 * Remove Invisible Characters
551 *
552 * This prevents sandwiching null characters
553 * between ascii characters, like Java\0script.
554 *
555 * @param string
556 * @param bool
557 * @return string
558 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400559 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500560 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400561 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200562
563 // every control character except newline (dec 10),
564 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400565 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500566 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400567 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
568 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500569 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200570
Pascal Kriete0ff50262011-04-05 14:52:03 -0400571 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500572
573 do
574 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400575 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500576 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400577 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500578
579 return $str;
580 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400581}
Derek Allard2067d1a2008-11-13 22:59:24 +0000582
kenjisfbac8b42011-08-25 10:51:44 +0900583// ------------------------------------------------------------------------
584
kenjisfbac8b42011-08-25 10:51:44 +0900585if ( ! function_exists('html_escape'))
586{
Timothy Warrenad475052012-04-19 13:21:06 -0400587 /**
588 * Returns HTML escaped variable
589 *
590 * @param mixed
591 * @return mixed
592 */
kenjisfbac8b42011-08-25 10:51:44 +0900593 function html_escape($var)
594 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200595 return is_array($var)
596 ? array_map('html_escape', $var)
597 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600598 }
Greg Akerd96f8822011-12-27 16:23:47 -0600599}
Greg Aker5c1aa632011-12-25 01:24:29 -0600600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601/* End of file Common.php */
Andrey Andreevbb30d792012-03-26 15:49:09 +0300602/* Location: ./system/core/Common.php */