blob: d6387209bb0a1c8927f6e5b0d7b0381b1558cd80 [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
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600153 if (class_exists($name) === FALSE)
154 {
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
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600167 if (class_exists($name) === FALSE)
168 {
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';
180 exit(2);
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 Andreev92aa67c2012-06-09 23:37:17 +0300246 if (defined('ENVIRONMENT') && 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.';
254 exit(1);
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.';
262 exit(1);
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
323 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
324 {
325 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
326 }
327 elseif (is_file(APPPATH.'config/mimes.php'))
328 {
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 {
350 return ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off');
351 }
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 {
377 $exit_status = $status_code + 28;
378 $status_code = 500;
379 }
380 else
381 {
382 $exit_status = 27;
383 }
384
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600385 $_error =& load_class('Exceptions', 'core');
386 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700387 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600388 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400389}
Derek Allard2067d1a2008-11-13 22:59:24 +0000390
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600391// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000392
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400393if ( ! function_exists('show_404'))
394{
Timothy Warrenad475052012-04-19 13:21:06 -0400395 /**
396 * 404 Page Handler
397 *
398 * This function is similar to the show_error() function above
399 * However, instead of the standard error template it displays
400 * 404 errors.
401 *
402 * @param string
403 * @param bool
404 * @return void
405 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400406 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600407 {
408 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400409 $_error->show_404($page, $log_error);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700410 exit(4);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600411 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400412}
Derek Allard2067d1a2008-11-13 22:59:24 +0000413
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600414// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400416if ( ! function_exists('log_message'))
417{
Timothy Warrenad475052012-04-19 13:21:06 -0400418 /**
419 * Error Logging Interface
420 *
421 * We use this as a simple mechanism to access the logging
422 * class and send messages to be logged.
423 *
424 * @param string
425 * @param string
426 * @param bool
427 * @return void
428 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600429 function log_message($level = 'error', $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Ted Woodb19a2032013-01-05 16:02:43 -0800431 static $_log, $_log_threshold;
432
433 if ($_log_threshold === NULL)
434 {
435 $_log_threshold = config_item('log_threshold');
436 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600437
Ted Woodb19a2032013-01-05 16:02:43 -0800438 if ($_log_threshold === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600439 {
440 return;
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Ted Woodb19a2032013-01-05 16:02:43 -0800443 if ($_log === NULL)
444 {
445 $_log =& load_class('Log', 'core');
446 }
447
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600448 $_log->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
Andrey Andreev188abaf2012-01-07 19:09:42 +0200530 if (strpos(php_sapi_name(), 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600531 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700532 if (!headers_sent()) header('Status: '.$code.' '.$text, TRUE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600533 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600534 else
535 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700536 if (!headers_sent()) 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 *
548 * This is the custom exception handler that is declaired at the top
549 * of Codeigniter.php. The main reason we use this is to permit
550 * 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}
729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730/* End of file Common.php */
Andrey Andreevbdb99992012-07-30 17:38:05 +0300731/* Location: ./system/core/Common.php */