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