1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Determines if the current version of PHP is greater then the supplied value
21 * Since there are a few places where we conditionally test for PHP > 5
22 * we'll set a static variable.
28 function is_php($version = '5.0.0')
31 $version = (string)$version;
33 if ( ! isset($_is_php[$version]))
35 $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
38 return $_is_php[$version];
41 // ------------------------------------------------------------------------
44 * Tests for file writability
46 * is_writable() returns TRUE on Windows servers when you really can't write to
47 * the file, based on the read-only attribute. is_writable() is also unreliable
48 * on Unix servers if safe_mode is on.
53 function is_really_writable($file)
55 // If we're on a Unix server with safe_mode off we call is_writable
56 if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
58 return is_writable($file);
61 // For windows servers and safe_mode "on" installations we'll actually
62 // write a file then read it. Bah...
65 $file = rtrim($file, '/').'/'.md5(rand(1,100));
67 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
73 @chmod($file, DIR_WRITE_MODE);
77 elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
86 // ------------------------------------------------------------------------
91 * This function acts as a singleton. If the requested class does not
92 * exist it is instantiated and set to a static variable. If it has
93 * previously been instantiated the variable is returned.
96 * @param string the class name being requested
97 * @param bool optional flag that lets classes get loaded but not instantiated
100 function &load_class($class, $instantiate = TRUE)
102 static $objects = array();
104 // Does the class exist? If so, we're done...
105 if (isset($objects[$class]))
107 return $objects[$class];
110 // If the requested class does not exist in the application/libraries
111 // folder we'll load the native class from the system/libraries folder.
112 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
114 require(BASEPATH.'libraries/'.$class.EXT);
115 require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
120 if (file_exists(APPPATH.'libraries/'.$class.EXT))
122 require(APPPATH.'libraries/'.$class.EXT);
123 $is_subclass = FALSE;
127 require(BASEPATH.'libraries/'.$class.EXT);
128 $is_subclass = FALSE;
132 if ($instantiate == FALSE)
134 $objects[$class] = TRUE;
135 return $objects[$class];
138 if ($is_subclass == TRUE)
140 $name = config_item('subclass_prefix').$class;
142 $objects[$class] =& instantiate_class(new $name());
143 return $objects[$class];
146 $name = ($class != 'Controller') ? 'CI_'.$class : $class;
148 $objects[$class] =& instantiate_class(new $name());
149 return $objects[$class];
155 * Returns a new class object by reference, used by load_class() and the DB class.
156 * Required to retain PHP 4 compatibility and also not make PHP 5.3 cry.
158 * Use: $obj =& instantiate_class(new Foo());
164 function &instantiate_class(&$class_object)
166 return $class_object;
170 * Loads the main config.php file
175 function &get_config()
179 if ( ! isset($main_conf))
181 if ( ! file_exists(APPPATH.'config/config'.EXT))
183 exit('The configuration file config'.EXT.' does not exist.');
186 require(APPPATH.'config/config'.EXT);
188 if ( ! isset($config) OR ! is_array($config))
190 exit('Your config file does not appear to be formatted correctly.');
193 $main_conf[0] =& $config;
195 return $main_conf[0];
204 function config_item($item)
206 static $config_item = array();
208 if ( ! isset($config_item[$item]))
210 $config =& get_config();
212 if ( ! isset($config[$item]))
216 $config_item[$item] = $config[$item];
219 return $config_item[$item];
226 * This function lets us invoke the exception class and
227 * display errors using the standard error template located
228 * in application/errors/errors.php
229 * This function will send the error page directly to the
235 function show_error($message, $status_code = 500)
237 $error =& load_class('Exceptions');
238 echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
246 * This function is similar to the show_error() function above
247 * However, instead of the standard error template it displays
253 function show_404($page = '')
255 $error =& load_class('Exceptions');
256 $error->show_404($page);
262 * Error Logging Interface
264 * We use this as a simple mechanism to access the logging
265 * class and send messages to be logged.
270 function log_message($level = 'error', $message, $php_error = FALSE)
274 $config =& get_config();
275 if ($config['log_threshold'] == 0)
280 $LOG =& load_class('Log');
281 $LOG->write_log($level, $message, $php_error);
286 * Set HTTP Status Header
289 * @param int the status code
293 function set_status_header($code = 200, $text = '')
299 203 => 'Non-Authoritative Information',
301 205 => 'Reset Content',
302 206 => 'Partial Content',
304 300 => 'Multiple Choices',
305 301 => 'Moved Permanently',
307 304 => 'Not Modified',
309 307 => 'Temporary Redirect',
311 400 => 'Bad Request',
312 401 => 'Unauthorized',
315 405 => 'Method Not Allowed',
316 406 => 'Not Acceptable',
317 407 => 'Proxy Authentication Required',
318 408 => 'Request Timeout',
321 411 => 'Length Required',
322 412 => 'Precondition Failed',
323 413 => 'Request Entity Too Large',
324 414 => 'Request-URI Too Long',
325 415 => 'Unsupported Media Type',
326 416 => 'Requested Range Not Satisfiable',
327 417 => 'Expectation Failed',
329 500 => 'Internal Server Error',
330 501 => 'Not Implemented',
331 502 => 'Bad Gateway',
332 503 => 'Service Unavailable',
333 504 => 'Gateway Timeout',
334 505 => 'HTTP Version Not Supported'
337 if ($code == '' OR ! is_numeric($code))
339 show_error('Status codes must be numeric', 500);
342 if (isset($stati[$code]) AND $text == '')
344 $text = $stati[$code];
349 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
352 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
354 if (substr(php_sapi_name(), 0, 3) == 'cgi')
356 header("Status: {$code} {$text}", TRUE);
358 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
360 header($server_protocol." {$code} {$text}", TRUE, $code);
364 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
372 * This is the custom exception handler that is declaired at the top
373 * of Codeigniter.php. The main reason we use this is permit
374 * PHP errors to be logged in our own log files since we may
375 * not have access to server logs. Since this function
376 * effectively intercepts PHP errors, however, we also need
377 * to display errors based on the current error_reporting level.
378 * We do that with the use of a PHP error template.
383 function _exception_handler($severity, $message, $filepath, $line)
385 // We don't bother with "strict" notices since they will fill up
386 // the log file with information that isn't normally very
387 // helpful. For example, if you are running PHP 5 and you
388 // use version 4 style class functions (without prefixes
389 // like "public", "private", etc.) you'll get notices telling
390 // you that these have been deprecated.
392 if ($severity == E_STRICT)
397 $error =& load_class('Exceptions');
399 // Should we display the error?
400 // We'll get the current error_reporting level and add its bits
401 // with the severity bits to find out.
403 if (($severity & error_reporting()) == $severity)
405 $error->show_php_error($severity, $message, $filepath, $line);
408 // Should we log the error? No? We're done...
409 $config =& get_config();
410 if ($config['log_threshold'] == 0)
415 $error->log_exception($severity, $message, $filepath, $line);
420 /* End of file Common.php */
421 /* Location: ./system/codeigniter/Common.php */