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 | * |
| 35 | * |
| 36 | * @access public |
| 37 | * @return object |
| 38 | */ |
| 39 | function &_load_class($class, $instantiate = TRUE) |
| 40 | { |
admin | 349b09c | 2006-09-15 20:07:00 +0000 | [diff] [blame] | 41 | static $objects = array(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 42 | |
| 43 | if ( ! isset($objects[$class])) |
| 44 | { |
| 45 | if (FALSE !== strpos($class, 'CI_')) |
| 46 | { |
| 47 | if (file_exists(APPPATH.'libraries/'.str_replace('CI_', '', $class).EXT)) |
| 48 | { |
| 49 | require(APPPATH.'libraries/'.str_replace('CI_', '', $class).EXT); |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | require(BASEPATH.'libraries/'.str_replace('CI_', '', $class).EXT); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if ($instantiate == TRUE) |
| 58 | { |
| 59 | if ($class == 'CI_Controller') |
| 60 | $class = 'Controller'; |
| 61 | |
| 62 | $objects[$class] =& new $class(); |
| 63 | } |
| 64 | else |
| 65 | { |
admin | 349b09c | 2006-09-15 20:07:00 +0000 | [diff] [blame] | 66 | $objects[$class] = TRUE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 70 | return $objects[$class]; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Loads the main config.php file |
| 75 | * |
| 76 | * |
| 77 | * @access private |
| 78 | * @return array |
| 79 | */ |
| 80 | function &_get_config() |
| 81 | { |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame^] | 82 | static $main_conf; |
| 83 | |
| 84 | if ( ! isset($main_conf)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 85 | { |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame^] | 86 | if ( ! file_exists(APPPATH.'config/config'.EXT)) |
| 87 | { |
| 88 | show_error('The configuration file config'.EXT.' does not exist.'); |
| 89 | } |
| 90 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 91 | require(APPPATH.'config/config'.EXT); |
| 92 | |
| 93 | if ( ! isset($config) OR ! is_array($config)) |
| 94 | { |
| 95 | show_error('Your config file does not appear to be formatted correctly.'); |
| 96 | } |
| 97 | |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame^] | 98 | $main_conf[0] =& $config; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 99 | } |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame^] | 100 | return $main_conf[0]; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Error Handler |
| 106 | * |
| 107 | * This function lets us invoke the exception class and |
| 108 | * display errors using the standard error template located |
| 109 | * in application/errors/errors.php |
| 110 | * This function will send the error page directly to the |
| 111 | * browser and exit. |
| 112 | * |
| 113 | * @access public |
| 114 | * @return void |
| 115 | */ |
| 116 | function show_error($message) |
| 117 | { |
admin | 43a46cc | 2006-09-15 20:44:52 +0000 | [diff] [blame] | 118 | $error =& _load_class('CI_Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 119 | echo $error->show_error('An Error Was Encountered', $message); |
| 120 | exit; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * 404 Page Handler |
| 126 | * |
| 127 | * This function is similar to the show_error() function above |
| 128 | * However, instead of the standard error template it displays |
| 129 | * 404 errors. |
| 130 | * |
| 131 | * @access public |
| 132 | * @return void |
| 133 | */ |
| 134 | function show_404($page = '') |
| 135 | { |
admin | 43a46cc | 2006-09-15 20:44:52 +0000 | [diff] [blame] | 136 | $error =& _load_class('CI_Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 137 | $error->show_404($page); |
| 138 | exit; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * Error Logging Interface |
| 144 | * |
| 145 | * We use this as a simple mechanism to access the logging |
| 146 | * class and send messages to be logged. |
| 147 | * |
| 148 | * @access public |
| 149 | * @return void |
| 150 | */ |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 151 | function log_message($level = 'error', $message, $php_error = FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 152 | { |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 153 | static $LOG; |
| 154 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 155 | $config =& _get_config(); |
| 156 | if ($config['log_errors'] === FALSE) |
| 157 | { |
| 158 | return; |
| 159 | } |
| 160 | |
admin | 43a46cc | 2006-09-15 20:44:52 +0000 | [diff] [blame] | 161 | $LOG =& _load_class('CI_Log'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 162 | $LOG->write_log($level, $message, $php_error); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Exception Handler |
| 168 | * |
| 169 | * This is the custom exception handler we defined at the |
| 170 | * top of this file. The main reason we use this is permit |
| 171 | * PHP errors to be logged in our own log files since we may |
| 172 | * not have access to server logs. Since this function |
| 173 | * effectively intercepts PHP errors, however, we also need |
| 174 | * to display errors based on the current error_reporting level. |
| 175 | * We do that with the use of a PHP error template. |
| 176 | * |
| 177 | * @access private |
| 178 | * @return void |
| 179 | */ |
| 180 | function _exception_handler($severity, $message, $filepath, $line) |
| 181 | { |
| 182 | // We don't bother with "strict" notices since they will fill up |
| 183 | // the log file with information that isn't normally very |
| 184 | // helpful. For example, if you are running PHP 5 and you |
| 185 | // use version 4 style class functions (without prefixes |
| 186 | // like "public", "private", etc.) you'll get notices telling |
| 187 | // you that these have been deprecated. |
| 188 | |
| 189 | if ($severity == E_STRICT) |
| 190 | { |
| 191 | return; |
| 192 | } |
| 193 | |
admin | 43a46cc | 2006-09-15 20:44:52 +0000 | [diff] [blame] | 194 | $error =& _load_class('CI_Exceptions'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 195 | |
| 196 | // Should we display the error? |
| 197 | // We'll get the current error_reporting level and add its bits |
| 198 | // with the severity bits to find out. |
| 199 | |
| 200 | if (($severity & error_reporting()) == $severity) |
| 201 | { |
| 202 | $error->show_php_error($severity, $message, $filepath, $line); |
| 203 | } |
| 204 | |
| 205 | // Should we log the error? No? We're done... |
| 206 | $config =& _get_config(); |
| 207 | if ($config['log_errors'] === FALSE) |
| 208 | { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | $error->log_exception($severity, $message, $filepath, $line); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | ?> |