blob: 9baf5e315914a01bb701981db0b1162317908aa2 [file] [log] [blame]
Daniel Hunsaker353f9832013-01-24 17:09:10 -07001
Andrey Andreevc5536aa2012-11-01 17:33:58 +02002<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00003/**
4 * CodeIgniter
5 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00006 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00007 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05008 * NOTICE OF LICENSE
Andrey Andreev188abaf2012-01-07 19:09:42 +02009 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050010 * Licensed under the Open Software License version 3.0
Andrey Andreev188abaf2012-01-07 19:09:42 +020011 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050012 * This source file is subject to the Open Software License (OSL 3.0) that is
13 * bundled with this package in the files license.txt / license.rst. It is
14 * also available through the world wide web at this URL:
15 * http://opensource.org/licenses/OSL-3.0
16 * If you did not receive a copy of the license and are unable to obtain it
17 * through the world wide web, please send an email to
18 * licensing@ellislab.com so we can send you a copy immediately.
19 *
Derek Allard2067d1a2008-11-13 22:59:24 +000020 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050021 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020022 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050023 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000024 * @link http://codeigniter.com
25 * @since Version 1.0
26 * @filesource
27 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020028defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000029
Derek Allard2067d1a2008-11-13 22:59:24 +000030/**
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060031 * Common Functions
32 *
33 * Loads the base classes and executes the request.
34 *
35 * @package CodeIgniter
Andrey Andreev92ebfb62012-05-17 12:49:24 +030036 * @subpackage CodeIgniter
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060037 * @category Common Functions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060039 * @link http://codeigniter.com/user_guide/
40 */
41
42// ------------------------------------------------------------------------
43
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040044if ( ! function_exists('is_php'))
45{
Timothy Warrenad475052012-04-19 13:21:06 -040046 /**
47 * Determines if the current version of PHP is greater then the supplied value
48 *
Andrey Andreev24bd2302012-06-05 22:29:12 +030049 * Since there are a few places where we conditionally test for PHP > 5.3
Timothy Warrenad475052012-04-19 13:21:06 -040050 * we'll set a static variable.
51 *
52 * @param string
53 * @return bool TRUE if the current version is $version or higher
54 */
Andrey Andreev24bd2302012-06-05 22:29:12 +030055 function is_php($version = '5.3.0')
Derek Jones086ee5a2009-07-28 14:42:12 +000056 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060057 static $_is_php;
Andrey Andreevb7b43962012-02-27 22:45:48 +020058 $version = (string) $version;
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060060 if ( ! isset($_is_php[$version]))
61 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030062 $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060063 }
Derek Jones086ee5a2009-07-28 14:42:12 +000064
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060065 return $_is_php[$version];
66 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040067}
Derek Jones5bcfd2e2009-07-28 14:42:42 +000068
Derek Jones086ee5a2009-07-28 14:42:12 +000069// ------------------------------------------------------------------------
70
Dan Horrigan3ef65bd2011-05-08 11:06:44 -040071if ( ! function_exists('is_really_writable'))
72{
Timothy Warrenad475052012-04-19 13:21:06 -040073 /**
74 * Tests for file writability
75 *
76 * is_writable() returns TRUE on Windows servers when you really can't write to
77 * the file, based on the read-only attribute. is_writable() is also unreliable
78 * on Unix servers if safe_mode is on.
79 *
80 * @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 Andreevb7b43962012-02-27 22:45:48 +020086 if (DIRECTORY_SEPARATOR === '/' && (bool) @ini_get('safe_mode') === FALSE)
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 {
Andrey Andreev536b7712012-01-07 21:31:25 +020096 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
Derek Jonesdc8e9ea2010-03-02 13:17:19 -060097 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
98 {
99 return FALSE;
100 }
101
102 fclose($fp);
103 @chmod($file, DIR_WRITE_MODE);
104 @unlink($file);
105 return TRUE;
106 }
Eric Barnes15083012011-03-21 22:13:12 -0400107 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === 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 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600133 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
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 {
151 $name = $prefix.$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 Andreeva52c7752012-10-11 10:54:02 +0300169 require_once(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.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';
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700180 exit(EXIT_UNKNOWN_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
Pascal Kriete58560022010-11-10 16:01:20 -0500186 $_classes[$class] = new $name();
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600187 return $_classes[$class];
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400189}
Derek Allard2067d1a2008-11-13 22:59:24 +0000190
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600191// --------------------------------------------------------------------
192
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400193if ( ! function_exists('is_loaded'))
194{
Timothy Warrenad475052012-04-19 13:21:06 -0400195 /**
196 * Keeps track of which libraries have been loaded. This function is
197 * called by the load_class() function above
198 *
199 * @param string
200 * @return array
201 */
Andrey Andreevd47baab2012-01-09 16:56:46 +0200202 function &is_loaded($class = '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600203 {
204 static $_is_loaded = array();
205
Alex Bilbieed944a32012-06-02 11:07:47 +0100206 if ($class !== '')
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600207 {
208 $_is_loaded[strtolower($class)] = $class;
209 }
210
211 return $_is_loaded;
212 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400213}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600214
215// ------------------------------------------------------------------------
Derek Jonesf0a9b332009-07-29 14:19:18 +0000216
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400217if ( ! function_exists('get_config'))
218{
Timothy Warrenad475052012-04-19 13:21:06 -0400219 /**
220 * Loads the main config.php file
221 *
222 * This function lets us grab the config file even if the Config class
223 * hasn't been instantiated yet
224 *
225 * @param array
226 * @return array
227 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600228 function &get_config($replace = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600230 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600232 if (isset($_config))
233 {
234 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200235 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600236
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300237 $file_path = APPPATH.'config/config.php';
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300238 $found = FALSE;
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300239 if (file_exists($file_path))
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300240 {
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300241 $found = TRUE;
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300242 require($file_path);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100243 }
joelcox2035fd82011-01-16 16:50:36 +0100244
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300245 // Is the config file in the environment folder?
Andrey Andreevdb529ca2013-01-28 11:00:02 +0200246 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300248 require($file_path);
249 }
250 elseif ( ! $found)
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300251 {
252 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700253 echo 'The configuration file does not exist.';
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700254 exit(EXIT_CONFIG);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600255 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100256
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600257 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 if ( ! isset($config) OR ! is_array($config))
259 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500260 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700261 echo 'Your config file does not appear to be formatted correctly.';
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700262 exit(EXIT_CONFIG);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600265 // Are any values being dynamically replaced?
266 if (count($replace) > 0)
267 {
268 foreach ($replace as $key => $val)
269 {
270 if (isset($config[$key]))
271 {
272 $config[$key] = $val;
273 }
274 }
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600277 return $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400279}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600280
281// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000282
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400283if ( ! function_exists('config_item'))
284{
Timothy Warrenad475052012-04-19 13:21:06 -0400285 /**
286 * Returns the specified config item
287 *
288 * @param string
289 * @return mixed
290 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600291 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600293 static $_config_item = array();
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600295 if ( ! isset($_config_item[$item]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600297 $config =& get_config();
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600299 if ( ! isset($config[$item]))
300 {
301 return FALSE;
302 }
303 $_config_item[$item] = $config[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600306 return $_config_item[$item];
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400308}
Derek Allard2067d1a2008-11-13 22:59:24 +0000309
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600310// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300312if ( ! function_exists('get_mimes'))
313{
314 /**
315 * Returns the MIME types array from config/mimes.php
316 *
317 * @return array
318 */
319 function &get_mimes()
320 {
321 static $_mimes = array();
322
Andrey Andreev06879112013-01-29 15:05:02 +0200323 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300324 {
325 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
326 }
Andrey Andreev06879112013-01-29 15:05:02 +0200327 elseif (file_exists(APPPATH.'config/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300328 {
329 $_mimes = include(APPPATH.'config/mimes.php');
330 }
331
332 return $_mimes;
333 }
334}
335
336// ------------------------------------------------------------------------
337
Andrey Andreev3fb02672012-10-22 16:48:01 +0300338if ( ! function_exists('is_https'))
339{
340 /**
341 * Is HTTPS?
342 *
343 * Determines if the application is accessed via an encrypted
344 * (HTTPS) connection.
345 *
346 * @return bool
347 */
348 function is_https()
349 {
maltzurra72192322013-02-14 11:12:37 +0100350 return (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on');
Andrey Andreev3fb02672012-10-22 16:48:01 +0300351 }
352}
353
354// ------------------------------------------------------------------------
355
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400356if ( ! function_exists('show_error'))
357{
Timothy Warrenad475052012-04-19 13:21:06 -0400358 /**
359 * Error Handler
360 *
361 * This function lets us invoke the exception class and
362 * display errors using the standard error template located
363 * in application/errors/errors.php
364 * This function will send the error page directly to the
365 * browser and exit.
366 *
367 * @param string
368 * @param int
369 * @param string
370 * @return void
371 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600372 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
373 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700374 $status_code = abs($status_code);
375 if ($status_code < 100)
376 {
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700377 $exit_status = $status_code + EXIT__AUTO_MIN;
378 if ($exit_status > EXIT__AUTO_MAX)
379 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700380 $exit_status = EXIT_ERROR;
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700381 }
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700382 $status_code = 500;
383 }
384 else
385 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700386 $exit_status = EXIT_ERROR;
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700387 }
388
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600389 $_error =& load_class('Exceptions', 'core');
390 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700391 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600392 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400393}
Derek Allard2067d1a2008-11-13 22:59:24 +0000394
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600395// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000396
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400397if ( ! function_exists('show_404'))
398{
Timothy Warrenad475052012-04-19 13:21:06 -0400399 /**
400 * 404 Page Handler
401 *
402 * This function is similar to the show_error() function above
403 * However, instead of the standard error template it displays
404 * 404 errors.
405 *
406 * @param string
407 * @param bool
408 * @return void
409 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400410 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600411 {
412 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400413 $_error->show_404($page, $log_error);
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700414 exit(EXIT_UNKNOWN_FILE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600415 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400416}
Derek Allard2067d1a2008-11-13 22:59:24 +0000417
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600418// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000419
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400420if ( ! function_exists('log_message'))
421{
Timothy Warrenad475052012-04-19 13:21:06 -0400422 /**
423 * Error Logging Interface
424 *
425 * We use this as a simple mechanism to access the logging
426 * class and send messages to be logged.
427 *
428 * @param string
429 * @param string
430 * @param bool
431 * @return void
432 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600433 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Ted Woodb19a2032013-01-05 16:02:43 -0800435 static $_log, $_log_threshold;
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200436
Ted Woodb19a2032013-01-05 16:02:43 -0800437 if ($_log_threshold === NULL)
438 {
439 $_log_threshold = config_item('log_threshold');
440 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600441
Ted Woodb19a2032013-01-05 16:02:43 -0800442 if ($_log_threshold === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600443 {
444 return;
445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Ted Woodb19a2032013-01-05 16:02:43 -0800447 if ($_log === NULL)
448 {
449 $_log =& load_class('Log', 'core');
450 }
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200451
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600452 $_log->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400454}
Derek Allard2067d1a2008-11-13 22:59:24 +0000455
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600456// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000457
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400458if ( ! function_exists('set_status_header'))
459{
Timothy Warrenad475052012-04-19 13:21:06 -0400460 /**
461 * Set HTTP Status Header
462 *
463 * @param int the status code
464 * @param string
465 * @return void
466 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600467 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000468 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600469 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400470 200 => 'OK',
471 201 => 'Created',
472 202 => 'Accepted',
473 203 => 'Non-Authoritative Information',
474 204 => 'No Content',
475 205 => 'Reset Content',
476 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000477
Timothy Warrenad475052012-04-19 13:21:06 -0400478 300 => 'Multiple Choices',
479 301 => 'Moved Permanently',
480 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300481 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400482 304 => 'Not Modified',
483 305 => 'Use Proxy',
484 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600485
Timothy Warrenad475052012-04-19 13:21:06 -0400486 400 => 'Bad Request',
487 401 => 'Unauthorized',
488 403 => 'Forbidden',
489 404 => 'Not Found',
490 405 => 'Method Not Allowed',
491 406 => 'Not Acceptable',
492 407 => 'Proxy Authentication Required',
493 408 => 'Request Timeout',
494 409 => 'Conflict',
495 410 => 'Gone',
496 411 => 'Length Required',
497 412 => 'Precondition Failed',
498 413 => 'Request Entity Too Large',
499 414 => 'Request-URI Too Long',
500 415 => 'Unsupported Media Type',
501 416 => 'Requested Range Not Satisfiable',
502 417 => 'Expectation Failed',
503 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600504
Timothy Warrenad475052012-04-19 13:21:06 -0400505 500 => 'Internal Server Error',
506 501 => 'Not Implemented',
507 502 => 'Bad Gateway',
508 503 => 'Service Unavailable',
509 504 => 'Gateway Timeout',
510 505 => 'HTTP Version Not Supported'
511 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600512
Andrey Andreev51d6d842012-06-15 16:41:09 +0300513 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600514 {
515 show_error('Status codes must be numeric', 500);
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Andrey Andreev51d6d842012-06-15 16:41:09 +0300518 is_int($code) OR $code = (int) $code;
519
520 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600521 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300522 if (isset($stati[$code]))
523 {
524 $text = $stati[$code];
525 }
526 else
527 {
528 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
529 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Andrey Andreevb7b43962012-02-27 22:45:48 +0200532 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600533
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700534 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600535 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700536 header('Status: '.$code.' '.$text, TRUE);
537 }
538 else
539 {
540 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600541 }
Derek Jones817163a2009-07-11 17:05:58 +0000542 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400543}
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600545// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000546
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400547if ( ! function_exists('_exception_handler'))
548{
Timothy Warrenad475052012-04-19 13:21:06 -0400549 /**
550 * Exception Handler
551 *
552 * This is the custom exception handler that is declaired at the top
553 * of Codeigniter.php. The main reason we use this is to permit
554 * PHP errors to be logged in our own log files since the user may
555 * not have access to server logs. Since this function
556 * effectively intercepts PHP errors, however, we also need
557 * to display errors based on the current error_reporting level.
558 * We do that with the use of a PHP error template.
559 *
560 * @param int
561 * @param string
562 * @param string
563 * @param int
564 * @return void
565 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600566 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200567 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600568 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200569
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300570 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600571 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300572 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600573 {
574 return;
575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Francesco Negri312bdc52012-08-04 07:32:19 -0400577 // Should we display the error?
578 if ((bool) ini_get('display_errors') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 $_error->show_php_error($severity, $message, $filepath, $line);
581 }
582
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600583 $_error->log_exception($severity, $message, $filepath, $line);
584 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400585}
Derek Allard2067d1a2008-11-13 22:59:24 +0000586
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400587// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200588
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400589if ( ! function_exists('remove_invisible_characters'))
590{
Timothy Warrenad475052012-04-19 13:21:06 -0400591 /**
592 * Remove Invisible Characters
593 *
594 * This prevents sandwiching null characters
595 * between ascii characters, like Java\0script.
596 *
597 * @param string
598 * @param bool
599 * @return string
600 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400601 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500602 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400603 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200604
605 // every control character except newline (dec 10),
606 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400607 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500608 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400609 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
610 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500611 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200612
Pascal Kriete0ff50262011-04-05 14:52:03 -0400613 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500614
615 do
616 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400617 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500618 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400619 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500620
621 return $str;
622 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400623}
Derek Allard2067d1a2008-11-13 22:59:24 +0000624
kenjisfbac8b42011-08-25 10:51:44 +0900625// ------------------------------------------------------------------------
626
kenjisfbac8b42011-08-25 10:51:44 +0900627if ( ! function_exists('html_escape'))
628{
Timothy Warrenad475052012-04-19 13:21:06 -0400629 /**
630 * Returns HTML escaped variable
631 *
632 * @param mixed
633 * @return mixed
634 */
kenjisfbac8b42011-08-25 10:51:44 +0900635 function html_escape($var)
636 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200637 return is_array($var)
638 ? array_map('html_escape', $var)
639 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600640 }
Greg Akerd96f8822011-12-27 16:23:47 -0600641}
Greg Aker5c1aa632011-12-25 01:24:29 -0600642
Chad Furmana1abada2012-07-29 01:03:50 -0400643// ------------------------------------------------------------------------
644
645if ( ! function_exists('_stringify_attributes'))
646{
647 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300648 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400649 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300650 * Helper function used to convert a string, array, or object
651 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400652 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300653 * @param mixed string, array, object
654 * @param bool
655 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400656 */
657 function _stringify_attributes($attributes, $js = FALSE)
658 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300659 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400660
661 if (empty($attributes))
662 {
663 return $atts;
664 }
665
666 if (is_string($attributes))
667 {
668 return ' '.$attributes;
669 }
670
671 $attributes = (array) $attributes;
672
673 foreach ($attributes as $key => $val)
674 {
675 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
676 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300677
Chad Furmana1abada2012-07-29 01:03:50 -0400678 return rtrim($atts, ',');
679 }
680}
681
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200682// ------------------------------------------------------------------------
683
684if ( ! function_exists('function_usable'))
685{
686 /**
687 * Function usable
688 *
689 * Executes a function_exists() check, and if the Suhosin PHP
690 * extension is loaded - checks whether the function that is
691 * checked might be disabled in there as well.
692 *
693 * This is useful as function_exists() will return FALSE for
694 * functions disabled via the *disable_functions* php.ini
695 * setting, but not for *suhosin.executor.func.blacklist* and
696 * *suhosin.executor.disable_eval*. These settings will just
697 * terminate script execution if a disabled function is executed.
698 *
699 * @link http://www.hardened-php.net/suhosin/
700 * @param string $function_name Function to check for
701 * @return bool TRUE if the function exists and is safe to call,
702 * FALSE otherwise.
703 */
704 function function_usable($function_name)
705 {
706 static $_suhosin_func_blacklist;
707
708 if (function_exists($function_name))
709 {
710 if ( ! isset($_suhosin_func_blacklist))
711 {
Andrey Andreevae634622012-12-17 10:30:18 +0200712 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200713 {
Andrey Andreevae634622012-12-17 10:30:18 +0200714 $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist')));
715
716 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval'))
717 {
718 $_suhosin_func_blacklist[] = 'eval';
719 }
720 }
721 else
722 {
723 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200724 }
725 }
726
Andrey Andreevae634622012-12-17 10:30:18 +0200727 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200728 }
729
730 return FALSE;
731 }
732}
733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734/* End of file Common.php */
Andrey Andreevbdb99992012-07-30 17:38:05 +0300735/* Location: ./system/core/Common.php */