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