Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 30 | * Common Functions |
| 31 | * |
| 32 | * Loads the base classes and executes the request. |
| 33 | * |
| 34 | * @package CodeIgniter |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 35 | * @subpackage CodeIgniter |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 36 | * @category Common Functions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/ |
| 39 | */ |
| 40 | |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 43 | if ( ! function_exists('is_php')) |
| 44 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 45 | /** |
| 46 | * Determines if the current version of PHP is greater then the supplied value |
| 47 | * |
Andrey Andreev | 24bd230 | 2012-06-05 22:29:12 +0300 | [diff] [blame] | 48 | * Since there are a few places where we conditionally test for PHP > 5.3 |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 49 | * we'll set a static variable. |
| 50 | * |
| 51 | * @param string |
| 52 | * @return bool TRUE if the current version is $version or higher |
| 53 | */ |
Andrey Andreev | 24bd230 | 2012-06-05 22:29:12 +0300 | [diff] [blame] | 54 | function is_php($version = '5.3.0') |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 55 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 56 | static $_is_php; |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 57 | $version = (string) $version; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 58 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 59 | if ( ! isset($_is_php[$version])) |
| 60 | { |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 61 | $_is_php[$version] = (version_compare(PHP_VERSION, $version) >= 0); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 62 | } |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 63 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 64 | return $_is_php[$version]; |
| 65 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 66 | } |
Derek Jones | 5bcfd2e | 2009-07-28 14:42:42 +0000 | [diff] [blame] | 67 | |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 68 | // ------------------------------------------------------------------------ |
| 69 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 70 | if ( ! function_exists('is_really_writable')) |
| 71 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 72 | /** |
| 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 Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 82 | function is_really_writable($file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 84 | // If we're on a Unix server with safe_mode off we call is_writable |
Andrey Andreev | 5932a73 | 2013-09-13 14:45:53 +0300 | [diff] [blame] | 85 | if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR (bool) @ini_get('safe_mode') === FALSE)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 86 | { |
| 87 | return is_writable($file); |
| 88 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 90 | /* For Windows servers and safe_mode "on" installations we'll actually |
| 91 | * write a file then read it. Bah... |
| 92 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 93 | if (is_dir($file)) |
| 94 | { |
vlakoff | 0612756 | 2013-03-30 00:06:39 +0100 | [diff] [blame] | 95 | $file = rtrim($file, '/').'/'.md5(mt_rand()); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 96 | 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 Barnes | 1508301 | 2011-03-21 22:13:12 -0400 | [diff] [blame] | 106 | elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | { |
| 108 | return FALSE; |
| 109 | } |
| 110 | |
| 111 | fclose($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | return TRUE; |
| 113 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 114 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | |
| 116 | // ------------------------------------------------------------------------ |
| 117 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 118 | if ( ! function_exists('load_class')) |
| 119 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 120 | /** |
| 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 Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 132 | function &load_class($class, $directory = 'libraries', $prefix = 'CI_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 134 | static $_classes = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 136 | // Does the class exist? If so, we're done... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 137 | if (isset($_classes[$class])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 139 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 141 | |
| 142 | $name = FALSE; |
| 143 | |
Shane Pearson | ab57a35 | 2011-08-22 16:11:20 -0500 | [diff] [blame] | 144 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 148 | if (file_exists($path.$directory.'/'.$class.'.php')) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 149 | { |
| 150 | $name = $prefix.$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 152 | if (class_exists($name, FALSE) === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 153 | { |
Andrey Andreev | a52c775 | 2012-10-11 10:54:02 +0300 | [diff] [blame] | 154 | require_once($path.$directory.'/'.$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 161 | // Is the request a class extension? If so we load it too |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 162 | if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 164 | $name = config_item('subclass_prefix').$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 166 | if (class_exists($name, FALSE) === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 167 | { |
Andrey Andreev | a52c775 | 2012-10-11 10:54:02 +0300 | [diff] [blame] | 168 | require_once(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // Did we find the class? |
| 173 | if ($name === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | // Note: We use exit() rather then show_error() in order to avoid a |
vlakoff | fe8fe45 | 2012-07-11 19:56:50 +0200 | [diff] [blame] | 176 | // self-referencing loop with the Exceptions class |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 177 | set_status_header(503); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 178 | echo 'Unable to locate the specified class: '.$class.'.php'; |
Daniel Hunsaker | 50dfe01 | 2013-03-04 02:05:20 -0700 | [diff] [blame] | 179 | exit(EXIT_UNKNOWN_CLASS); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 181 | |
| 182 | // Keep track of what we just loaded |
| 183 | is_loaded($class); |
| 184 | |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 185 | $_classes[$class] = new $name(); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 186 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 188 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 190 | // -------------------------------------------------------------------- |
| 191 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 192 | if ( ! function_exists('is_loaded')) |
| 193 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 194 | /** |
| 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 Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 201 | function &is_loaded($class = '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 202 | { |
| 203 | static $_is_loaded = array(); |
| 204 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 205 | if ($class !== '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 206 | { |
| 207 | $_is_loaded[strtolower($class)] = $class; |
| 208 | } |
| 209 | |
| 210 | return $_is_loaded; |
| 211 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 212 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 213 | |
| 214 | // ------------------------------------------------------------------------ |
Derek Jones | f0a9b33 | 2009-07-29 14:19:18 +0000 | [diff] [blame] | 215 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 216 | if ( ! function_exists('get_config')) |
| 217 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 218 | /** |
| 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 | */ |
Andrey Andreev | 49890a9 | 2013-08-19 19:56:18 +0300 | [diff] [blame] | 227 | function &get_config(Array $replace = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 229 | static $_config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
vlakoff | 05d043b | 2013-08-19 04:55:34 +0200 | [diff] [blame] | 231 | if (empty($_config)) |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 232 | { |
| 233 | $file_path = APPPATH.'config/config.php'; |
| 234 | $found = FALSE; |
| 235 | if (file_exists($file_path)) |
| 236 | { |
| 237 | $found = TRUE; |
| 238 | require($file_path); |
| 239 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 240 | |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 241 | // Is the config file in the environment folder? |
| 242 | if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php')) |
| 243 | { |
| 244 | require($file_path); |
| 245 | } |
| 246 | elseif ( ! $found) |
| 247 | { |
| 248 | set_status_header(503); |
| 249 | echo 'The configuration file does not exist.'; |
| 250 | exit(EXIT_CONFIG); |
| 251 | } |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 252 | |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 253 | // Does the $config array exist in the file? |
| 254 | if ( ! isset($config) OR ! is_array($config)) |
| 255 | { |
| 256 | set_status_header(503); |
| 257 | echo 'Your config file does not appear to be formatted correctly.'; |
| 258 | exit(EXIT_CONFIG); |
| 259 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 260 | |
vlakoff | 05d043b | 2013-08-19 04:55:34 +0200 | [diff] [blame] | 261 | // references cannot be directly assigned to static variables, so we use an array |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 262 | $_config[0] =& $config; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
vlakoff | 67e5ca6 | 2013-08-19 04:52:00 +0200 | [diff] [blame] | 265 | // Are any values being dynamically added or replaced? |
vlakoff | 2f7810a | 2013-08-19 04:46:26 +0200 | [diff] [blame] | 266 | foreach ($replace as $key => $val) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 267 | { |
vlakoff | 05d043b | 2013-08-19 04:55:34 +0200 | [diff] [blame] | 268 | $_config[0][$key] = $val; |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 269 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
vlakoff | 05d043b | 2013-08-19 04:55:34 +0200 | [diff] [blame] | 271 | return $_config[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 273 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 274 | |
| 275 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 277 | if ( ! function_exists('config_item')) |
| 278 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 279 | /** |
| 280 | * Returns the specified config item |
| 281 | * |
| 282 | * @param string |
| 283 | * @return mixed |
| 284 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 285 | function config_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | { |
vlakoff | af431ce | 2013-07-19 02:06:41 +0200 | [diff] [blame] | 287 | static $_config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | |
vlakoff | af431ce | 2013-07-19 02:06:41 +0200 | [diff] [blame] | 289 | if (empty($_config)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
vlakoff | af431ce | 2013-07-19 02:06:41 +0200 | [diff] [blame] | 291 | // references cannot be directly assigned to static variables, so we use an array |
| 292 | $_config[0] =& get_config(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | |
vlakoff | af431ce | 2013-07-19 02:06:41 +0200 | [diff] [blame] | 295 | return isset($_config[0][$item]) ? $_config[0][$item] : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 297 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 299 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 301 | if ( ! function_exists('get_mimes')) |
| 302 | { |
| 303 | /** |
| 304 | * Returns the MIME types array from config/mimes.php |
| 305 | * |
| 306 | * @return array |
| 307 | */ |
| 308 | function &get_mimes() |
| 309 | { |
| 310 | static $_mimes = array(); |
| 311 | |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 312 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 313 | { |
| 314 | $_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
| 315 | } |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 316 | elseif (file_exists(APPPATH.'config/mimes.php')) |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 317 | { |
| 318 | $_mimes = include(APPPATH.'config/mimes.php'); |
| 319 | } |
| 320 | |
| 321 | return $_mimes; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // ------------------------------------------------------------------------ |
| 326 | |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 327 | if ( ! function_exists('is_https')) |
| 328 | { |
| 329 | /** |
| 330 | * Is HTTPS? |
| 331 | * |
| 332 | * Determines if the application is accessed via an encrypted |
| 333 | * (HTTPS) connection. |
| 334 | * |
| 335 | * @return bool |
| 336 | */ |
| 337 | function is_https() |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 338 | { |
Andrey Andreev | 333b80e | 2013-07-01 16:21:54 +0300 | [diff] [blame] | 339 | if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 340 | { |
| 341 | return TRUE; |
| 342 | } |
| 343 | elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') |
| 344 | { |
| 345 | return TRUE; |
| 346 | } |
Andrey Andreev | 333b80e | 2013-07-01 16:21:54 +0300 | [diff] [blame] | 347 | elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 348 | { |
| 349 | return TRUE; |
| 350 | } |
Richard Deurwaarder (Xeli) | 9899997 | 2013-06-24 15:19:30 +0200 | [diff] [blame] | 351 | |
Richard Deurwaarder (Xeli) | 7cc2945 | 2013-06-24 15:06:19 +0200 | [diff] [blame] | 352 | return FALSE; |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 353 | } |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // ------------------------------------------------------------------------ |
| 357 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 358 | if ( ! function_exists('show_error')) |
| 359 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 360 | /** |
| 361 | * Error Handler |
| 362 | * |
| 363 | * This function lets us invoke the exception class and |
| 364 | * display errors using the standard error template located |
vlakoff | 52301c7 | 2013-03-29 14:23:34 +0100 | [diff] [blame] | 365 | * in application/views/errors/error_general.php |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 366 | * This function will send the error page directly to the |
| 367 | * browser and exit. |
| 368 | * |
| 369 | * @param string |
| 370 | * @param int |
| 371 | * @param string |
| 372 | * @return void |
| 373 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 374 | function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') |
| 375 | { |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 376 | $status_code = abs($status_code); |
| 377 | if ($status_code < 100) |
| 378 | { |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 379 | $exit_status = $status_code + EXIT__AUTO_MIN; |
| 380 | if ($exit_status > EXIT__AUTO_MAX) |
| 381 | { |
Daniel Hunsaker | 50dfe01 | 2013-03-04 02:05:20 -0700 | [diff] [blame] | 382 | $exit_status = EXIT_ERROR; |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 383 | } |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 384 | $status_code = 500; |
| 385 | } |
| 386 | else |
| 387 | { |
Daniel Hunsaker | 50dfe01 | 2013-03-04 02:05:20 -0700 | [diff] [blame] | 388 | $exit_status = EXIT_ERROR; |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 389 | } |
Andrey Andreev | 5a6814e | 2013-03-04 15:44:12 +0200 | [diff] [blame] | 390 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 391 | $_error =& load_class('Exceptions', 'core'); |
| 392 | echo $_error->show_error($heading, $message, 'error_general', $status_code); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 393 | exit($exit_status); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 394 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 395 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 397 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 399 | if ( ! function_exists('show_404')) |
| 400 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 401 | /** |
| 402 | * 404 Page Handler |
| 403 | * |
| 404 | * This function is similar to the show_error() function above |
| 405 | * However, instead of the standard error template it displays |
| 406 | * 404 errors. |
| 407 | * |
| 408 | * @param string |
| 409 | * @param bool |
| 410 | * @return void |
| 411 | */ |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 412 | function show_404($page = '', $log_error = TRUE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 413 | { |
| 414 | $_error =& load_class('Exceptions', 'core'); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 415 | $_error->show_404($page, $log_error); |
Daniel Hunsaker | 50dfe01 | 2013-03-04 02:05:20 -0700 | [diff] [blame] | 416 | exit(EXIT_UNKNOWN_FILE); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 417 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 418 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 420 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 422 | if ( ! function_exists('log_message')) |
| 423 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 424 | /** |
| 425 | * Error Logging Interface |
| 426 | * |
| 427 | * We use this as a simple mechanism to access the logging |
| 428 | * class and send messages to be logged. |
| 429 | * |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 430 | * @param string the error level: 'error', 'debug' or 'info' |
| 431 | * @param string the error message |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 432 | * @return void |
| 433 | */ |
Andrey Andreev | 838c9a9 | 2013-09-13 14:05:13 +0300 | [diff] [blame] | 434 | function log_message($level, $message) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | { |
Andrey Andreev | 2f8d2d3 | 2013-08-07 15:54:47 +0300 | [diff] [blame] | 436 | static $_log; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | |
Andrey Andreev | 49890a9 | 2013-08-19 19:56:18 +0300 | [diff] [blame] | 438 | if ($_log === NULL) |
Ted Wood | b19a203 | 2013-01-05 16:02:43 -0800 | [diff] [blame] | 439 | { |
vlakoff | 61f1aa0 | 2013-08-07 11:29:17 +0200 | [diff] [blame] | 440 | // references cannot be directly assigned to static variables, so we use an array |
| 441 | $_log[0] =& load_class('Log', 'core'); |
Ted Wood | b19a203 | 2013-01-05 16:02:43 -0800 | [diff] [blame] | 442 | } |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 443 | |
Andrey Andreev | 838c9a9 | 2013-09-13 14:05:13 +0300 | [diff] [blame] | 444 | $_log[0]->write_log($level, $message); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 446 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 448 | // ------------------------------------------------------------------------ |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 449 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 450 | if ( ! function_exists('set_status_header')) |
| 451 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 452 | /** |
| 453 | * Set HTTP Status Header |
| 454 | * |
| 455 | * @param int the status code |
| 456 | * @param string |
| 457 | * @return void |
| 458 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 459 | function set_status_header($code = 200, $text = '') |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 460 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 461 | $stati = array( |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 462 | 200 => 'OK', |
| 463 | 201 => 'Created', |
| 464 | 202 => 'Accepted', |
| 465 | 203 => 'Non-Authoritative Information', |
| 466 | 204 => 'No Content', |
| 467 | 205 => 'Reset Content', |
| 468 | 206 => 'Partial Content', |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 469 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 470 | 300 => 'Multiple Choices', |
| 471 | 301 => 'Moved Permanently', |
| 472 | 302 => 'Found', |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 473 | 303 => 'See Other', |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 474 | 304 => 'Not Modified', |
| 475 | 305 => 'Use Proxy', |
| 476 | 307 => 'Temporary Redirect', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 477 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 478 | 400 => 'Bad Request', |
| 479 | 401 => 'Unauthorized', |
| 480 | 403 => 'Forbidden', |
| 481 | 404 => 'Not Found', |
| 482 | 405 => 'Method Not Allowed', |
| 483 | 406 => 'Not Acceptable', |
| 484 | 407 => 'Proxy Authentication Required', |
| 485 | 408 => 'Request Timeout', |
| 486 | 409 => 'Conflict', |
| 487 | 410 => 'Gone', |
| 488 | 411 => 'Length Required', |
| 489 | 412 => 'Precondition Failed', |
| 490 | 413 => 'Request Entity Too Large', |
| 491 | 414 => 'Request-URI Too Long', |
| 492 | 415 => 'Unsupported Media Type', |
| 493 | 416 => 'Requested Range Not Satisfiable', |
| 494 | 417 => 'Expectation Failed', |
| 495 | 422 => 'Unprocessable Entity', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 496 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 497 | 500 => 'Internal Server Error', |
| 498 | 501 => 'Not Implemented', |
| 499 | 502 => 'Bad Gateway', |
| 500 | 503 => 'Service Unavailable', |
| 501 | 504 => 'Gateway Timeout', |
| 502 | 505 => 'HTTP Version Not Supported' |
| 503 | ); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 504 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 505 | if (empty($code) OR ! is_numeric($code)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 506 | { |
| 507 | show_error('Status codes must be numeric', 500); |
| 508 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 509 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 510 | is_int($code) OR $code = (int) $code; |
| 511 | |
| 512 | if (empty($text)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 513 | { |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 514 | if (isset($stati[$code])) |
| 515 | { |
| 516 | $text = $stati[$code]; |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | show_error('No status text available. Please check your status code number or supply your own message text.', 500); |
| 521 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 522 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 524 | $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE; |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 525 | |
vlakoff | 40d1249 | 2013-08-06 14:46:00 +0200 | [diff] [blame] | 526 | if (strpos(PHP_SAPI, 'cgi') === 0) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 527 | { |
Daniel Hunsaker | 8626e93 | 2013-03-04 05:14:22 -0700 | [diff] [blame] | 528 | header('Status: '.$code.' '.$text, TRUE); |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | header(($server_protocol ? $server_protocol : 'HTTP/1.1').' '.$code.' '.$text, TRUE, $code); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 533 | } |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 534 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 535 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 536 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 537 | // -------------------------------------------------------------------- |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 538 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 539 | if ( ! function_exists('_exception_handler')) |
| 540 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 541 | /** |
| 542 | * Exception Handler |
| 543 | * |
vlakoff | c941d85 | 2013-08-06 14:44:40 +0200 | [diff] [blame] | 544 | * This is the custom exception handler that is declared at the top |
| 545 | * of CodeIgniter.php. The main reason we use this is to permit |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 546 | * PHP errors to be logged in our own log files since the user may |
| 547 | * not have access to server logs. Since this function |
| 548 | * effectively intercepts PHP errors, however, we also need |
| 549 | * to display errors based on the current error_reporting level. |
| 550 | * We do that with the use of a PHP error template. |
| 551 | * |
Andrey Andreev | 0850a28 | 2013-10-21 14:26:18 +0300 | [diff] [blame^] | 552 | * @param int $severity |
| 553 | * @param string $message |
| 554 | * @param string $filepath |
| 555 | * @param int $line |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 556 | * @return void |
| 557 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 558 | function _exception_handler($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 559 | { |
Andrey Andreev | 0850a28 | 2013-10-21 14:26:18 +0300 | [diff] [blame^] | 560 | $is_error = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) === $severity); |
Jesse van Assen | 7eb116a | 2013-07-06 10:42:14 +0200 | [diff] [blame] | 561 | |
| 562 | // When an error occurred, set the status header to '500 Internal Server Error' |
| 563 | // to indicate to the client something went wrong. |
| 564 | // This can't be done within the $_error->show_php_error method because |
| 565 | // it is only called when the display_errors flag is set (which isn't usually |
| 566 | // the case in a production environment) or when errors are ignored because |
| 567 | // they are above the error_reporting threshold. |
| 568 | if ($is_error) |
| 569 | { |
| 570 | set_status_header(500); |
| 571 | } |
| 572 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 573 | $_error =& load_class('Exceptions', 'core'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 574 | |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 575 | // Should we ignore the error? We'll get the current error_reporting |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 576 | // level and add its bits with the severity bits to find out. |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 577 | if (($severity & error_reporting()) !== $severity) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 578 | { |
| 579 | return; |
| 580 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | |
Francesco Negri | 312bdc5 | 2012-08-04 07:32:19 -0400 | [diff] [blame] | 582 | // Should we display the error? |
| 583 | if ((bool) ini_get('display_errors') === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | { |
| 585 | $_error->show_php_error($severity, $message, $filepath, $line); |
| 586 | } |
| 587 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 588 | $_error->log_exception($severity, $message, $filepath, $line); |
Jesse van Assen | 7eb116a | 2013-07-06 10:42:14 +0200 | [diff] [blame] | 589 | |
| 590 | // If the error is fatal, the execution of the script should be stopped because |
| 591 | // errors can't be recovered from. Halting the script conforms with PHP's |
| 592 | // default error handling. See http://www.php.net/manual/en/errorfunc.constants.php |
| 593 | if ($is_error) |
| 594 | { |
Jesse van Assen | cf60fa7 | 2013-09-27 11:58:44 +0200 | [diff] [blame] | 595 | exit(EXIT_ERROR); |
Jesse van Assen | 7eb116a | 2013-07-06 10:42:14 +0200 | [diff] [blame] | 596 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 597 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 598 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 600 | // ------------------------------------------------------------------------ |
| 601 | |
| 602 | if ( ! function_exists('_shutdown_handler')) |
| 603 | { |
| 604 | /** |
| 605 | * Shutdown Handler |
| 606 | * |
| 607 | * This is the shutdown handler that is declared at the top |
| 608 | * of CodeIgniter.php. The main reason we use this is to simulate |
| 609 | * a complete custom exception handler. |
| 610 | * |
vkeranov | d6f3d31 | 2013-09-14 21:39:49 +0300 | [diff] [blame] | 611 | * E_STRICT is purposivly neglected because such events may have |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 612 | * been caught. Duplication or none? None is preferred for now. |
| 613 | * |
| 614 | * @link http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a |
| 615 | * @return void |
| 616 | */ |
| 617 | function _shutdown_handler() |
| 618 | { |
| 619 | $last_error = function_exists('error_get_last') ? error_get_last() : NULL; |
| 620 | if (isset($last_error) && |
| 621 | ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) |
| 622 | { |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 623 | _exception_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 628 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 629 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 630 | if ( ! function_exists('remove_invisible_characters')) |
| 631 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 632 | /** |
| 633 | * Remove Invisible Characters |
| 634 | * |
| 635 | * This prevents sandwiching null characters |
| 636 | * between ascii characters, like Java\0script. |
| 637 | * |
| 638 | * @param string |
| 639 | * @param bool |
| 640 | * @return string |
| 641 | */ |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 642 | function remove_invisible_characters($str, $url_encoded = TRUE) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 643 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 644 | $non_displayables = array(); |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 645 | |
| 646 | // every control character except newline (dec 10), |
| 647 | // carriage return (dec 13) and horizontal tab (dec 09) |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 648 | if ($url_encoded) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 649 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 650 | $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 |
| 651 | $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 652 | } |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 653 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 654 | $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 655 | |
| 656 | do |
| 657 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 658 | $str = preg_replace($non_displayables, '', $str, -1, $count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 659 | } |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 660 | while ($count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 661 | |
| 662 | return $str; |
| 663 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 664 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 665 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 666 | // ------------------------------------------------------------------------ |
| 667 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 668 | if ( ! function_exists('html_escape')) |
| 669 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 670 | /** |
| 671 | * Returns HTML escaped variable |
| 672 | * |
| 673 | * @param mixed |
| 674 | * @return mixed |
| 675 | */ |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 676 | function html_escape($var) |
| 677 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 678 | return is_array($var) |
| 679 | ? array_map('html_escape', $var) |
| 680 | : htmlspecialchars($var, ENT_QUOTES, config_item('charset')); |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 681 | } |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 682 | } |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 683 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 684 | // ------------------------------------------------------------------------ |
| 685 | |
| 686 | if ( ! function_exists('_stringify_attributes')) |
| 687 | { |
| 688 | /** |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 689 | * Stringify attributes for use in HTML tags. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 690 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 691 | * Helper function used to convert a string, array, or object |
| 692 | * of attributes to a string. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 693 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 694 | * @param mixed string, array, object |
| 695 | * @param bool |
| 696 | * @return string |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 697 | */ |
| 698 | function _stringify_attributes($attributes, $js = FALSE) |
| 699 | { |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 700 | $atts = NULL; |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 701 | |
| 702 | if (empty($attributes)) |
| 703 | { |
| 704 | return $atts; |
| 705 | } |
| 706 | |
| 707 | if (is_string($attributes)) |
| 708 | { |
| 709 | return ' '.$attributes; |
| 710 | } |
| 711 | |
| 712 | $attributes = (array) $attributes; |
| 713 | |
| 714 | foreach ($attributes as $key => $val) |
| 715 | { |
| 716 | $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"'; |
| 717 | } |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 718 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 719 | return rtrim($atts, ','); |
| 720 | } |
| 721 | } |
| 722 | |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 723 | // ------------------------------------------------------------------------ |
| 724 | |
| 725 | if ( ! function_exists('function_usable')) |
| 726 | { |
| 727 | /** |
| 728 | * Function usable |
| 729 | * |
| 730 | * Executes a function_exists() check, and if the Suhosin PHP |
| 731 | * extension is loaded - checks whether the function that is |
| 732 | * checked might be disabled in there as well. |
| 733 | * |
| 734 | * This is useful as function_exists() will return FALSE for |
| 735 | * functions disabled via the *disable_functions* php.ini |
| 736 | * setting, but not for *suhosin.executor.func.blacklist* and |
| 737 | * *suhosin.executor.disable_eval*. These settings will just |
| 738 | * terminate script execution if a disabled function is executed. |
| 739 | * |
| 740 | * @link http://www.hardened-php.net/suhosin/ |
| 741 | * @param string $function_name Function to check for |
| 742 | * @return bool TRUE if the function exists and is safe to call, |
| 743 | * FALSE otherwise. |
| 744 | */ |
| 745 | function function_usable($function_name) |
| 746 | { |
| 747 | static $_suhosin_func_blacklist; |
| 748 | |
| 749 | if (function_exists($function_name)) |
| 750 | { |
| 751 | if ( ! isset($_suhosin_func_blacklist)) |
| 752 | { |
Andrey Andreev | ae63462 | 2012-12-17 10:30:18 +0200 | [diff] [blame] | 753 | if (extension_loaded('suhosin')) |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 754 | { |
Andrey Andreev | ae63462 | 2012-12-17 10:30:18 +0200 | [diff] [blame] | 755 | $_suhosin_func_blacklist = explode(',', trim(@ini_get('suhosin.executor.func.blacklist'))); |
| 756 | |
| 757 | if ( ! in_array('eval', $_suhosin_func_blacklist, TRUE) && @ini_get('suhosin.executor.disable_eval')) |
| 758 | { |
| 759 | $_suhosin_func_blacklist[] = 'eval'; |
| 760 | } |
| 761 | } |
| 762 | else |
| 763 | { |
| 764 | $_suhosin_func_blacklist = array(); |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
Andrey Andreev | ae63462 | 2012-12-17 10:30:18 +0200 | [diff] [blame] | 768 | return ! in_array($function_name, $_suhosin_func_blacklist, TRUE); |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | return FALSE; |
| 772 | } |
| 773 | } |
Richard Deurwaarder (Xeli) | 668d009 | 2013-06-24 13:50:52 +0200 | [diff] [blame] | 774 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 775 | /* End of file Common.php */ |
Andrey Andreev | 13c818e | 2013-09-14 21:44:36 +0300 | [diff] [blame] | 776 | /* Location: ./system/core/Common.php */ |