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 | * |
Derek Jones | 77b513b | 2009-08-06 14:39:25 +0000 | [diff] [blame] | 50 | * @param string |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 51 | * @return bool TRUE if the current version is $version or higher |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 52 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 53 | if ( ! function_exists('is_php')) |
| 54 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 55 | function is_php($version = '5.0.0') |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 56 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 57 | static $_is_php; |
| 58 | $version = (string)$version; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 60 | if ( ! isset($_is_php[$version])) |
| 61 | { |
| 62 | $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE; |
| 63 | } |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 64 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 65 | return $_is_php[$version]; |
| 66 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 67 | } |
Derek Jones | 5bcfd2e | 2009-07-28 14:42:42 +0000 | [diff] [blame] | 68 | |
Derek Jones | 086ee5a | 2009-07-28 14:42:12 +0000 | [diff] [blame] | 69 | // ------------------------------------------------------------------------ |
| 70 | |
| 71 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | * Tests for file writability |
| 73 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 74 | * 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] | 75 | * 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] | 76 | * on Unix servers if safe_mode is on. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 80 | if ( ! function_exists('is_really_writable')) |
| 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 |
| 85 | if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE) |
| 86 | { |
| 87 | return is_writable($file); |
| 88 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 90 | // For windows servers and safe_mode "on" installations we'll actually |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 91 | // write a file then read it. Bah... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 92 | if (is_dir($file)) |
| 93 | { |
| 94 | $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | |
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 | |
| 118 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 119 | * Class registry |
| 120 | * |
| 121 | * This function acts as a singleton. If the requested class does not |
| 122 | * exist it is instantiated and set to a static variable. If it has |
| 123 | * previously been instantiated the variable is returned. |
| 124 | * |
| 125 | * @param string the class name being requested |
| 126 | * @param string the directory where the class should be found |
| 127 | * @param string the class name prefix |
| 128 | * @return object |
| 129 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 130 | if ( ! function_exists('load_class')) |
| 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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [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 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [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 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 161 | // Is the request a class extension? If so we load it too |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [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 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [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 |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 177 | exit('Unable to locate the specified class: '.$class.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 179 | |
| 180 | // Keep track of what we just loaded |
| 181 | is_loaded($class); |
| 182 | |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 183 | $_classes[$class] = new $name(); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 184 | return $_classes[$class]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 186 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 188 | // -------------------------------------------------------------------- |
| 189 | |
| 190 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 191 | * Keeps track of which libraries have been loaded. This function is |
| 192 | * called by the load_class() function above |
| 193 | * |
| 194 | * @return array |
| 195 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 196 | if ( ! function_exists('is_loaded')) |
| 197 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 198 | function is_loaded($class = '') |
| 199 | { |
| 200 | static $_is_loaded = array(); |
| 201 | |
| 202 | if ($class != '') |
| 203 | { |
| 204 | $_is_loaded[strtolower($class)] = $class; |
| 205 | } |
| 206 | |
| 207 | return $_is_loaded; |
| 208 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 209 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 210 | |
| 211 | // ------------------------------------------------------------------------ |
Derek Jones | f0a9b33 | 2009-07-29 14:19:18 +0000 | [diff] [blame] | 212 | |
| 213 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 214 | * Loads the main config.php file |
| 215 | * |
| 216 | * This function lets us grab the config file even if the Config class |
| 217 | * hasn't been instantiated yet |
| 218 | * |
| 219 | * @return array |
| 220 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 221 | if ( ! function_exists('get_config')) |
| 222 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 223 | function &get_config($replace = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 225 | static $_config; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 227 | if (isset($_config)) |
| 228 | { |
| 229 | return $_config[0]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 231 | |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 232 | // Is the config file in the environment folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 233 | 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] | 234 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 235 | $file_path = APPPATH.'config/config.php'; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 236 | } |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 237 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 238 | // Fetch the config file |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 239 | if ( ! file_exists($file_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | { |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 241 | exit('The configuration file does not exist.'); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 242 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 243 | |
joelcox | 2035fd8 | 2011-01-16 16:50:36 +0100 | [diff] [blame] | 244 | require($file_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 246 | // Does the $config array exist in the file? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | if ( ! isset($config) OR ! is_array($config)) |
| 248 | { |
| 249 | exit('Your config file does not appear to be formatted correctly.'); |
| 250 | } |
| 251 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 252 | // Are any values being dynamically replaced? |
| 253 | if (count($replace) > 0) |
| 254 | { |
| 255 | foreach ($replace as $key => $val) |
| 256 | { |
| 257 | if (isset($config[$key])) |
| 258 | { |
| 259 | $config[$key] = $val; |
| 260 | } |
| 261 | } |
| 262 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 263 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 264 | return $_config[0] =& $config; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 266 | } |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 267 | |
| 268 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | |
| 270 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 271 | * Returns the specified config item |
| 272 | * |
| 273 | * @return mixed |
| 274 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 275 | if ( ! function_exists('config_item')) |
| 276 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 277 | function config_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 279 | static $_config_item = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 281 | if ( ! isset($_config_item[$item])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 283 | $config =& get_config(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 285 | if ( ! isset($config[$item])) |
| 286 | { |
| 287 | return FALSE; |
| 288 | } |
| 289 | $_config_item[$item] = $config[$item]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 292 | return $_config_item[$item]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 294 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 296 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | |
| 298 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 299 | * Error Handler |
| 300 | * |
| 301 | * This function lets us invoke the exception class and |
| 302 | * display errors using the standard error template located |
| 303 | * in application/errors/errors.php |
| 304 | * This function will send the error page directly to the |
| 305 | * browser and exit. |
| 306 | * |
| 307 | * @return void |
| 308 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 309 | if ( ! function_exists('show_error')) |
| 310 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 311 | function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') |
| 312 | { |
| 313 | $_error =& load_class('Exceptions', 'core'); |
| 314 | echo $_error->show_error($heading, $message, 'error_general', $status_code); |
| 315 | exit; |
| 316 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 317 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 319 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | |
| 321 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 322 | * 404 Page Handler |
| 323 | * |
| 324 | * This function is similar to the show_error() function above |
| 325 | * However, instead of the standard error template it displays |
| 326 | * 404 errors. |
| 327 | * |
| 328 | * @return void |
| 329 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 330 | if ( ! function_exists('show_404')) |
| 331 | { |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 332 | function show_404($page = '', $log_error = TRUE) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 333 | { |
| 334 | $_error =& load_class('Exceptions', 'core'); |
Derek Allard | 2ddc949 | 2010-08-05 10:08:33 -0400 | [diff] [blame] | 335 | $_error->show_404($page, $log_error); |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 336 | exit; |
| 337 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 338 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 340 | // ------------------------------------------------------------------------ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | |
| 342 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 343 | * Error Logging Interface |
| 344 | * |
| 345 | * We use this as a simple mechanism to access the logging |
| 346 | * class and send messages to be logged. |
| 347 | * |
| 348 | * @return void |
| 349 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 350 | if ( ! function_exists('log_message')) |
| 351 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 352 | function log_message($level = 'error', $message, $php_error = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 354 | static $_log; |
| 355 | |
| 356 | if (config_item('log_threshold') == 0) |
| 357 | { |
| 358 | return; |
| 359 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 361 | $_log =& load_class('Log'); |
| 362 | $_log->write_log($level, $message, $php_error); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 364 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 366 | // ------------------------------------------------------------------------ |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 367 | |
| 368 | /** |
| 369 | * Set HTTP Status Header |
| 370 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 371 | * @param int the status code |
| 372 | * @param string |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 373 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 375 | if ( ! function_exists('set_status_header')) |
| 376 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 377 | function set_status_header($code = 200, $text = '') |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 378 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 379 | $stati = array( |
| 380 | 200 => 'OK', |
| 381 | 201 => 'Created', |
| 382 | 202 => 'Accepted', |
| 383 | 203 => 'Non-Authoritative Information', |
| 384 | 204 => 'No Content', |
| 385 | 205 => 'Reset Content', |
| 386 | 206 => 'Partial Content', |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 387 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 388 | 300 => 'Multiple Choices', |
| 389 | 301 => 'Moved Permanently', |
| 390 | 302 => 'Found', |
| 391 | 304 => 'Not Modified', |
| 392 | 305 => 'Use Proxy', |
| 393 | 307 => 'Temporary Redirect', |
| 394 | |
| 395 | 400 => 'Bad Request', |
| 396 | 401 => 'Unauthorized', |
| 397 | 403 => 'Forbidden', |
| 398 | 404 => 'Not Found', |
| 399 | 405 => 'Method Not Allowed', |
| 400 | 406 => 'Not Acceptable', |
| 401 | 407 => 'Proxy Authentication Required', |
| 402 | 408 => 'Request Timeout', |
| 403 | 409 => 'Conflict', |
| 404 | 410 => 'Gone', |
| 405 | 411 => 'Length Required', |
| 406 | 412 => 'Precondition Failed', |
| 407 | 413 => 'Request Entity Too Large', |
| 408 | 414 => 'Request-URI Too Long', |
| 409 | 415 => 'Unsupported Media Type', |
| 410 | 416 => 'Requested Range Not Satisfiable', |
| 411 | 417 => 'Expectation Failed', |
Chris Rosser | 0ec05c1 | 2011-11-21 17:56:13 +0000 | [diff] [blame] | 412 | 422 => 'Unprocessable Entity', |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 413 | |
| 414 | 500 => 'Internal Server Error', |
| 415 | 501 => 'Not Implemented', |
| 416 | 502 => 'Bad Gateway', |
| 417 | 503 => 'Service Unavailable', |
| 418 | 504 => 'Gateway Timeout', |
| 419 | 505 => 'HTTP Version Not Supported' |
| 420 | ); |
| 421 | |
| 422 | if ($code == '' OR ! is_numeric($code)) |
| 423 | { |
| 424 | show_error('Status codes must be numeric', 500); |
| 425 | } |
| 426 | |
| 427 | if (isset($stati[$code]) AND $text == '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 429 | $text = $stati[$code]; |
| 430 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 432 | if ($text == '') |
| 433 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 434 | 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] | 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 437 | $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE; |
| 438 | |
| 439 | if (substr(php_sapi_name(), 0, 3) == 'cgi') |
| 440 | { |
| 441 | header("Status: {$code} {$text}", TRUE); |
| 442 | } |
| 443 | elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0') |
| 444 | { |
| 445 | header($server_protocol." {$code} {$text}", TRUE, $code); |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | header("HTTP/1.1 {$code} {$text}", TRUE, $code); |
| 450 | } |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 451 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 452 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 453 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 454 | // -------------------------------------------------------------------- |
Derek Jones | 817163a | 2009-07-11 17:05:58 +0000 | [diff] [blame] | 455 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 457 | * Exception Handler |
| 458 | * |
| 459 | * This is the custom exception handler that is declaired at the top |
| 460 | * of Codeigniter.php. The main reason we use this is to permit |
| 461 | * PHP errors to be logged in our own log files since the user may |
| 462 | * not have access to server logs. Since this function |
| 463 | * effectively intercepts PHP errors, however, we also need |
| 464 | * to display errors based on the current error_reporting level. |
| 465 | * We do that with the use of a PHP error template. |
| 466 | * |
| 467 | * @return void |
| 468 | */ |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 469 | if ( ! function_exists('_exception_handler')) |
| 470 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 471 | function _exception_handler($severity, $message, $filepath, $line) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 472 | { |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 473 | // We don't bother with "strict" notices since they tend to fill up |
| 474 | // the log file with excess information that isn't normally very helpful. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 475 | // For example, if you are running PHP 5 and you use version 4 style |
| 476 | // class functions (without prefixes like "public", "private", etc.) |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 477 | // you'll get notices telling you that these have been deprecated. |
| 478 | if ($severity == E_STRICT) |
| 479 | { |
| 480 | return; |
| 481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 483 | $_error =& load_class('Exceptions', 'core'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 484 | |
| 485 | // Should we display the error? We'll get the current error_reporting |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 486 | // level and add its bits with the severity bits to find out. |
| 487 | if (($severity & error_reporting()) == $severity) |
| 488 | { |
| 489 | $_error->show_php_error($severity, $message, $filepath, $line); |
| 490 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 492 | // Should we log the error? No? We're done... |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 493 | if (config_item('log_threshold') == 0) |
| 494 | { |
| 495 | return; |
| 496 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | |
Derek Jones | dc8e9ea | 2010-03-02 13:17:19 -0600 | [diff] [blame] | 498 | $_error->log_exception($severity, $message, $filepath, $line); |
| 499 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 500 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 502 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 504 | /** |
| 505 | * Remove Invisible Characters |
| 506 | * |
| 507 | * This prevents sandwiching null characters |
| 508 | * between ascii characters, like Java\0script. |
| 509 | * |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 510 | * @param string |
| 511 | * @return string |
| 512 | */ |
| 513 | if ( ! function_exists('remove_invisible_characters')) |
| 514 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 515 | function remove_invisible_characters($str, $url_encoded = TRUE) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 516 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 517 | $non_displayables = array(); |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 518 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 519 | // every control character except newline (dec 10) |
| 520 | // carriage return (dec 13), and horizontal tab (dec 09) |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 521 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 522 | if ($url_encoded) |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 523 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 524 | $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 |
| 525 | $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 526 | } |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 527 | |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 528 | $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] | 529 | |
| 530 | do |
| 531 | { |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 532 | $str = preg_replace($non_displayables, '', $str, -1, $count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 533 | } |
Pascal Kriete | 0ff5026 | 2011-04-05 14:52:03 -0400 | [diff] [blame] | 534 | while ($count); |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 535 | |
| 536 | return $str; |
| 537 | } |
Dan Horrigan | 3ef65bd | 2011-05-08 11:06:44 -0400 | [diff] [blame] | 538 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 540 | // ------------------------------------------------------------------------ |
| 541 | |
| 542 | /** |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 543 | * Returns HTML escaped variable |
| 544 | * |
| 545 | * @param mixed |
| 546 | * @return mixed |
| 547 | */ |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 548 | if ( ! function_exists('html_escape')) |
| 549 | { |
| 550 | function html_escape($var) |
| 551 | { |
| 552 | if (is_array($var)) |
| 553 | { |
| 554 | return array_map('html_escape', $var); |
| 555 | } |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 556 | |
| 557 | return htmlspecialchars($var, ENT_QUOTES, config_item('charset')); |
kenjis | fbac8b4 | 2011-08-25 10:51:44 +0900 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Greg Aker | 5c1aa63 | 2011-12-25 01:24:29 -0600 | [diff] [blame^] | 561 | // ------------------------------------------------------------------------ |
| 562 | |
| 563 | /** |
| 564 | * Load Environmental config directory files. |
| 565 | * |
| 566 | * In several places we check to see if the environment is loaded up and if the file |
| 567 | * that is being requested lives in said environment. Otherwise load up the file from |
| 568 | * the main CI config dir. |
| 569 | * |
| 570 | * @todo Optimize a bit to lessen the file system hits if the file has been loaded. |
| 571 | * @param string filename without extension. eg: 'config' or 'hooks' |
| 572 | * @param boolean whether or not to do a `require_once()` or a simple `include()` |
| 573 | * @return void |
| 574 | */ |
| 575 | if ( ! function_exists('load_environ_config')) |
| 576 | { |
| 577 | function load_environ_config($file, $require=FALSE) |
| 578 | { |
| 579 | if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php')) |
| 580 | { |
| 581 | if ($require) |
| 582 | { |
| 583 | require_once(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php'); |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | include(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php'); |
| 588 | } |
| 589 | |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | if ($require) |
| 594 | { |
| 595 | require_once(APPPATH.'config/'.$file.'.php'); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | include(APPPATH.'config/'.$file.'.php'); |
| 600 | } |
| 601 | |
| 602 | return; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | // ------------------------------------------------------------------------ |
| 607 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | /* End of file Common.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 609 | /* Location: ./system/core/Common.php */ |