blob: 6b3d73100f7f37661f7e56b08c3c9bf4981c40fc [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 Andreevb7b43962012-02-27 22:45:48 +020085 if (DIRECTORY_SEPARATOR === '/' && (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 */
vlakoff2f7810a2013-08-19 04:46:26 +0200227 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
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600231 if (isset($_config))
232 {
vlakoff8d70c0a2013-08-17 07:31:29 +0200233 $config =& $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200234 }
vlakoff8d70c0a2013-08-17 07:31:29 +0200235 else
236 {
237 $file_path = APPPATH.'config/config.php';
238 $found = FALSE;
239 if (file_exists($file_path))
240 {
241 $found = TRUE;
242 require($file_path);
243 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600244
vlakoff8d70c0a2013-08-17 07:31:29 +0200245 // Is the config file in the environment folder?
246 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
247 {
248 require($file_path);
249 }
250 elseif ( ! $found)
251 {
252 set_status_header(503);
253 echo 'The configuration file does not exist.';
254 exit(EXIT_CONFIG);
255 }
joelcox2035fd82011-01-16 16:50:36 +0100256
vlakoff8d70c0a2013-08-17 07:31:29 +0200257 // Does the $config array exist in the file?
258 if ( ! isset($config) OR ! is_array($config))
259 {
260 set_status_header(503);
261 echo 'Your config file does not appear to be formatted correctly.';
262 exit(EXIT_CONFIG);
263 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100264
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 {
vlakoff67e5ca62013-08-19 04:52:00 +0200271 $config[$key] = $val;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
vlakoff8d70c0a2013-08-17 07:31:29 +0200274 return $config;
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
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400361if ( ! function_exists('show_error'))
362{
Timothy Warrenad475052012-04-19 13:21:06 -0400363 /**
364 * Error Handler
365 *
366 * This function lets us invoke the exception class and
367 * display errors using the standard error template located
vlakoff52301c72013-03-29 14:23:34 +0100368 * in application/views/errors/error_general.php
Timothy Warrenad475052012-04-19 13:21:06 -0400369 * This function will send the error page directly to the
370 * browser and exit.
371 *
372 * @param string
373 * @param int
374 * @param string
375 * @return void
376 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600377 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
378 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700379 $status_code = abs($status_code);
380 if ($status_code < 100)
381 {
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700382 $exit_status = $status_code + EXIT__AUTO_MIN;
383 if ($exit_status > EXIT__AUTO_MAX)
384 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700385 $exit_status = EXIT_ERROR;
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700386 }
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700387 $status_code = 500;
388 }
389 else
390 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700391 $exit_status = EXIT_ERROR;
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700392 }
Andrey Andreev5a6814e2013-03-04 15:44:12 +0200393
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600394 $_error =& load_class('Exceptions', 'core');
395 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700396 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600397 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400398}
Derek Allard2067d1a2008-11-13 22:59:24 +0000399
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600400// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000401
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400402if ( ! function_exists('show_404'))
403{
Timothy Warrenad475052012-04-19 13:21:06 -0400404 /**
405 * 404 Page Handler
406 *
407 * This function is similar to the show_error() function above
408 * However, instead of the standard error template it displays
409 * 404 errors.
410 *
411 * @param string
412 * @param bool
413 * @return void
414 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400415 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600416 {
417 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400418 $_error->show_404($page, $log_error);
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700419 exit(EXIT_UNKNOWN_FILE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600420 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400421}
Derek Allard2067d1a2008-11-13 22:59:24 +0000422
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600423// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000424
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400425if ( ! function_exists('log_message'))
426{
Timothy Warrenad475052012-04-19 13:21:06 -0400427 /**
428 * Error Logging Interface
429 *
430 * We use this as a simple mechanism to access the logging
431 * class and send messages to be logged.
432 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200433 * @param string the error level: 'error', 'debug' or 'info'
434 * @param string the error message
435 * @param bool whether the error is a native PHP error
Timothy Warrenad475052012-04-19 13:21:06 -0400436 * @return void
437 */
vlakoffd0c30ab2013-05-07 07:49:23 +0200438 function log_message($level, $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Andrey Andreev2f8d2d32013-08-07 15:54:47 +0300440 static $_log;
Barry Mienydd671972010-10-04 16:33:58 +0200441
Ted Woodb19a2032013-01-05 16:02:43 -0800442 if ($_log === NULL)
443 {
vlakoff61f1aa02013-08-07 11:29:17 +0200444 // references cannot be directly assigned to static variables, so we use an array
445 $_log[0] =& load_class('Log', 'core');
Ted Woodb19a2032013-01-05 16:02:43 -0800446 }
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200447
vlakoff61f1aa02013-08-07 11:29:17 +0200448 $_log[0]->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400450}
Derek Allard2067d1a2008-11-13 22:59:24 +0000451
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600452// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000453
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400454if ( ! function_exists('set_status_header'))
455{
Timothy Warrenad475052012-04-19 13:21:06 -0400456 /**
457 * Set HTTP Status Header
458 *
459 * @param int the status code
460 * @param string
461 * @return void
462 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600463 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000464 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600465 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400466 200 => 'OK',
467 201 => 'Created',
468 202 => 'Accepted',
469 203 => 'Non-Authoritative Information',
470 204 => 'No Content',
471 205 => 'Reset Content',
472 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000473
Timothy Warrenad475052012-04-19 13:21:06 -0400474 300 => 'Multiple Choices',
475 301 => 'Moved Permanently',
476 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300477 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400478 304 => 'Not Modified',
479 305 => 'Use Proxy',
480 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600481
Timothy Warrenad475052012-04-19 13:21:06 -0400482 400 => 'Bad Request',
483 401 => 'Unauthorized',
484 403 => 'Forbidden',
485 404 => 'Not Found',
486 405 => 'Method Not Allowed',
487 406 => 'Not Acceptable',
488 407 => 'Proxy Authentication Required',
489 408 => 'Request Timeout',
490 409 => 'Conflict',
491 410 => 'Gone',
492 411 => 'Length Required',
493 412 => 'Precondition Failed',
494 413 => 'Request Entity Too Large',
495 414 => 'Request-URI Too Long',
496 415 => 'Unsupported Media Type',
497 416 => 'Requested Range Not Satisfiable',
498 417 => 'Expectation Failed',
499 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600500
Timothy Warrenad475052012-04-19 13:21:06 -0400501 500 => 'Internal Server Error',
502 501 => 'Not Implemented',
503 502 => 'Bad Gateway',
504 503 => 'Service Unavailable',
505 504 => 'Gateway Timeout',
506 505 => 'HTTP Version Not Supported'
507 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600508
Andrey Andreev51d6d842012-06-15 16:41:09 +0300509 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600510 {
511 show_error('Status codes must be numeric', 500);
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Andrey Andreev51d6d842012-06-15 16:41:09 +0300514 is_int($code) OR $code = (int) $code;
515
516 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600517 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300518 if (isset($stati[$code]))
519 {
520 $text = $stati[$code];
521 }
522 else
523 {
524 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
525 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Andrey Andreevb7b43962012-02-27 22:45:48 +0200528 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600529
vlakoff40d12492013-08-06 14:46:00 +0200530 if (strpos(PHP_SAPI, 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600531 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700532 header('Status: '.$code.' '.$text, TRUE);
533 }
534 else
535 {
536 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600537 }
Derek Jones817163a2009-07-11 17:05:58 +0000538 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400539}
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600541// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000542
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400543if ( ! function_exists('_exception_handler'))
544{
Timothy Warrenad475052012-04-19 13:21:06 -0400545 /**
546 * Exception Handler
547 *
vlakoffc941d852013-08-06 14:44:40 +0200548 * This is the custom exception handler that is declared at the top
549 * of CodeIgniter.php. The main reason we use this is to permit
Timothy Warrenad475052012-04-19 13:21:06 -0400550 * PHP errors to be logged in our own log files since the user may
551 * not have access to server logs. Since this function
552 * effectively intercepts PHP errors, however, we also need
553 * to display errors based on the current error_reporting level.
554 * We do that with the use of a PHP error template.
555 *
556 * @param int
557 * @param string
558 * @param string
559 * @param int
560 * @return void
561 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600562 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200563 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600564 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200565
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300566 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600567 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300568 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600569 {
570 return;
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Francesco Negri312bdc52012-08-04 07:32:19 -0400573 // Should we display the error?
574 if ((bool) ini_get('display_errors') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
576 $_error->show_php_error($severity, $message, $filepath, $line);
577 }
578
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600579 $_error->log_exception($severity, $message, $filepath, $line);
580 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400581}
Derek Allard2067d1a2008-11-13 22:59:24 +0000582
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400583// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200584
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400585if ( ! function_exists('remove_invisible_characters'))
586{
Timothy Warrenad475052012-04-19 13:21:06 -0400587 /**
588 * Remove Invisible Characters
589 *
590 * This prevents sandwiching null characters
591 * between ascii characters, like Java\0script.
592 *
593 * @param string
594 * @param bool
595 * @return string
596 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400597 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500598 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400599 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200600
601 // every control character except newline (dec 10),
602 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400603 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500604 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400605 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
606 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500607 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200608
Pascal Kriete0ff50262011-04-05 14:52:03 -0400609 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500610
611 do
612 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400613 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500614 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400615 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500616
617 return $str;
618 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400619}
Derek Allard2067d1a2008-11-13 22:59:24 +0000620
kenjisfbac8b42011-08-25 10:51:44 +0900621// ------------------------------------------------------------------------
622
kenjisfbac8b42011-08-25 10:51:44 +0900623if ( ! function_exists('html_escape'))
624{
Timothy Warrenad475052012-04-19 13:21:06 -0400625 /**
626 * Returns HTML escaped variable
627 *
628 * @param mixed
629 * @return mixed
630 */
kenjisfbac8b42011-08-25 10:51:44 +0900631 function html_escape($var)
632 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200633 return is_array($var)
634 ? array_map('html_escape', $var)
635 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600636 }
Greg Akerd96f8822011-12-27 16:23:47 -0600637}
Greg Aker5c1aa632011-12-25 01:24:29 -0600638
Chad Furmana1abada2012-07-29 01:03:50 -0400639// ------------------------------------------------------------------------
640
641if ( ! function_exists('_stringify_attributes'))
642{
643 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300644 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400645 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300646 * Helper function used to convert a string, array, or object
647 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400648 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300649 * @param mixed string, array, object
650 * @param bool
651 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400652 */
653 function _stringify_attributes($attributes, $js = FALSE)
654 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300655 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400656
657 if (empty($attributes))
658 {
659 return $atts;
660 }
661
662 if (is_string($attributes))
663 {
664 return ' '.$attributes;
665 }
666
667 $attributes = (array) $attributes;
668
669 foreach ($attributes as $key => $val)
670 {
671 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
672 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300673
Chad Furmana1abada2012-07-29 01:03:50 -0400674 return rtrim($atts, ',');
675 }
676}
677
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200678// ------------------------------------------------------------------------
679
680if ( ! function_exists('function_usable'))
681{
682 /**
683 * Function usable
684 *
685 * Executes a function_exists() check, and if the Suhosin PHP
686 * extension is loaded - checks whether the function that is
687 * checked might be disabled in there as well.
688 *
689 * This is useful as function_exists() will return FALSE for
690 * functions disabled via the *disable_functions* php.ini
691 * setting, but not for *suhosin.executor.func.blacklist* and
692 * *suhosin.executor.disable_eval*. These settings will just
693 * terminate script execution if a disabled function is executed.
694 *
695 * @link http://www.hardened-php.net/suhosin/
696 * @param string $function_name Function to check for
697 * @return bool TRUE if the function exists and is safe to call,
698 * FALSE otherwise.
699 */
700 function function_usable($function_name)
701 {
702 static $_suhosin_func_blacklist;
703
704 if (function_exists($function_name))
705 {
706 if ( ! isset($_suhosin_func_blacklist))
707 {
Andrey Andreevae634622012-12-17 10:30:18 +0200708 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200709 {
Andrey Andreevae634622012-12-17 10:30:18 +0200710 $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist')));
711
712 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval'))
713 {
714 $_suhosin_func_blacklist[] = 'eval';
715 }
716 }
717 else
718 {
719 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200720 }
721 }
722
Andrey Andreevae634622012-12-17 10:30:18 +0200723 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200724 }
725
726 return FALSE;
727 }
728}
Richard Deurwaarder (Xeli)668d0092013-06-24 13:50:52 +0200729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730/* End of file Common.php */
Richard Deurwaarder (Xeli)4055d572013-06-24 14:59:20 +0200731/* Location: ./system/core/Common.php */