]>
jfr.im git - z_archive/KronOS.git/blob - video/system/core/Common.php
07534c51f955e52af075f7e0255963786c7aec84
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 if ( ! function_exists('is_php'))
44 function is_php($version = '5.0.0')
47 $version = (string)$version;
49 if ( ! isset($_is_php[$version]))
51 $_is_php[$version] = (version_compare(PHP_VERSION
, $version) < 0) ? FALSE : TRUE;
54 return $_is_php[$version];
58 // ------------------------------------------------------------------------
61 * Tests for file writability
63 * is_writable() returns TRUE on Windows servers when you really can't write to
64 * the file, based on the read-only attribute. is_writable() is also unreliable
65 * on Unix servers if safe_mode is on.
70 if ( ! function_exists('is_really_writable'))
72 function is_really_writable($file)
74 // If we're on a Unix server with safe_mode off we call is_writable
75 if (DIRECTORY_SEPARATOR
== '/' AND @ini_get("safe_mode") == FALSE)
77 return is_writable($file);
80 // For windows servers and safe_mode "on" installations we'll actually
81 // write a file then read it. Bah...
84 $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
86 if (($fp = @fopen($file, FOPEN_WRITE_CREATE
)) === FALSE)
92 @chmod($file, DIR_WRITE_MODE
);
96 elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE
)) === FALSE)
106 // ------------------------------------------------------------------------
111 * This function acts as a singleton. If the requested class does not
112 * exist it is instantiated and set to a static variable. If it has
113 * previously been instantiated the variable is returned.
116 * @param string the class name being requested
117 * @param string the directory where the class should be found
118 * @param string the class name prefix
121 if ( ! function_exists('load_class'))
123 function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
125 static $_classes = array();
127 // Does the class exist? If so, we're done...
128 if (isset($_classes[$class]))
130 return $_classes[$class];
135 // Look for the class first in the local application/libraries folder
136 // then in the native system/libraries folder
137 foreach (array(APPPATH
, BASEPATH
) as $path)
139 if (file_exists($path.$directory.'/'.$class.'.php'))
141 $name = $prefix.$class;
143 if (class_exists($name) === FALSE)
145 require($path.$directory.'/'.$class.'.php');
152 // Is the request a class extension? If so we load it too
153 if (file_exists(APPPATH
.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
155 $name = config_item('subclass_prefix').$class;
157 if (class_exists($name) === FALSE)
159 require(APPPATH
.$directory.'/'.config_item('subclass_prefix').$class.'.php');
163 // Did we find the class?
166 // Note: We use exit() rather then show_error() in order to avoid a
167 // self-referencing loop with the Excptions class
168 exit('Unable to locate the specified class: '.$class.'.php');
171 // Keep track of what we just loaded
174 $_classes[$class] = new $name();
175 return $_classes[$class];
179 // --------------------------------------------------------------------
182 * Keeps track of which libraries have been loaded. This function is
183 * called by the load_class() function above
188 if ( ! function_exists('is_loaded'))
190 function &is_loaded($class = '')
192 static $_is_loaded = array();
196 $_is_loaded[strtolower($class)] = $class;
203 // ------------------------------------------------------------------------
206 * Loads the main config.php file
208 * This function lets us grab the config file even if the Config class
209 * hasn't been instantiated yet
214 if ( ! function_exists('get_config'))
216 function &get_config($replace = array())
225 // Is the config file in the environment folder?
226 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH
.'config/'.ENVIRONMENT
.'/config.php'))
228 $file_path = APPPATH
.'config/config.php';
231 // Fetch the config file
232 if ( ! file_exists($file_path))
234 exit('The configuration file does not exist.');
239 // Does the $config array exist in the file?
240 if ( ! isset($config) OR ! is_array($config))
242 exit('Your config file does not appear to be formatted correctly.');
245 // Are any values being dynamically replaced?
246 if (count($replace) > 0)
248 foreach ($replace as $key => $val)
250 if (isset($config[$key]))
252 $config[$key] = $val;
257 return $_config[0] =& $config;
261 // ------------------------------------------------------------------------
264 * Returns the specified config item
269 if ( ! function_exists('config_item'))
271 function config_item($item)
273 static $_config_item = array();
275 if ( ! isset($_config_item[$item]))
277 $config =& get_config();
279 if ( ! isset($config[$item]))
283 $_config_item[$item] = $config[$item];
286 return $_config_item[$item];
290 // ------------------------------------------------------------------------
295 * This function lets us invoke the exception class and
296 * display errors using the standard error template located
297 * in application/errors/errors.php
298 * This function will send the error page directly to the
304 if ( ! function_exists('show_error'))
306 function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
308 $_error =& load_class('Exceptions', 'core');
309 echo $_error->show_error($heading, $message, 'error_general', $status_code);
314 // ------------------------------------------------------------------------
319 * This function is similar to the show_error() function above
320 * However, instead of the standard error template it displays
326 if ( ! function_exists('show_404'))
328 function show_404($page = '', $log_error = TRUE)
330 $_error =& load_class('Exceptions', 'core');
331 $_error->show_404($page, $log_error);
336 // ------------------------------------------------------------------------
339 * Error Logging Interface
341 * We use this as a simple mechanism to access the logging
342 * class and send messages to be logged.
347 if ( ! function_exists('log_message'))
349 function log_message($level = 'error', $message, $php_error = FALSE)
353 if (config_item('log_threshold') == 0)
358 $_log =& load_class('Log');
359 $_log->write_log($level, $message, $php_error);
363 // ------------------------------------------------------------------------
366 * Set HTTP Status Header
369 * @param int the status code
373 if ( ! function_exists('set_status_header'))
375 function set_status_header($code = 200, $text = '')
381 203 => 'Non-Authoritative Information',
383 205 => 'Reset Content',
384 206 => 'Partial Content',
386 300 => 'Multiple Choices',
387 301 => 'Moved Permanently',
389 304 => 'Not Modified',
391 307 => 'Temporary Redirect',
393 400 => 'Bad Request',
394 401 => 'Unauthorized',
397 405 => 'Method Not Allowed',
398 406 => 'Not Acceptable',
399 407 => 'Proxy Authentication Required',
400 408 => 'Request Timeout',
403 411 => 'Length Required',
404 412 => 'Precondition Failed',
405 413 => 'Request Entity Too Large',
406 414 => 'Request-URI Too Long',
407 415 => 'Unsupported Media Type',
408 416 => 'Requested Range Not Satisfiable',
409 417 => 'Expectation Failed',
411 500 => 'Internal Server Error',
412 501 => 'Not Implemented',
413 502 => 'Bad Gateway',
414 503 => 'Service Unavailable',
415 504 => 'Gateway Timeout',
416 505 => 'HTTP Version Not Supported'
419 if ($code == '' OR ! is_numeric($code))
421 show_error('Status codes must be numeric', 500);
424 if (isset($stati[$code]) AND $text == '')
426 $text = $stati[$code];
431 show_error('No status text available. Please check your status code number or supply your own message text.', 500);
434 $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
436 if (substr(php_sapi_name(), 0, 3) == 'cgi')
438 header("Status: {$code} {$text}", TRUE);
440 elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
442 header($server_protocol." {$code} {$text}", TRUE, $code);
446 header("HTTP/1.1 {$code} {$text}", TRUE, $code);
451 // --------------------------------------------------------------------
456 * This is the custom exception handler that is declaired at the top
457 * of Codeigniter.php. The main reason we use this is to permit
458 * PHP errors to be logged in our own log files since the user may
459 * not have access to server logs. Since this function
460 * effectively intercepts PHP errors, however, we also need
461 * to display errors based on the current error_reporting level.
462 * We do that with the use of a PHP error template.
467 if ( ! function_exists('_exception_handler'))
469 function _exception_handler($severity, $message, $filepath, $line)
471 // We don't bother with "strict" notices since they tend to fill up
472 // the log file with excess information that isn't normally very helpful.
473 // For example, if you are running PHP 5 and you use version 4 style
474 // class functions (without prefixes like "public", "private", etc.)
475 // you'll get notices telling you that these have been deprecated.
476 if ($severity == E_STRICT
)
481 $_error =& load_class('Exceptions', 'core');
483 // Should we display the error? We'll get the current error_reporting
484 // level and add its bits with the severity bits to find out.
485 if (($severity & error_reporting()) == $severity)
487 $_error->show_php_error($severity, $message, $filepath, $line);
490 // Should we log the error? No? We're done...
491 if (config_item('log_threshold') == 0)
496 $_error->log_exception($severity, $message, $filepath, $line);
500 // --------------------------------------------------------------------
503 * Remove Invisible Characters
505 * This prevents sandwiching null characters
506 * between ascii characters, like Java\0script.
512 if ( ! function_exists('remove_invisible_characters'))
514 function remove_invisible_characters($str, $url_encoded = TRUE)
516 $non_displayables = array();
518 // every control character except newline (dec 10)
519 // carriage return (dec 13), and horizontal tab (dec 09)
523 $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
524 $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
527 $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
531 $str = preg_replace($non_displayables, '', $str, -1, $count);
539 // ------------------------------------------------------------------------
542 * Returns HTML escaped variable
548 if ( ! function_exists('html_escape'))
550 function html_escape($var)
554 return array_map('html_escape', $var);
558 return htmlspecialchars($var, ENT_QUOTES
, config_item('charset'));
563 /* End of file Common.php */
564 /* Location: ./system/core/Common.php */