Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, 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 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 29 | * Common Functions |
| 30 | * |
| 31 | * Loads the base classes and executes the request. |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage codeigniter |
| 35 | * @category Common Functions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/ |
| 38 | */ |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 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 | * |
| 48 | * Since there are a few places where we conditionally test for PHP > 5 |
| 49 | * we'll set a static variable. |
| 50 | * |
| 51 | * @param string |
| 52 | * @return bool TRUE if the current version is $version or higher |
| 53 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 54 | function is_php($version = '5.0.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 | { |
| 61 | $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE; |
| 62 | } |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 63 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 64 | return $_is_php[$version]; |
| 65 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 66 | } |
Derek Jones | 5bcfd2e | 2009-07-28 14:42:42 +0000 | [diff] [blame] | 67 | |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 68 | // ------------------------------------------------------------------------ |
| 69 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 70 | if ( ! function_exists('is_really_writable')) |
| 71 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 72 | /** |
| 73 | * Tests for file writability |
| 74 | * |
| 75 | * is_writable() returns TRUE on Windows servers when you really can't write to |
| 76 | * the file, based on the read-only attribute. is_writable() is also unreliable |
| 77 | * on Unix servers if safe_mode is on. |
| 78 | * |
| 79 | * @param string |
| 80 | * @return void |
| 81 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 82 | function is_really_writable($file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 84 | // If we're on a Unix server with safe_mode off we call is_writable |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 85 | if (DIRECTORY_SEPARATOR === '/' && (bool) @ini_get('safe_mode') === FALSE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 86 | { |
| 87 | return is_writable($file); |
| 88 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 90 | /* For Windows servers and safe_mode "on" installations we'll actually |
| 91 | * write a file then read it. Bah... |
| 92 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 93 | if (is_dir($file)) |
| 94 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 95 | $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100)); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 96 | if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) |
| 97 | { |
| 98 | return FALSE; |
| 99 | } |
| 100 | |
| 101 | fclose($fp); |
| 102 | @chmod($file, DIR_WRITE_MODE); |
| 103 | @unlink($file); |
| 104 | return TRUE; |
| 105 | } |
Eric Barnes | 1508301 | 2011-03-21 22:13:12 -0400 | [diff] [blame] | 106 | elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | { |
| 108 | return FALSE; |
| 109 | } |
| 110 | |
| 111 | fclose($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | return TRUE; |
| 113 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 114 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | |
| 116 | // ------------------------------------------------------------------------ |
| 117 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 118 | if ( ! function_exists('load_class')) |
| 119 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 120 | /** |
| 121 | * Class registry |
| 122 | * |
| 123 | * This function acts as a singleton. If the requested class does not |
| 124 | * exist it is instantiated and set to a static variable. If it has |
| 125 | * previously been instantiated the variable is returned. |
| 126 | * |
| 127 | * @param string the class name being requested |
| 128 | * @param string the directory where the class should be found |
| 129 | * @param string the class name prefix |
| 130 | * @return object |
| 131 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 132 | function &load_class($class, $directory = 'libraries', $prefix = 'CI_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 134 | static $_classes = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 136 | // Does the class exist? If so, we're done... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 137 | if (isset($_classes[$class])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 139 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 141 | |
| 142 | $name = FALSE; |
| 143 | |
Shane Pearson | ab57a35 | 2011-08-22 16:11:20 -0500 | [diff] [blame] | 144 | // Look for the class first in the local application/libraries folder |
| 145 | // then in the native system/libraries folder |
| 146 | foreach (array(APPPATH, BASEPATH) as $path) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 148 | if (file_exists($path.$directory.'/'.$class.'.php')) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 149 | { |
| 150 | $name = $prefix.$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 152 | if (class_exists($name) === FALSE) |
| 153 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 154 | require($path.$directory.'/'.$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 161 | // Is the request a class extension? If so we load it too |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 162 | if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 164 | $name = config_item('subclass_prefix').$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 166 | if (class_exists($name) === FALSE) |
| 167 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 168 | require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // Did we find the class? |
| 173 | if ($name === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | // Note: We use exit() rather then show_error() in order to avoid a |
| 176 | // self-referencing loop with the Excptions class |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 177 | set_status_header(503); |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 178 | exit('Unable to locate the specified class: '.$class.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 180 | |
| 181 | // Keep track of what we just loaded |
| 182 | is_loaded($class); |
| 183 | |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 184 | $_classes[$class] = new $name(); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 185 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 187 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 189 | // -------------------------------------------------------------------- |
| 190 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 191 | if ( ! function_exists('is_loaded')) |
| 192 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 193 | /** |
| 194 | * Keeps track of which libraries have been loaded. This function is |
| 195 | * called by the load_class() function above |
| 196 | * |
| 197 | * @param string |
| 198 | * @return array |
| 199 | */ |
Andrey Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 200 | function &is_loaded($class = '') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 201 | { |
| 202 | static $_is_loaded = array(); |
| 203 | |
| 204 | if ($class != '') |
| 205 | { |
| 206 | $_is_loaded[strtolower($class)] = $class; |
| 207 | } |
| 208 | |
| 209 | return $_is_loaded; |
| 210 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 211 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 212 | |
| 213 | // ------------------------------------------------------------------------ |
Derek Jones | f0a9b33 | 2009-07-29 14:19:18 +0000 | [diff] [blame] | 214 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 215 | if ( ! function_exists('get_config')) |
| 216 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 217 | /** |
| 218 | * Loads the main config.php file |
| 219 | * |
| 220 | * This function lets us grab the config file even if the Config class |
| 221 | * hasn't been instantiated yet |
| 222 | * |
| 223 | * @param array |
| 224 | * @return array |
| 225 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 226 | function &get_config($replace = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 228 | static $_config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 230 | if (isset($_config)) |
| 231 | { |
| 232 | return $_config[0]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 234 | |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 235 | // Is the config file in the environment folder? |
Michiel Vugteveen | 0609d58 | 2012-01-08 13:26:17 +0100 | [diff] [blame] | 236 | if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php')) |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 237 | { |
Andrey Andreev | 536b771 | 2012-01-07 21:31:25 +0200 | [diff] [blame] | 238 | $file_path = APPPATH.'config/config.php'; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 239 | } |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 240 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 241 | // Fetch the config file |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 242 | if ( ! file_exists($file_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | { |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 244 | set_status_header(503); |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 245 | exit('The configuration file does not exist.'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 246 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 247 | |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 248 | require($file_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 250 | // Does the $config array exist in the file? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | if ( ! isset($config) OR ! is_array($config)) |
| 252 | { |
Kevin Cupp | d63e401 | 2012-02-05 14:14:32 -0500 | [diff] [blame] | 253 | set_status_header(503); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | exit('Your config file does not appear to be formatted correctly.'); |
| 255 | } |
| 256 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 257 | // Are any values being dynamically replaced? |
| 258 | if (count($replace) > 0) |
| 259 | { |
| 260 | foreach ($replace as $key => $val) |
| 261 | { |
| 262 | if (isset($config[$key])) |
| 263 | { |
| 264 | $config[$key] = $val; |
| 265 | } |
| 266 | } |
| 267 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 268 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 269 | return $_config[0] =& $config; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 271 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 272 | |
| 273 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 275 | if ( ! function_exists('config_item')) |
| 276 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 277 | /** |
| 278 | * Returns the specified config item |
| 279 | * |
| 280 | * @param string |
| 281 | * @return mixed |
| 282 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 283 | function config_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 285 | static $_config_item = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 286 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 287 | if ( ! isset($_config_item[$item])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 289 | $config =& get_config(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 291 | if ( ! isset($config[$item])) |
| 292 | { |
| 293 | return FALSE; |
| 294 | } |
| 295 | $_config_item[$item] = $config[$item]; |
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 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 298 | return $_config_item[$item]; |
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 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 304 | if ( ! function_exists('show_error')) |
| 305 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 306 | /** |
| 307 | * Error Handler |
| 308 | * |
| 309 | * This function lets us invoke the exception class and |
| 310 | * display errors using the standard error template located |
| 311 | * in application/errors/errors.php |
| 312 | * This function will send the error page directly to the |
| 313 | * browser and exit. |
| 314 | * |
| 315 | * @param string |
| 316 | * @param int |
| 317 | * @param string |
| 318 | * @return void |
| 319 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 320 | function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') |
| 321 | { |
| 322 | $_error =& load_class('Exceptions', 'core'); |
| 323 | echo $_error->show_error($heading, $message, 'error_general', $status_code); |
| 324 | exit; |
| 325 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 326 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 328 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 330 | if ( ! function_exists('show_404')) |
| 331 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 332 | /** |
| 333 | * 404 Page Handler |
| 334 | * |
| 335 | * This function is similar to the show_error() function above |
| 336 | * However, instead of the standard error template it displays |
| 337 | * 404 errors. |
| 338 | * |
| 339 | * @param string |
| 340 | * @param bool |
| 341 | * @return void |
| 342 | */ |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 343 | function show_404($page = '', $log_error = TRUE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 344 | { |
| 345 | $_error =& load_class('Exceptions', 'core'); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 346 | $_error->show_404($page, $log_error); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 347 | exit; |
| 348 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 349 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 351 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 353 | if ( ! function_exists('log_message')) |
| 354 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 355 | /** |
| 356 | * Error Logging Interface |
| 357 | * |
| 358 | * We use this as a simple mechanism to access the logging |
| 359 | * class and send messages to be logged. |
| 360 | * |
| 361 | * @param string |
| 362 | * @param string |
| 363 | * @param bool |
| 364 | * @return void |
| 365 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 366 | function log_message($level = 'error', $message, $php_error = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 368 | static $_log; |
| 369 | |
| 370 | if (config_item('log_threshold') == 0) |
| 371 | { |
| 372 | return; |
| 373 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 375 | $_log =& load_class('Log'); |
| 376 | $_log->write_log($level, $message, $php_error); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 378 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 380 | // ------------------------------------------------------------------------ |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 381 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 382 | if ( ! function_exists('set_status_header')) |
| 383 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 384 | /** |
| 385 | * Set HTTP Status Header |
| 386 | * |
| 387 | * @param int the status code |
| 388 | * @param string |
| 389 | * @return void |
| 390 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 391 | function set_status_header($code = 200, $text = '') |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 392 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 393 | $stati = array( |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 394 | 200 => 'OK', |
| 395 | 201 => 'Created', |
| 396 | 202 => 'Accepted', |
| 397 | 203 => 'Non-Authoritative Information', |
| 398 | 204 => 'No Content', |
| 399 | 205 => 'Reset Content', |
| 400 | 206 => 'Partial Content', |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 401 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 402 | 300 => 'Multiple Choices', |
| 403 | 301 => 'Moved Permanently', |
| 404 | 302 => 'Found', |
| 405 | 304 => 'Not Modified', |
| 406 | 305 => 'Use Proxy', |
| 407 | 307 => 'Temporary Redirect', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 408 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 409 | 400 => 'Bad Request', |
| 410 | 401 => 'Unauthorized', |
| 411 | 403 => 'Forbidden', |
| 412 | 404 => 'Not Found', |
| 413 | 405 => 'Method Not Allowed', |
| 414 | 406 => 'Not Acceptable', |
| 415 | 407 => 'Proxy Authentication Required', |
| 416 | 408 => 'Request Timeout', |
| 417 | 409 => 'Conflict', |
| 418 | 410 => 'Gone', |
| 419 | 411 => 'Length Required', |
| 420 | 412 => 'Precondition Failed', |
| 421 | 413 => 'Request Entity Too Large', |
| 422 | 414 => 'Request-URI Too Long', |
| 423 | 415 => 'Unsupported Media Type', |
| 424 | 416 => 'Requested Range Not Satisfiable', |
| 425 | 417 => 'Expectation Failed', |
| 426 | 422 => 'Unprocessable Entity', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 427 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 428 | 500 => 'Internal Server Error', |
| 429 | 501 => 'Not Implemented', |
| 430 | 502 => 'Bad Gateway', |
| 431 | 503 => 'Service Unavailable', |
| 432 | 504 => 'Gateway Timeout', |
| 433 | 505 => 'HTTP Version Not Supported' |
| 434 | ); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 435 | |
| 436 | if ($code == '' OR ! is_numeric($code)) |
| 437 | { |
| 438 | show_error('Status codes must be numeric', 500); |
| 439 | } |
| 440 | |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 441 | if (isset($stati[$code]) && $text == '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 443 | $text = $stati[$code]; |
| 444 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 446 | if ($text == '') |
| 447 | { |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 448 | show_error('No status text available. Please check your status code number or supply your own message text.', 500); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 449 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 450 | |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 451 | $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE; |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 452 | |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 453 | if (strpos(php_sapi_name(), 'cgi') === 0) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 454 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 455 | header('Status: '.$code.' '.$text, TRUE); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 456 | } |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 457 | elseif ($server_protocol === 'HTTP/1.0') |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 458 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 459 | header('HTTP/1.0 '.$code.' '.$text, TRUE, $code); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 460 | } |
| 461 | else |
| 462 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 463 | header('HTTP/1.1 '.$code.' '.$text, TRUE, $code); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 464 | } |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 465 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 466 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 468 | // -------------------------------------------------------------------- |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 469 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 470 | if ( ! function_exists('_exception_handler')) |
| 471 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 472 | /** |
| 473 | * Exception Handler |
| 474 | * |
| 475 | * This is the custom exception handler that is declaired at the top |
| 476 | * of Codeigniter.php. The main reason we use this is to permit |
| 477 | * PHP errors to be logged in our own log files since the user may |
| 478 | * not have access to server logs. Since this function |
| 479 | * effectively intercepts PHP errors, however, we also need |
| 480 | * to display errors based on the current error_reporting level. |
| 481 | * We do that with the use of a PHP error template. |
| 482 | * |
| 483 | * @param int |
| 484 | * @param string |
| 485 | * @param string |
| 486 | * @param int |
| 487 | * @return void |
| 488 | */ |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 489 | function _exception_handler($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 491 | // We don't bother with "strict" notices since they tend to fill up |
| 492 | // the log file with excess information that isn't normally very helpful. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 493 | // For example, if you are running PHP 5 and you use version 4 style |
| 494 | // class functions (without prefixes like "public", "private", etc.) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 495 | // you'll get notices telling you that these have been deprecated. |
| 496 | if ($severity == E_STRICT) |
| 497 | { |
| 498 | return; |
| 499 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 501 | $_error =& load_class('Exceptions', 'core'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 502 | |
| 503 | // Should we display the error? We'll get the current error_reporting |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 504 | // level and add its bits with the severity bits to find out. |
| 505 | if (($severity & error_reporting()) == $severity) |
| 506 | { |
| 507 | $_error->show_php_error($severity, $message, $filepath, $line); |
| 508 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 510 | // Should we log the error? No? We're done... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 511 | if (config_item('log_threshold') == 0) |
| 512 | { |
| 513 | return; |
| 514 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 515 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 516 | $_error->log_exception($severity, $message, $filepath, $line); |
| 517 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 518 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 519 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 520 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 522 | if ( ! function_exists('remove_invisible_characters')) |
| 523 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 524 | /** |
| 525 | * Remove Invisible Characters |
| 526 | * |
| 527 | * This prevents sandwiching null characters |
| 528 | * between ascii characters, like Java\0script. |
| 529 | * |
| 530 | * @param string |
| 531 | * @param bool |
| 532 | * @return string |
| 533 | */ |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 534 | function remove_invisible_characters($str, $url_encoded = TRUE) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 535 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 536 | $non_displayables = array(); |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 537 | |
| 538 | // every control character except newline (dec 10), |
| 539 | // carriage return (dec 13) and horizontal tab (dec 09) |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 540 | if ($url_encoded) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 541 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 542 | $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 |
| 543 | $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 544 | } |
Andrey Andreev | 188abaf | 2012-01-07 19:09:42 +0200 | [diff] [blame] | 545 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 546 | $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] | 547 | |
| 548 | do |
| 549 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 550 | $str = preg_replace($non_displayables, '', $str, -1, $count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 551 | } |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 552 | while ($count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 553 | |
| 554 | return $str; |
| 555 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 556 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 558 | // ------------------------------------------------------------------------ |
| 559 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 560 | if ( ! function_exists('html_escape')) |
| 561 | { |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame^] | 562 | /** |
| 563 | * Returns HTML escaped variable |
| 564 | * |
| 565 | * @param mixed |
| 566 | * @return mixed |
| 567 | */ |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 568 | function html_escape($var) |
| 569 | { |
Andrey Andreev | b7b4396 | 2012-02-27 22:45:48 +0200 | [diff] [blame] | 570 | return is_array($var) |
| 571 | ? array_map('html_escape', $var) |
| 572 | : htmlspecialchars($var, ENT_QUOTES, config_item('charset')); |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 573 | } |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 574 | } |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame] | 575 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | /* End of file Common.php */ |
Andrey Andreev | bb30d79 | 2012-03-26 15:49:09 +0300 | [diff] [blame] | 577 | /* Location: ./system/core/Common.php */ |