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