blob: 98a3fcb358b4d153820672bc984777be5bd2b6bd [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 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600227 function &get_config($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 {
233 return $_config[0];
Barry Mienydd671972010-10-04 16:33:58 +0200234 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600235
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300236 $file_path = APPPATH.'config/config.php';
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300237 $found = FALSE;
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300238 if (file_exists($file_path))
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300239 {
Thanasis Polychronakis142eef92012-05-21 14:38:22 +0300240 $found = TRUE;
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300241 require($file_path);
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100242 }
joelcox2035fd82011-01-16 16:50:36 +0100243
Thanasis Polychronakisb6e0b582012-05-14 21:31:04 +0300244 // Is the config file in the environment folder?
Andrey Andreevdb529ca2013-01-28 11:00:02 +0200245 if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300247 require($file_path);
248 }
249 elseif ( ! $found)
Thanasis Polychronakis8991cb82012-05-20 18:44:21 +0300250 {
251 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700252 echo 'The configuration file does not exist.';
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700253 exit(EXIT_CONFIG);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600254 }
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100255
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600256 // Does the $config array exist in the file?
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 if ( ! isset($config) OR ! is_array($config))
258 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500259 set_status_header(503);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700260 echo 'Your config file does not appear to be formatted correctly.';
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700261 exit(EXIT_CONFIG);
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600264 // Are any values being dynamically replaced?
265 if (count($replace) > 0)
266 {
267 foreach ($replace as $key => $val)
268 {
269 if (isset($config[$key]))
270 {
271 $config[$key] = $val;
272 }
273 }
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600276 return $_config[0] =& $config;
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400278}
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600279
280// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000281
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400282if ( ! function_exists('config_item'))
283{
Timothy Warrenad475052012-04-19 13:21:06 -0400284 /**
285 * Returns the specified config item
286 *
287 * @param string
288 * @return mixed
289 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600290 function config_item($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
vlakoffaf431ce2013-07-19 02:06:41 +0200292 static $_config;
Barry Mienydd671972010-10-04 16:33:58 +0200293
vlakoffaf431ce2013-07-19 02:06:41 +0200294 if (empty($_config))
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
vlakoffaf431ce2013-07-19 02:06:41 +0200296 // references cannot be directly assigned to static variables, so we use an array
297 $_config[0] =& get_config();
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
vlakoffaf431ce2013-07-19 02:06:41 +0200300 return isset($_config[0][$item]) ? $_config[0][$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400302}
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600304// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300306if ( ! function_exists('get_mimes'))
307{
308 /**
309 * Returns the MIME types array from config/mimes.php
310 *
311 * @return array
312 */
313 function &get_mimes()
314 {
315 static $_mimes = array();
316
Andrey Andreev06879112013-01-29 15:05:02 +0200317 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300318 {
319 $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
320 }
Andrey Andreev06879112013-01-29 15:05:02 +0200321 elseif (file_exists(APPPATH.'config/mimes.php'))
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300322 {
323 $_mimes = include(APPPATH.'config/mimes.php');
324 }
325
326 return $_mimes;
327 }
328}
329
330// ------------------------------------------------------------------------
331
Andrey Andreev3fb02672012-10-22 16:48:01 +0300332if ( ! function_exists('is_https'))
333{
334 /**
335 * Is HTTPS?
336 *
337 * Determines if the application is accessed via an encrypted
338 * (HTTPS) connection.
339 *
340 * @return bool
341 */
342 function is_https()
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200343 {
Andrey Andreev333b80e2013-07-01 16:21:54 +0300344 if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200345 {
346 return TRUE;
347 }
348 elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
349 {
350 return TRUE;
351 }
Andrey Andreev333b80e2013-07-01 16:21:54 +0300352 elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200353 {
354 return TRUE;
355 }
Richard Deurwaarder (Xeli)98999972013-06-24 15:19:30 +0200356
Richard Deurwaarder (Xeli)7cc29452013-06-24 15:06:19 +0200357 return FALSE;
Richard Deurwaarder (Xeli)23dc0522013-06-24 14:52:47 +0200358 }
Andrey Andreev3fb02672012-10-22 16:48:01 +0300359}
360
361// ------------------------------------------------------------------------
362
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400363if ( ! function_exists('show_error'))
364{
Timothy Warrenad475052012-04-19 13:21:06 -0400365 /**
366 * Error Handler
367 *
368 * This function lets us invoke the exception class and
369 * display errors using the standard error template located
vlakoff52301c72013-03-29 14:23:34 +0100370 * in application/views/errors/error_general.php
Timothy Warrenad475052012-04-19 13:21:06 -0400371 * This function will send the error page directly to the
372 * browser and exit.
373 *
374 * @param string
375 * @param int
376 * @param string
377 * @return void
378 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600379 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
380 {
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700381 $status_code = abs($status_code);
382 if ($status_code < 100)
383 {
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700384 $exit_status = $status_code + EXIT__AUTO_MIN;
385 if ($exit_status > EXIT__AUTO_MAX)
386 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700387 $exit_status = EXIT_ERROR;
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -0700388 }
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700389 $status_code = 500;
390 }
391 else
392 {
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700393 $exit_status = EXIT_ERROR;
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700394 }
Andrey Andreev5a6814e2013-03-04 15:44:12 +0200395
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600396 $_error =& load_class('Exceptions', 'core');
397 echo $_error->show_error($heading, $message, 'error_general', $status_code);
Daniel Hunsaker353f9832013-01-24 17:09:10 -0700398 exit($exit_status);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600399 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400400}
Derek Allard2067d1a2008-11-13 22:59:24 +0000401
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600402// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000403
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400404if ( ! function_exists('show_404'))
405{
Timothy Warrenad475052012-04-19 13:21:06 -0400406 /**
407 * 404 Page Handler
408 *
409 * This function is similar to the show_error() function above
410 * However, instead of the standard error template it displays
411 * 404 errors.
412 *
413 * @param string
414 * @param bool
415 * @return void
416 */
Derek Allard2ddc9492010-08-05 10:08:33 -0400417 function show_404($page = '', $log_error = TRUE)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600418 {
419 $_error =& load_class('Exceptions', 'core');
Derek Allard2ddc9492010-08-05 10:08:33 -0400420 $_error->show_404($page, $log_error);
Daniel Hunsaker50dfe012013-03-04 02:05:20 -0700421 exit(EXIT_UNKNOWN_FILE);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600422 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400423}
Derek Allard2067d1a2008-11-13 22:59:24 +0000424
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600425// ------------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000426
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400427if ( ! function_exists('log_message'))
428{
Timothy Warrenad475052012-04-19 13:21:06 -0400429 /**
430 * Error Logging Interface
431 *
432 * We use this as a simple mechanism to access the logging
433 * class and send messages to be logged.
434 *
vlakoffd0c30ab2013-05-07 07:49:23 +0200435 * @param string the error level: 'error', 'debug' or 'info'
436 * @param string the error message
437 * @param bool whether the error is a native PHP error
Timothy Warrenad475052012-04-19 13:21:06 -0400438 * @return void
439 */
vlakoffd0c30ab2013-05-07 07:49:23 +0200440 function log_message($level, $message, $php_error = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Ted Woodb19a2032013-01-05 16:02:43 -0800442 static $_log, $_log_threshold;
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200443
Ted Woodb19a2032013-01-05 16:02:43 -0800444 if ($_log_threshold === NULL)
445 {
446 $_log_threshold = config_item('log_threshold');
447 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600448
Ted Woodb19a2032013-01-05 16:02:43 -0800449 if ($_log_threshold === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600450 {
451 return;
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Ted Woodb19a2032013-01-05 16:02:43 -0800454 if ($_log === NULL)
455 {
vlakoff61f1aa02013-08-07 11:29:17 +0200456 // references cannot be directly assigned to static variables, so we use an array
457 $_log[0] =& load_class('Log', 'core');
Ted Woodb19a2032013-01-05 16:02:43 -0800458 }
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200459
vlakoff61f1aa02013-08-07 11:29:17 +0200460 $_log[0]->write_log($level, $message, $php_error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400462}
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600464// ------------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000465
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400466if ( ! function_exists('set_status_header'))
467{
Timothy Warrenad475052012-04-19 13:21:06 -0400468 /**
469 * Set HTTP Status Header
470 *
471 * @param int the status code
472 * @param string
473 * @return void
474 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600475 function set_status_header($code = 200, $text = '')
Derek Jones817163a2009-07-11 17:05:58 +0000476 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600477 $stati = array(
Timothy Warrenad475052012-04-19 13:21:06 -0400478 200 => 'OK',
479 201 => 'Created',
480 202 => 'Accepted',
481 203 => 'Non-Authoritative Information',
482 204 => 'No Content',
483 205 => 'Reset Content',
484 206 => 'Partial Content',
Derek Jones817163a2009-07-11 17:05:58 +0000485
Timothy Warrenad475052012-04-19 13:21:06 -0400486 300 => 'Multiple Choices',
487 301 => 'Moved Permanently',
488 302 => 'Found',
Andrey Andreev51d6d842012-06-15 16:41:09 +0300489 303 => 'See Other',
Timothy Warrenad475052012-04-19 13:21:06 -0400490 304 => 'Not Modified',
491 305 => 'Use Proxy',
492 307 => 'Temporary Redirect',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600493
Timothy Warrenad475052012-04-19 13:21:06 -0400494 400 => 'Bad Request',
495 401 => 'Unauthorized',
496 403 => 'Forbidden',
497 404 => 'Not Found',
498 405 => 'Method Not Allowed',
499 406 => 'Not Acceptable',
500 407 => 'Proxy Authentication Required',
501 408 => 'Request Timeout',
502 409 => 'Conflict',
503 410 => 'Gone',
504 411 => 'Length Required',
505 412 => 'Precondition Failed',
506 413 => 'Request Entity Too Large',
507 414 => 'Request-URI Too Long',
508 415 => 'Unsupported Media Type',
509 416 => 'Requested Range Not Satisfiable',
510 417 => 'Expectation Failed',
511 422 => 'Unprocessable Entity',
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600512
Timothy Warrenad475052012-04-19 13:21:06 -0400513 500 => 'Internal Server Error',
514 501 => 'Not Implemented',
515 502 => 'Bad Gateway',
516 503 => 'Service Unavailable',
517 504 => 'Gateway Timeout',
518 505 => 'HTTP Version Not Supported'
519 );
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600520
Andrey Andreev51d6d842012-06-15 16:41:09 +0300521 if (empty($code) OR ! is_numeric($code))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600522 {
523 show_error('Status codes must be numeric', 500);
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Andrey Andreev51d6d842012-06-15 16:41:09 +0300526 is_int($code) OR $code = (int) $code;
527
528 if (empty($text))
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600529 {
Andrey Andreev51d6d842012-06-15 16:41:09 +0300530 if (isset($stati[$code]))
531 {
532 $text = $stati[$code];
533 }
534 else
535 {
536 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
537 }
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Andrey Andreevb7b43962012-02-27 22:45:48 +0200540 $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600541
vlakoff40d12492013-08-06 14:46:00 +0200542 if (strpos(PHP_SAPI, 'cgi') === 0)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600543 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700544 header('Status: '.$code.' '.$text, TRUE);
545 }
546 else
547 {
548 header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code);
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600549 }
Derek Jones817163a2009-07-11 17:05:58 +0000550 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400551}
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600553// --------------------------------------------------------------------
Derek Jones817163a2009-07-11 17:05:58 +0000554
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400555if ( ! function_exists('_exception_handler'))
556{
Timothy Warrenad475052012-04-19 13:21:06 -0400557 /**
558 * Exception Handler
559 *
vlakoffc941d852013-08-06 14:44:40 +0200560 * This is the custom exception handler that is declared at the top
561 * of CodeIgniter.php. The main reason we use this is to permit
Timothy Warrenad475052012-04-19 13:21:06 -0400562 * PHP errors to be logged in our own log files since the user may
563 * not have access to server logs. Since this function
564 * effectively intercepts PHP errors, however, we also need
565 * to display errors based on the current error_reporting level.
566 * We do that with the use of a PHP error template.
567 *
568 * @param int
569 * @param string
570 * @param string
571 * @param int
572 * @return void
573 */
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600574 function _exception_handler($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200575 {
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600576 $_error =& load_class('Exceptions', 'core');
Barry Mienydd671972010-10-04 16:33:58 +0200577
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300578 // Should we ignore the error? We'll get the current error_reporting
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600579 // level and add its bits with the severity bits to find out.
Francesco Negri0e0c37b2012-08-04 14:16:50 +0300580 if (($severity & error_reporting()) !== $severity)
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600581 {
582 return;
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Francesco Negri312bdc52012-08-04 07:32:19 -0400585 // Should we display the error?
586 if ((bool) ini_get('display_errors') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 $_error->show_php_error($severity, $message, $filepath, $line);
589 }
590
Derek Jonesdc8e9ea2010-03-02 13:17:19 -0600591 $_error->log_exception($severity, $message, $filepath, $line);
592 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400593}
Derek Allard2067d1a2008-11-13 22:59:24 +0000594
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400595// --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200596
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400597if ( ! function_exists('remove_invisible_characters'))
598{
Timothy Warrenad475052012-04-19 13:21:06 -0400599 /**
600 * Remove Invisible Characters
601 *
602 * This prevents sandwiching null characters
603 * between ascii characters, like Java\0script.
604 *
605 * @param string
606 * @param bool
607 * @return string
608 */
Pascal Kriete0ff50262011-04-05 14:52:03 -0400609 function remove_invisible_characters($str, $url_encoded = TRUE)
Greg Aker757dda62010-04-14 19:06:19 -0500610 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400611 $non_displayables = array();
Andrey Andreev188abaf2012-01-07 19:09:42 +0200612
613 // every control character except newline (dec 10),
614 // carriage return (dec 13) and horizontal tab (dec 09)
Pascal Kriete0ff50262011-04-05 14:52:03 -0400615 if ($url_encoded)
Greg Aker757dda62010-04-14 19:06:19 -0500616 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400617 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
618 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
Greg Aker757dda62010-04-14 19:06:19 -0500619 }
Andrey Andreev188abaf2012-01-07 19:09:42 +0200620
Pascal Kriete0ff50262011-04-05 14:52:03 -0400621 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
Greg Aker757dda62010-04-14 19:06:19 -0500622
623 do
624 {
Pascal Kriete0ff50262011-04-05 14:52:03 -0400625 $str = preg_replace($non_displayables, '', $str, -1, $count);
Greg Aker757dda62010-04-14 19:06:19 -0500626 }
Pascal Kriete0ff50262011-04-05 14:52:03 -0400627 while ($count);
Greg Aker757dda62010-04-14 19:06:19 -0500628
629 return $str;
630 }
Dan Horrigan3ef65bd2011-05-08 11:06:44 -0400631}
Derek Allard2067d1a2008-11-13 22:59:24 +0000632
kenjisfbac8b42011-08-25 10:51:44 +0900633// ------------------------------------------------------------------------
634
kenjisfbac8b42011-08-25 10:51:44 +0900635if ( ! function_exists('html_escape'))
636{
Timothy Warrenad475052012-04-19 13:21:06 -0400637 /**
638 * Returns HTML escaped variable
639 *
640 * @param mixed
641 * @return mixed
642 */
kenjisfbac8b42011-08-25 10:51:44 +0900643 function html_escape($var)
644 {
Andrey Andreevb7b43962012-02-27 22:45:48 +0200645 return is_array($var)
646 ? array_map('html_escape', $var)
647 : htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
Greg Aker5c1aa632011-12-25 01:24:29 -0600648 }
Greg Akerd96f8822011-12-27 16:23:47 -0600649}
Greg Aker5c1aa632011-12-25 01:24:29 -0600650
Chad Furmana1abada2012-07-29 01:03:50 -0400651// ------------------------------------------------------------------------
652
653if ( ! function_exists('_stringify_attributes'))
654{
655 /**
Andrey Andreevbdb99992012-07-30 17:38:05 +0300656 * Stringify attributes for use in HTML tags.
Chad Furmana1abada2012-07-29 01:03:50 -0400657 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300658 * Helper function used to convert a string, array, or object
659 * of attributes to a string.
Chad Furmana1abada2012-07-29 01:03:50 -0400660 *
Andrey Andreevbdb99992012-07-30 17:38:05 +0300661 * @param mixed string, array, object
662 * @param bool
663 * @return string
Chad Furmana1abada2012-07-29 01:03:50 -0400664 */
665 function _stringify_attributes($attributes, $js = FALSE)
666 {
Andrey Andreevbdb99992012-07-30 17:38:05 +0300667 $atts = NULL;
Chad Furmana1abada2012-07-29 01:03:50 -0400668
669 if (empty($attributes))
670 {
671 return $atts;
672 }
673
674 if (is_string($attributes))
675 {
676 return ' '.$attributes;
677 }
678
679 $attributes = (array) $attributes;
680
681 foreach ($attributes as $key => $val)
682 {
683 $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
684 }
Andrey Andreevbdb99992012-07-30 17:38:05 +0300685
Chad Furmana1abada2012-07-29 01:03:50 -0400686 return rtrim($atts, ',');
687 }
688}
689
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200690// ------------------------------------------------------------------------
691
692if ( ! function_exists('function_usable'))
693{
694 /**
695 * Function usable
696 *
697 * Executes a function_exists() check, and if the Suhosin PHP
698 * extension is loaded - checks whether the function that is
699 * checked might be disabled in there as well.
700 *
701 * This is useful as function_exists() will return FALSE for
702 * functions disabled via the *disable_functions* php.ini
703 * setting, but not for *suhosin.executor.func.blacklist* and
704 * *suhosin.executor.disable_eval*. These settings will just
705 * terminate script execution if a disabled function is executed.
706 *
707 * @link http://www.hardened-php.net/suhosin/
708 * @param string $function_name Function to check for
709 * @return bool TRUE if the function exists and is safe to call,
710 * FALSE otherwise.
711 */
712 function function_usable($function_name)
713 {
714 static $_suhosin_func_blacklist;
715
716 if (function_exists($function_name))
717 {
718 if ( ! isset($_suhosin_func_blacklist))
719 {
Andrey Andreevae634622012-12-17 10:30:18 +0200720 if (extension_loaded('suhosin'))
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200721 {
Andrey Andreevae634622012-12-17 10:30:18 +0200722 $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist')));
723
724 if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval'))
725 {
726 $_suhosin_func_blacklist[] = 'eval';
727 }
728 }
729 else
730 {
731 $_suhosin_func_blacklist = array();
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200732 }
733 }
734
Andrey Andreevae634622012-12-17 10:30:18 +0200735 return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200736 }
737
738 return FALSE;
739 }
740}
Richard Deurwaarder (Xeli)668d0092013-06-24 13:50:52 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742/* End of file Common.php */
Richard Deurwaarder (Xeli)4055d572013-06-24 14:59:20 +0200743/* Location: ./system/core/Common.php */