Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 26 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 28 | */
|
| 29 |
|
| 30 | // ------------------------------------------------------------------------
|
| 31 |
|
| 32 | /**
|
| 33 | * Class registry
|
| 34 | *
|
| 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 | *
|
| 39 | * @access public
|
| 40 | * @param string the class name being requested
|
| 41 | * @param bool optional flag that lets classes get loaded but not instantiated
|
| 42 | * @return object
|
| 43 | */
|
| 44 | function &load_class($class, $instantiate = TRUE)
|
| 45 | {
|
| 46 | static $objects = array();
|
| 47 |
|
| 48 | // Does the class exist? If so, we're done...
|
| 49 | if (isset($objects[$class]))
|
| 50 | {
|
| 51 | return $objects[$class];
|
| 52 | }
|
| 53 |
|
| 54 | // If the requested class does not exist in the application/libraries
|
| 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))
|
| 57 | {
|
| 58 | require(BASEPATH.'libraries/'.$class.EXT);
|
| 59 | require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
|
| 60 | $is_subclass = TRUE;
|
| 61 | }
|
| 62 | else
|
| 63 | {
|
| 64 | if (file_exists(APPPATH.'libraries/'.$class.EXT))
|
| 65 | {
|
| 66 | require(APPPATH.'libraries/'.$class.EXT);
|
| 67 | $is_subclass = FALSE;
|
| 68 | }
|
| 69 | else
|
| 70 | {
|
| 71 | require(BASEPATH.'libraries/'.$class.EXT);
|
| 72 | $is_subclass = FALSE;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | if ($instantiate == FALSE)
|
| 77 | {
|
| 78 | $objects[$class] = TRUE;
|
| 79 | return $objects[$class];
|
| 80 | }
|
| 81 |
|
| 82 | if ($is_subclass == TRUE)
|
| 83 | {
|
| 84 | $name = config_item('subclass_prefix').$class;
|
| 85 | $objects[$class] =& new $name();
|
| 86 | return $objects[$class];
|
| 87 | }
|
| 88 |
|
| 89 | $name = ($class != 'Controller') ? 'CI_'.$class : $class;
|
| 90 |
|
| 91 | $objects[$class] =& new $name();
|
| 92 | return $objects[$class];
|
| 93 | }
|
| 94 |
|
| 95 | /**
|
| 96 | * Loads the main config.php file
|
| 97 | *
|
| 98 | * @access private
|
| 99 | * @return array
|
| 100 | */
|
| 101 | function &get_config()
|
| 102 | {
|
| 103 | static $main_conf;
|
| 104 |
|
| 105 | if ( ! isset($main_conf))
|
| 106 | {
|
| 107 | if ( ! file_exists(APPPATH.'config/config'.EXT))
|
| 108 | {
|
| 109 | exit('The configuration file config'.EXT.' does not exist.');
|
| 110 | }
|
| 111 |
|
| 112 | require(APPPATH.'config/config'.EXT);
|
| 113 |
|
| 114 | if ( ! isset($config) OR ! is_array($config))
|
| 115 | {
|
| 116 | exit('Your config file does not appear to be formatted correctly.');
|
| 117 | }
|
| 118 |
|
| 119 | $main_conf[0] =& $config;
|
| 120 | }
|
| 121 | return $main_conf[0];
|
| 122 | }
|
| 123 |
|
| 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 |
|
| 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 | {
|
| 163 | $error =& load_class('Exceptions');
|
| 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 | {
|
| 181 | $error =& load_class('Exceptions');
|
| 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 | */
|
| 196 | function log_message($level = 'error', $message, $php_error = FALSE)
|
| 197 | {
|
| 198 | static $LOG;
|
| 199 |
|
| 200 | $config =& get_config();
|
| 201 | if ($config['log_threshold'] == 0)
|
| 202 | {
|
| 203 | return;
|
| 204 | }
|
| 205 |
|
| 206 | $LOG =& load_class('Log');
|
| 207 | $LOG->write_log($level, $message, $php_error);
|
| 208 | }
|
| 209 |
|
| 210 | /**
|
| 211 | * Exception Handler
|
| 212 | *
|
| 213 | * This is the custom exception handler that is declaired at the top
|
| 214 | * of Codeigniter.php. The main reason we use this is permit
|
| 215 | * PHP errors to be logged in our own log files since we may
|
| 216 | * not have access to server logs. Since this function
|
| 217 | * effectively intercepts PHP errors, however, we also need
|
| 218 | * to display errors based on the current error_reporting level.
|
| 219 | * We do that with the use of a PHP error template.
|
| 220 | *
|
| 221 | * @access private
|
| 222 | * @return void
|
| 223 | */
|
| 224 | function _exception_handler($severity, $message, $filepath, $line)
|
| 225 | {
|
| 226 | // We don't bother with "strict" notices since they will fill up
|
| 227 | // the log file with information that isn't normally very
|
| 228 | // helpful. For example, if you are running PHP 5 and you
|
| 229 | // use version 4 style class functions (without prefixes
|
| 230 | // like "public", "private", etc.) you'll get notices telling
|
| 231 | // you that these have been deprecated.
|
| 232 |
|
| 233 | if ($severity == E_STRICT)
|
| 234 | {
|
| 235 | return;
|
| 236 | }
|
| 237 |
|
| 238 | $error =& load_class('Exceptions');
|
| 239 |
|
| 240 | // Should we display the error?
|
| 241 | // We'll get the current error_reporting level and add its bits
|
| 242 | // with the severity bits to find out.
|
| 243 |
|
| 244 | if (($severity & error_reporting()) == $severity)
|
| 245 | {
|
| 246 | $error->show_php_error($severity, $message, $filepath, $line);
|
| 247 | }
|
| 248 |
|
| 249 | // Should we log the error? No? We're done...
|
| 250 | $config =& get_config();
|
| 251 | if ($config['log_threshold'] == 0)
|
| 252 | {
|
| 253 | return;
|
| 254 | }
|
| 255 |
|
| 256 | $error->log_exception($severity, $message, $filepath, $line);
|
| 257 | }
|
| 258 |
|
| 259 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 260 | ?> |