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