Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 | /**
|
Derek Jones | a25530f | 2008-01-28 17:11:02 +0000 | [diff] [blame] | 33 | * Tests for file writability
|
| 34 | *
|
| 35 | * is_writable() returns TRUE on Windows servers
|
| 36 | * when you really can't write to the file
|
| 37 | * as the OS reports to PHP as FALSE only if the
|
| 38 | * read-only attribute is marked. Ugh?
|
| 39 | *
|
| 40 | * @access private
|
| 41 | * @return void
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 42 | */
|
Derek Jones | a25530f | 2008-01-28 17:11:02 +0000 | [diff] [blame] | 43 | function is_really_writable($file)
|
| 44 | {
|
| 45 | if (is_dir($file))
|
| 46 | {
|
| 47 | $file = rtrim($file, '/').'/'.md5(rand(1,100));
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 48 |
|
Derek Jones | 3be20e2 | 2008-05-05 20:07:09 +0000 | [diff] [blame] | 49 | if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
|
Derek Jones | a25530f | 2008-01-28 17:11:02 +0000 | [diff] [blame] | 50 | {
|
| 51 | return FALSE;
|
| 52 | }
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 53 |
|
Derek Jones | a25530f | 2008-01-28 17:11:02 +0000 | [diff] [blame] | 54 | fclose($fp);
|
Derek Jones | 3ad8efe | 2008-04-04 18:56:04 +0000 | [diff] [blame] | 55 | @chmod($file, DIR_WRITE_MODE);
|
Derek Jones | a25530f | 2008-01-28 17:11:02 +0000 | [diff] [blame] | 56 | @unlink($file);
|
| 57 | return TRUE;
|
| 58 | }
|
Derek Jones | 3be20e2 | 2008-05-05 20:07:09 +0000 | [diff] [blame] | 59 | elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
|
Derek Jones | a25530f | 2008-01-28 17:11:02 +0000 | [diff] [blame] | 60 | {
|
| 61 | return FALSE;
|
| 62 | }
|
| 63 |
|
| 64 | fclose($fp);
|
| 65 | return TRUE;
|
| 66 | }
|
| 67 |
|
| 68 | // ------------------------------------------------------------------------
|
| 69 |
|
| 70 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 71 | * Class registry
|
| 72 | *
|
| 73 | * This function acts as a singleton. If the requested class does not
|
| 74 | * exist it is instantiated and set to a static variable. If it has
|
| 75 | * previously been instantiated the variable is returned.
|
| 76 | *
|
| 77 | * @access public
|
| 78 | * @param string the class name being requested
|
| 79 | * @param bool optional flag that lets classes get loaded but not instantiated
|
| 80 | * @return object
|
| 81 | */
|
| 82 | function &load_class($class, $instantiate = TRUE)
|
| 83 | {
|
| 84 | static $objects = array();
|
| 85 |
|
| 86 | // Does the class exist? If so, we're done...
|
| 87 | if (isset($objects[$class]))
|
| 88 | {
|
| 89 | return $objects[$class];
|
| 90 | }
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 91 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 92 | // If the requested class does not exist in the application/libraries
|
| 93 | // folder we'll load the native class from the system/libraries folder.
|
| 94 | if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
|
| 95 | {
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame] | 96 | require(BASEPATH.'libraries/'.$class.EXT);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 97 | require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame] | 98 | $is_subclass = TRUE;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 99 | }
|
| 100 | else
|
| 101 | {
|
| 102 | if (file_exists(APPPATH.'libraries/'.$class.EXT))
|
| 103 | {
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame] | 104 | require(APPPATH.'libraries/'.$class.EXT);
|
| 105 | $is_subclass = FALSE;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 106 | }
|
| 107 | else
|
| 108 | {
|
| 109 | require(BASEPATH.'libraries/'.$class.EXT);
|
| 110 | $is_subclass = FALSE;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | if ($instantiate == FALSE)
|
| 115 | {
|
| 116 | $objects[$class] = TRUE;
|
| 117 | return $objects[$class];
|
| 118 | }
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 119 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 120 | if ($is_subclass == TRUE)
|
| 121 | {
|
| 122 | $name = config_item('subclass_prefix').$class;
|
| 123 | $objects[$class] =& new $name();
|
| 124 | return $objects[$class];
|
| 125 | }
|
| 126 |
|
| 127 | $name = ($class != 'Controller') ? 'CI_'.$class : $class;
|
| 128 |
|
| 129 | $objects[$class] =& new $name();
|
| 130 | return $objects[$class];
|
| 131 | }
|
| 132 |
|
| 133 | /**
|
| 134 | * Loads the main config.php file
|
| 135 | *
|
| 136 | * @access private
|
| 137 | * @return array
|
| 138 | */
|
| 139 | function &get_config()
|
| 140 | {
|
| 141 | static $main_conf;
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 142 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 143 | if ( ! isset($main_conf))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 144 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 145 | if ( ! file_exists(APPPATH.'config/config'.EXT))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 146 | {
|
| 147 | exit('The configuration file config'.EXT.' does not exist.');
|
| 148 | }
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 149 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 150 | require(APPPATH.'config/config'.EXT);
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 151 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 152 | if ( ! isset($config) OR ! is_array($config))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 153 | {
|
| 154 | exit('Your config file does not appear to be formatted correctly.');
|
| 155 | }
|
| 156 |
|
| 157 | $main_conf[0] =& $config;
|
| 158 | }
|
| 159 | return $main_conf[0];
|
| 160 | }
|
| 161 |
|
| 162 | /**
|
| 163 | * Gets a config item
|
| 164 | *
|
| 165 | * @access public
|
| 166 | * @return mixed
|
| 167 | */
|
| 168 | function config_item($item)
|
| 169 | {
|
| 170 | static $config_item = array();
|
| 171 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 172 | if ( ! isset($config_item[$item]))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 173 | {
|
| 174 | $config =& get_config();
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 175 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 176 | if ( ! isset($config[$item]))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 177 | {
|
| 178 | return FALSE;
|
| 179 | }
|
| 180 | $config_item[$item] = $config[$item];
|
| 181 | }
|
| 182 |
|
| 183 | return $config_item[$item];
|
| 184 | }
|
| 185 |
|
| 186 |
|
| 187 | /**
|
| 188 | * Error Handler
|
| 189 | *
|
| 190 | * This function lets us invoke the exception class and
|
| 191 | * display errors using the standard error template located
|
| 192 | * in application/errors/errors.php
|
| 193 | * This function will send the error page directly to the
|
| 194 | * browser and exit.
|
| 195 | *
|
| 196 | * @access public
|
| 197 | * @return void
|
| 198 | */
|
| 199 | function show_error($message)
|
| 200 | {
|
| 201 | $error =& load_class('Exceptions');
|
| 202 | echo $error->show_error('An Error Was Encountered', $message);
|
| 203 | exit;
|
| 204 | }
|
| 205 |
|
| 206 |
|
| 207 | /**
|
| 208 | * 404 Page Handler
|
| 209 | *
|
| 210 | * This function is similar to the show_error() function above
|
| 211 | * However, instead of the standard error template it displays
|
| 212 | * 404 errors.
|
| 213 | *
|
| 214 | * @access public
|
| 215 | * @return void
|
| 216 | */
|
| 217 | function show_404($page = '')
|
| 218 | {
|
| 219 | $error =& load_class('Exceptions');
|
| 220 | $error->show_404($page);
|
| 221 | exit;
|
| 222 | }
|
| 223 |
|
| 224 |
|
| 225 | /**
|
| 226 | * Error Logging Interface
|
| 227 | *
|
| 228 | * We use this as a simple mechanism to access the logging
|
| 229 | * class and send messages to be logged.
|
| 230 | *
|
| 231 | * @access public
|
| 232 | * @return void
|
| 233 | */
|
| 234 | function log_message($level = 'error', $message, $php_error = FALSE)
|
| 235 | {
|
| 236 | static $LOG;
|
| 237 |
|
| 238 | $config =& get_config();
|
| 239 | if ($config['log_threshold'] == 0)
|
| 240 | {
|
| 241 | return;
|
| 242 | }
|
| 243 |
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame] | 244 | $LOG =& load_class('Log');
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 245 | $LOG->write_log($level, $message, $php_error);
|
| 246 | }
|
| 247 |
|
| 248 | /**
|
| 249 | * Exception Handler
|
| 250 | *
|
| 251 | * This is the custom exception handler that is declaired at the top
|
| 252 | * of Codeigniter.php. The main reason we use this is permit
|
| 253 | * PHP errors to be logged in our own log files since we may
|
| 254 | * not have access to server logs. Since this function
|
| 255 | * effectively intercepts PHP errors, however, we also need
|
| 256 | * to display errors based on the current error_reporting level.
|
| 257 | * We do that with the use of a PHP error template.
|
| 258 | *
|
| 259 | * @access private
|
| 260 | * @return void
|
| 261 | */
|
| 262 | function _exception_handler($severity, $message, $filepath, $line)
|
| 263 | {
|
| 264 | // We don't bother with "strict" notices since they will fill up
|
| 265 | // the log file with information that isn't normally very
|
| 266 | // helpful. For example, if you are running PHP 5 and you
|
| 267 | // use version 4 style class functions (without prefixes
|
| 268 | // like "public", "private", etc.) you'll get notices telling
|
| 269 | // you that these have been deprecated.
|
| 270 |
|
| 271 | if ($severity == E_STRICT)
|
| 272 | {
|
| 273 | return;
|
| 274 | }
|
| 275 |
|
| 276 | $error =& load_class('Exceptions');
|
| 277 |
|
| 278 | // Should we display the error?
|
| 279 | // We'll get the current error_reporting level and add its bits
|
| 280 | // with the severity bits to find out.
|
| 281 |
|
| 282 | if (($severity & error_reporting()) == $severity)
|
| 283 | {
|
| 284 | $error->show_php_error($severity, $message, $filepath, $line);
|
| 285 | }
|
| 286 |
|
| 287 | // Should we log the error? No? We're done...
|
| 288 | $config =& get_config();
|
| 289 | if ($config['log_threshold'] == 0)
|
| 290 | {
|
| 291 | return;
|
| 292 | }
|
| 293 |
|
| 294 | $error->log_exception($severity, $message, $filepath, $line);
|
| 295 | }
|
| 296 |
|
| 297 |
|
Derek Allard | 0fd8f02 | 2008-05-13 02:01:47 +0000 | [diff] [blame] | 298 |
|
| 299 | /* End of file Common.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 300 | /* Location: ./system/codeigniter/Common.php */ |