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