blob: 2dd31d3e985dae56925fba4b32ffd2b76f69e3e9 [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 Andreeva52c7752012-10-11 10:54:02 +0300153 require_once($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 Andreeva52c7752012-10-11 10:54:02 +0300167 require_once(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
Andrey Andreev3fb02672012-10-22 16:48:01 +0300333if ( ! function_exists('is_https'))
334{
335 /**
336 * Is HTTPS?
337 *
338 * Determines if the application is accessed via an encrypted
339 * (HTTPS) connection.
340 *
341 * @return bool
342 */
343 function is_https()
344 {
345 return ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off');
346 }
347}
348
349// ------------------------------------------------------------------------
350
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400351if ( ! function_exists('show_error'))
352{
Timothy Warrenad475052012-04-19 13:21:06 -0400353 /**
354 * Error Handler
355 *
356 * This function lets us invoke the exception class and
357 * display errors using the standard error template located
358 * in application/errors/errors.php
359 * This function will send the error page directly to the
360 * browser and exit.
361 *
362 * @param string
363 * @param int
364 * @param string
365 * @return void
366 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600367 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
368 {
369 $_error =& load_class('Exceptions', 'core');
370 echo $_error->show_error($heading, $message, 'error_general', $status_code);
371 exit;
372 }
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 Allard2067d1a2008-11-13 22:59:24 +0000376
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400377if ( ! function_exists('show_404'))
378{
Timothy Warrenad475052012-04-19 13:21:06 -0400379 /**
380 * 404 Page Handler
381 *
382 * This function is similar to the show_error() function above
383 * However, instead of the standard error template it displays
384 * 404 errors.
385 *
386 * @param string
387 * @param bool
388 * @return void
389 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400390 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600391 {
392 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400393 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600394 exit;
395 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400396}
Derek Allard2067d1a2008-11-13 22:59:24 +0000397
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600398// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000399
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400400if ( ! function_exists('log_message'))
401{
Timothy Warrenad475052012-04-19 13:21:06 -0400402 /**
403 * Error Logging Interface
404 *
405 * We use this as a simple mechanism to access the logging
406 * class and send messages to be logged.
407 *
408 * @param string
409 * @param string
410 * @param bool
411 * @return void
412 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600413 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600415 static $_log;
416
Alex Bilbieed944a32012-06-02 11:07:47 +0100417 if (config_item('log_threshold') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600418 {
419 return;
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600422 $_log =& load_class('Log');
423 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400425}
Derek Allard2067d1a2008-11-13 22:59:24 +0000426
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600427// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000428
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400429if ( ! function_exists('set_status_header'))
430{
Timothy Warrenad475052012-04-19 13:21:06 -0400431 /**
432 * Set HTTP Status Header
433 *
434 * @param int the status code
435 * @param string
436 * @return void
437 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600438 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000439 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600440 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400441 200 => 'OK',
442 201 => 'Created',
443 202 => 'Accepted',
444 203 => 'Non-Authoritative Information',
445 204 => 'No Content',
446 205 => 'Reset Content',
447 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000448
Timothy Warrenad475052012-04-19 13:21:06 -0400449 300 => 'Multiple Choices',
450 301 => 'Moved Permanently',
451 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300452 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400453 304 => 'Not Modified',
454 305 => 'Use Proxy',
455 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600456
Timothy Warrenad475052012-04-19 13:21:06 -0400457 400 => 'Bad Request',
458 401 => 'Unauthorized',
459 403 => 'Forbidden',
460 404 => 'Not Found',
461 405 => 'Method Not Allowed',
462 406 => 'Not Acceptable',
463 407 => 'Proxy Authentication Required',
464 408 => 'Request Timeout',
465 409 => 'Conflict',
466 410 => 'Gone',
467 411 => 'Length Required',
468 412 => 'Precondition Failed',
469 413 => 'Request Entity Too Large',
470 414 => 'Request-URI Too Long',
471 415 => 'Unsupported Media Type',
472 416 => 'Requested Range Not Satisfiable',
473 417 => 'Expectation Failed',
474 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600475
Timothy Warrenad475052012-04-19 13:21:06 -0400476 500 => 'Internal Server Error',
477 501 => 'Not Implemented',
478 502 => 'Bad Gateway',
479 503 => 'Service Unavailable',
480 504 => 'Gateway Timeout',
481 505 => 'HTTP Version Not Supported'
482 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600483
Andrey Andreev51d6d842012-06-15 16:41:09 +0300484 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600485 {
486 show_error('Status codes must be numeric', 500);
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Andrey Andreev51d6d842012-06-15 16:41:09 +0300489 is_int($code) OR $code = (int) $code;
490
491 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600492 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300493 if (isset($stati[$code]))
494 {
495 $text = $stati[$code];
496 }
497 else
498 {
499 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
500 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Andrey Andreevb7b43962012-02-27 22:45:48 +0200503 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600504
Andrey Andreev188abaf2012-01-07 19:09:42 +0200505 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600506 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200507 header('Status: '.$code.' '.$text, TRUE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600508 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600509 else
510 {
Daniel Morris7885c5c2012-10-04 21:44:09 +0100511 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600512 }
Derek Jones817163a2009-07-11 17:05:58 +0000513 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400514}
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600516// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000517
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400518if ( ! function_exists('_exception_handler'))
519{
Timothy Warrenad475052012-04-19 13:21:06 -0400520 /**
521 * Exception Handler
522 *
523 * This is the custom exception handler that is declaired at the top
524 * of Codeigniter.php. The main reason we use this is to permit
525 * PHP errors to be logged in our own log files since the user may
526 * not have access to server logs. Since this function
527 * effectively intercepts PHP errors, however, we also need
528 * to display errors based on the current error_reporting level.
529 * We do that with the use of a PHP error template.
530 *
531 * @param int
532 * @param string
533 * @param string
534 * @param int
535 * @return void
536 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600537 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200538 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600539 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200540
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300541 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600542 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300543 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600544 {
545 return;
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Francesco Negri312bdc52012-08-04 07:32:19 -0400548 // Should we display the error?
549 if ((bool) ini_get('display_errors') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
551 $_error->show_php_error($severity, $message, $filepath, $line);
552 }
553
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600554 $_error->log_exception($severity, $message, $filepath, $line);
555 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400556}
Derek Allard2067d1a2008-11-13 22:59:24 +0000557
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400558// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200559
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400560if ( ! function_exists('remove_invisible_characters'))
561{
Timothy Warrenad475052012-04-19 13:21:06 -0400562 /**
563 * Remove Invisible Characters
564 *
565 * This prevents sandwiching null characters
566 * between ascii characters, like Java\0script.
567 *
568 * @param string
569 * @param bool
570 * @return string
571 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400572 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500573 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400574 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200575
576 // every control character except newline (dec 10),
577 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400578 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500579 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400580 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
581 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500582 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200583
Pascal Kriete0ff50262011-04-05 14:52:03 -0400584 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500585
586 do
587 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400588 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500589 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400590 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500591
592 return $str;
593 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400594}
Derek Allard2067d1a2008-11-13 22:59:24 +0000595
kenjisfbac8b42011-08-25 10:51:44 +0900596// ------------------------------------------------------------------------
597
kenjisfbac8b42011-08-25 10:51:44 +0900598if ( ! function_exists('html_escape'))
599{
Timothy Warrenad475052012-04-19 13:21:06 -0400600 /**
601 * Returns HTML escaped variable
602 *
603 * @param mixed
604 * @return mixed
605 */
kenjisfbac8b42011-08-25 10:51:44 +0900606 function html_escape($var)
607 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200608 return is_array($var)
609 ? array_map('html_escape', $var)
610 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600611 }
Greg Akerd96f8822011-12-27 16:23:47 -0600612}
Greg Aker5c1aa632011-12-25 01:24:29 -0600613
Chad Furmana1abada2012-07-29 01:03:50 -0400614// ------------------------------------------------------------------------
615
616if ( ! function_exists('_stringify_attributes'))
617{
618 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300619 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400620 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300621 * Helper function used to convert a string, array, or object
622 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400623 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300624 * @param mixed string, array, object
625 * @param bool
626 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400627 */
628 function _stringify_attributes($attributes, $js = FALSE)
629 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300630 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400631
632 if (empty($attributes))
633 {
634 return $atts;
635 }
636
637 if (is_string($attributes))
638 {
639 return ' '.$attributes;
640 }
641
642 $attributes = (array) $attributes;
643
644 foreach ($attributes as $key => $val)
645 {
646 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
647 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300648
Chad Furmana1abada2012-07-29 01:03:50 -0400649 return rtrim($atts, ',');
650 }
651}
652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653/* End of file Common.php */
Andrey Andreevbdb99992012-07-30 17:38:05 +0300654/* Location: ./system/core/Common.php */