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