blob: b850fd39a15e1a4cf70a05bf680378886ffb6e83 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev188abaf2012-01-07 19:09:42 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev188abaf2012-01-07 19:09:42 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060041 * Common Functions
42 *
43 * Loads the base classes and executes the request.
44 *
45 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030046 * @subpackage CodeIgniter
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060047 * @category Common Functions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060049 * @link http://codeigniter.com/user_guide/
50 */
51
52// ------------------------------------------------------------------------
53
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040054if ( ! function_exists('is_php'))
55{
Timothy Warrenad475052012-04-19 13:21:06 -040056 /**
kakysha654e5c92015-01-27 20:51:50 +030057 * Determines if the current version of PHP is equal to or greater than the supplied value
Timothy Warrenad475052012-04-19 13:21:06 -040058 *
Timothy Warrenad475052012-04-19 13:21:06 -040059 * @param string
60 * @return bool TRUE if the current version is $version or higher
61 */
vlakoff629d3752014-04-05 09:52:01 +020062 function is_php($version)
Derek Jones086ee5a2009-07-28 14:42:12 +000063 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060064 static $_is_php;
Andrey Andreevb7b43962012-02-27 22:45:48 +020065 $version = (string) $version;
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060067 if ( ! isset($_is_php[$version]))
68 {
vlakoff629d3752014-04-05 09:52:01 +020069 $_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060070 }
Derek Jones086ee5a2009-07-28 14:42:12 +000071
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060072 return $_is_php[$version];
73 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040074}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000075
Derek Jones086ee5a2009-07-28 14:42:12 +000076// ------------------------------------------------------------------------
77
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040078if ( ! function_exists('is_really_writable'))
79{
Timothy Warrenad475052012-04-19 13:21:06 -040080 /**
81 * Tests for file writability
82 *
83 * is_writable() returns TRUE on Windows servers when you really can't write to
84 * the file, based on the read-only attribute. is_writable() is also unreliable
85 * on Unix servers if safe_mode is on.
86 *
Andrey Andreev2b284f92014-01-25 00:25:56 +020087 * @link https://bugs.php.net/bug.php?id=54709
Timothy Warrenad475052012-04-19 13:21:06 -040088 * @param string
Gabriel Potkány1fb50002015-02-04 01:45:59 +010089 * @return bool
Timothy Warrenad475052012-04-19 13:21:06 -040090 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060091 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060093 // If we're on a Unix server with safe_mode off we call is_writable
Andrey Andreevf6274742014-02-20 18:05:58 +020094 if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR ! ini_get('safe_mode')))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060095 {
96 return is_writable($file);
97 }
Derek Allard2067d1a2008-11-13 22:59:24 +000098
Andrey Andreev188abaf2012-01-07 19:09:42 +020099 /* For Windows servers and safe_mode "on" installations we'll actually
100 * write a file then read it. Bah...
101 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600102 if (is_dir($file))
103 {
vlakoff06127562013-03-30 00:06:39 +0100104 $file = rtrim($file, '/').'/'.md5(mt_rand());
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200105 if (($fp = @fopen($file, 'ab')) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600106 {
107 return FALSE;
108 }
109
110 fclose($fp);
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200111 @chmod($file, 0777);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600112 @unlink($file);
113 return TRUE;
114 }
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200115 elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 return FALSE;
118 }
119
120 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 return TRUE;
122 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400123}
Derek Allard2067d1a2008-11-13 22:59:24 +0000124
125// ------------------------------------------------------------------------
126
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400127if ( ! function_exists('load_class'))
128{
Timothy Warrenad475052012-04-19 13:21:06 -0400129 /**
130 * Class registry
131 *
132 * This function acts as a singleton. If the requested class does not
133 * exist it is instantiated and set to a static variable. If it has
134 * previously been instantiated the variable is returned.
135 *
136 * @param string the class name being requested
137 * @param string the directory where the class should be found
vlakoff102105d2014-05-18 05:13:33 +0200138 * @param string an optional argument to pass to the class constructor
Timothy Warrenad475052012-04-19 13:21:06 -0400139 * @return object
140 */
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200141 function &load_class($class, $directory = 'libraries', $param = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600143 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200144
Andrey Andreev188abaf2012-01-07 19:09:42 +0200145 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600146 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600148 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600150
151 $name = FALSE;
152
Shane Pearsonab57a352011-08-22 16:11:20 -0500153 // Look for the class first in the local application/libraries folder
154 // then in the native system/libraries folder
155 foreach (array(APPPATH, BASEPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200156 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200157 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600158 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200159 $name = 'CI_'.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreev49e68de2013-02-21 16:30:55 +0200161 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600162 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300163 require_once($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600166 break;
167 }
168 }
169
Andrey Andreev188abaf2012-01-07 19:09:42 +0200170 // Is the request a class extension? If so we load it too
Andrey Andreev536b7712012-01-07 21:31:25 +0200171 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200172 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600173 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200174
Andrey Andreev49e68de2013-02-21 16:30:55 +0200175 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600176 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200177 require_once(APPPATH.$directory.'/'.$name.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600178 }
179 }
180
181 // Did we find the class?
182 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
mult1mate5c58e672015-04-08 16:03:31 +0300184 // Note: We use exit() rather than show_error() in order to avoid a
vlakofffe8fe452012-07-11 19:56:50 +0200185 // self-referencing loop with the Exceptions class
Kevin Cuppd63e4012012-02-05 14:14:32 -0500186 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700187 echo 'Unable to locate the specified class: '.$class.'.php';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200188 exit(5); // EXIT_UNK_CLASS
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600190
191 // Keep track of what we just loaded
192 is_loaded($class);
193
Andrey Andreevde9ec102014-02-24 17:16:32 +0200194 $_classes[$class] = isset($param)
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200195 ? new $name($param)
196 : new $name();
Andrey Andreevde9ec102014-02-24 17:16:32 +0200197 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400199}
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600201// --------------------------------------------------------------------
202
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400203if ( ! function_exists('is_loaded'))
204{
Timothy Warrenad475052012-04-19 13:21:06 -0400205 /**
206 * Keeps track of which libraries have been loaded. This function is
207 * called by the load_class() function above
208 *
209 * @param string
210 * @return array
211 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200212 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600213 {
214 static $_is_loaded = array();
215
Alex Bilbieed944a32012-06-02 11:07:47 +0100216 if ($class !== '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600217 {
218 $_is_loaded[strtolower($class)] = $class;
219 }
220
221 return $_is_loaded;
222 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400223}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600224
225// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000226
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400227if ( ! function_exists('get_config'))
228{
Timothy Warrenad475052012-04-19 13:21:06 -0400229 /**
230 * Loads the main config.php file
231 *
232 * This function lets us grab the config file even if the Config class
233 * hasn't been instantiated yet
234 *
235 * @param array
236 * @return array
237 */
Andrey Andreev49890a92013-08-19 19:56:18 +0300238 function &get_config(Array $replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
vlakoff5a346882014-05-17 07:17:41 +0200240 static $config;
Barry Mienydd671972010-10-04 16:33:58 +0200241
vlakoff5a346882014-05-17 07:17:41 +0200242 if (empty($config))
vlakoff8d70c0a2013-08-17 07:31:29 +0200243 {
244 $file_path = APPPATH.'config/config.php';
245 $found = FALSE;
246 if (file_exists($file_path))
247 {
248 $found = TRUE;
249 require($file_path);
250 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600251
vlakoff8d70c0a2013-08-17 07:31:29 +0200252 // Is the config file in the environment folder?
253 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
254 {
255 require($file_path);
256 }
257 elseif ( ! $found)
258 {
259 set_status_header(503);
260 echo 'The configuration file does not exist.';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200261 exit(3); // EXIT_CONFIG
vlakoff8d70c0a2013-08-17 07:31:29 +0200262 }
joelcox2035fd82011-01-16 16:50:36 +0100263
vlakoff8d70c0a2013-08-17 07:31:29 +0200264 // Does the $config array exist in the file?
265 if ( ! isset($config) OR ! is_array($config))
266 {
267 set_status_header(503);
268 echo 'Your config file does not appear to be formatted correctly.';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200269 exit(3); // EXIT_CONFIG
vlakoff8d70c0a2013-08-17 07:31:29 +0200270 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272
vlakoff67e5ca62013-08-19 04:52:00 +0200273 // Are any values being dynamically added or replaced?
vlakoff2f7810a2013-08-19 04:46:26 +0200274 foreach ($replace as $key => $val)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600275 {
vlakoff5a346882014-05-17 07:17:41 +0200276 $config[$key] = $val;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
vlakoff5a346882014-05-17 07:17:41 +0200279 return $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400281}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600282
283// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400285if ( ! function_exists('config_item'))
286{
Timothy Warrenad475052012-04-19 13:21:06 -0400287 /**
288 * Returns the specified config item
289 *
290 * @param string
291 * @return mixed
292 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600293 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
vlakoffaf431ce2013-07-19 02:06:41 +0200295 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200296
vlakoffaf431ce2013-07-19 02:06:41 +0200297 if (empty($_config))
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
vlakoffaf431ce2013-07-19 02:06:41 +0200299 // references cannot be directly assigned to static variables, so we use an array
300 $_config[0] =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Andrey Andreevd444d442014-10-06 00:00:08 +0300303 return isset($_config[0][$item]) ? $_config[0][$item] : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400305}
Derek Allard2067d1a2008-11-13 22:59:24 +0000306
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600307// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000308
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300309if ( ! function_exists('get_mimes'))
310{
311 /**
312 * Returns the MIME types array from config/mimes.php
313 *
314 * @return array
315 */
316 function &get_mimes()
317 {
vlakoff69550c52014-05-19 13:45:02 +0200318 static $_mimes;
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300319
vlakoff69550c52014-05-19 13:45:02 +0200320 if (empty($_mimes))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300321 {
vlakoff69550c52014-05-19 13:45:02 +0200322 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
323 {
324 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
325 }
326 elseif (file_exists(APPPATH.'config/mimes.php'))
327 {
328 $_mimes = include(APPPATH.'config/mimes.php');
329 }
330 else
331 {
332 $_mimes = array();
333 }
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300334 }
335
336 return $_mimes;
337 }
338}
339
340// ------------------------------------------------------------------------
341
Andrey Andreev3fb02672012-10-22 16:48:01 +0300342if ( ! function_exists('is_https'))
343{
344 /**
345 * Is HTTPS?
346 *
347 * Determines if the application is accessed via an encrypted
348 * (HTTPS) connection.
349 *
350 * @return bool
351 */
352 function is_https()
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200353 {
Andrey Andreev333b80e2013-07-01 16:21:54 +0300354 if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200355 {
356 return TRUE;
357 }
358 elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
359 {
360 return TRUE;
361 }
Andrey Andreev333b80e2013-07-01 16:21:54 +0300362 elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200363 {
364 return TRUE;
365 }
Richard Deurwaarder (Xeli)98999972013-06-24 15:19:30 +0200366
Richard Deurwaarder (Xeli)7cc29452013-06-24 15:06:19 +0200367 return FALSE;
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200368 }
Andrey Andreev3fb02672012-10-22 16:48:01 +0300369}
370
371// ------------------------------------------------------------------------
372
Andrey Andreevf964b162013-11-12 17:04:55 +0200373if ( ! function_exists('is_cli'))
374{
375
376 /**
377 * Is CLI?
378 *
379 * Test to see if a request was made from the command line.
380 *
381 * @return bool
382 */
383 function is_cli()
384 {
385 return (PHP_SAPI === 'cli' OR defined('STDIN'));
386 }
387}
388
389// ------------------------------------------------------------------------
390
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400391if ( ! function_exists('show_error'))
392{
Timothy Warrenad475052012-04-19 13:21:06 -0400393 /**
394 * Error Handler
395 *
396 * This function lets us invoke the exception class and
397 * display errors using the standard error template located
vlakoff52301c72013-03-29 14:23:34 +0100398 * in application/views/errors/error_general.php
Timothy Warrenad475052012-04-19 13:21:06 -0400399 * This function will send the error page directly to the
400 * browser and exit.
401 *
402 * @param string
403 * @param int
404 * @param string
405 * @return void
406 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600407 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
408 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700409 $status_code = abs($status_code);
410 if ($status_code < 100)
411 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200412 $exit_status = $status_code + 9; // 9 is EXIT__AUTO_MIN
413 if ($exit_status > 125) // 125 is EXIT__AUTO_MAX
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700414 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200415 $exit_status = 1; // EXIT_ERROR
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700416 }
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200417
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700418 $status_code = 500;
419 }
420 else
421 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200422 $exit_status = 1; // EXIT_ERROR
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700423 }
Andrey Andreev5a6814e2013-03-04 15:44:12 +0200424
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600425 $_error =& load_class('Exceptions', 'core');
426 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700427 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600428 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400429}
Derek Allard2067d1a2008-11-13 22:59:24 +0000430
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600431// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000432
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400433if ( ! function_exists('show_404'))
434{
Timothy Warrenad475052012-04-19 13:21:06 -0400435 /**
436 * 404 Page Handler
437 *
438 * This function is similar to the show_error() function above
439 * However, instead of the standard error template it displays
440 * 404 errors.
441 *
442 * @param string
443 * @param bool
444 * @return void
445 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400446 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600447 {
448 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400449 $_error->show_404($page, $log_error);
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200450 exit(4); // EXIT_UNKNOWN_FILE
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600451 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400452}
Derek Allard2067d1a2008-11-13 22:59:24 +0000453
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600454// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000455
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400456if ( ! function_exists('log_message'))
457{
Timothy Warrenad475052012-04-19 13:21:06 -0400458 /**
459 * Error Logging Interface
460 *
461 * We use this as a simple mechanism to access the logging
462 * class and send messages to be logged.
463 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200464 * @param string the error level: 'error', 'debug' or 'info'
465 * @param string the error message
Timothy Warrenad475052012-04-19 13:21:06 -0400466 * @return void
467 */
Andrey Andreev838c9a92013-09-13 14:05:13 +0300468 function log_message($level, $message)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreev2f8d2d32013-08-07 15:54:47 +0300470 static $_log;
Barry Mienydd671972010-10-04 16:33:58 +0200471
Andrey Andreev49890a92013-08-19 19:56:18 +0300472 if ($_log === NULL)
Ted Woodb19a2032013-01-05 16:02:43 -0800473 {
vlakoff61f1aa02013-08-07 11:29:17 +0200474 // references cannot be directly assigned to static variables, so we use an array
475 $_log[0] =& load_class('Log', 'core');
Ted Woodb19a2032013-01-05 16:02:43 -0800476 }
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200477
Andrey Andreev838c9a92013-09-13 14:05:13 +0300478 $_log[0]->write_log($level, $message);
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400480}
Derek Allard2067d1a2008-11-13 22:59:24 +0000481
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600482// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000483
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400484if ( ! function_exists('set_status_header'))
485{
Timothy Warrenad475052012-04-19 13:21:06 -0400486 /**
487 * Set HTTP Status Header
488 *
489 * @param int the status code
490 * @param string
491 * @return void
492 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600493 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000494 {
Andrey Andreevc545c012015-02-19 11:36:10 +0200495 if (is_cli())
496 {
497 return;
498 }
499
Andrey Andreev51d6d842012-06-15 16:41:09 +0300500 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600501 {
502 show_error('Status codes must be numeric', 500);
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Andrey Andreev51d6d842012-06-15 16:41:09 +0300505 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600506 {
Fieahb4ebb392015-02-22 23:55:15 +0800507 is_int($code) OR $code = (int) $code;
508 $stati = array(
ftwbzhao2b7a97c2015-04-14 11:19:38 +0800509 100 => 'Continue',
510 101 => 'Switching Protocols',
511
Fieahb4ebb392015-02-22 23:55:15 +0800512 200 => 'OK',
513 201 => 'Created',
514 202 => 'Accepted',
515 203 => 'Non-Authoritative Information',
516 204 => 'No Content',
517 205 => 'Reset Content',
518 206 => 'Partial Content',
519
520 300 => 'Multiple Choices',
521 301 => 'Moved Permanently',
522 302 => 'Found',
523 303 => 'See Other',
524 304 => 'Not Modified',
525 305 => 'Use Proxy',
526 307 => 'Temporary Redirect',
527
528 400 => 'Bad Request',
529 401 => 'Unauthorized',
ftwbzhao2b7a97c2015-04-14 11:19:38 +0800530 402 => 'Payment Required',
Fieahb4ebb392015-02-22 23:55:15 +0800531 403 => 'Forbidden',
532 404 => 'Not Found',
533 405 => 'Method Not Allowed',
534 406 => 'Not Acceptable',
535 407 => 'Proxy Authentication Required',
536 408 => 'Request Timeout',
537 409 => 'Conflict',
538 410 => 'Gone',
539 411 => 'Length Required',
540 412 => 'Precondition Failed',
541 413 => 'Request Entity Too Large',
542 414 => 'Request-URI Too Long',
543 415 => 'Unsupported Media Type',
544 416 => 'Requested Range Not Satisfiable',
545 417 => 'Expectation Failed',
546 422 => 'Unprocessable Entity',
547
548 500 => 'Internal Server Error',
549 501 => 'Not Implemented',
550 502 => 'Bad Gateway',
551 503 => 'Service Unavailable',
552 504 => 'Gateway Timeout',
553 505 => 'HTTP Version Not Supported'
554 );
555
Andrey Andreev51d6d842012-06-15 16:41:09 +0300556 if (isset($stati[$code]))
557 {
558 $text = $stati[$code];
559 }
560 else
561 {
562 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
563 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
vlakoff40d12492013-08-06 14:46:00 +0200566 if (strpos(PHP_SAPI, 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600567 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700568 header('Status: '.$code.' '.$text, TRUE);
569 }
570 else
571 {
bjjay01daaca2015-01-31 22:29:50 +0800572 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
573 header($server_protocol.' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600574 }
Derek Jones817163a2009-07-11 17:05:58 +0000575 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400576}
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600578// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000579
Andrey Andreev4b838af2014-10-28 23:46:45 +0200580if ( ! function_exists('_error_handler'))
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400581{
Timothy Warrenad475052012-04-19 13:21:06 -0400582 /**
Andrey Andreev4b838af2014-10-28 23:46:45 +0200583 * Error Handler
Timothy Warrenad475052012-04-19 13:21:06 -0400584 *
Andrey Andreev4b838af2014-10-28 23:46:45 +0200585 * This is the custom error handler that is declared at the (relative)
586 * top of CodeIgniter.php. The main reason we use this is to permit
Timothy Warrenad475052012-04-19 13:21:06 -0400587 * PHP errors to be logged in our own log files since the user may
Andrey Andreev4b838af2014-10-28 23:46:45 +0200588 * not have access to server logs. Since this function effectively
589 * intercepts PHP errors, however, we also need to display errors
590 * based on the current error_reporting level.
Timothy Warrenad475052012-04-19 13:21:06 -0400591 * We do that with the use of a PHP error template.
592 *
Andrey Andreev0850a282013-10-21 14:26:18 +0300593 * @param int $severity
594 * @param string $message
595 * @param string $filepath
596 * @param int $line
Timothy Warrenad475052012-04-19 13:21:06 -0400597 * @return void
598 */
Andrey Andreev4b838af2014-10-28 23:46:45 +0200599 function _error_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200600 {
Andrey Andreev0850a282013-10-21 14:26:18 +0300601 $is_error = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) === $severity);
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200602
603 // When an error occurred, set the status header to '500 Internal Server Error'
604 // to indicate to the client something went wrong.
605 // This can't be done within the $_error->show_php_error method because
606 // it is only called when the display_errors flag is set (which isn't usually
607 // the case in a production environment) or when errors are ignored because
608 // they are above the error_reporting threshold.
609 if ($is_error)
610 {
611 set_status_header(500);
Andrey Andreev02545892014-02-19 23:49:31 +0200612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300614 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600615 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300616 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600617 {
618 return;
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Cristian Kocza2be27442014-02-19 21:52:38 +0200621 $_error =& load_class('Exceptions', 'core');
Andrey Andreev76160bc2014-01-30 22:26:14 +0200622 $_error->log_exception($severity, $message, $filepath, $line);
623
Francesco Negri312bdc52012-08-04 07:32:19 -0400624 // Should we display the error?
Andrey Andreev38666662015-01-13 15:53:25 +0200625 if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
627 $_error->show_php_error($severity, $message, $filepath, $line);
628 }
629
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200630 // If the error is fatal, the execution of the script should be stopped because
631 // errors can't be recovered from. Halting the script conforms with PHP's
632 // default error handling. See http://www.php.net/manual/en/errorfunc.constants.php
633 if ($is_error)
634 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200635 exit(1); // EXIT_ERROR
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200636 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600637 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400638}
Derek Allard2067d1a2008-11-13 22:59:24 +0000639
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800640// ------------------------------------------------------------------------
641
Andrey Andreev4b838af2014-10-28 23:46:45 +0200642if ( ! function_exists('_exception_handler'))
643{
644 /**
645 * Exception Handler
646 *
647 * Sends uncaught exceptions to the logger and displays them
648 * only if display_errors is On so that they don't show up in
649 * production environments.
650 *
651 * @param Exception $exception
652 * @return void
653 */
654 function _exception_handler($exception)
655 {
656 $_error =& load_class('Exceptions', 'core');
657 $_error->log_exception('error', 'Exception: '.$exception->getMessage(), $exception->getFile(), $exception->getLine());
658
659 // Should we display the error?
Andrey Andreev38666662015-01-13 15:53:25 +0200660 if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors')))
Andrey Andreev4b838af2014-10-28 23:46:45 +0200661 {
662 $_error->show_exception($exception);
663 }
664
665 exit(1); // EXIT_ERROR
666 }
667}
668
669// ------------------------------------------------------------------------
670
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800671if ( ! function_exists('_shutdown_handler'))
672{
673 /**
674 * Shutdown Handler
675 *
676 * This is the shutdown handler that is declared at the top
677 * of CodeIgniter.php. The main reason we use this is to simulate
678 * a complete custom exception handler.
679 *
vkeranovd6f3d312013-09-14 21:39:49 +0300680 * E_STRICT is purposivly neglected because such events may have
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800681 * been caught. Duplication or none? None is preferred for now.
682 *
683 * @link http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a
684 * @return void
685 */
686 function _shutdown_handler()
687 {
Andrey Andreevafca3522013-11-14 15:26:59 +0200688 $last_error = error_get_last();
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800689 if (isset($last_error) &&
690 ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING)))
691 {
Andrey Andreeva0471dc2014-11-04 19:22:38 +0200692 _error_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']);
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800693 }
694 }
695}
696
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400697// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200698
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400699if ( ! function_exists('remove_invisible_characters'))
700{
Timothy Warrenad475052012-04-19 13:21:06 -0400701 /**
702 * Remove Invisible Characters
703 *
704 * This prevents sandwiching null characters
705 * between ascii characters, like Java\0script.
706 *
707 * @param string
708 * @param bool
709 * @return string
710 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400711 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500712 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400713 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200714
715 // every control character except newline (dec 10),
716 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400717 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500718 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400719 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
720 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500721 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200722
Pascal Kriete0ff50262011-04-05 14:52:03 -0400723 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500724
725 do
726 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400727 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500728 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400729 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500730
731 return $str;
732 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400733}
Derek Allard2067d1a2008-11-13 22:59:24 +0000734
kenjisfbac8b42011-08-25 10:51:44 +0900735// ------------------------------------------------------------------------
736
kenjisfbac8b42011-08-25 10:51:44 +0900737if ( ! function_exists('html_escape'))
738{
Timothy Warrenad475052012-04-19 13:21:06 -0400739 /**
Ivan Tcholakov4f458582014-08-25 11:20:22 +0300740 * Returns HTML escaped variable.
Timothy Warrenad475052012-04-19 13:21:06 -0400741 *
Ivan Tcholakov993f98c2014-08-25 12:13:31 +0300742 * @param mixed $var The input string or array of strings to be escaped.
743 * @param bool $double_encode $double_encode set to FALSE prevents escaping twice.
744 * @return mixed The escaped string or array of strings as a result.
Timothy Warrenad475052012-04-19 13:21:06 -0400745 */
Ivan Tcholakov4f458582014-08-25 11:20:22 +0300746 function html_escape($var, $double_encode = TRUE)
kenjisfbac8b42011-08-25 10:51:44 +0900747 {
Joshua Logsdon993e3402015-03-25 12:03:43 -0400748 if (empty($var))
749 {
750 return $var;
751 }
Andrey Andreev6b620fb2015-04-20 12:46:46 +0300752
Ivan Tcholakov62224372014-08-25 15:48:33 +0300753 if (is_array($var))
754 {
Ivan Tcholakovc851dc52014-08-26 01:49:11 +0300755 return array_map('html_escape', $var, array_fill(0, count($var), $double_encode));
Ivan Tcholakov62224372014-08-25 15:48:33 +0300756 }
757
758 return htmlspecialchars($var, ENT_QUOTES, config_item('charset'), $double_encode);
Greg Aker5c1aa632011-12-25 01:24:29 -0600759 }
Greg Akerd96f8822011-12-27 16:23:47 -0600760}
Greg Aker5c1aa632011-12-25 01:24:29 -0600761
Chad Furmana1abada2012-07-29 01:03:50 -0400762// ------------------------------------------------------------------------
763
764if ( ! function_exists('_stringify_attributes'))
765{
766 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300767 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400768 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300769 * Helper function used to convert a string, array, or object
770 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400771 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300772 * @param mixed string, array, object
773 * @param bool
774 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400775 */
776 function _stringify_attributes($attributes, $js = FALSE)
777 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300778 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400779
780 if (empty($attributes))
781 {
782 return $atts;
783 }
784
785 if (is_string($attributes))
786 {
787 return ' '.$attributes;
788 }
789
790 $attributes = (array) $attributes;
791
792 foreach ($attributes as $key => $val)
793 {
794 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
795 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300796
Chad Furmana1abada2012-07-29 01:03:50 -0400797 return rtrim($atts, ',');
798 }
799}
800
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200801// ------------------------------------------------------------------------
802
803if ( ! function_exists('function_usable'))
804{
805 /**
806 * Function usable
807 *
808 * Executes a function_exists() check, and if the Suhosin PHP
809 * extension is loaded - checks whether the function that is
810 * checked might be disabled in there as well.
811 *
812 * This is useful as function_exists() will return FALSE for
813 * functions disabled via the *disable_functions* php.ini
814 * setting, but not for *suhosin.executor.func.blacklist* and
815 * *suhosin.executor.disable_eval*. These settings will just
816 * terminate script execution if a disabled function is executed.
817 *
Andrey Andreevaaa8ddb2014-02-03 14:10:44 +0200818 * The above described behavior turned out to be a bug in Suhosin,
819 * but even though a fix was commited for 0.9.34 on 2012-02-12,
820 * that version is yet to be released. This function will therefore
821 * be just temporary, but would probably be kept for a few years.
822 *
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200823 * @link http://www.hardened-php.net/suhosin/
824 * @param string $function_name Function to check for
825 * @return bool TRUE if the function exists and is safe to call,
826 * FALSE otherwise.
827 */
828 function function_usable($function_name)
829 {
830 static $_suhosin_func_blacklist;
831
832 if (function_exists($function_name))
833 {
834 if ( ! isset($_suhosin_func_blacklist))
835 {
Andrey Andreevae634622012-12-17 10:30:18 +0200836 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200837 {
Andrey Andreevf6274742014-02-20 18:05:58 +0200838 $_suhosin_func_blacklist = explode(',', trim(ini_get('suhosin.executor.func.blacklist')));
Andrey Andreevae634622012-12-17 10:30:18 +0200839
Andrey Andreevf6274742014-02-20 18:05:58 +0200840 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && ini_get('suhosin.executor.disable_eval'))
Andrey Andreevae634622012-12-17 10:30:18 +0200841 {
842 $_suhosin_func_blacklist[] = 'eval';
843 }
844 }
845 else
846 {
847 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200848 }
849 }
850
Andrey Andreevae634622012-12-17 10:30:18 +0200851 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200852 }
853
854 return FALSE;
855 }
856}