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