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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 41 | * Common Functions |
| 42 | * |
| 43 | * Loads the base classes and executes the request. |
| 44 | * |
| 45 | * @package CodeIgniter |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 46 | * @subpackage CodeIgniter |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 47 | * @category Common Functions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | // ------------------------------------------------------------------------ |
| 53 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 54 | if ( ! function_exists('is_php')) |
| 55 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 56 | /** |
kakysha | 654e5c9 | 2015-01-27 20:51:50 +0300 | [diff] [blame] | 57 | * Determines if the current version of PHP is equal to or greater than the supplied value |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 58 | * |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 59 | * @param string |
| 60 | * @return bool TRUE if the current version is $version or higher |
| 61 | */ |
vlakoff | 629d375 | 2014-04-05 09:52:01 +0200 | [diff] [blame] | 62 | function is_php($version) |
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 | static $_is_php; |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 65 | $version = (string) $version; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 67 | if ( ! isset($_is_php[$version])) |
| 68 | { |
vlakoff | 629d375 | 2014-04-05 09:52:01 +0200 | [diff] [blame] | 69 | $_is_php[$version] = version_compare(PHP_VERSION, $version, '>='); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 70 | } |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 71 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 72 | return $_is_php[$version]; |
| 73 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 74 | } |
Derek Jones | 5bcfd2e | 2009-07-28 14:42:42 +0000 | [diff] [blame] | 75 | |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 76 | // ------------------------------------------------------------------------ |
| 77 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 78 | if ( ! function_exists('is_really_writable')) |
| 79 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 80 | /** |
| 81 | * Tests for file writability |
| 82 | * |
| 83 | * is_writable() returns TRUE on Windows servers when you really can't write to |
| 84 | * the file, based on the read-only attribute. is_writable() is also unreliable |
| 85 | * on Unix servers if safe_mode is on. |
| 86 | * |
Andrey Andreev | 2b284f9 | 2014-01-25 00:25:56 +0200 | [diff] [blame] | 87 | * @link https://bugs.php.net/bug.php?id=54709 |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 88 | * @param string |
Gabriel Potkány | 1fb5000 | 2015-02-04 01:45:59 +0100 | [diff] [blame] | 89 | * @return bool |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 90 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 91 | function is_really_writable($file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 93 | // 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] | 94 | if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR ! ini_get('safe_mode'))) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 95 | { |
| 96 | return is_writable($file); |
| 97 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 99 | /* For Windows servers and safe_mode "on" installations we'll actually |
| 100 | * write a file then read it. Bah... |
| 101 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 102 | if (is_dir($file)) |
| 103 | { |
vlakoff | 0612756 | 2013-03-30 00:06:39 +0100 | [diff] [blame] | 104 | $file = rtrim($file, '/').'/'.md5(mt_rand()); |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 105 | if (($fp = @fopen($file, 'ab')) === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 106 | { |
| 107 | return FALSE; |
| 108 | } |
| 109 | |
| 110 | fclose($fp); |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 111 | @chmod($file, 0777); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 112 | @unlink($file); |
| 113 | return TRUE; |
| 114 | } |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 115 | elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | { |
| 117 | return FALSE; |
| 118 | } |
| 119 | |
| 120 | fclose($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | return TRUE; |
| 122 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 123 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | |
| 125 | // ------------------------------------------------------------------------ |
| 126 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 127 | if ( ! function_exists('load_class')) |
| 128 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 129 | /** |
| 130 | * Class registry |
| 131 | * |
| 132 | * This function acts as a singleton. If the requested class does not |
| 133 | * exist it is instantiated and set to a static variable. If it has |
| 134 | * previously been instantiated the variable is returned. |
| 135 | * |
| 136 | * @param string the class name being requested |
| 137 | * @param string the directory where the class should be found |
Andrey Andreev | ee9d428 | 2017-06-05 10:44:37 +0300 | [diff] [blame] | 138 | * @param mixed an optional argument to pass to the class constructor |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 139 | * @return object |
| 140 | */ |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 141 | function &load_class($class, $directory = 'libraries', $param = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 143 | static $_classes = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 145 | // Does the class exist? If so, we're done... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 146 | if (isset($_classes[$class])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 148 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 150 | |
| 151 | $name = FALSE; |
| 152 | |
Shane Pearson | ab57a35 | 2011-08-22 16:11:20 -0500 | [diff] [blame] | 153 | // Look for the class first in the local application/libraries folder |
| 154 | // then in the native system/libraries folder |
| 155 | foreach (array(APPPATH, BASEPATH) as $path) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 157 | if (file_exists($path.$directory.'/'.$class.'.php')) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 158 | { |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 159 | $name = 'CI_'.$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 161 | if (class_exists($name, FALSE) === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 162 | { |
Andrey Andreev | a52c775 | 2012-10-11 10:54:02 +0300 | [diff] [blame] | 163 | require_once($path.$directory.'/'.$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 164 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 170 | // Is the request a class extension? If so we load it too |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 171 | if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 173 | $name = config_item('subclass_prefix').$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 175 | if (class_exists($name, FALSE) === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 176 | { |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 177 | require_once(APPPATH.$directory.'/'.$name.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | // Did we find the class? |
| 182 | if ($name === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | { |
mult1mate | 5c58e67 | 2015-04-08 16:03:31 +0300 | [diff] [blame] | 184 | // Note: We use exit() rather than show_error() in order to avoid a |
vlakoff | fe8fe45 | 2012-07-11 19:56:50 +0200 | [diff] [blame] | 185 | // self-referencing loop with the Exceptions class |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 186 | set_status_header(503); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 187 | echo 'Unable to locate the specified class: '.$class.'.php'; |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 188 | exit(5); // EXIT_UNK_CLASS |
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 | // Keep track of what we just loaded |
| 192 | is_loaded($class); |
| 193 | |
Andrey Andreev | de9ec10 | 2014-02-24 17:16:32 +0200 | [diff] [blame] | 194 | $_classes[$class] = isset($param) |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 195 | ? new $name($param) |
| 196 | : new $name(); |
Andrey Andreev | de9ec10 | 2014-02-24 17:16:32 +0200 | [diff] [blame] | 197 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 199 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 201 | // -------------------------------------------------------------------- |
| 202 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 203 | if ( ! function_exists('is_loaded')) |
| 204 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 205 | /** |
| 206 | * Keeps track of which libraries have been loaded. This function is |
| 207 | * called by the load_class() function above |
| 208 | * |
| 209 | * @param string |
| 210 | * @return array |
| 211 | */ |
Andrey Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 212 | function &is_loaded($class = '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 213 | { |
| 214 | static $_is_loaded = array(); |
| 215 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 216 | if ($class !== '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 217 | { |
| 218 | $_is_loaded[strtolower($class)] = $class; |
| 219 | } |
| 220 | |
| 221 | return $_is_loaded; |
| 222 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 223 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 224 | |
| 225 | // ------------------------------------------------------------------------ |
Derek Jones | f0a9b33 | 2009-07-29 14:19:18 +0000 | [diff] [blame] | 226 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 227 | if ( ! function_exists('get_config')) |
| 228 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 229 | /** |
| 230 | * Loads the main config.php file |
| 231 | * |
| 232 | * This function lets us grab the config file even if the Config class |
| 233 | * hasn't been instantiated yet |
| 234 | * |
| 235 | * @param array |
| 236 | * @return array |
| 237 | */ |
Andrey Andreev | 49890a9 | 2013-08-19 19:56:18 +0300 | [diff] [blame] | 238 | function &get_config(Array $replace = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | { |
vlakoff | 5a34688 | 2014-05-17 07:17:41 +0200 | [diff] [blame] | 240 | static $config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
vlakoff | 5a34688 | 2014-05-17 07:17:41 +0200 | [diff] [blame] | 242 | if (empty($config)) |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 243 | { |
| 244 | $file_path = APPPATH.'config/config.php'; |
| 245 | $found = FALSE; |
| 246 | if (file_exists($file_path)) |
| 247 | { |
| 248 | $found = TRUE; |
| 249 | require($file_path); |
| 250 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 251 | |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 252 | // Is the config file in the environment folder? |
| 253 | if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php')) |
| 254 | { |
| 255 | require($file_path); |
| 256 | } |
| 257 | elseif ( ! $found) |
| 258 | { |
| 259 | set_status_header(503); |
| 260 | echo 'The configuration file does not exist.'; |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 261 | exit(3); // EXIT_CONFIG |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 262 | } |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 263 | |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 264 | // Does the $config array exist in the file? |
| 265 | if ( ! isset($config) OR ! is_array($config)) |
| 266 | { |
| 267 | set_status_header(503); |
| 268 | echo 'Your config file does not appear to be formatted correctly.'; |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 269 | exit(3); // EXIT_CONFIG |
vlakoff | 8d70c0a | 2013-08-17 07:31:29 +0200 | [diff] [blame] | 270 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | } |
| 272 | |
vlakoff | 67e5ca6 | 2013-08-19 04:52:00 +0200 | [diff] [blame] | 273 | // Are any values being dynamically added or replaced? |
vlakoff | 2f7810a | 2013-08-19 04:46:26 +0200 | [diff] [blame] | 274 | foreach ($replace as $key => $val) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 275 | { |
vlakoff | 5a34688 | 2014-05-17 07:17:41 +0200 | [diff] [blame] | 276 | $config[$key] = $val; |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 277 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | |
vlakoff | 5a34688 | 2014-05-17 07:17:41 +0200 | [diff] [blame] | 279 | return $config; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 281 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 282 | |
| 283 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 285 | if ( ! function_exists('config_item')) |
| 286 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 287 | /** |
| 288 | * Returns the specified config item |
| 289 | * |
| 290 | * @param string |
| 291 | * @return mixed |
| 292 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 293 | function config_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | { |
vlakoff | af431ce | 2013-07-19 02:06:41 +0200 | [diff] [blame] | 295 | static $_config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
vlakoff | af431ce | 2013-07-19 02:06:41 +0200 | [diff] [blame] | 297 | if (empty($_config)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | { |
vlakoff | af431ce | 2013-07-19 02:06:41 +0200 | [diff] [blame] | 299 | // references cannot be directly assigned to static variables, so we use an array |
| 300 | $_config[0] =& get_config(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame] | 303 | return isset($_config[0][$item]) ? $_config[0][$item] : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 305 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 307 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 309 | if ( ! function_exists('get_mimes')) |
| 310 | { |
| 311 | /** |
| 312 | * Returns the MIME types array from config/mimes.php |
| 313 | * |
| 314 | * @return array |
| 315 | */ |
| 316 | function &get_mimes() |
| 317 | { |
vlakoff | 69550c5 | 2014-05-19 13:45:02 +0200 | [diff] [blame] | 318 | static $_mimes; |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 319 | |
vlakoff | 69550c5 | 2014-05-19 13:45:02 +0200 | [diff] [blame] | 320 | if (empty($_mimes)) |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 321 | { |
Andrey Andreev | 8057d79 | 2017-02-06 10:34:33 +0200 | [diff] [blame] | 322 | $_mimes = file_exists(APPPATH.'config/mimes.php') |
| 323 | ? include(APPPATH.'config/mimes.php') |
| 324 | : array(); |
Andrey Andreev | d60e51b | 2017-02-06 10:28:36 +0200 | [diff] [blame] | 325 | |
vlakoff | 69550c5 | 2014-05-19 13:45:02 +0200 | [diff] [blame] | 326 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
| 327 | { |
Andrey Andreev | d60e51b | 2017-02-06 10:28:36 +0200 | [diff] [blame] | 328 | $_mimes = array_merge($_mimes, include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')); |
vlakoff | 69550c5 | 2014-05-19 13:45:02 +0200 | [diff] [blame] | 329 | } |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | return $_mimes; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // ------------------------------------------------------------------------ |
| 337 | |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 338 | if ( ! function_exists('is_https')) |
| 339 | { |
| 340 | /** |
| 341 | * Is HTTPS? |
| 342 | * |
| 343 | * Determines if the application is accessed via an encrypted |
| 344 | * (HTTPS) connection. |
| 345 | * |
| 346 | * @return bool |
| 347 | */ |
| 348 | function is_https() |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 349 | { |
Andrey Andreev | 333b80e | 2013-07-01 16:21:54 +0300 | [diff] [blame] | 350 | if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 351 | { |
| 352 | return TRUE; |
| 353 | } |
Andrey Andreev | 85f3d1a | 2016-07-25 10:28:21 +0300 | [diff] [blame] | 354 | elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 355 | { |
| 356 | return TRUE; |
| 357 | } |
Andrey Andreev | 333b80e | 2013-07-01 16:21:54 +0300 | [diff] [blame] | 358 | 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] | 359 | { |
| 360 | return TRUE; |
| 361 | } |
Richard Deurwaarder (Xeli) | 9899997 | 2013-06-24 15:19:30 +0200 | [diff] [blame] | 362 | |
Richard Deurwaarder (Xeli) | 7cc2945 | 2013-06-24 15:06:19 +0200 | [diff] [blame] | 363 | return FALSE; |
Richard Deurwaarder (Xeli) | 23dc052 | 2013-06-24 14:52:47 +0200 | [diff] [blame] | 364 | } |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // ------------------------------------------------------------------------ |
| 368 | |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 369 | if ( ! function_exists('is_cli')) |
| 370 | { |
| 371 | |
| 372 | /** |
| 373 | * Is CLI? |
| 374 | * |
| 375 | * Test to see if a request was made from the command line. |
| 376 | * |
| 377 | * @return bool |
| 378 | */ |
| 379 | function is_cli() |
| 380 | { |
| 381 | return (PHP_SAPI === 'cli' OR defined('STDIN')); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // ------------------------------------------------------------------------ |
| 386 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 387 | if ( ! function_exists('show_error')) |
| 388 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 389 | /** |
| 390 | * Error Handler |
| 391 | * |
| 392 | * This function lets us invoke the exception class and |
| 393 | * display errors using the standard error template located |
vlakoff | 52301c7 | 2013-03-29 14:23:34 +0100 | [diff] [blame] | 394 | * in application/views/errors/error_general.php |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 395 | * This function will send the error page directly to the |
| 396 | * browser and exit. |
| 397 | * |
| 398 | * @param string |
| 399 | * @param int |
| 400 | * @param string |
| 401 | * @return void |
| 402 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 403 | function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') |
| 404 | { |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 405 | $status_code = abs($status_code); |
| 406 | if ($status_code < 100) |
| 407 | { |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 408 | $exit_status = $status_code + 9; // 9 is EXIT__AUTO_MIN |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 409 | $status_code = 500; |
| 410 | } |
| 411 | else |
| 412 | { |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 413 | $exit_status = 1; // EXIT_ERROR |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 414 | } |
Andrey Andreev | 5a6814e | 2013-03-04 15:44:12 +0200 | [diff] [blame] | 415 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 416 | $_error =& load_class('Exceptions', 'core'); |
| 417 | echo $_error->show_error($heading, $message, 'error_general', $status_code); |
Daniel Hunsaker | 353f983 | 2013-01-24 17:09:10 -0700 | [diff] [blame] | 418 | exit($exit_status); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 419 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 420 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 422 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 424 | if ( ! function_exists('show_404')) |
| 425 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 426 | /** |
| 427 | * 404 Page Handler |
| 428 | * |
| 429 | * This function is similar to the show_error() function above |
| 430 | * However, instead of the standard error template it displays |
| 431 | * 404 errors. |
| 432 | * |
| 433 | * @param string |
| 434 | * @param bool |
| 435 | * @return void |
| 436 | */ |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 437 | function show_404($page = '', $log_error = TRUE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 438 | { |
| 439 | $_error =& load_class('Exceptions', 'core'); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 440 | $_error->show_404($page, $log_error); |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 441 | exit(4); // EXIT_UNKNOWN_FILE |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 442 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 443 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 445 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 447 | if ( ! function_exists('log_message')) |
| 448 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 449 | /** |
| 450 | * Error Logging Interface |
| 451 | * |
| 452 | * We use this as a simple mechanism to access the logging |
| 453 | * class and send messages to be logged. |
| 454 | * |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 455 | * @param string the error level: 'error', 'debug' or 'info' |
| 456 | * @param string the error message |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 457 | * @return void |
| 458 | */ |
Andrey Andreev | 838c9a9 | 2013-09-13 14:05:13 +0300 | [diff] [blame] | 459 | function log_message($level, $message) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | { |
Andrey Andreev | 2f8d2d3 | 2013-08-07 15:54:47 +0300 | [diff] [blame] | 461 | static $_log; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | |
Andrey Andreev | 49890a9 | 2013-08-19 19:56:18 +0300 | [diff] [blame] | 463 | if ($_log === NULL) |
Ted Wood | b19a203 | 2013-01-05 16:02:43 -0800 | [diff] [blame] | 464 | { |
vlakoff | 61f1aa0 | 2013-08-07 11:29:17 +0200 | [diff] [blame] | 465 | // references cannot be directly assigned to static variables, so we use an array |
| 466 | $_log[0] =& load_class('Log', 'core'); |
Ted Wood | b19a203 | 2013-01-05 16:02:43 -0800 | [diff] [blame] | 467 | } |
Andrey Andreev | a107a0f | 2013-02-15 22:30:31 +0200 | [diff] [blame] | 468 | |
Andrey Andreev | 838c9a9 | 2013-09-13 14:05:13 +0300 | [diff] [blame] | 469 | $_log[0]->write_log($level, $message); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 471 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 473 | // ------------------------------------------------------------------------ |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 474 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 475 | if ( ! function_exists('set_status_header')) |
| 476 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 477 | /** |
| 478 | * Set HTTP Status Header |
| 479 | * |
| 480 | * @param int the status code |
| 481 | * @param string |
| 482 | * @return void |
| 483 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 484 | function set_status_header($code = 200, $text = '') |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 485 | { |
Andrey Andreev | c545c01 | 2015-02-19 11:36:10 +0200 | [diff] [blame] | 486 | if (is_cli()) |
| 487 | { |
| 488 | return; |
| 489 | } |
| 490 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 491 | if (empty($code) OR ! is_numeric($code)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 492 | { |
| 493 | show_error('Status codes must be numeric', 500); |
| 494 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 496 | if (empty($text)) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 497 | { |
Fieah | b4ebb39 | 2015-02-22 23:55:15 +0800 | [diff] [blame] | 498 | is_int($code) OR $code = (int) $code; |
| 499 | $stati = array( |
ftwbzhao | 2b7a97c | 2015-04-14 11:19:38 +0800 | [diff] [blame] | 500 | 100 => 'Continue', |
| 501 | 101 => 'Switching Protocols', |
| 502 | |
Fieah | b4ebb39 | 2015-02-22 23:55:15 +0800 | [diff] [blame] | 503 | 200 => 'OK', |
| 504 | 201 => 'Created', |
| 505 | 202 => 'Accepted', |
| 506 | 203 => 'Non-Authoritative Information', |
| 507 | 204 => 'No Content', |
| 508 | 205 => 'Reset Content', |
| 509 | 206 => 'Partial Content', |
| 510 | |
| 511 | 300 => 'Multiple Choices', |
| 512 | 301 => 'Moved Permanently', |
| 513 | 302 => 'Found', |
| 514 | 303 => 'See Other', |
| 515 | 304 => 'Not Modified', |
| 516 | 305 => 'Use Proxy', |
| 517 | 307 => 'Temporary Redirect', |
| 518 | |
| 519 | 400 => 'Bad Request', |
| 520 | 401 => 'Unauthorized', |
ftwbzhao | 2b7a97c | 2015-04-14 11:19:38 +0800 | [diff] [blame] | 521 | 402 => 'Payment Required', |
Fieah | b4ebb39 | 2015-02-22 23:55:15 +0800 | [diff] [blame] | 522 | 403 => 'Forbidden', |
| 523 | 404 => 'Not Found', |
| 524 | 405 => 'Method Not Allowed', |
| 525 | 406 => 'Not Acceptable', |
| 526 | 407 => 'Proxy Authentication Required', |
| 527 | 408 => 'Request Timeout', |
| 528 | 409 => 'Conflict', |
| 529 | 410 => 'Gone', |
| 530 | 411 => 'Length Required', |
| 531 | 412 => 'Precondition Failed', |
| 532 | 413 => 'Request Entity Too Large', |
| 533 | 414 => 'Request-URI Too Long', |
| 534 | 415 => 'Unsupported Media Type', |
| 535 | 416 => 'Requested Range Not Satisfiable', |
| 536 | 417 => 'Expectation Failed', |
| 537 | 422 => 'Unprocessable Entity', |
Andrey Andreev | f2f6d8a | 2016-10-11 16:00:57 +0300 | [diff] [blame] | 538 | 426 => 'Upgrade Required', |
| 539 | 428 => 'Precondition Required', |
| 540 | 429 => 'Too Many Requests', |
| 541 | 431 => 'Request Header Fields Too Large', |
Fieah | b4ebb39 | 2015-02-22 23:55:15 +0800 | [diff] [blame] | 542 | |
| 543 | 500 => 'Internal Server Error', |
| 544 | 501 => 'Not Implemented', |
| 545 | 502 => 'Bad Gateway', |
| 546 | 503 => 'Service Unavailable', |
| 547 | 504 => 'Gateway Timeout', |
Andrey Andreev | f2f6d8a | 2016-10-11 16:00:57 +0300 | [diff] [blame] | 548 | 505 => 'HTTP Version Not Supported', |
| 549 | 511 => 'Network Authentication Required', |
Fieah | b4ebb39 | 2015-02-22 23:55:15 +0800 | [diff] [blame] | 550 | ); |
| 551 | |
Andrey Andreev | 51d6d84 | 2012-06-15 16:41:09 +0300 | [diff] [blame] | 552 | if (isset($stati[$code])) |
| 553 | { |
| 554 | $text = $stati[$code]; |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | show_error('No status text available. Please check your status code number or supply your own message text.', 500); |
| 559 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 560 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 561 | |
vlakoff | 40d1249 | 2013-08-06 14:46:00 +0200 | [diff] [blame] | 562 | if (strpos(PHP_SAPI, 'cgi') === 0) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 563 | { |
Daniel Hunsaker | 8626e93 | 2013-03-04 05:14:22 -0700 | [diff] [blame] | 564 | header('Status: '.$code.' '.$text, TRUE); |
Andrey Andreev | df33ec2 | 2017-03-20 17:43:58 +0200 | [diff] [blame] | 565 | return; |
Daniel Hunsaker | 8626e93 | 2013-03-04 05:14:22 -0700 | [diff] [blame] | 566 | } |
Andrey Andreev | df33ec2 | 2017-03-20 17:43:58 +0200 | [diff] [blame] | 567 | |
| 568 | $server_protocol = (isset($_SERVER['SERVER_PROTOCOL']) && in_array($_SERVER['SERVER_PROTOCOL'], array('HTTP/1.0', 'HTTP/1.1', 'HTTP/2'), TRUE)) |
| 569 | ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
| 570 | header($server_protocol.' '.$code.' '.$text, TRUE, $code); |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 571 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 572 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 573 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 574 | // -------------------------------------------------------------------- |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 575 | |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 576 | if ( ! function_exists('_error_handler')) |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 577 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 578 | /** |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 579 | * Error Handler |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 580 | * |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 581 | * This is the custom error handler that is declared at the (relative) |
| 582 | * top of CodeIgniter.php. The main reason we use this is to permit |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 583 | * PHP errors to be logged in our own log files since the user may |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 584 | * not have access to server logs. Since this function effectively |
| 585 | * intercepts PHP errors, however, we also need to display errors |
| 586 | * based on the current error_reporting level. |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 587 | * We do that with the use of a PHP error template. |
| 588 | * |
Andrey Andreev | 0850a28 | 2013-10-21 14:26:18 +0300 | [diff] [blame] | 589 | * @param int $severity |
| 590 | * @param string $message |
| 591 | * @param string $filepath |
| 592 | * @param int $line |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 593 | * @return void |
| 594 | */ |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 595 | function _error_handler($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 596 | { |
Andrey Andreev | c114deb | 2016-08-19 19:17:59 +0300 | [diff] [blame] | 597 | $is_error = (((E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) === $severity); |
Jesse van Assen | 7eb116a | 2013-07-06 10:42:14 +0200 | [diff] [blame] | 598 | |
| 599 | // When an error occurred, set the status header to '500 Internal Server Error' |
| 600 | // to indicate to the client something went wrong. |
| 601 | // This can't be done within the $_error->show_php_error method because |
| 602 | // it is only called when the display_errors flag is set (which isn't usually |
| 603 | // the case in a production environment) or when errors are ignored because |
| 604 | // they are above the error_reporting threshold. |
| 605 | if ($is_error) |
| 606 | { |
| 607 | set_status_header(500); |
Andrey Andreev | 0254589 | 2014-02-19 23:49:31 +0200 | [diff] [blame] | 608 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 609 | |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 610 | // Should we ignore the error? We'll get the current error_reporting |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 611 | // level and add its bits with the severity bits to find out. |
Francesco Negri | 0e0c37b | 2012-08-04 14:16:50 +0300 | [diff] [blame] | 612 | if (($severity & error_reporting()) !== $severity) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 613 | { |
| 614 | return; |
| 615 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 616 | |
Cristian Kocza | 2be2744 | 2014-02-19 21:52:38 +0200 | [diff] [blame] | 617 | $_error =& load_class('Exceptions', 'core'); |
Andrey Andreev | 76160bc | 2014-01-30 22:26:14 +0200 | [diff] [blame] | 618 | $_error->log_exception($severity, $message, $filepath, $line); |
| 619 | |
Francesco Negri | 312bdc5 | 2012-08-04 07:32:19 -0400 | [diff] [blame] | 620 | // Should we display the error? |
Andrey Andreev | 3866666 | 2015-01-13 15:53:25 +0200 | [diff] [blame] | 621 | if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | { |
| 623 | $_error->show_php_error($severity, $message, $filepath, $line); |
| 624 | } |
| 625 | |
Jesse van Assen | 7eb116a | 2013-07-06 10:42:14 +0200 | [diff] [blame] | 626 | // If the error is fatal, the execution of the script should be stopped because |
| 627 | // errors can't be recovered from. Halting the script conforms with PHP's |
| 628 | // default error handling. See http://www.php.net/manual/en/errorfunc.constants.php |
| 629 | if ($is_error) |
| 630 | { |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 631 | exit(1); // EXIT_ERROR |
Jesse van Assen | 7eb116a | 2013-07-06 10:42:14 +0200 | [diff] [blame] | 632 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 633 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 634 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 635 | |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 636 | // ------------------------------------------------------------------------ |
| 637 | |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 638 | if ( ! function_exists('_exception_handler')) |
| 639 | { |
| 640 | /** |
| 641 | * Exception Handler |
| 642 | * |
| 643 | * Sends uncaught exceptions to the logger and displays them |
| 644 | * only if display_errors is On so that they don't show up in |
| 645 | * production environments. |
| 646 | * |
| 647 | * @param Exception $exception |
| 648 | * @return void |
| 649 | */ |
| 650 | function _exception_handler($exception) |
| 651 | { |
| 652 | $_error =& load_class('Exceptions', 'core'); |
| 653 | $_error->log_exception('error', 'Exception: '.$exception->getMessage(), $exception->getFile(), $exception->getLine()); |
| 654 | |
Andrey Andreev | 4ffe634 | 2016-10-21 16:30:31 +0300 | [diff] [blame] | 655 | is_cli() OR set_status_header(500); |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 656 | // Should we display the error? |
Andrey Andreev | 3866666 | 2015-01-13 15:53:25 +0200 | [diff] [blame] | 657 | if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) |
Andrey Andreev | 4b838af | 2014-10-28 23:46:45 +0200 | [diff] [blame] | 658 | { |
| 659 | $_error->show_exception($exception); |
| 660 | } |
| 661 | |
| 662 | exit(1); // EXIT_ERROR |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // ------------------------------------------------------------------------ |
| 667 | |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 668 | if ( ! function_exists('_shutdown_handler')) |
| 669 | { |
| 670 | /** |
| 671 | * Shutdown Handler |
| 672 | * |
| 673 | * This is the shutdown handler that is declared at the top |
| 674 | * of CodeIgniter.php. The main reason we use this is to simulate |
| 675 | * a complete custom exception handler. |
| 676 | * |
Claudio Galdiolo | 62fe011 | 2015-08-13 10:21:42 -0400 | [diff] [blame] | 677 | * E_STRICT is purposively neglected because such events may have |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 678 | * been caught. Duplication or none? None is preferred for now. |
| 679 | * |
| 680 | * @link http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a |
| 681 | * @return void |
| 682 | */ |
| 683 | function _shutdown_handler() |
| 684 | { |
Andrey Andreev | afca352 | 2013-11-14 15:26:59 +0200 | [diff] [blame] | 685 | $last_error = error_get_last(); |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 686 | if (isset($last_error) && |
| 687 | ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) |
| 688 | { |
Andrey Andreev | a0471dc | 2014-11-04 19:22:38 +0200 | [diff] [blame] | 689 | _error_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']); |
Kaiwang Chen | 21fe9da | 2013-09-11 13:09:41 +0800 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 694 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 695 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 696 | if ( ! function_exists('remove_invisible_characters')) |
| 697 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 698 | /** |
| 699 | * Remove Invisible Characters |
| 700 | * |
| 701 | * This prevents sandwiching null characters |
| 702 | * between ascii characters, like Java\0script. |
| 703 | * |
| 704 | * @param string |
| 705 | * @param bool |
| 706 | * @return string |
| 707 | */ |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 708 | function remove_invisible_characters($str, $url_encoded = TRUE) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 709 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 710 | $non_displayables = array(); |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 711 | |
| 712 | // every control character except newline (dec 10), |
| 713 | // carriage return (dec 13) and horizontal tab (dec 09) |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 714 | if ($url_encoded) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 715 | { |
Andrey Andreev | 384a461 | 2016-07-25 10:30:04 +0300 | [diff] [blame] | 716 | $non_displayables[] = '/%0[0-8bcef]/i'; // url encoded 00-08, 11, 12, 14, 15 |
| 717 | $non_displayables[] = '/%1[0-9a-f]/i'; // url encoded 16-31 |
Andrey Andreev | d60e51b | 2017-02-06 10:28:36 +0200 | [diff] [blame] | 718 | $non_displayables[] = '/%7f/i'; // url encoded 127 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 719 | } |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 720 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 721 | $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] | 722 | |
| 723 | do |
| 724 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 725 | $str = preg_replace($non_displayables, '', $str, -1, $count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 726 | } |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 727 | while ($count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 728 | |
| 729 | return $str; |
| 730 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 731 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 732 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 733 | // ------------------------------------------------------------------------ |
| 734 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 735 | if ( ! function_exists('html_escape')) |
| 736 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 737 | /** |
Ivan Tcholakov | 4f45858 | 2014-08-25 11:20:22 +0300 | [diff] [blame] | 738 | * Returns HTML escaped variable. |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 739 | * |
Ivan Tcholakov | 993f98c | 2014-08-25 12:13:31 +0300 | [diff] [blame] | 740 | * @param mixed $var The input string or array of strings to be escaped. |
| 741 | * @param bool $double_encode $double_encode set to FALSE prevents escaping twice. |
| 742 | * @return mixed The escaped string or array of strings as a result. |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 743 | */ |
Ivan Tcholakov | 4f45858 | 2014-08-25 11:20:22 +0300 | [diff] [blame] | 744 | function html_escape($var, $double_encode = TRUE) |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 745 | { |
Joshua Logsdon | 993e340 | 2015-03-25 12:03:43 -0400 | [diff] [blame] | 746 | if (empty($var)) |
| 747 | { |
| 748 | return $var; |
| 749 | } |
Andrey Andreev | 6b620fb | 2015-04-20 12:46:46 +0300 | [diff] [blame] | 750 | |
Ivan Tcholakov | 6222437 | 2014-08-25 15:48:33 +0300 | [diff] [blame] | 751 | if (is_array($var)) |
| 752 | { |
Andrey Andreev | d2ea460 | 2015-10-30 11:47:35 +0200 | [diff] [blame] | 753 | foreach (array_keys($var) as $key) |
| 754 | { |
| 755 | $var[$key] = html_escape($var[$key], $double_encode); |
| 756 | } |
| 757 | |
| 758 | return $var; |
Ivan Tcholakov | 6222437 | 2014-08-25 15:48:33 +0300 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | return htmlspecialchars($var, ENT_QUOTES, config_item('charset'), $double_encode); |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 762 | } |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 763 | } |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 764 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 765 | // ------------------------------------------------------------------------ |
| 766 | |
| 767 | if ( ! function_exists('_stringify_attributes')) |
| 768 | { |
| 769 | /** |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 770 | * Stringify attributes for use in HTML tags. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 771 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 772 | * Helper function used to convert a string, array, or object |
| 773 | * of attributes to a string. |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 774 | * |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 775 | * @param mixed string, array, object |
| 776 | * @param bool |
| 777 | * @return string |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 778 | */ |
| 779 | function _stringify_attributes($attributes, $js = FALSE) |
| 780 | { |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 781 | $atts = NULL; |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 782 | |
| 783 | if (empty($attributes)) |
| 784 | { |
| 785 | return $atts; |
| 786 | } |
| 787 | |
| 788 | if (is_string($attributes)) |
| 789 | { |
| 790 | return ' '.$attributes; |
| 791 | } |
| 792 | |
| 793 | $attributes = (array) $attributes; |
| 794 | |
| 795 | foreach ($attributes as $key => $val) |
| 796 | { |
| 797 | $atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"'; |
| 798 | } |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 799 | |
Chad Furman | a1abada | 2012-07-29 01:03:50 -0400 | [diff] [blame] | 800 | return rtrim($atts, ','); |
| 801 | } |
| 802 | } |
| 803 | |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 804 | // ------------------------------------------------------------------------ |
| 805 | |
| 806 | if ( ! function_exists('function_usable')) |
| 807 | { |
| 808 | /** |
| 809 | * Function usable |
| 810 | * |
| 811 | * Executes a function_exists() check, and if the Suhosin PHP |
| 812 | * extension is loaded - checks whether the function that is |
| 813 | * checked might be disabled in there as well. |
| 814 | * |
| 815 | * This is useful as function_exists() will return FALSE for |
| 816 | * functions disabled via the *disable_functions* php.ini |
| 817 | * setting, but not for *suhosin.executor.func.blacklist* and |
| 818 | * *suhosin.executor.disable_eval*. These settings will just |
| 819 | * terminate script execution if a disabled function is executed. |
| 820 | * |
Andrey Andreev | aaa8ddb | 2014-02-03 14:10:44 +0200 | [diff] [blame] | 821 | * The above described behavior turned out to be a bug in Suhosin, |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 822 | * but even though a fix was committed for 0.9.34 on 2012-02-12, |
Andrey Andreev | aaa8ddb | 2014-02-03 14:10:44 +0200 | [diff] [blame] | 823 | * that version is yet to be released. This function will therefore |
| 824 | * be just temporary, but would probably be kept for a few years. |
| 825 | * |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 826 | * @link http://www.hardened-php.net/suhosin/ |
| 827 | * @param string $function_name Function to check for |
| 828 | * @return bool TRUE if the function exists and is safe to call, |
| 829 | * FALSE otherwise. |
| 830 | */ |
| 831 | function function_usable($function_name) |
| 832 | { |
| 833 | static $_suhosin_func_blacklist; |
| 834 | |
| 835 | if (function_exists($function_name)) |
| 836 | { |
| 837 | if ( ! isset($_suhosin_func_blacklist)) |
| 838 | { |
Andrey Andreev | 9e25daf | 2015-07-22 13:50:30 +0300 | [diff] [blame] | 839 | $_suhosin_func_blacklist = extension_loaded('suhosin') |
| 840 | ? explode(',', trim(ini_get('suhosin.executor.func.blacklist'))) |
| 841 | : array(); |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Andrey Andreev | ae63462 | 2012-12-17 10:30:18 +0200 | [diff] [blame] | 844 | return ! in_array($function_name, $_suhosin_func_blacklist, TRUE); |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | return FALSE; |
| 848 | } |
| 849 | } |