blob: e50f7794ae949786414f7ece36cbc2624c9f9236 [file] [log] [blame]
Razican114ab092011-04-25 17:26:45 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060019 * Common Functions
20 *
21 * Loads the base classes and executes the request.
22 *
23 * @package CodeIgniter
24 * @subpackage codeigniter
25 * @category Common Functions
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/
28 */
29
30// ------------------------------------------------------------------------
31
32/**
Derek Jones086ee5a2009-07-28 14:42:12 +000033* Determines if the current version of PHP is greater then the supplied value
34*
35* Since there are a few places where we conditionally test for PHP > 5
36* we'll set a static variable.
37*
38* @access public
Derek Jones77b513b2009-08-06 14:39:25 +000039* @param string
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060040* @return bool TRUE if the current version is $version or higher
Derek Jones086ee5a2009-07-28 14:42:12 +000041*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040042if ( ! function_exists('is_php'))
43{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060044 function is_php($version = '5.0.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000045 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060046 static $_is_php;
47 $version = (string)$version;
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060049 if ( ! isset($_is_php[$version]))
50 {
51 $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
52 }
Derek Jones086ee5a2009-07-28 14:42:12 +000053
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060054 return $_is_php[$version];
55 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040056}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000057
Derek Jones086ee5a2009-07-28 14:42:12 +000058// ------------------------------------------------------------------------
59
60/**
Derek Allard2067d1a2008-11-13 22:59:24 +000061 * Tests for file writability
62 *
Barry Mienydd671972010-10-04 16:33:58 +020063 * is_writable() returns TRUE on Windows servers when you really can't write to
Razican114ab092011-04-25 17:26:45 +020064 * the file, based on the read-only attribute. is_writable() is also unreliable
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060065 * on Unix servers if safe_mode is on.
Derek Allard2067d1a2008-11-13 22:59:24 +000066 *
67 * @access private
68 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020069 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040070if ( ! function_exists('is_really_writable'))
71{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060072 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060074 // If we're on a Unix server with safe_mode off we call is_writable
75 if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
76 {
77 return is_writable($file);
78 }
Derek Allard2067d1a2008-11-13 22:59:24 +000079
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060080 // For windows servers and safe_mode "on" installations we'll actually
Razican114ab092011-04-25 17:26:45 +020081 // write a file then read it. Bah...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060082 if (is_dir($file))
83 {
84 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
Derek Allard2067d1a2008-11-13 22:59:24 +000085
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060086 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
87 {
88 return FALSE;
89 }
90
91 fclose($fp);
92 @chmod($file, DIR_WRITE_MODE);
93 @unlink($file);
94 return TRUE;
95 }
Eric Barnes15083012011-03-21 22:13:12 -040096 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 return FALSE;
99 }
100
101 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 return TRUE;
103 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400104}
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
106// ------------------------------------------------------------------------
107
108/**
109* Class registry
110*
Razican114ab092011-04-25 17:26:45 +0200111* This function acts as a singleton. If the requested class does not
112* exist it is instantiated and set to a static variable. If it has
Derek Allard2067d1a2008-11-13 22:59:24 +0000113* previously been instantiated the variable is returned.
114*
115* @access public
116* @param string the class name being requested
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600117* @param string the directory where the class should be found
118* @param string the class name prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000119* @return object
120*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400121if ( ! function_exists('load_class'))
122{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600123 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600125 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200126
Razican114ab092011-04-25 17:26:45 +0200127 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600128 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600130 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600132
133 $name = FALSE;
134
135 // Look for the class first in the native system/libraries folder
136 // thenin the local application/libraries folder
137 foreach (array(BASEPATH, APPPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200138 {
Greg Aker3a746652011-04-19 10:59:47 -0500139 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600140 {
141 $name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600143 if (class_exists($name) === FALSE)
144 {
Greg Aker3a746652011-04-19 10:59:47 -0500145 require($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600148 break;
149 }
150 }
151
Razican114ab092011-04-25 17:26:45 +0200152 // Is the request a class extension? If so we load it too
Greg Aker3a746652011-04-19 10:59:47 -0500153 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200154 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600155 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600157 if (class_exists($name) === FALSE)
158 {
Greg Aker3a746652011-04-19 10:59:47 -0500159 require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600160 }
161 }
162
163 // Did we find the class?
164 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Barry Mienydd671972010-10-04 16:33:58 +0200166 // Note: We use exit() rather then show_error() in order to avoid a
167 // self-referencing loop with the Excptions class
Greg Aker3a746652011-04-19 10:59:47 -0500168 exit('Unable to locate the specified class: '.$class.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600170
171 // Keep track of what we just loaded
172 is_loaded($class);
173
Pascal Kriete58560022010-11-10 16:01:20 -0500174 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600175 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400177}
Derek Allard2067d1a2008-11-13 22:59:24 +0000178
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600179// --------------------------------------------------------------------
180
181/**
Razican114ab092011-04-25 17:26:45 +0200182* Keeps track of which libraries have been loaded. This function is
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600183* called by the load_class() function above
184*
185* @access public
186* @return array
187*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400188if ( ! function_exists('is_loaded'))
189{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600190 function is_loaded($class = '')
191 {
192 static $_is_loaded = array();
193
194 if ($class != '')
195 {
196 $_is_loaded[strtolower($class)] = $class;
197 }
198
199 return $_is_loaded;
200 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400201}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600202
203// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000204
205/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000206* Loads the main config.php file
207*
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600208* This function lets us grab the config file even if the Config class
209* hasn't been instantiated yet
210*
Derek Allard2067d1a2008-11-13 22:59:24 +0000211* @access private
212* @return array
213*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400214if ( ! function_exists('get_config'))
215{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600216 function &get_config($replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600218 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600220 if (isset($_config))
221 {
222 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200223 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600224
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100225 // Is the config file in the environment folder?
Greg Aker3a746652011-04-19 10:59:47 -0500226 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100227 {
Greg Aker3a746652011-04-19 10:59:47 -0500228 $file_path = APPPATH.'config/config.php';
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100229 }
joelcox2035fd82011-01-16 16:50:36 +0100230
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600231 // Fetch the config file
joelcox2035fd82011-01-16 16:50:36 +0100232 if ( ! file_exists($file_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100234 exit('The configuration file does not exist.');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600235 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100236
joelcox2035fd82011-01-16 16:50:36 +0100237 require($file_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600239 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 if ( ! isset($config) OR ! is_array($config))
241 {
242 exit('Your config file does not appear to be formatted correctly.');
243 }
244
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600245 // Are any values being dynamically replaced?
246 if (count($replace) > 0)
247 {
248 foreach ($replace as $key => $val)
249 {
250 if (isset($config[$key]))
251 {
252 $config[$key] = $val;
253 }
254 }
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600257 return $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400259}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600260
261// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000262
263/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600264* Returns the specified config item
Derek Allard2067d1a2008-11-13 22:59:24 +0000265*
266* @access public
267* @return mixed
268*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400269if ( ! function_exists('config_item'))
270{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600271 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600273 static $_config_item = array();
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600275 if ( ! isset($_config_item[$item]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600277 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600279 if ( ! isset($config[$item]))
280 {
281 return FALSE;
282 }
283 $_config_item[$item] = $config[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600286 return $_config_item[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400288}
Derek Allard2067d1a2008-11-13 22:59:24 +0000289
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600290// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000291
292/**
293* Error Handler
294*
295* This function lets us invoke the exception class and
296* display errors using the standard error template located
297* in application/errors/errors.php
298* This function will send the error page directly to the
299* browser and exit.
300*
301* @access public
302* @return void
303*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400304if ( ! function_exists('show_error'))
305{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600306 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
307 {
308 $_error =& load_class('Exceptions', 'core');
309 echo $_error->show_error($heading, $message, 'error_general', $status_code);
310 exit;
311 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400312}
Derek Allard2067d1a2008-11-13 22:59:24 +0000313
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600314// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000315
316/**
317* 404 Page Handler
318*
319* This function is similar to the show_error() function above
320* However, instead of the standard error template it displays
321* 404 errors.
322*
323* @access public
324* @return void
325*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400326if ( ! function_exists('show_404'))
327{
Derek Allard2ddc9492010-08-05 10:08:33 -0400328 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600329 {
330 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400331 $_error->show_404($page, $log_error);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600332 exit;
333 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400334}
Derek Allard2067d1a2008-11-13 22:59:24 +0000335
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600336// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000337
338/**
339* Error Logging Interface
340*
341* We use this as a simple mechanism to access the logging
342* class and send messages to be logged.
343*
344* @access public
345* @return void
346*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400347if ( ! function_exists('log_message'))
348{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600349 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600351 static $_log;
352
353 if (config_item('log_threshold') == 0)
354 {
355 return;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600358 $_log =& load_class('Log');
359 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400361}
Derek Allard2067d1a2008-11-13 22:59:24 +0000362
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600363// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000364
365/**
366 * Set HTTP Status Header
367 *
368 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200369 * @param int the status code
370 * @param string
Derek Jones817163a2009-07-11 17:05:58 +0000371 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200372 */
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400373if ( ! function_exists('set_status_header'))
374{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600375 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000376 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600377 $stati = array(
378 200 => 'OK',
379 201 => 'Created',
380 202 => 'Accepted',
381 203 => 'Non-Authoritative Information',
382 204 => 'No Content',
383 205 => 'Reset Content',
384 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000385
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600386 300 => 'Multiple Choices',
387 301 => 'Moved Permanently',
388 302 => 'Found',
389 304 => 'Not Modified',
390 305 => 'Use Proxy',
391 307 => 'Temporary Redirect',
392
393 400 => 'Bad Request',
394 401 => 'Unauthorized',
395 403 => 'Forbidden',
396 404 => 'Not Found',
397 405 => 'Method Not Allowed',
398 406 => 'Not Acceptable',
399 407 => 'Proxy Authentication Required',
400 408 => 'Request Timeout',
401 409 => 'Conflict',
402 410 => 'Gone',
403 411 => 'Length Required',
404 412 => 'Precondition Failed',
405 413 => 'Request Entity Too Large',
406 414 => 'Request-URI Too Long',
407 415 => 'Unsupported Media Type',
408 416 => 'Requested Range Not Satisfiable',
409 417 => 'Expectation Failed',
410
411 500 => 'Internal Server Error',
412 501 => 'Not Implemented',
413 502 => 'Bad Gateway',
414 503 => 'Service Unavailable',
415 504 => 'Gateway Timeout',
416 505 => 'HTTP Version Not Supported'
417 );
418
419 if ($code == '' OR ! is_numeric($code))
420 {
421 show_error('Status codes must be numeric', 500);
422 }
423
424 if (isset($stati[$code]) AND $text == '')
Barry Mienydd671972010-10-04 16:33:58 +0200425 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600426 $text = $stati[$code];
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600429 if ($text == '')
430 {
Razican114ab092011-04-25 17:26:45 +0200431 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 -0600432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600434 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
435
436 if (substr(php_sapi_name(), 0, 3) == 'cgi')
437 {
438 header("Status: {$code} {$text}", TRUE);
439 }
440 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
441 {
442 header($server_protocol." {$code} {$text}", TRUE, $code);
443 }
444 else
445 {
446 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
447 }
Derek Jones817163a2009-07-11 17:05:58 +0000448 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400449}
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600451// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453/**
454* Exception Handler
455*
456* This is the custom exception handler that is declaired at the top
Razican114ab092011-04-25 17:26:45 +0200457* of Codeigniter.php. The main reason we use this is to permit
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600458* PHP errors to be logged in our own log files since the user may
Derek Allard2067d1a2008-11-13 22:59:24 +0000459* not have access to server logs. Since this function
460* effectively intercepts PHP errors, however, we also need
461* to display errors based on the current error_reporting level.
462* We do that with the use of a PHP error template.
463*
464* @access private
465* @return void
466*/
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400467if ( ! function_exists('_exception_handler'))
468{
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600469 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200470 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600471 // We don't bother with "strict" notices since they tend to fill up
472 // the log file with excess information that isn't normally very helpful.
Barry Mienydd671972010-10-04 16:33:58 +0200473 // For example, if you are running PHP 5 and you use version 4 style
474 // class functions (without prefixes like "public", "private", etc.)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600475 // you'll get notices telling you that these have been deprecated.
476 if ($severity == E_STRICT)
477 {
478 return;
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600481 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200482
483 // Should we display the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600484 // level and add its bits with the severity bits to find out.
485 if (($severity & error_reporting()) == $severity)
486 {
487 $_error->show_php_error($severity, $message, $filepath, $line);
488 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000489
Razican114ab092011-04-25 17:26:45 +0200490 // Should we log the error? No? We're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600491 if (config_item('log_threshold') == 0)
492 {
493 return;
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600496 $_error->log_exception($severity, $message, $filepath, $line);
497 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400498}
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400500// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200501
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400502/**
503 * Remove Invisible Characters
504 *
505 * This prevents sandwiching null characters
506 * between ascii characters, like Java\0script.
507 *
508 * @access public
509 * @param string
510 * @return string
511 */
512if ( ! function_exists('remove_invisible_characters'))
513{
Pascal Kriete0ff50262011-04-05 14:52:03 -0400514 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500515 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400516 $non_displayables = array();
Razican114ab092011-04-25 17:26:45 +0200517
Pascal Kriete0ff50262011-04-05 14:52:03 -0400518 // every control character except newline (dec 10)
519 // carriage return (dec 13), and horizontal tab (dec 09)
Razican114ab092011-04-25 17:26:45 +0200520
Pascal Kriete0ff50262011-04-05 14:52:03 -0400521 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500522 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400523 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
524 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500525 }
Razican114ab092011-04-25 17:26:45 +0200526
Pascal Kriete0ff50262011-04-05 14:52:03 -0400527 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500528
529 do
530 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400531 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500532 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400533 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500534
535 return $str;
536 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400537}
Derek Allard2067d1a2008-11-13 22:59:24 +0000538
539/* End of file Common.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600540/* Location: ./system/core/Common.php */