admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Common Functions |
| 20 | * |
| 21 | * Loads the base classes and executes the request. |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage codeigniter |
| 25 | * @category Common Functions |
| 26 | * @author Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/ |
| 28 | */ |
| 29 | |
| 30 | // ------------------------------------------------------------------------ |
| 31 | |
| 32 | /** |
| 33 | * Class registry |
| 34 | * |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 35 | * This function acts as a singleton. If the requested class does not |
| 36 | * exist it is instantiated and set to a static variable. If it has |
| 37 | * previously been instantiated the variable is returned. |
| 38 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 39 | * @access public |
admin | 4a2ed69 | 2006-09-29 01:14:52 +0000 | [diff] [blame] | 40 | * @param string the class name being requested |
| 41 | * @param bool optional flag that lets classes get loaded but not instantiated |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 42 | * @return object |
| 43 | */ |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 44 | function &load_class($class, $instantiate = TRUE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 45 | { |
admin | 349b09c | 2006-09-15 20:07:00 +0000 | [diff] [blame] | 46 | static $objects = array(); |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 47 | |
| 48 | // Does the class exist? If so, we're done... |
| 49 | if (isset($objects[$class])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 50 | { |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 51 | return $objects[$class]; |
| 52 | } |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 53 | |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 54 | // If the requested class does not exist in the application/libraries |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 55 | // folder we'll load the native class from the system/libraries folder. |
| 56 | if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT)) |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 57 | { |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 58 | require(BASEPATH.'libraries/'.$class.EXT); |
| 59 | require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT); |
| 60 | $is_subclass = TRUE; |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 61 | } |
| 62 | else |
| 63 | { |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 64 | if (file_exists(APPPATH.'libraries/'.$class.EXT)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 65 | { |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 66 | require(APPPATH.'libraries/'.$class.EXT); |
| 67 | $is_subclass = FALSE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 68 | } |
| 69 | else |
| 70 | { |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 71 | require(BASEPATH.'libraries/'.$class.EXT); |
| 72 | $is_subclass = FALSE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 75 | |
| 76 | if ($instantiate == FALSE) |
| 77 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 78 | $objects[$class] = TRUE; |
| 79 | return $objects[$class]; |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if ($is_subclass == TRUE) |
| 83 | { |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 84 | $name = config_item('subclass_prefix').$class; |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 85 | $objects[$class] =& new $name(); |
| 86 | return $objects[$class]; |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | $name = ($class != 'Controller') ? 'CI_'.$class : $class; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 90 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 91 | $objects[$class] =& new $name(); |
| 92 | return $objects[$class]; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Loads the main config.php file |
| 97 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 98 | * @access private |
| 99 | * @return array |
| 100 | */ |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 101 | function &get_config() |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 102 | { |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 103 | static $main_conf; |
| 104 | |
| 105 | if ( ! isset($main_conf)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 106 | { |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 107 | if ( ! file_exists(APPPATH.'config/config'.EXT)) |
| 108 | { |
| 109 | show_error('The configuration file config'.EXT.' does not exist.'); |
| 110 | } |
| 111 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 112 | require(APPPATH.'config/config'.EXT); |
| 113 | |
| 114 | if ( ! isset($config) OR ! is_array($config)) |
| 115 | { |
| 116 | show_error('Your config file does not appear to be formatted correctly.'); |
| 117 | } |
| 118 | |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 119 | $main_conf[0] =& $config; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 120 | } |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 121 | return $main_conf[0]; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 122 | } |
| 123 | |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Gets a config item |
| 126 | * |
| 127 | * @access public |
| 128 | * @return mixed |
| 129 | */ |
| 130 | function &config_item($item) |
| 131 | { |
| 132 | static $config_item = array(); |
| 133 | |
| 134 | if ( ! isset($config_item[$item])) |
| 135 | { |
| 136 | $config =& get_config(); |
| 137 | |
| 138 | if ( ! isset($config[$item])) |
| 139 | { |
| 140 | return FALSE; |
| 141 | } |
| 142 | $config_item[$item] = $config[$item]; |
| 143 | } |
| 144 | |
| 145 | return $config_item[$item]; |
| 146 | } |
| 147 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Error Handler |
| 151 | * |
| 152 | * This function lets us invoke the exception class and |
| 153 | * display errors using the standard error template located |
| 154 | * in application/errors/errors.php |
| 155 | * This function will send the error page directly to the |
| 156 | * browser and exit. |
| 157 | * |
| 158 | * @access public |
| 159 | * @return void |
| 160 | */ |
| 161 | function show_error($message) |
| 162 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 163 | $error =& load_class('Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 164 | echo $error->show_error('An Error Was Encountered', $message); |
| 165 | exit; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * 404 Page Handler |
| 171 | * |
| 172 | * This function is similar to the show_error() function above |
| 173 | * However, instead of the standard error template it displays |
| 174 | * 404 errors. |
| 175 | * |
| 176 | * @access public |
| 177 | * @return void |
| 178 | */ |
| 179 | function show_404($page = '') |
| 180 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 181 | $error =& load_class('Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 182 | $error->show_404($page); |
| 183 | exit; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Error Logging Interface |
| 189 | * |
| 190 | * We use this as a simple mechanism to access the logging |
| 191 | * class and send messages to be logged. |
| 192 | * |
| 193 | * @access public |
| 194 | * @return void |
| 195 | */ |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 196 | function log_message($level = 'error', $message, $php_error = FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 197 | { |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 198 | static $LOG; |
| 199 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 200 | $config = get_config(); |
admin | b06d69f | 2006-10-13 21:44:17 +0000 | [diff] [blame] | 201 | if ($config['log_threshold'] == 0) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 202 | { |
| 203 | return; |
| 204 | } |
| 205 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 206 | $LOG =& load_class('Log'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 207 | $LOG->write_log($level, $message, $php_error); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * Exception Handler |
| 213 | * |
| 214 | * This is the custom exception handler we defined at the |
| 215 | * top of this file. The main reason we use this is permit |
| 216 | * PHP errors to be logged in our own log files since we may |
| 217 | * not have access to server logs. Since this function |
| 218 | * effectively intercepts PHP errors, however, we also need |
| 219 | * to display errors based on the current error_reporting level. |
| 220 | * We do that with the use of a PHP error template. |
| 221 | * |
| 222 | * @access private |
| 223 | * @return void |
| 224 | */ |
| 225 | function _exception_handler($severity, $message, $filepath, $line) |
| 226 | { |
| 227 | // We don't bother with "strict" notices since they will fill up |
| 228 | // the log file with information that isn't normally very |
| 229 | // helpful. For example, if you are running PHP 5 and you |
| 230 | // use version 4 style class functions (without prefixes |
| 231 | // like "public", "private", etc.) you'll get notices telling |
| 232 | // you that these have been deprecated. |
| 233 | |
| 234 | if ($severity == E_STRICT) |
| 235 | { |
| 236 | return; |
| 237 | } |
| 238 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 239 | $error =& load_class('Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 240 | |
| 241 | // Should we display the error? |
| 242 | // We'll get the current error_reporting level and add its bits |
| 243 | // with the severity bits to find out. |
| 244 | |
| 245 | if (($severity & error_reporting()) == $severity) |
| 246 | { |
| 247 | $error->show_php_error($severity, $message, $filepath, $line); |
| 248 | } |
| 249 | |
| 250 | // Should we log the error? No? We're done... |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 251 | $config = get_config(); |
admin | b06d69f | 2006-10-13 21:44:17 +0000 | [diff] [blame] | 252 | if ($config['log_threshold'] == 0) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 253 | { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | $error->log_exception($severity, $message, $filepath, $line); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | ?> |