blob: 9e05f3db4124d6b767ccc66d7d0762645cd646e4 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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*
Derek Jones77b513b2009-08-06 14:39:25 +000050* @param string
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060051* @return bool TRUE if the current version is $version or higher
Derek Jones086ee5a2009-07-28 14:42:12 +000052*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040053if ( ! function_exists('is_php'))
54{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060055 function is_php($version = '5.0.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000056 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060057 static $_is_php;
58 $version = (string)$version;
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060060 if ( ! isset($_is_php[$version]))
61 {
62 $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
63 }
Derek Jones086ee5a2009-07-28 14:42:12 +000064
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060065 return $_is_php[$version];
66 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040067}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000068
Derek Jones086ee5a2009-07-28 14:42:12 +000069// ------------------------------------------------------------------------
70
71/**
Derek Allard2067d1a2008-11-13 22:59:24 +000072 * Tests for file writability
73 *
Barry Mienydd671972010-10-04 16:33:58 +020074 * is_writable() returns TRUE on Windows servers when you really can't write to
Derek Jones4b9c6292011-07-01 17:40:48 -050075 * the file, based on the read-only attribute. is_writable() is also unreliable
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060076 * on Unix servers if safe_mode is on.
Derek Allard2067d1a2008-11-13 22:59:24 +000077 *
Derek Allard2067d1a2008-11-13 22:59:24 +000078 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020079 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040080if ( ! function_exists('is_really_writable'))
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
85 if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
86 {
87 return is_writable($file);
88 }
Derek Allard2067d1a2008-11-13 22:59:24 +000089
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060090 // For windows servers and safe_mode "on" installations we'll actually
Derek Jones4b9c6292011-07-01 17:40:48 -050091 // write a file then read it. Bah...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060092 if (is_dir($file))
93 {
94 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
Derek Allard2067d1a2008-11-13 22:59:24 +000095
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
118/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600119 * Class registry
120 *
121 * This function acts as a singleton. If the requested class does not
122 * exist it is instantiated and set to a static variable. If it has
123 * previously been instantiated the variable is returned.
124 *
125 * @param string the class name being requested
126 * @param string the directory where the class should be found
127 * @param string the class name prefix
128 * @return object
129 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400130if ( ! function_exists('load_class'))
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
Derek Jones4b9c6292011-07-01 17:40:48 -0500136 // 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 {
Greg Aker3a746652011-04-19 10:59:47 -0500148 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 {
Greg Aker3a746652011-04-19 10:59:47 -0500154 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
Derek Jones4b9c6292011-07-01 17:40:48 -0500161 // Is the request a class extension? If so we load it too
Greg Aker3a746652011-04-19 10:59:47 -0500162 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 {
Greg Aker3a746652011-04-19 10:59:47 -0500168 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
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
190/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600191 * Keeps track of which libraries have been loaded. This function is
192 * called by the load_class() function above
193 *
194 * @return array
195 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400196if ( ! function_exists('is_loaded'))
197{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600198 function is_loaded($class = '')
199 {
200 static $_is_loaded = array();
201
202 if ($class != '')
203 {
204 $_is_loaded[strtolower($class)] = $class;
205 }
206
207 return $_is_loaded;
208 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400209}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600210
211// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000212
213/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600214 * Loads the main config.php file
215 *
216 * This function lets us grab the config file even if the Config class
217 * hasn't been instantiated yet
218 *
219 * @return array
220 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400221if ( ! function_exists('get_config'))
222{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600223 function &get_config($replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600225 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600227 if (isset($_config))
228 {
229 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200230 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600231
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100232 // Is the config file in the environment folder?
Greg Aker3a746652011-04-19 10:59:47 -0500233 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100234 {
Greg Aker3a746652011-04-19 10:59:47 -0500235 $file_path = APPPATH.'config/config.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100236 }
joelcox2035fd82011-01-16 16:50:36 +0100237
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600238 // Fetch the config file
joelcox2035fd82011-01-16 16:50:36 +0100239 if ( ! file_exists($file_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100241 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600242 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100243
joelcox2035fd82011-01-16 16:50:36 +0100244 require($file_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600246 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 if ( ! isset($config) OR ! is_array($config))
248 {
249 exit('Your config file does not appear to be formatted correctly.');
250 }
251
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600252 // Are any values being dynamically replaced?
253 if (count($replace) > 0)
254 {
255 foreach ($replace as $key => $val)
256 {
257 if (isset($config[$key]))
258 {
259 $config[$key] = $val;
260 }
261 }
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600264 return $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400266}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600267
268// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
270/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600271 * Returns the specified config item
272 *
273 * @return mixed
274 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400275if ( ! function_exists('config_item'))
276{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600277 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600279 static $_config_item = array();
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600281 if ( ! isset($_config_item[$item]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600283 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600285 if ( ! isset($config[$item]))
286 {
287 return FALSE;
288 }
289 $_config_item[$item] = $config[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600292 return $_config_item[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400294}
Derek Allard2067d1a2008-11-13 22:59:24 +0000295
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600296// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000297
298/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600299 * Error Handler
300 *
301 * This function lets us invoke the exception class and
302 * display errors using the standard error template located
303 * in application/errors/errors.php
304 * This function will send the error page directly to the
305 * browser and exit.
306 *
307 * @return void
308 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400309if ( ! function_exists('show_error'))
310{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600311 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
312 {
313 $_error =& load_class('Exceptions', 'core');
314 echo $_error->show_error($heading, $message, 'error_general', $status_code);
315 exit;
316 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400317}
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600319// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
321/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600322 * 404 Page Handler
323 *
324 * This function is similar to the show_error() function above
325 * However, instead of the standard error template it displays
326 * 404 errors.
327 *
328 * @return void
329 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400330if ( ! function_exists('show_404'))
331{
Derek Allard2ddc9492010-08-05 10:08:33 -0400332 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600333 {
334 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400335 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600336 exit;
337 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400338}
Derek Allard2067d1a2008-11-13 22:59:24 +0000339
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600340// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000341
342/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600343 * Error Logging Interface
344 *
345 * We use this as a simple mechanism to access the logging
346 * class and send messages to be logged.
347 *
348 * @return void
349 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400350if ( ! function_exists('log_message'))
351{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600352 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600354 static $_log;
355
356 if (config_item('log_threshold') == 0)
357 {
358 return;
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600361 $_log =& load_class('Log');
362 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400364}
Derek Allard2067d1a2008-11-13 22:59:24 +0000365
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600366// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000367
368/**
369 * Set HTTP Status Header
370 *
Barry Mienydd671972010-10-04 16:33:58 +0200371 * @param int the status code
372 * @param string
Derek Jones817163a2009-07-11 17:05:58 +0000373 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200374 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400375if ( ! function_exists('set_status_header'))
376{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600377 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000378 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600379 $stati = array(
380 200 => 'OK',
381 201 => 'Created',
382 202 => 'Accepted',
383 203 => 'Non-Authoritative Information',
384 204 => 'No Content',
385 205 => 'Reset Content',
386 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000387
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600388 300 => 'Multiple Choices',
389 301 => 'Moved Permanently',
390 302 => 'Found',
391 304 => 'Not Modified',
392 305 => 'Use Proxy',
393 307 => 'Temporary Redirect',
394
395 400 => 'Bad Request',
396 401 => 'Unauthorized',
397 403 => 'Forbidden',
398 404 => 'Not Found',
399 405 => 'Method Not Allowed',
400 406 => 'Not Acceptable',
401 407 => 'Proxy Authentication Required',
402 408 => 'Request Timeout',
403 409 => 'Conflict',
404 410 => 'Gone',
405 411 => 'Length Required',
406 412 => 'Precondition Failed',
407 413 => 'Request Entity Too Large',
408 414 => 'Request-URI Too Long',
409 415 => 'Unsupported Media Type',
410 416 => 'Requested Range Not Satisfiable',
411 417 => 'Expectation Failed',
Chris Rosser0ec05c12011-11-21 17:56:13 +0000412 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600413
414 500 => 'Internal Server Error',
415 501 => 'Not Implemented',
416 502 => 'Bad Gateway',
417 503 => 'Service Unavailable',
418 504 => 'Gateway Timeout',
419 505 => 'HTTP Version Not Supported'
420 );
421
422 if ($code == '' OR ! is_numeric($code))
423 {
424 show_error('Status codes must be numeric', 500);
425 }
426
427 if (isset($stati[$code]) AND $text == '')
Barry Mienydd671972010-10-04 16:33:58 +0200428 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600429 $text = $stati[$code];
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600432 if ($text == '')
433 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500434 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 -0600435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600437 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
438
439 if (substr(php_sapi_name(), 0, 3) == 'cgi')
440 {
441 header("Status: {$code} {$text}", TRUE);
442 }
443 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
444 {
445 header($server_protocol." {$code} {$text}", TRUE, $code);
446 }
447 else
448 {
449 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
450 }
Derek Jones817163a2009-07-11 17:05:58 +0000451 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400452}
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600454// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600457 * Exception Handler
458 *
459 * This is the custom exception handler that is declaired at the top
460 * of Codeigniter.php. The main reason we use this is to permit
461 * PHP errors to be logged in our own log files since the user may
462 * not have access to server logs. Since this function
463 * effectively intercepts PHP errors, however, we also need
464 * to display errors based on the current error_reporting level.
465 * We do that with the use of a PHP error template.
466 *
467 * @return void
468 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400469if ( ! function_exists('_exception_handler'))
470{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600471 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200472 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600473 // We don't bother with "strict" notices since they tend to fill up
474 // the log file with excess information that isn't normally very helpful.
Barry Mienydd671972010-10-04 16:33:58 +0200475 // For example, if you are running PHP 5 and you use version 4 style
476 // class functions (without prefixes like "public", "private", etc.)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600477 // you'll get notices telling you that these have been deprecated.
478 if ($severity == E_STRICT)
479 {
480 return;
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600483 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200484
485 // Should we display the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600486 // level and add its bits with the severity bits to find out.
487 if (($severity & error_reporting()) == $severity)
488 {
489 $_error->show_php_error($severity, $message, $filepath, $line);
490 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000491
Derek Jones4b9c6292011-07-01 17:40:48 -0500492 // Should we log the error? No? We're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600493 if (config_item('log_threshold') == 0)
494 {
495 return;
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600498 $_error->log_exception($severity, $message, $filepath, $line);
499 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400500}
Derek Allard2067d1a2008-11-13 22:59:24 +0000501
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400502// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200503
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400504/**
505 * Remove Invisible Characters
506 *
507 * This prevents sandwiching null characters
508 * between ascii characters, like Java\0script.
509 *
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400510 * @param string
511 * @return string
512 */
513if ( ! function_exists('remove_invisible_characters'))
514{
Pascal Kriete0ff50262011-04-05 14:52:03 -0400515 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500516 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400517 $non_displayables = array();
Derek Jones4b9c6292011-07-01 17:40:48 -0500518
Pascal Kriete0ff50262011-04-05 14:52:03 -0400519 // every control character except newline (dec 10)
520 // carriage return (dec 13), and horizontal tab (dec 09)
Derek Jones4b9c6292011-07-01 17:40:48 -0500521
Pascal Kriete0ff50262011-04-05 14:52:03 -0400522 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500523 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400524 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
525 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500526 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500527
Pascal Kriete0ff50262011-04-05 14:52:03 -0400528 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500529
530 do
531 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400532 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500533 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400534 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500535
536 return $str;
537 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400538}
Derek Allard2067d1a2008-11-13 22:59:24 +0000539
kenjisfbac8b42011-08-25 10:51:44 +0900540// ------------------------------------------------------------------------
541
542/**
Greg Aker5c1aa632011-12-25 01:24:29 -0600543 * Returns HTML escaped variable
544 *
545 * @param mixed
546 * @return mixed
547 */
kenjisfbac8b42011-08-25 10:51:44 +0900548if ( ! function_exists('html_escape'))
549{
550 function html_escape($var)
551 {
552 if (is_array($var))
553 {
554 return array_map('html_escape', $var);
555 }
Greg Aker5c1aa632011-12-25 01:24:29 -0600556
557 return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
kenjisfbac8b42011-08-25 10:51:44 +0900558 }
559}
560
Greg Aker5c1aa632011-12-25 01:24:29 -0600561// ------------------------------------------------------------------------
562
563/**
564 * Load Environmental config directory files.
565 *
566 * In several places we check to see if the environment is loaded up and if the file
567 * that is being requested lives in said environment. Otherwise load up the file from
568 * the main CI config dir.
569 *
570 * @todo Optimize a bit to lessen the file system hits if the file has been loaded.
571 * @param string filename without extension. eg: 'config' or 'hooks'
572 * @param boolean whether or not to do a `require_once()` or a simple `include()`
573 * @return void
574 */
575 if ( ! function_exists('load_environ_config'))
576 {
577 function load_environ_config($file, $require=FALSE)
578 {
579 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php'))
580 {
581 if ($require)
582 {
583 require_once(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php');
584 }
585 else
586 {
587 include(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php');
588 }
589
590 return;
591 }
592
593 if ($require)
594 {
595 require_once(APPPATH.'config/'.$file.'.php');
596 }
597 else
598 {
599 include(APPPATH.'config/'.$file.'.php');
600 }
601
602 return;
603 }
604 }
605
606// ------------------------------------------------------------------------
607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608/* End of file Common.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600609/* Location: ./system/core/Common.php */