1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * Loads the base classes and executes the request.
23 * @package CodeIgniter
24 * @subpackage codeigniter
25 * @category Common Functions
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/
30 // ------------------------------------------------------------------------
33 * Determines if the current version of PHP is greater then the supplied value
35 * Since there are a few places where we conditionally test for PHP > 5
36 * we'll set a static variable.
40 * @return bool TRUE if the current version is $version or higher
42 function is_php($version = '5.0.0')
45 $version = (string)$version;
47 if ( ! isset($_is_php[$version]))
49 $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
52 return $_is_php[$version];
55 // ------------------------------------------------------------------------
58 * Tests for file writability
60 * is_writable() returns TRUE on Windows servers when you really can't write to
61 * the file, based on the read-only attribute. is_writable() is also unreliable
62 * on Unix servers if safe_mode is on.
67 function is_really_writable($file)
69 // If we're on a Unix server with safe_mode off we call is_writable
70 if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
72 return is_writable($file);
75 // For windows servers and safe_mode "on" installations we'll actually
76 // write a file then read it. Bah...
79 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
81 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
87 @chmod($file, DIR_WRITE_MODE);
91 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
100 // ------------------------------------------------------------------------
105 * This function acts as a singleton. If the requested class does not
106 * exist it is instantiated and set to a static variable. If it has
107 * previously been instantiated the variable is returned.
110 * @param string the class name being requested
111 * @param string the directory where the class should be found
112 * @param string the class name prefix
115 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
117 static $_classes = array();
119 // Does the class exist? If so, we're done...
120 if (isset($_classes[$class]))
122 return $_classes[$class];
127 // Look for the class first in the native system/libraries folder
128 // thenin the local application/libraries folder
129 foreach (array(BASEPATH, APPPATH) as $path)
131 if (file_exists($path.$directory.'/'.$class.EXT))
133 $name = $prefix.$class;
135 if (class_exists($name) === FALSE)
137 require($path.$directory.'/'.$class.EXT);
144 // Is the request a class extension? If so we load it too
145 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT))
147 $name = config_item('subclass_prefix').$class;
149 if (class_exists($name) === FALSE)
151 require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT);
155 // Did we find the class?
158 // Note: We use exit() rather then show_error() in order to avoid a
159 // self-referencing loop with the Excptions class
160 exit('Unable to locate the specified class: '.$class.EXT);
163 // Keep track of what we just loaded
166 $_classes[$class] = new $name();
167 return $_classes[$class];
170 // --------------------------------------------------------------------
173 * Keeps track of which libraries have been loaded. This function is
174 * called by the load_class() function above
179 function is_loaded($class = '')
181 static $_is_loaded = array();
185 $_is_loaded[strtolower($class)] = $class;
191 // ------------------------------------------------------------------------
194 * Loads the main config.php file
196 * This function lets us grab the config file even if the Config class
197 * hasn't been instantiated yet
202 function &get_config($replace = array())
211 // Is the config file in the environment folder?
212 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT))
214 $file_path = APPPATH.'config/config'.EXT;
217 // Fetch the config file
218 if ( ! file_exists($file_path))
220 exit('The configuration file does not exist.');
225 // Does the $config array exist in the file?
226 if ( ! isset($config) OR ! is_array($config))
228 exit('Your config file does not appear to be formatted correctly.');
231 // Are any values being dynamically replaced?
232 if (count($replace) > 0)
234 foreach ($replace as $key => $val)
236 if (isset($config[$key]))
238 $config[$key] = $val;
243 return $_config[0] =& $config;
246 // ------------------------------------------------------------------------
249 * Returns the specified config item
254 function config_item($item)
256 static $_config_item = array();
258 if ( ! isset($_config_item[$item]))
260 $config =& get_config();
262 if ( ! isset($config[$item]))
266 $_config_item[$item] = $config[$item];
269 return $_config_item[$item];
272 // ------------------------------------------------------------------------
277 * This function lets us invoke the exception class and
278 * display errors using the standard error template located
279 * in application/errors/errors.php
280 * This function will send the error page directly to the
286 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
288 $_error =& load_class('Exceptions', 'core');
289 echo $_error->show_error($heading, $message, 'error_general', $status_code);
293 // ------------------------------------------------------------------------
298 * This function is similar to the show_error() function above
299 * However, instead of the standard error template it displays
305 function show_404($page = '', $log_error = TRUE)
307 $_error =& load_class('Exceptions', 'core');
308 $_error->show_404($page, $log_error);
312 // ------------------------------------------------------------------------
315 * Error Logging Interface
317 * We use this as a simple mechanism to access the logging
318 * class and send messages to be logged.
323 function log_message($level = 'error', $message, $php_error = FALSE)
327 if (config_item('log_threshold') == 0)
332 $_log =& load_class('Log');
333 $_log->write_log($level, $message, $php_error);
336 // ------------------------------------------------------------------------
339 * Set HTTP Status Header
342 * @param int the status code
346 function set_status_header($code = 200, $text = '')
352 203 => 'Non-Authoritative Information',
354 205 => 'Reset Content',
355 206 => 'Partial Content',
357 300 => 'Multiple Choices',
358 301 => 'Moved Permanently',
360 304 => 'Not Modified',
362 307 => 'Temporary Redirect',
364 400 => 'Bad Request',
365 401 => 'Unauthorized',
368 405 => 'Method Not Allowed',
369 406 => 'Not Acceptable',
370 407 => 'Proxy Authentication Required',
371 408 => 'Request Timeout',
374 411 => 'Length Required',
375 412 => 'Precondition Failed',
376 413 => 'Request Entity Too Large',
377 414 => 'Request-URI Too Long',
378 415 => 'Unsupported Media Type',
379 416 => 'Requested Range Not Satisfiable',
380 417 => 'Expectation Failed',
382 500 => 'Internal Server Error',
383 501 => 'Not Implemented',
384 502 => 'Bad Gateway',
385 503 => 'Service Unavailable',
386 504 => 'Gateway Timeout',
387 505 => 'HTTP Version Not Supported'
390 if ($code == '' OR ! is_numeric($code))
392 show_error('Status codes must be numeric', 500);
395 if (isset($stati[$code]) AND $text == '')
397 $text = $stati[$code];
402 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
405 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
407 if (substr(php_sapi_name(), 0, 3) == 'cgi')
409 header("Status: {$code} {$text}", TRUE);
411 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
413 header($server_protocol." {$code} {$text}", TRUE, $code);
417 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
421 // --------------------------------------------------------------------
426 * This is the custom exception handler that is declaired at the top
427 * of Codeigniter.php. The main reason we use this is to permit
428 * PHP errors to be logged in our own log files since the user may
429 * not have access to server logs. Since this function
430 * effectively intercepts PHP errors, however, we also need
431 * to display errors based on the current error_reporting level.
432 * We do that with the use of a PHP error template.
437 function _exception_handler($severity, $message, $filepath, $line)
439 // We don't bother with "strict" notices since they tend to fill up
440 // the log file with excess information that isn't normally very helpful.
441 // For example, if you are running PHP 5 and you use version 4 style
442 // class functions (without prefixes like "public", "private", etc.)
443 // you'll get notices telling you that these have been deprecated.
444 if ($severity == E_STRICT)
449 $_error =& load_class('Exceptions', 'core');
451 // Should we display the error? We'll get the current error_reporting
452 // level and add its bits with the severity bits to find out.
453 if (($severity & error_reporting()) == $severity)
455 $_error->show_php_error($severity, $message, $filepath, $line);
458 // Should we log the error? No? We're done...
459 if (config_item('log_threshold') == 0)
464 $_error->log_exception($severity, $message, $filepath, $line);
467 // --------------------------------------------------------------------
470 * Remove Invisible Characters
472 * This prevents sandwiching null characters
473 * between ascii characters, like Java\0script.
479 function remove_invisible_characters($str, $url_encoded = TRUE)
481 $non_displayables = array();
483 // every control character except newline (dec 10)
484 // carriage return (dec 13), and horizontal tab (dec 09)
488 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
489 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
492 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
496 $str = preg_replace($non_displayables, '', $str, -1, $count);
504 /* End of file Common.php */
505 /* Location: ./system/core/Common.php */