blob: 075b221bd8893afe07b67e6aff51556a51efa2e5 [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
34 * @subpackage codeigniter
35 * @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
Timothy Warrenad475052012-04-19 13:21:06 -040042
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040043if ( ! function_exists('is_php'))
44{
Timothy Warrenad475052012-04-19 13:21:06 -040045 /**
46 * Determines if the current version of PHP is greater then the supplied value
47 *
48 * Since there are a few places where we conditionally test for PHP > 5
49 * we'll set a static variable.
50 *
51 * @param string
52 * @return bool TRUE if the current version is $version or higher
53 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060054 function is_php($version = '5.0.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000055 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060056 static $_is_php;
Andrey Andreevb7b43962012-02-27 22:45:48 +020057 $version = (string) $version;
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060059 if ( ! isset($_is_php[$version]))
60 {
61 $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
62 }
Derek Jones086ee5a2009-07-28 14:42:12 +000063
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060064 return $_is_php[$version];
65 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040066}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000067
Derek Jones086ee5a2009-07-28 14:42:12 +000068// ------------------------------------------------------------------------
69
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040070if ( ! function_exists('is_really_writable'))
71{
Timothy Warrenad475052012-04-19 13:21:06 -040072 /**
73 * Tests for file writability
74 *
75 * is_writable() returns TRUE on Windows servers when you really can't write to
76 * the file, based on the read-only attribute. is_writable() is also unreliable
77 * on Unix servers if safe_mode is on.
78 *
79 * @param string
80 * @return void
81 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060082 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060084 // If we're on a Unix server with safe_mode off we call is_writable
Andrey Andreevb7b43962012-02-27 22:45:48 +020085 if (DIRECTORY_SEPARATOR === '/' && (bool) @ini_get('safe_mode') === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060086 {
87 return is_writable($file);
88 }
Derek Allard2067d1a2008-11-13 22:59:24 +000089
Andrey Andreev188abaf2012-01-07 19:09:42 +020090 /* For Windows servers and safe_mode "on" installations we'll actually
91 * write a file then read it. Bah...
92 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060093 if (is_dir($file))
94 {
Andrey Andreev536b7712012-01-07 21:31:25 +020095 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060096 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
97 {
98 return FALSE;
99 }
100
101 fclose($fp);
102 @chmod($file, DIR_WRITE_MODE);
103 @unlink($file);
104 return TRUE;
105 }
Eric Barnes15083012011-03-21 22:13:12 -0400106 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
108 return FALSE;
109 }
110
111 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 return TRUE;
113 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400114}
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
116// ------------------------------------------------------------------------
117
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400118if ( ! function_exists('load_class'))
119{
Timothy Warrenad475052012-04-19 13:21:06 -0400120 /**
121 * 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 * @param string the class name being requested
128 * @param string the directory where the class should be found
129 * @param string the class name prefix
130 * @return object
131 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600132 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600134 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200135
Andrey Andreev188abaf2012-01-07 19:09:42 +0200136 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600137 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600139 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600141
142 $name = FALSE;
143
Shane Pearsonab57a352011-08-22 16:11:20 -0500144 // Look for the class first in the local application/libraries folder
145 // then in the native system/libraries folder
146 foreach (array(APPPATH, BASEPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200147 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200148 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600149 {
150 $name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600152 if (class_exists($name) === FALSE)
153 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200154 require($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600157 break;
158 }
159 }
160
Andrey Andreev188abaf2012-01-07 19:09:42 +0200161 // Is the request a class extension? If so we load it too
Andrey Andreev536b7712012-01-07 21:31:25 +0200162 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200163 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600164 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600166 if (class_exists($name) === FALSE)
167 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200168 require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600169 }
170 }
171
172 // Did we find the class?
173 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Barry Mienydd671972010-10-04 16:33:58 +0200175 // Note: We use exit() rather then show_error() in order to avoid a
176 // self-referencing loop with the Excptions class
Kevin Cuppd63e4012012-02-05 14:14:32 -0500177 set_status_header(503);
Greg Aker3a746652011-04-19 10:59:47 -0500178 exit('Unable to locate the specified class: '.$class.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600180
181 // Keep track of what we just loaded
182 is_loaded($class);
183
Pascal Kriete58560022010-11-10 16:01:20 -0500184 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600185 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400187}
Derek Allard2067d1a2008-11-13 22:59:24 +0000188
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600189// --------------------------------------------------------------------
190
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400191if ( ! function_exists('is_loaded'))
192{
Timothy Warrenad475052012-04-19 13:21:06 -0400193 /**
194 * Keeps track of which libraries have been loaded. This function is
195 * called by the load_class() function above
196 *
197 * @param string
198 * @return array
199 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200200 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600201 {
202 static $_is_loaded = array();
203
204 if ($class != '')
205 {
206 $_is_loaded[strtolower($class)] = $class;
207 }
208
209 return $_is_loaded;
210 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400211}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600212
213// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000214
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400215if ( ! function_exists('get_config'))
216{
Timothy Warrenad475052012-04-19 13:21:06 -0400217 /**
218 * 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 * @param array
224 * @return array
225 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600226 function &get_config($replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600228 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600230 if (isset($_config))
231 {
232 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200233 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600234
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100235 // Is the config file in the environment folder?
Michiel Vugteveen0609d582012-01-08 13:26:17 +0100236 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100237 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200238 $file_path = APPPATH.'config/config.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100239 }
joelcox2035fd82011-01-16 16:50:36 +0100240
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600241 // Fetch the config file
joelcox2035fd82011-01-16 16:50:36 +0100242 if ( ! file_exists($file_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500244 set_status_header(503);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100245 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600246 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100247
joelcox2035fd82011-01-16 16:50:36 +0100248 require($file_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000249
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600250 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 if ( ! isset($config) OR ! is_array($config))
252 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500253 set_status_header(503);
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 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
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400275if ( ! function_exists('config_item'))
276{
Timothy Warrenad475052012-04-19 13:21:06 -0400277 /**
278 * Returns the specified config item
279 *
280 * @param string
281 * @return mixed
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
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400304if ( ! function_exists('show_error'))
305{
Timothy Warrenad475052012-04-19 13:21:06 -0400306 /**
307 * Error Handler
308 *
309 * This function lets us invoke the exception class and
310 * display errors using the standard error template located
311 * in application/errors/errors.php
312 * This function will send the error page directly to the
313 * browser and exit.
314 *
315 * @param string
316 * @param int
317 * @param string
318 * @return void
319 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600320 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
321 {
322 $_error =& load_class('Exceptions', 'core');
323 echo $_error->show_error($heading, $message, 'error_general', $status_code);
324 exit;
325 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400326}
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600328// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400330if ( ! function_exists('show_404'))
331{
Timothy Warrenad475052012-04-19 13:21:06 -0400332 /**
333 * 404 Page Handler
334 *
335 * This function is similar to the show_error() function above
336 * However, instead of the standard error template it displays
337 * 404 errors.
338 *
339 * @param string
340 * @param bool
341 * @return void
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
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400353if ( ! function_exists('log_message'))
354{
Timothy Warrenad475052012-04-19 13:21:06 -0400355 /**
356 * Error Logging Interface
357 *
358 * We use this as a simple mechanism to access the logging
359 * class and send messages to be logged.
360 *
361 * @param string
362 * @param string
363 * @param bool
364 * @return void
365 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600366 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600368 static $_log;
369
370 if (config_item('log_threshold') == 0)
371 {
372 return;
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600375 $_log =& load_class('Log');
376 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
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 Jones817163a2009-07-11 17:05:58 +0000381
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400382if ( ! function_exists('set_status_header'))
383{
Timothy Warrenad475052012-04-19 13:21:06 -0400384 /**
385 * Set HTTP Status Header
386 *
387 * @param int the status code
388 * @param string
389 * @return void
390 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600391 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000392 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600393 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400394 200 => 'OK',
395 201 => 'Created',
396 202 => 'Accepted',
397 203 => 'Non-Authoritative Information',
398 204 => 'No Content',
399 205 => 'Reset Content',
400 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000401
Timothy Warrenad475052012-04-19 13:21:06 -0400402 300 => 'Multiple Choices',
403 301 => 'Moved Permanently',
404 302 => 'Found',
405 304 => 'Not Modified',
406 305 => 'Use Proxy',
407 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600408
Timothy Warrenad475052012-04-19 13:21:06 -0400409 400 => 'Bad Request',
410 401 => 'Unauthorized',
411 403 => 'Forbidden',
412 404 => 'Not Found',
413 405 => 'Method Not Allowed',
414 406 => 'Not Acceptable',
415 407 => 'Proxy Authentication Required',
416 408 => 'Request Timeout',
417 409 => 'Conflict',
418 410 => 'Gone',
419 411 => 'Length Required',
420 412 => 'Precondition Failed',
421 413 => 'Request Entity Too Large',
422 414 => 'Request-URI Too Long',
423 415 => 'Unsupported Media Type',
424 416 => 'Requested Range Not Satisfiable',
425 417 => 'Expectation Failed',
426 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600427
Timothy Warrenad475052012-04-19 13:21:06 -0400428 500 => 'Internal Server Error',
429 501 => 'Not Implemented',
430 502 => 'Bad Gateway',
431 503 => 'Service Unavailable',
432 504 => 'Gateway Timeout',
433 505 => 'HTTP Version Not Supported'
434 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600435
436 if ($code == '' OR ! is_numeric($code))
437 {
438 show_error('Status codes must be numeric', 500);
439 }
440
Andrey Andreevb7b43962012-02-27 22:45:48 +0200441 if (isset($stati[$code]) && $text == '')
Barry Mienydd671972010-10-04 16:33:58 +0200442 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600443 $text = $stati[$code];
444 }
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600446 if ($text == '')
447 {
Andrey Andreev188abaf2012-01-07 19:09:42 +0200448 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 -0600449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Andrey Andreevb7b43962012-02-27 22:45:48 +0200451 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600452
Andrey Andreev188abaf2012-01-07 19:09:42 +0200453 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600454 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200455 header('Status: '.$code.' '.$text, TRUE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600456 }
Andrey Andreevb7b43962012-02-27 22:45:48 +0200457 elseif ($server_protocol === 'HTTP/1.0')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600458 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200459 header('HTTP/1.0 '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600460 }
461 else
462 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200463 header('HTTP/1.1 '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600464 }
Derek Jones817163a2009-07-11 17:05:58 +0000465 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400466}
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600468// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000469
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400470if ( ! function_exists('_exception_handler'))
471{
Timothy Warrenad475052012-04-19 13:21:06 -0400472 /**
473 * Exception Handler
474 *
475 * This is the custom exception handler that is declaired at the top
476 * of Codeigniter.php. The main reason we use this is to permit
477 * PHP errors to be logged in our own log files since the user may
478 * not have access to server logs. Since this function
479 * effectively intercepts PHP errors, however, we also need
480 * to display errors based on the current error_reporting level.
481 * We do that with the use of a PHP error template.
482 *
483 * @param int
484 * @param string
485 * @param string
486 * @param int
487 * @return void
488 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600489 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200490 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600491 // We don't bother with "strict" notices since they tend to fill up
492 // the log file with excess information that isn't normally very helpful.
Barry Mienydd671972010-10-04 16:33:58 +0200493 // For example, if you are running PHP 5 and you use version 4 style
494 // class functions (without prefixes like "public", "private", etc.)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600495 // you'll get notices telling you that these have been deprecated.
496 if ($severity == E_STRICT)
497 {
498 return;
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600501 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200502
503 // Should we display the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600504 // level and add its bits with the severity bits to find out.
505 if (($severity & error_reporting()) == $severity)
506 {
507 $_error->show_php_error($severity, $message, $filepath, $line);
508 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000509
Derek Jones4b9c6292011-07-01 17:40:48 -0500510 // Should we log the error? No? We're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600511 if (config_item('log_threshold') == 0)
512 {
513 return;
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600516 $_error->log_exception($severity, $message, $filepath, $line);
517 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400518}
Derek Allard2067d1a2008-11-13 22:59:24 +0000519
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400520// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200521
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400522if ( ! function_exists('remove_invisible_characters'))
523{
Timothy Warrenad475052012-04-19 13:21:06 -0400524 /**
525 * Remove Invisible Characters
526 *
527 * This prevents sandwiching null characters
528 * between ascii characters, like Java\0script.
529 *
530 * @param string
531 * @param bool
532 * @return string
533 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400534 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500535 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400536 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200537
538 // every control character except newline (dec 10),
539 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400540 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500541 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400542 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
543 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500544 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200545
Pascal Kriete0ff50262011-04-05 14:52:03 -0400546 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500547
548 do
549 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400550 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500551 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400552 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500553
554 return $str;
555 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400556}
Derek Allard2067d1a2008-11-13 22:59:24 +0000557
kenjisfbac8b42011-08-25 10:51:44 +0900558// ------------------------------------------------------------------------
559
kenjisfbac8b42011-08-25 10:51:44 +0900560if ( ! function_exists('html_escape'))
561{
Timothy Warrenad475052012-04-19 13:21:06 -0400562 /**
563 * Returns HTML escaped variable
564 *
565 * @param mixed
566 * @return mixed
567 */
kenjisfbac8b42011-08-25 10:51:44 +0900568 function html_escape($var)
569 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200570 return is_array($var)
571 ? array_map('html_escape', $var)
572 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600573 }
Greg Akerd96f8822011-12-27 16:23:47 -0600574}
Greg Aker5c1aa632011-12-25 01:24:29 -0600575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576/* End of file Common.php */
Andrey Andreevbb30d792012-03-26 15:49:09 +0300577/* Location: ./system/core/Common.php */