blob: fda747b0511de6c9864fb7f772948c05c16b9a2b [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 */
vlakoff629d3752014-04-05 09:52:01 +020054 function is_php($version)
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 {
vlakoff629d3752014-04-05 09:52:01 +020061 $_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');
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 *
Andrey Andreev2b284f92014-01-25 00:25:56 +020079 * @link https://bugs.php.net/bug.php?id=54709
Timothy Warrenad475052012-04-19 13:21:06 -040080 * @param string
81 * @return void
82 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060083 function is_really_writable($file)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060085 // If we're on a Unix server with safe_mode off we call is_writable
Andrey Andreevf6274742014-02-20 18:05:58 +020086 if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR ! ini_get('safe_mode')))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060087 {
88 return is_writable($file);
89 }
Derek Allard2067d1a2008-11-13 22:59:24 +000090
Andrey Andreev188abaf2012-01-07 19:09:42 +020091 /* For Windows servers and safe_mode "on" installations we'll actually
92 * write a file then read it. Bah...
93 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060094 if (is_dir($file))
95 {
vlakoff06127562013-03-30 00:06:39 +010096 $file = rtrim($file, '/').'/'.md5(mt_rand());
Andrey Andreev7cf682a2014-03-13 14:55:45 +020097 if (($fp = @fopen($file, 'ab')) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060098 {
99 return FALSE;
100 }
101
102 fclose($fp);
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200103 @chmod($file, 0777);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600104 @unlink($file);
105 return TRUE;
106 }
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200107 elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
109 return FALSE;
110 }
111
112 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return TRUE;
114 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400115}
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117// ------------------------------------------------------------------------
118
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400119if ( ! function_exists('load_class'))
120{
Timothy Warrenad475052012-04-19 13:21:06 -0400121 /**
122 * Class registry
123 *
124 * This function acts as a singleton. If the requested class does not
125 * exist it is instantiated and set to a static variable. If it has
126 * previously been instantiated the variable is returned.
127 *
128 * @param string the class name being requested
129 * @param string the directory where the class should be found
130 * @param string the class name prefix
131 * @return object
132 */
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200133 function &load_class($class, $directory = 'libraries', $param = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600135 static $_classes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200136
Andrey Andreev188abaf2012-01-07 19:09:42 +0200137 // Does the class exist? If so, we're done...
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600138 if (isset($_classes[$class]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600140 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600142
143 $name = FALSE;
144
Shane Pearsonab57a352011-08-22 16:11:20 -0500145 // Look for the class first in the local application/libraries folder
146 // then in the native system/libraries folder
147 foreach (array(APPPATH, BASEPATH) as $path)
Barry Mienydd671972010-10-04 16:33:58 +0200148 {
Andrey Andreev536b7712012-01-07 21:31:25 +0200149 if (file_exists($path.$directory.'/'.$class.'.php'))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600150 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200151 $name = 'CI_'.$class;
Barry Mienydd671972010-10-04 16:33:58 +0200152
Andrey Andreev49e68de2013-02-21 16:30:55 +0200153 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600154 {
Andrey Andreeva52c7752012-10-11 10:54:02 +0300155 require_once($path.$directory.'/'.$class.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600158 break;
159 }
160 }
161
Andrey Andreev188abaf2012-01-07 19:09:42 +0200162 // Is the request a class extension? If so we load it too
Andrey Andreev536b7712012-01-07 21:31:25 +0200163 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600165 $name = config_item('subclass_prefix').$class;
Barry Mienydd671972010-10-04 16:33:58 +0200166
Andrey Andreev49e68de2013-02-21 16:30:55 +0200167 if (class_exists($name, FALSE) === FALSE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600168 {
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200169 require_once(APPPATH.$directory.'/'.$name.'.php');
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600170 }
171 }
172
173 // Did we find the class?
174 if ($name === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Barry Mienydd671972010-10-04 16:33:58 +0200176 // Note: We use exit() rather then show_error() in order to avoid a
vlakofffe8fe452012-07-11 19:56:50 +0200177 // self-referencing loop with the Exceptions class
Kevin Cuppd63e4012012-02-05 14:14:32 -0500178 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700179 echo 'Unable to locate the specified class: '.$class.'.php';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200180 exit(5); // EXIT_UNK_CLASS
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600182
183 // Keep track of what we just loaded
184 is_loaded($class);
185
Andrey Andreevde9ec102014-02-24 17:16:32 +0200186 $_classes[$class] = isset($param)
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200187 ? new $name($param)
188 : new $name();
Andrey Andreevde9ec102014-02-24 17:16:32 +0200189 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400191}
Derek Allard2067d1a2008-11-13 22:59:24 +0000192
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600193// --------------------------------------------------------------------
194
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400195if ( ! function_exists('is_loaded'))
196{
Timothy Warrenad475052012-04-19 13:21:06 -0400197 /**
198 * Keeps track of which libraries have been loaded. This function is
199 * called by the load_class() function above
200 *
201 * @param string
202 * @return array
203 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200204 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600205 {
206 static $_is_loaded = array();
207
Alex Bilbieed944a32012-06-02 11:07:47 +0100208 if ($class !== '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600209 {
210 $_is_loaded[strtolower($class)] = $class;
211 }
212
213 return $_is_loaded;
214 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400215}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600216
217// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000218
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400219if ( ! function_exists('get_config'))
220{
Timothy Warrenad475052012-04-19 13:21:06 -0400221 /**
222 * Loads the main config.php file
223 *
224 * This function lets us grab the config file even if the Config class
225 * hasn't been instantiated yet
226 *
227 * @param array
228 * @return array
229 */
Andrey Andreev49890a92013-08-19 19:56:18 +0300230 function &get_config(Array $replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600232 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200233
vlakoff05d043b2013-08-19 04:55:34 +0200234 if (empty($_config))
vlakoff8d70c0a2013-08-17 07:31:29 +0200235 {
236 $file_path = APPPATH.'config/config.php';
237 $found = FALSE;
238 if (file_exists($file_path))
239 {
240 $found = TRUE;
241 require($file_path);
242 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600243
vlakoff8d70c0a2013-08-17 07:31:29 +0200244 // Is the config file in the environment folder?
245 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
246 {
247 require($file_path);
248 }
249 elseif ( ! $found)
250 {
251 set_status_header(503);
252 echo 'The configuration file does not exist.';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200253 exit(3); // EXIT_CONFIG
vlakoff8d70c0a2013-08-17 07:31:29 +0200254 }
joelcox2035fd82011-01-16 16:50:36 +0100255
vlakoff8d70c0a2013-08-17 07:31:29 +0200256 // Does the $config array exist in the file?
257 if ( ! isset($config) OR ! is_array($config))
258 {
259 set_status_header(503);
260 echo 'Your config file does not appear to be formatted correctly.';
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200261 exit(3); // EXIT_CONFIG
vlakoff8d70c0a2013-08-17 07:31:29 +0200262 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100263
vlakoff05d043b2013-08-19 04:55:34 +0200264 // references cannot be directly assigned to static variables, so we use an array
vlakoff8d70c0a2013-08-17 07:31:29 +0200265 $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
267
vlakoff67e5ca62013-08-19 04:52:00 +0200268 // Are any values being dynamically added or replaced?
vlakoff2f7810a2013-08-19 04:46:26 +0200269 foreach ($replace as $key => $val)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600270 {
vlakoff05d043b2013-08-19 04:55:34 +0200271 $_config[0][$key] = $val;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
vlakoff05d043b2013-08-19 04:55:34 +0200274 return $_config[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400276}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600277
278// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400280if ( ! function_exists('config_item'))
281{
Timothy Warrenad475052012-04-19 13:21:06 -0400282 /**
283 * Returns the specified config item
284 *
285 * @param string
286 * @return mixed
287 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600288 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
vlakoffaf431ce2013-07-19 02:06:41 +0200290 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200291
vlakoffaf431ce2013-07-19 02:06:41 +0200292 if (empty($_config))
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
vlakoffaf431ce2013-07-19 02:06:41 +0200294 // references cannot be directly assigned to static variables, so we use an array
295 $_config[0] =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
vlakoffaf431ce2013-07-19 02:06:41 +0200298 return isset($_config[0][$item]) ? $_config[0][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400300}
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600302// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300304if ( ! function_exists('get_mimes'))
305{
306 /**
307 * Returns the MIME types array from config/mimes.php
308 *
309 * @return array
310 */
311 function &get_mimes()
312 {
313 static $_mimes = array();
314
Andrey Andreev06879112013-01-29 15:05:02 +0200315 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300316 {
317 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
318 }
Andrey Andreev06879112013-01-29 15:05:02 +0200319 elseif (file_exists(APPPATH.'config/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300320 {
321 $_mimes = include(APPPATH.'config/mimes.php');
322 }
323
324 return $_mimes;
325 }
326}
327
328// ------------------------------------------------------------------------
329
Andrey Andreev3fb02672012-10-22 16:48:01 +0300330if ( ! function_exists('is_https'))
331{
332 /**
333 * Is HTTPS?
334 *
335 * Determines if the application is accessed via an encrypted
336 * (HTTPS) connection.
337 *
338 * @return bool
339 */
340 function is_https()
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200341 {
Andrey Andreev333b80e2013-07-01 16:21:54 +0300342 if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200343 {
344 return TRUE;
345 }
346 elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
347 {
348 return TRUE;
349 }
Andrey Andreev333b80e2013-07-01 16:21:54 +0300350 elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200351 {
352 return TRUE;
353 }
Richard Deurwaarder (Xeli)98999972013-06-24 15:19:30 +0200354
Richard Deurwaarder (Xeli)7cc29452013-06-24 15:06:19 +0200355 return FALSE;
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200356 }
Andrey Andreev3fb02672012-10-22 16:48:01 +0300357}
358
359// ------------------------------------------------------------------------
360
Andrey Andreevf964b162013-11-12 17:04:55 +0200361if ( ! function_exists('is_cli'))
362{
363
364 /**
365 * Is CLI?
366 *
367 * Test to see if a request was made from the command line.
368 *
369 * @return bool
370 */
371 function is_cli()
372 {
373 return (PHP_SAPI === 'cli' OR defined('STDIN'));
374 }
375}
376
377// ------------------------------------------------------------------------
378
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400379if ( ! function_exists('show_error'))
380{
Timothy Warrenad475052012-04-19 13:21:06 -0400381 /**
382 * Error Handler
383 *
384 * This function lets us invoke the exception class and
385 * display errors using the standard error template located
vlakoff52301c72013-03-29 14:23:34 +0100386 * in application/views/errors/error_general.php
Timothy Warrenad475052012-04-19 13:21:06 -0400387 * This function will send the error page directly to the
388 * browser and exit.
389 *
390 * @param string
391 * @param int
392 * @param string
393 * @return void
394 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600395 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
396 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700397 $status_code = abs($status_code);
398 if ($status_code < 100)
399 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200400 $exit_status = $status_code + 9; // 9 is EXIT__AUTO_MIN
401 if ($exit_status > 125) // 125 is EXIT__AUTO_MAX
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700402 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200403 $exit_status = 1; // EXIT_ERROR
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700404 }
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200405
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700406 $status_code = 500;
407 }
408 else
409 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200410 $exit_status = 1; // EXIT_ERROR
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700411 }
Andrey Andreev5a6814e2013-03-04 15:44:12 +0200412
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600413 $_error =& load_class('Exceptions', 'core');
414 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700415 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600416 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400417}
Derek Allard2067d1a2008-11-13 22:59:24 +0000418
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600419// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000420
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400421if ( ! function_exists('show_404'))
422{
Timothy Warrenad475052012-04-19 13:21:06 -0400423 /**
424 * 404 Page Handler
425 *
426 * This function is similar to the show_error() function above
427 * However, instead of the standard error template it displays
428 * 404 errors.
429 *
430 * @param string
431 * @param bool
432 * @return void
433 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400434 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600435 {
436 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400437 $_error->show_404($page, $log_error);
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200438 exit(4); // EXIT_UNKNOWN_FILE
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600439 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400440}
Derek Allard2067d1a2008-11-13 22:59:24 +0000441
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600442// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000443
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400444if ( ! function_exists('log_message'))
445{
Timothy Warrenad475052012-04-19 13:21:06 -0400446 /**
447 * Error Logging Interface
448 *
449 * We use this as a simple mechanism to access the logging
450 * class and send messages to be logged.
451 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200452 * @param string the error level: 'error', 'debug' or 'info'
453 * @param string the error message
Timothy Warrenad475052012-04-19 13:21:06 -0400454 * @return void
455 */
Andrey Andreev838c9a92013-09-13 14:05:13 +0300456 function log_message($level, $message)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev2f8d2d32013-08-07 15:54:47 +0300458 static $_log;
Barry Mienydd671972010-10-04 16:33:58 +0200459
Andrey Andreev49890a92013-08-19 19:56:18 +0300460 if ($_log === NULL)
Ted Woodb19a2032013-01-05 16:02:43 -0800461 {
vlakoff61f1aa02013-08-07 11:29:17 +0200462 // references cannot be directly assigned to static variables, so we use an array
463 $_log[0] =& load_class('Log', 'core');
Ted Woodb19a2032013-01-05 16:02:43 -0800464 }
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200465
Andrey Andreev838c9a92013-09-13 14:05:13 +0300466 $_log[0]->write_log($level, $message);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400468}
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600470// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000471
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400472if ( ! function_exists('set_status_header'))
473{
Timothy Warrenad475052012-04-19 13:21:06 -0400474 /**
475 * Set HTTP Status Header
476 *
477 * @param int the status code
478 * @param string
479 * @return void
480 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600481 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000482 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600483 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400484 200 => 'OK',
485 201 => 'Created',
486 202 => 'Accepted',
487 203 => 'Non-Authoritative Information',
488 204 => 'No Content',
489 205 => 'Reset Content',
490 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000491
Timothy Warrenad475052012-04-19 13:21:06 -0400492 300 => 'Multiple Choices',
493 301 => 'Moved Permanently',
494 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300495 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400496 304 => 'Not Modified',
497 305 => 'Use Proxy',
498 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600499
Timothy Warrenad475052012-04-19 13:21:06 -0400500 400 => 'Bad Request',
501 401 => 'Unauthorized',
502 403 => 'Forbidden',
503 404 => 'Not Found',
504 405 => 'Method Not Allowed',
505 406 => 'Not Acceptable',
506 407 => 'Proxy Authentication Required',
507 408 => 'Request Timeout',
508 409 => 'Conflict',
509 410 => 'Gone',
510 411 => 'Length Required',
511 412 => 'Precondition Failed',
512 413 => 'Request Entity Too Large',
513 414 => 'Request-URI Too Long',
514 415 => 'Unsupported Media Type',
515 416 => 'Requested Range Not Satisfiable',
516 417 => 'Expectation Failed',
517 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600518
Timothy Warrenad475052012-04-19 13:21:06 -0400519 500 => 'Internal Server Error',
520 501 => 'Not Implemented',
521 502 => 'Bad Gateway',
522 503 => 'Service Unavailable',
523 504 => 'Gateway Timeout',
524 505 => 'HTTP Version Not Supported'
525 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600526
Andrey Andreev51d6d842012-06-15 16:41:09 +0300527 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600528 {
529 show_error('Status codes must be numeric', 500);
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Andrey Andreev51d6d842012-06-15 16:41:09 +0300532 is_int($code) OR $code = (int) $code;
533
534 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600535 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300536 if (isset($stati[$code]))
537 {
538 $text = $stati[$code];
539 }
540 else
541 {
542 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
543 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Andrey Andreevb7b43962012-02-27 22:45:48 +0200546 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600547
vlakoff40d12492013-08-06 14:46:00 +0200548 if (strpos(PHP_SAPI, 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600549 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700550 header('Status: '.$code.' '.$text, TRUE);
551 }
552 else
553 {
554 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600555 }
Derek Jones817163a2009-07-11 17:05:58 +0000556 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400557}
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600559// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000560
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400561if ( ! function_exists('_exception_handler'))
562{
Timothy Warrenad475052012-04-19 13:21:06 -0400563 /**
564 * Exception Handler
565 *
vlakoffc941d852013-08-06 14:44:40 +0200566 * This is the custom exception handler that is declared at the top
567 * of CodeIgniter.php. The main reason we use this is to permit
Timothy Warrenad475052012-04-19 13:21:06 -0400568 * PHP errors to be logged in our own log files since the user may
569 * not have access to server logs. Since this function
570 * effectively intercepts PHP errors, however, we also need
571 * to display errors based on the current error_reporting level.
572 * We do that with the use of a PHP error template.
573 *
Andrey Andreev0850a282013-10-21 14:26:18 +0300574 * @param int $severity
575 * @param string $message
576 * @param string $filepath
577 * @param int $line
Timothy Warrenad475052012-04-19 13:21:06 -0400578 * @return void
579 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600580 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200581 {
Andrey Andreev0850a282013-10-21 14:26:18 +0300582 $is_error = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) === $severity);
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200583
584 // When an error occurred, set the status header to '500 Internal Server Error'
585 // to indicate to the client something went wrong.
586 // This can't be done within the $_error->show_php_error method because
587 // it is only called when the display_errors flag is set (which isn't usually
588 // the case in a production environment) or when errors are ignored because
589 // they are above the error_reporting threshold.
590 if ($is_error)
591 {
592 set_status_header(500);
Andrey Andreev02545892014-02-19 23:49:31 +0200593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300595 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600596 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300597 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600598 {
599 return;
600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Cristian Kocza2be27442014-02-19 21:52:38 +0200602 $_error =& load_class('Exceptions', 'core');
Andrey Andreev76160bc2014-01-30 22:26:14 +0200603 $_error->log_exception($severity, $message, $filepath, $line);
604
Francesco Negri312bdc52012-08-04 07:32:19 -0400605 // Should we display the error?
Andrey Andreevf6274742014-02-20 18:05:58 +0200606 if (ini_get('display_errors'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
608 $_error->show_php_error($severity, $message, $filepath, $line);
609 }
610
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200611 // If the error is fatal, the execution of the script should be stopped because
612 // errors can't be recovered from. Halting the script conforms with PHP's
613 // default error handling. See http://www.php.net/manual/en/errorfunc.constants.php
614 if ($is_error)
615 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200616 exit(1); // EXIT_ERROR
Jesse van Assen7eb116a2013-07-06 10:42:14 +0200617 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600618 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400619}
Derek Allard2067d1a2008-11-13 22:59:24 +0000620
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800621// ------------------------------------------------------------------------
622
623if ( ! function_exists('_shutdown_handler'))
624{
625 /**
626 * Shutdown Handler
627 *
628 * This is the shutdown handler that is declared at the top
629 * of CodeIgniter.php. The main reason we use this is to simulate
630 * a complete custom exception handler.
631 *
vkeranovd6f3d312013-09-14 21:39:49 +0300632 * E_STRICT is purposivly neglected because such events may have
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800633 * been caught. Duplication or none? None is preferred for now.
634 *
635 * @link http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a
636 * @return void
637 */
638 function _shutdown_handler()
639 {
Andrey Andreevafca3522013-11-14 15:26:59 +0200640 $last_error = error_get_last();
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800641 if (isset($last_error) &&
642 ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING)))
643 {
Kaiwang Chen21fe9da2013-09-11 13:09:41 +0800644 _exception_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']);
645 }
646 }
647}
648
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400649// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200650
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400651if ( ! function_exists('remove_invisible_characters'))
652{
Timothy Warrenad475052012-04-19 13:21:06 -0400653 /**
654 * Remove Invisible Characters
655 *
656 * This prevents sandwiching null characters
657 * between ascii characters, like Java\0script.
658 *
659 * @param string
660 * @param bool
661 * @return string
662 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400663 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500664 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400665 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200666
667 // every control character except newline (dec 10),
668 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400669 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500670 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400671 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
672 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500673 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200674
Pascal Kriete0ff50262011-04-05 14:52:03 -0400675 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500676
677 do
678 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400679 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500680 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400681 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500682
683 return $str;
684 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400685}
Derek Allard2067d1a2008-11-13 22:59:24 +0000686
kenjisfbac8b42011-08-25 10:51:44 +0900687// ------------------------------------------------------------------------
688
kenjisfbac8b42011-08-25 10:51:44 +0900689if ( ! function_exists('html_escape'))
690{
Timothy Warrenad475052012-04-19 13:21:06 -0400691 /**
692 * Returns HTML escaped variable
693 *
694 * @param mixed
695 * @return mixed
696 */
kenjisfbac8b42011-08-25 10:51:44 +0900697 function html_escape($var)
698 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200699 return is_array($var)
700 ? array_map('html_escape', $var)
701 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600702 }
Greg Akerd96f8822011-12-27 16:23:47 -0600703}
Greg Aker5c1aa632011-12-25 01:24:29 -0600704
Chad Furmana1abada2012-07-29 01:03:50 -0400705// ------------------------------------------------------------------------
706
707if ( ! function_exists('_stringify_attributes'))
708{
709 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300710 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400711 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300712 * Helper function used to convert a string, array, or object
713 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400714 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300715 * @param mixed string, array, object
716 * @param bool
717 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400718 */
719 function _stringify_attributes($attributes, $js = FALSE)
720 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300721 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400722
723 if (empty($attributes))
724 {
725 return $atts;
726 }
727
728 if (is_string($attributes))
729 {
730 return ' '.$attributes;
731 }
732
733 $attributes = (array) $attributes;
734
735 foreach ($attributes as $key => $val)
736 {
737 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
738 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300739
Chad Furmana1abada2012-07-29 01:03:50 -0400740 return rtrim($atts, ',');
741 }
742}
743
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200744// ------------------------------------------------------------------------
745
746if ( ! function_exists('function_usable'))
747{
748 /**
749 * Function usable
750 *
751 * Executes a function_exists() check, and if the Suhosin PHP
752 * extension is loaded - checks whether the function that is
753 * checked might be disabled in there as well.
754 *
755 * This is useful as function_exists() will return FALSE for
756 * functions disabled via the *disable_functions* php.ini
757 * setting, but not for *suhosin.executor.func.blacklist* and
758 * *suhosin.executor.disable_eval*. These settings will just
759 * terminate script execution if a disabled function is executed.
760 *
Andrey Andreevaaa8ddb2014-02-03 14:10:44 +0200761 * The above described behavior turned out to be a bug in Suhosin,
762 * but even though a fix was commited for 0.9.34 on 2012-02-12,
763 * that version is yet to be released. This function will therefore
764 * be just temporary, but would probably be kept for a few years.
765 *
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200766 * @link http://www.hardened-php.net/suhosin/
767 * @param string $function_name Function to check for
768 * @return bool TRUE if the function exists and is safe to call,
769 * FALSE otherwise.
770 */
771 function function_usable($function_name)
772 {
773 static $_suhosin_func_blacklist;
774
775 if (function_exists($function_name))
776 {
777 if ( ! isset($_suhosin_func_blacklist))
778 {
Andrey Andreevae634622012-12-17 10:30:18 +0200779 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200780 {
Andrey Andreevf6274742014-02-20 18:05:58 +0200781 $_suhosin_func_blacklist = explode(',', trim(ini_get('suhosin.executor.func.blacklist')));
Andrey Andreevae634622012-12-17 10:30:18 +0200782
Andrey Andreevf6274742014-02-20 18:05:58 +0200783 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && ini_get('suhosin.executor.disable_eval'))
Andrey Andreevae634622012-12-17 10:30:18 +0200784 {
785 $_suhosin_func_blacklist[] = 'eval';
786 }
787 }
788 else
789 {
790 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200791 }
792 }
793
Andrey Andreevae634622012-12-17 10:30:18 +0200794 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200795 }
796
797 return FALSE;
798 }
799}
Richard Deurwaarder (Xeli)668d0092013-06-24 13:50:52 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801/* End of file Common.php */
Andrey Andreev13c818e2013-09-14 21:44:36 +0300802/* Location: ./system/core/Common.php */