blob: 56008efe8b68fdfee971b859726349e4e70d2eab [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060030 * Common Functions
31 *
32 * Loads the base classes and executes the request.
33 *
34 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030035 * @subpackage CodeIgniter
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060036 * @category Common Functions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060038 * @link http://codeigniter.com/user_guide/
39 */
40
41// ------------------------------------------------------------------------
42
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 *
Andrey Andreev24bd2302012-06-05 22:29:12 +030048 * Since there are a few places where we conditionally test for PHP > 5.3
Timothy Warrenad475052012-04-19 13:21:06 -040049 * we'll set a static variable.
50 *
51 * @param string
52 * @return bool TRUE if the current version is $version or higher
53 */
Andrey Andreev24bd2302012-06-05 22:29:12 +030054 function is_php($version = '5.3.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 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061 $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060062 }
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 Andreev5932a732013-09-13 14:45:53 +030085 if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR (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 {
vlakoff06127562013-03-30 00:06:39 +010095 $file = rtrim($file, '/').'/'.md5(mt_rand());
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
Andrey Andreev49e68de2013-02-21 16:30:55 +0200152 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600153 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300154 require_once($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
Andrey Andreev49e68de2013-02-21 16:30:55 +0200166 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600167 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300168 require_once(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
vlakofffe8fe452012-07-11 19:56:50 +0200176 // self-referencing loop with the Exceptions class
Kevin Cuppd63e4012012-02-05 14:14:32 -0500177 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700178 echo 'Unable to locate the specified class: '.$class.'.php';
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700179 exit(EXIT_UNKNOWN_CLASS);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600181
182 // Keep track of what we just loaded
183 is_loaded($class);
184
Pascal Kriete58560022010-11-10 16:01:20 -0500185 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600186 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400188}
Derek Allard2067d1a2008-11-13 22:59:24 +0000189
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600190// --------------------------------------------------------------------
191
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400192if ( ! function_exists('is_loaded'))
193{
Timothy Warrenad475052012-04-19 13:21:06 -0400194 /**
195 * Keeps track of which libraries have been loaded. This function is
196 * called by the load_class() function above
197 *
198 * @param string
199 * @return array
200 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200201 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600202 {
203 static $_is_loaded = array();
204
Alex Bilbieed944a32012-06-02 11:07:47 +0100205 if ($class !== '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600206 {
207 $_is_loaded[strtolower($class)] = $class;
208 }
209
210 return $_is_loaded;
211 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400212}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600213
214// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000215
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400216if ( ! function_exists('get_config'))
217{
Timothy Warrenad475052012-04-19 13:21:06 -0400218 /**
219 * Loads the main config.php file
220 *
221 * This function lets us grab the config file even if the Config class
222 * hasn't been instantiated yet
223 *
224 * @param array
225 * @return array
226 */
Andrey Andreev49890a92013-08-19 19:56:18 +0300227 function &get_config(Array $replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600229 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200230
vlakoff05d043b2013-08-19 04:55:34 +0200231 if (empty($_config))
vlakoff8d70c0a2013-08-17 07:31:29 +0200232 {
233 $file_path = APPPATH.'config/config.php';
234 $found = FALSE;
235 if (file_exists($file_path))
236 {
237 $found = TRUE;
238 require($file_path);
239 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600240
vlakoff8d70c0a2013-08-17 07:31:29 +0200241 // Is the config file in the environment folder?
242 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
243 {
244 require($file_path);
245 }
246 elseif ( ! $found)
247 {
248 set_status_header(503);
249 echo 'The configuration file does not exist.';
250 exit(EXIT_CONFIG);
251 }
joelcox2035fd82011-01-16 16:50:36 +0100252
vlakoff8d70c0a2013-08-17 07:31:29 +0200253 // Does the $config array exist in the file?
254 if ( ! isset($config) OR ! is_array($config))
255 {
256 set_status_header(503);
257 echo 'Your config file does not appear to be formatted correctly.';
258 exit(EXIT_CONFIG);
259 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100260
vlakoff05d043b2013-08-19 04:55:34 +0200261 // references cannot be directly assigned to static variables, so we use an array
vlakoff8d70c0a2013-08-17 07:31:29 +0200262 $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264
vlakoff67e5ca62013-08-19 04:52:00 +0200265 // Are any values being dynamically added or replaced?
vlakoff2f7810a2013-08-19 04:46:26 +0200266 foreach ($replace as $key => $val)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600267 {
vlakoff05d043b2013-08-19 04:55:34 +0200268 $_config[0][$key] = $val;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
vlakoff05d043b2013-08-19 04:55:34 +0200271 return $_config[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400273}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600274
275// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400277if ( ! function_exists('config_item'))
278{
Timothy Warrenad475052012-04-19 13:21:06 -0400279 /**
280 * Returns the specified config item
281 *
282 * @param string
283 * @return mixed
284 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600285 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
vlakoffaf431ce2013-07-19 02:06:41 +0200287 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200288
vlakoffaf431ce2013-07-19 02:06:41 +0200289 if (empty($_config))
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
vlakoffaf431ce2013-07-19 02:06:41 +0200291 // references cannot be directly assigned to static variables, so we use an array
292 $_config[0] =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
vlakoffaf431ce2013-07-19 02:06:41 +0200295 return isset($_config[0][$item]) ? $_config[0][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400297}
Derek Allard2067d1a2008-11-13 22:59:24 +0000298
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600299// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000300
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300301if ( ! function_exists('get_mimes'))
302{
303 /**
304 * Returns the MIME types array from config/mimes.php
305 *
306 * @return array
307 */
308 function &get_mimes()
309 {
310 static $_mimes = array();
311
Andrey Andreev06879112013-01-29 15:05:02 +0200312 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300313 {
314 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
315 }
Andrey Andreev06879112013-01-29 15:05:02 +0200316 elseif (file_exists(APPPATH.'config/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300317 {
318 $_mimes = include(APPPATH.'config/mimes.php');
319 }
320
321 return $_mimes;
322 }
323}
324
325// ------------------------------------------------------------------------
326
Andrey Andreev3fb02672012-10-22 16:48:01 +0300327if ( ! function_exists('is_https'))
328{
329 /**
330 * Is HTTPS?
331 *
332 * Determines if the application is accessed via an encrypted
333 * (HTTPS) connection.
334 *
335 * @return bool
336 */
337 function is_https()
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200338 {
Andrey Andreev333b80e2013-07-01 16:21:54 +0300339 if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200340 {
341 return TRUE;
342 }
343 elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
344 {
345 return TRUE;
346 }
Andrey Andreev333b80e2013-07-01 16:21:54 +0300347 elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200348 {
349 return TRUE;
350 }
Richard Deurwaarder (Xeli)98999972013-06-24 15:19:30 +0200351
Richard Deurwaarder (Xeli)7cc29452013-06-24 15:06:19 +0200352 return FALSE;
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200353 }
Andrey Andreev3fb02672012-10-22 16:48:01 +0300354}
355
356// ------------------------------------------------------------------------
357
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400358if ( ! function_exists('show_error'))
359{
Timothy Warrenad475052012-04-19 13:21:06 -0400360 /**
361 * Error Handler
362 *
363 * This function lets us invoke the exception class and
364 * display errors using the standard error template located
vlakoff52301c72013-03-29 14:23:34 +0100365 * in application/views/errors/error_general.php
Timothy Warrenad475052012-04-19 13:21:06 -0400366 * This function will send the error page directly to the
367 * browser and exit.
368 *
369 * @param string
370 * @param int
371 * @param string
372 * @return void
373 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600374 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
375 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700376 $status_code = abs($status_code);
377 if ($status_code < 100)
378 {
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700379 $exit_status = $status_code + EXIT__AUTO_MIN;
380 if ($exit_status > EXIT__AUTO_MAX)
381 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700382 $exit_status = EXIT_ERROR;
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700383 }
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700384 $status_code = 500;
385 }
386 else
387 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700388 $exit_status = EXIT_ERROR;
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700389 }
Andrey Andreev5a6814e2013-03-04 15:44:12 +0200390
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600391 $_error =& load_class('Exceptions', 'core');
392 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700393 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600394 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400395}
Derek Allard2067d1a2008-11-13 22:59:24 +0000396
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600397// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000398
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400399if ( ! function_exists('show_404'))
400{
Timothy Warrenad475052012-04-19 13:21:06 -0400401 /**
402 * 404 Page Handler
403 *
404 * This function is similar to the show_error() function above
405 * However, instead of the standard error template it displays
406 * 404 errors.
407 *
408 * @param string
409 * @param bool
410 * @return void
411 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400412 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600413 {
414 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400415 $_error->show_404($page, $log_error);
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700416 exit(EXIT_UNKNOWN_FILE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600417 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400418}
Derek Allard2067d1a2008-11-13 22:59:24 +0000419
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600420// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000421
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400422if ( ! function_exists('log_message'))
423{
Timothy Warrenad475052012-04-19 13:21:06 -0400424 /**
425 * Error Logging Interface
426 *
427 * We use this as a simple mechanism to access the logging
428 * class and send messages to be logged.
429 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200430 * @param string the error level: 'error', 'debug' or 'info'
431 * @param string the error message
Timothy Warrenad475052012-04-19 13:21:06 -0400432 * @return void
433 */
Andrey Andreev838c9a92013-09-13 14:05:13 +0300434 function log_message($level, $message)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreev2f8d2d32013-08-07 15:54:47 +0300436 static $_log;
Barry Mienydd671972010-10-04 16:33:58 +0200437
Andrey Andreev49890a92013-08-19 19:56:18 +0300438 if ($_log === NULL)
Ted Woodb19a2032013-01-05 16:02:43 -0800439 {
vlakoff61f1aa02013-08-07 11:29:17 +0200440 // references cannot be directly assigned to static variables, so we use an array
441 $_log[0] =& load_class('Log', 'core');
Ted Woodb19a2032013-01-05 16:02:43 -0800442 }
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200443
Andrey Andreev838c9a92013-09-13 14:05:13 +0300444 $_log[0]->write_log($level, $message);
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400446}
Derek Allard2067d1a2008-11-13 22:59:24 +0000447
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600448// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000449
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400450if ( ! function_exists('set_status_header'))
451{
Timothy Warrenad475052012-04-19 13:21:06 -0400452 /**
453 * Set HTTP Status Header
454 *
455 * @param int the status code
456 * @param string
457 * @return void
458 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600459 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000460 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600461 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400462 200 => 'OK',
463 201 => 'Created',
464 202 => 'Accepted',
465 203 => 'Non-Authoritative Information',
466 204 => 'No Content',
467 205 => 'Reset Content',
468 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000469
Timothy Warrenad475052012-04-19 13:21:06 -0400470 300 => 'Multiple Choices',
471 301 => 'Moved Permanently',
472 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300473 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400474 304 => 'Not Modified',
475 305 => 'Use Proxy',
476 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600477
Timothy Warrenad475052012-04-19 13:21:06 -0400478 400 => 'Bad Request',
479 401 => 'Unauthorized',
480 403 => 'Forbidden',
481 404 => 'Not Found',
482 405 => 'Method Not Allowed',
483 406 => 'Not Acceptable',
484 407 => 'Proxy Authentication Required',
485 408 => 'Request Timeout',
486 409 => 'Conflict',
487 410 => 'Gone',
488 411 => 'Length Required',
489 412 => 'Precondition Failed',
490 413 => 'Request Entity Too Large',
491 414 => 'Request-URI Too Long',
492 415 => 'Unsupported Media Type',
493 416 => 'Requested Range Not Satisfiable',
494 417 => 'Expectation Failed',
495 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600496
Timothy Warrenad475052012-04-19 13:21:06 -0400497 500 => 'Internal Server Error',
498 501 => 'Not Implemented',
499 502 => 'Bad Gateway',
500 503 => 'Service Unavailable',
501 504 => 'Gateway Timeout',
502 505 => 'HTTP Version Not Supported'
503 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600504
Andrey Andreev51d6d842012-06-15 16:41:09 +0300505 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600506 {
507 show_error('Status codes must be numeric', 500);
508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Andrey Andreev51d6d842012-06-15 16:41:09 +0300510 is_int($code) OR $code = (int) $code;
511
512 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600513 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300514 if (isset($stati[$code]))
515 {
516 $text = $stati[$code];
517 }
518 else
519 {
520 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
521 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Andrey Andreevb7b43962012-02-27 22:45:48 +0200524 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600525
vlakoff40d12492013-08-06 14:46:00 +0200526 if (strpos(PHP_SAPI, 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600527 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700528 header('Status: '.$code.' '.$text, TRUE);
529 }
530 else
531 {
532 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600533 }
Derek Jones817163a2009-07-11 17:05:58 +0000534 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400535}
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600537// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000538
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400539if ( ! function_exists('_exception_handler'))
540{
Timothy Warrenad475052012-04-19 13:21:06 -0400541 /**
542 * Exception Handler
543 *
vlakoffc941d852013-08-06 14:44:40 +0200544 * This is the custom exception handler that is declared at the top
545 * of CodeIgniter.php. The main reason we use this is to permit
Timothy Warrenad475052012-04-19 13:21:06 -0400546 * PHP errors to be logged in our own log files since the user may
547 * not have access to server logs. Since this function
548 * effectively intercepts PHP errors, however, we also need
549 * to display errors based on the current error_reporting level.
550 * We do that with the use of a PHP error template.
551 *
Andrey Andreev0850a282013-10-21 14:26:18 +0300552 * @param int $severity
553 * @param string $message
554 * @param string $filepath
555 * @param int $line
Timothy Warrenad475052012-04-19 13:21:06 -0400556 * @return void
557 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600558 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200559 {
Andrey Andreev0850a282013-10-21 14:26:18 +0300560 $is_error = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) === $severity);
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200561
562 // When an error occurred, set the status header to '500 Internal Server Error'
563 // to indicate to the client something went wrong.
564 // This can't be done within the $_error->show_php_error method because
565 // it is only called when the display_errors flag is set (which isn't usually
566 // the case in a production environment) or when errors are ignored because
567 // they are above the error_reporting threshold.
568 if ($is_error)
569 {
570 set_status_header(500);
571 }
572
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600573 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200574
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300575 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600576 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300577 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600578 {
579 return;
580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Francesco Negri312bdc52012-08-04 07:32:19 -0400582 // Should we display the error?
583 if ((bool) ini_get('display_errors') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 $_error->show_php_error($severity, $message, $filepath, $line);
586 }
587
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600588 $_error->log_exception($severity, $message, $filepath, $line);
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200589
590 // If the error is fatal, the execution of the script should be stopped because
591 // errors can't be recovered from. Halting the script conforms with PHP's
592 // default error handling. See http://www.php.net/manual/en/errorfunc.constants.php
593 if ($is_error)
594 {
Jesse van Assencf60fa72013-09-27 11:58:44 +0200595 exit(EXIT_ERROR);
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200596 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600597 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400598}
Derek Allard2067d1a2008-11-13 22:59:24 +0000599
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800600// ------------------------------------------------------------------------
601
602if ( ! function_exists('_shutdown_handler'))
603{
604 /**
605 * Shutdown Handler
606 *
607 * This is the shutdown handler that is declared at the top
608 * of CodeIgniter.php. The main reason we use this is to simulate
609 * a complete custom exception handler.
610 *
vkeranovd6f3d312013-09-14 21:39:49 +0300611 * E_STRICT is purposivly neglected because such events may have
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800612 * been caught. Duplication or none? None is preferred for now.
613 *
614 * @link http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a
615 * @return void
616 */
617 function _shutdown_handler()
618 {
619 $last_error = function_exists('error_get_last') ? error_get_last() : NULL;
620 if (isset($last_error) &&
621 ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING)))
622 {
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800623 _exception_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']);
624 }
625 }
626}
627
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400628// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200629
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400630if ( ! function_exists('remove_invisible_characters'))
631{
Timothy Warrenad475052012-04-19 13:21:06 -0400632 /**
633 * Remove Invisible Characters
634 *
635 * This prevents sandwiching null characters
636 * between ascii characters, like Java\0script.
637 *
638 * @param string
639 * @param bool
640 * @return string
641 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400642 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500643 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400644 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200645
646 // every control character except newline (dec 10),
647 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400648 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500649 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400650 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
651 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500652 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200653
Pascal Kriete0ff50262011-04-05 14:52:03 -0400654 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500655
656 do
657 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400658 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500659 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400660 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500661
662 return $str;
663 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400664}
Derek Allard2067d1a2008-11-13 22:59:24 +0000665
kenjisfbac8b42011-08-25 10:51:44 +0900666// ------------------------------------------------------------------------
667
kenjisfbac8b42011-08-25 10:51:44 +0900668if ( ! function_exists('html_escape'))
669{
Timothy Warrenad475052012-04-19 13:21:06 -0400670 /**
671 * Returns HTML escaped variable
672 *
673 * @param mixed
674 * @return mixed
675 */
kenjisfbac8b42011-08-25 10:51:44 +0900676 function html_escape($var)
677 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200678 return is_array($var)
679 ? array_map('html_escape', $var)
680 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600681 }
Greg Akerd96f8822011-12-27 16:23:47 -0600682}
Greg Aker5c1aa632011-12-25 01:24:29 -0600683
Chad Furmana1abada2012-07-29 01:03:50 -0400684// ------------------------------------------------------------------------
685
686if ( ! function_exists('_stringify_attributes'))
687{
688 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300689 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400690 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300691 * Helper function used to convert a string, array, or object
692 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400693 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300694 * @param mixed string, array, object
695 * @param bool
696 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400697 */
698 function _stringify_attributes($attributes, $js = FALSE)
699 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300700 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400701
702 if (empty($attributes))
703 {
704 return $atts;
705 }
706
707 if (is_string($attributes))
708 {
709 return ' '.$attributes;
710 }
711
712 $attributes = (array) $attributes;
713
714 foreach ($attributes as $key => $val)
715 {
716 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
717 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300718
Chad Furmana1abada2012-07-29 01:03:50 -0400719 return rtrim($atts, ',');
720 }
721}
722
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200723// ------------------------------------------------------------------------
724
725if ( ! function_exists('function_usable'))
726{
727 /**
728 * Function usable
729 *
730 * Executes a function_exists() check, and if the Suhosin PHP
731 * extension is loaded - checks whether the function that is
732 * checked might be disabled in there as well.
733 *
734 * This is useful as function_exists() will return FALSE for
735 * functions disabled via the *disable_functions* php.ini
736 * setting, but not for *suhosin.executor.func.blacklist* and
737 * *suhosin.executor.disable_eval*. These settings will just
738 * terminate script execution if a disabled function is executed.
739 *
740 * @link http://www.hardened-php.net/suhosin/
741 * @param string $function_name Function to check for
742 * @return bool TRUE if the function exists and is safe to call,
743 * FALSE otherwise.
744 */
745 function function_usable($function_name)
746 {
747 static $_suhosin_func_blacklist;
748
749 if (function_exists($function_name))
750 {
751 if ( ! isset($_suhosin_func_blacklist))
752 {
Andrey Andreevae634622012-12-17 10:30:18 +0200753 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200754 {
Andrey Andreevae634622012-12-17 10:30:18 +0200755 $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist')));
756
757 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval'))
758 {
759 $_suhosin_func_blacklist[] = 'eval';
760 }
761 }
762 else
763 {
764 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200765 }
766 }
767
Andrey Andreevae634622012-12-17 10:30:18 +0200768 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200769 }
770
771 return FALSE;
772 }
773}
Richard Deurwaarder (Xeli)668d0092013-06-24 13:50:52 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775/* End of file Common.php */
Andrey Andreev13c818e2013-09-14 21:44:36 +0300776/* Location: ./system/core/Common.php */