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 |
| 40 | * @return object |
| 41 | */ |
| 42 | function &_load_class($class, $instantiate = TRUE) |
| 43 | { |
admin | 349b09c | 2006-09-15 20:07:00 +0000 | [diff] [blame] | 44 | static $objects = array(); |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 45 | |
| 46 | // Does the class exist? If so, we're done... |
| 47 | if (isset($objects[$class])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | { |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 49 | return $objects[$class]; |
| 50 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 51 | |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 52 | // This is a special case. It's a class in the Base5.php file |
| 53 | // which we don't need to load. We only instantiate it. |
| 54 | if ($class == 'Instance') |
| 55 | { |
| 56 | return $objects[$class] =& new $class(); |
| 57 | } |
| 58 | |
| 59 | // If the requested class does not exist in the application/libraries |
| 60 | // folder we'll load the native class from the system/libraries folder. |
| 61 | |
| 62 | $is_subclass = FALSE; |
| 63 | if ( ! file_exists(APPPATH.'libraries/'.$class.EXT)) |
| 64 | { |
| 65 | require(BASEPATH.'libraries/'.$class.EXT); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | // A core class can either be extended or replaced by putting an |
| 70 | // identially named file in the application/libraries folder. |
| 71 | // We need to need to determine if the class being requested is |
| 72 | // a sub-class or an independent instance so we'll open the file, |
| 73 | // read the top portion of it. If the class extends the base class |
| 74 | // we need to load it's parent. If it doesn't extend the base we'll |
| 75 | // only load the requested class. |
| 76 | |
| 77 | // Note: I'm not thrilled with this approach since it requires us to |
| 78 | // read the file, but I can't think of any other way to allow classes |
| 79 | // to be extended on-the-fly. I did benchmark the difference with and |
| 80 | // without the file reading and I'm not seeing a perceptable difference. |
| 81 | |
| 82 | $fp = fopen(APPPATH.'libraries/'.$class.EXT, "rb"); |
| 83 | if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/", fread($fp, '8000'))) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 84 | { |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 85 | require(BASEPATH.'libraries/'.$class.EXT); |
| 86 | require(APPPATH.'libraries/'.$class.EXT); |
| 87 | $is_subclass = TRUE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 88 | } |
| 89 | else |
| 90 | { |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 91 | require(APPPATH.'libraries/'.$class.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 92 | } |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 93 | fclose($fp); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 94 | } |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 95 | |
| 96 | if ($instantiate == FALSE) |
| 97 | { |
| 98 | return $objects[$class] = TRUE; |
| 99 | } |
| 100 | |
| 101 | if ($is_subclass == TRUE) |
| 102 | { |
| 103 | $name = 'MY_'.$class; |
| 104 | return $objects[$class] =& new $name(); |
| 105 | } |
| 106 | |
| 107 | $name = ($class != 'Controller') ? 'CI_'.$class : $class; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 108 | |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 109 | return $objects[$class] =& new $name(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Loads the main config.php file |
| 114 | * |
| 115 | * |
| 116 | * @access private |
| 117 | * @return array |
| 118 | */ |
| 119 | function &_get_config() |
| 120 | { |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 121 | static $main_conf; |
| 122 | |
| 123 | if ( ! isset($main_conf)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 124 | { |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 125 | if ( ! file_exists(APPPATH.'config/config'.EXT)) |
| 126 | { |
| 127 | show_error('The configuration file config'.EXT.' does not exist.'); |
| 128 | } |
| 129 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 130 | require(APPPATH.'config/config'.EXT); |
| 131 | |
| 132 | if ( ! isset($config) OR ! is_array($config)) |
| 133 | { |
| 134 | show_error('Your config file does not appear to be formatted correctly.'); |
| 135 | } |
| 136 | |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 137 | $main_conf[0] =& $config; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 138 | } |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 139 | return $main_conf[0]; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Error Handler |
| 145 | * |
| 146 | * This function lets us invoke the exception class and |
| 147 | * display errors using the standard error template located |
| 148 | * in application/errors/errors.php |
| 149 | * This function will send the error page directly to the |
| 150 | * browser and exit. |
| 151 | * |
| 152 | * @access public |
| 153 | * @return void |
| 154 | */ |
| 155 | function show_error($message) |
| 156 | { |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 157 | $error =& _load_class('Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 158 | echo $error->show_error('An Error Was Encountered', $message); |
| 159 | exit; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * 404 Page Handler |
| 165 | * |
| 166 | * This function is similar to the show_error() function above |
| 167 | * However, instead of the standard error template it displays |
| 168 | * 404 errors. |
| 169 | * |
| 170 | * @access public |
| 171 | * @return void |
| 172 | */ |
| 173 | function show_404($page = '') |
| 174 | { |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 175 | $error =& _load_class('Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 176 | $error->show_404($page); |
| 177 | exit; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /** |
| 182 | * Error Logging Interface |
| 183 | * |
| 184 | * We use this as a simple mechanism to access the logging |
| 185 | * class and send messages to be logged. |
| 186 | * |
| 187 | * @access public |
| 188 | * @return void |
| 189 | */ |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 190 | function log_message($level = 'error', $message, $php_error = FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 191 | { |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 192 | static $LOG; |
| 193 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 194 | $config =& _get_config(); |
| 195 | if ($config['log_errors'] === FALSE) |
| 196 | { |
| 197 | return; |
| 198 | } |
| 199 | |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 200 | $LOG =& _load_class('Log'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 201 | $LOG->write_log($level, $message, $php_error); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * Exception Handler |
| 207 | * |
| 208 | * This is the custom exception handler we defined at the |
| 209 | * top of this file. The main reason we use this is permit |
| 210 | * PHP errors to be logged in our own log files since we may |
| 211 | * not have access to server logs. Since this function |
| 212 | * effectively intercepts PHP errors, however, we also need |
| 213 | * to display errors based on the current error_reporting level. |
| 214 | * We do that with the use of a PHP error template. |
| 215 | * |
| 216 | * @access private |
| 217 | * @return void |
| 218 | */ |
| 219 | function _exception_handler($severity, $message, $filepath, $line) |
| 220 | { |
| 221 | // We don't bother with "strict" notices since they will fill up |
| 222 | // the log file with information that isn't normally very |
| 223 | // helpful. For example, if you are running PHP 5 and you |
| 224 | // use version 4 style class functions (without prefixes |
| 225 | // like "public", "private", etc.) you'll get notices telling |
| 226 | // you that these have been deprecated. |
| 227 | |
| 228 | if ($severity == E_STRICT) |
| 229 | { |
| 230 | return; |
| 231 | } |
| 232 | |
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame^] | 233 | $error =& _load_class('Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 234 | |
| 235 | // Should we display the error? |
| 236 | // We'll get the current error_reporting level and add its bits |
| 237 | // with the severity bits to find out. |
| 238 | |
| 239 | if (($severity & error_reporting()) == $severity) |
| 240 | { |
| 241 | $error->show_php_error($severity, $message, $filepath, $line); |
| 242 | } |
| 243 | |
| 244 | // Should we log the error? No? We're done... |
| 245 | $config =& _get_config(); |
| 246 | if ($config['log_errors'] === FALSE) |
| 247 | { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | $error->log_exception($severity, $message, $filepath, $line); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | ?> |