这个类是公共函数类,这里面的函数都非常经典,也是codeigniter的核心函数。
<?php if ( ! defined('BASEPATH')) exit('没有权限访问脚本'); /** *判断PHP的版本号 * * @access public * @param string * @return bool 当系统版本号等于或大于传入版本号参数$version的时候,返回TRUE */ function is_php($version = '5.0.0'){ static $_is_php; $version = (string)$version; if ( ! isset($_is_php[$version])){ $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE; } return $_is_php[$version]; } // ------------------------------------------------------------------------ /** * 测试文件可写性 * *测试的时候区分环境,进行不同的操作 * @access private * @return void */ function is_really_writable($file){ // 如果在Unix系统下,且safe_mode模块未开启 则执行 is_writable if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE){ return is_writable($file); } //如果在windows系统下,或者safe_mode模块设置未开启时,则执行以下函数 // 判断文件是否是一个目录,并写入一个文件,然后读它. if (is_dir($file)){ $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100)); // FOPEN_WRITE_CREATE = "rb" 以写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 //b的意思是强制以二进制方式写入 if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE){ return FALSE; } fclose($fp); @chmod($file, DIR_WRITE_MODE);//改变文件模式 @unlink($file); return TRUE; }elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE){ return FALSE; } fclose($fp); return TRUE; } // ------------------------------------------------------------------------ /** * 加载并实例化类 * * 这个函数是一个非常重要的函数,是系统的核心,它是一个引用类型, *通过传递不同的参数,它能够实例化不同的类,然后返回这个类的引用 * * @access public * @param string 所需要加载的类名称 * @param string 类所在的文件夹,默认是libraries * @param string 类的前缀 * @return object 返回类的引用 */ function &load_class($class, $directory = 'libraries', $prefix = 'CI_'){ //定义成静态的方法是为了防止多次加载,从而节省资源 static $_classes = array(); // 类是否存在?如果存在,则返回该类 if (isset($_classes[$class])){ return $_classes[$class]; } $name = FALSE; // 首先在系统文件"system/libraries"下查看是否存在该文件 // 然后再到用户建立的应用程序文件夹"application/libraries"下查找该文件 foreach (array(BASEPATH, APPPATH) as $path){ if (file_exists($path.$directory.'/'.$class.EXT)){ $name = $prefix.$class; if (class_exists($name) === FALSE){ require($path.$directory.'/'.$class.EXT); } break; } } // 接着查找自定义类 if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT)){ $name = config_item('subclass_prefix').$class; if (class_exists($name) === FALSE){ require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT); } } // 如果未找到类 if ($name === FALSE){ //在这里使用exit() 而不是使用show_error(),是为了避免再次加载函数,出现死循环 exit('无法找到指定的类: '.$class.EXT); } // 跟踪记录类的加载情况 is_loaded($class); $_classes[$class] = new $name(); return $_classes[$class]; } // -------------------------------------------------------------------- /** * 跟踪记录类的加载情况 * 这个函数是在load_class调用 * * @access public * @return array */ function is_loaded($class = ''){ static $_is_loaded = array(); if ($class != ''){ $_is_loaded[strtolower($class)] = $class; } return $_is_loaded; } // ------------------------------------------------------------------------ /** * *获取配置文件config.php中的配置信息 * * 这个函数能够在及时配置类还没实例化的情况下抓取配置文件 * * @access private * @return array */ function &get_config($replace = array()){ static $_config; if (isset($_config)){ return $_config[0]; } $file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT; //判断文件是否存在,如果存在就引入 if ( ! file_exists($file_path)){ $file_path = APPPATH.'config/config'.EXT; if ( ! file_exists($file_path)){ exit('配置文件不存在.'); } } require($file_path); // 判断$config变量是否存在,且必须是array类型 if ( ! isset($config) OR ! is_array($config)){ exit('配置文件格式不正确.'); } // 替换配置文件中的配置 if (count($replace) > 0){ foreach ($replace as $key => $val){ if (isset($config[$key])){ $config[$key] = $val; } } } return $_config[0] =& $config; } // ------------------------------------------------------------------------ /** * 获取config.php配置文件中的单项配置 * * @access public * @return mixed */ function config_item($item){ static $_config_item = array(); if ( ! isset($_config_item[$item])){ $config =& get_config(); if ( ! isset($config[$item])){ return FALSE; } $_config_item[$item] = $config[$item]; } return $_config_item[$item]; } // ------------------------------------------------------------------------ /** * 错误显示 * * * @access public * @return void */ function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') { $_error =& load_class('Exceptions', 'core'); echo $_error->show_error($heading, $message, 'error_general', $status_code); exit; } // ------------------------------------------------------------------------ /** * 404 错误显示 * * This function is similar to the show_error() function above * However, instead of the standard error template it displays * 404 errors. * * @access public * @return void */ function show_404($page = '', $log_error = TRUE) { $_error =& load_class('Exceptions', 'core'); $_error->show_404($page, $log_error); exit; } // ------------------------------------------------------------------------ /** * 错误日志接口函数 * * We use this as a simple mechanism to access the logging * class and send messages to be logged. * * @access public * @return void */ function log_message($level = 'error', $message, $php_error = FALSE) { static $_log; if (config_item('log_threshold') == 0) { return; } $_log =& load_class('Log'); $_log->write_log($level, $message, $php_error); } // ------------------------------------------------------------------------ /** * 设置HTTP状态头 * * @access public * @param int the status code * @param string * @return void */ function set_status_header($code = 200, $text = ''){ $stati = array( 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported' ); if ($code == '' OR ! is_numeric($code)) { show_error('Status codes must be numeric', 500); } if (isset($stati[$code]) AND $text == '') { $text = $stati[$code]; } if ($text == '') { show_error('No status text available. Please check your status code number or supply your own message text.', 500); } $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE; if (substr(php_sapi_name(), 0, 3) == 'cgi') { header("Status: {$code} {$text}", TRUE); } elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0') { header($server_protocol." {$code} {$text}", TRUE, $code); } else { header("HTTP/1.1 {$code} {$text}", TRUE, $code); } } // -------------------------------------------------------------------- /** * 自定义错误处理函数 * * This is the custom exception handler that is declaired at the top * of Codeigniter.php. The main reason we use this is to permit * PHP errors to be logged in our own log files since the user may * not have access to server logs. Since this function * effectively intercepts PHP errors, however, we also need * to display errors based on the current error_reporting level. * We do that with the use of a PHP error template. * * @access private * @return void */ function _exception_handler($severity, $message, $filepath, $line) { // We don't bother with "strict" notices since they tend to fill up // the log file with excess information that isn't normally very helpful. // For example, if you are running PHP 5 and you use version 4 style // class functions (without prefixes like "public", "private", etc.) // you'll get notices telling you that these have been deprecated. if ($severity == E_STRICT) { return; } $_error =& load_class('Exceptions', 'core'); // Should we display the error? We'll get the current error_reporting // level and add its bits with the severity bits to find out. if (($severity & error_reporting()) == $severity) { $_error->show_php_error($severity, $message, $filepath, $line); } // Should we log the error? No? We're done... if (config_item('log_threshold') == 0) { return; } $_error->log_exception($severity, $message, $filepath, $line); } // -------------------------------------------------------------------- /** * Remove Invisible Characters * * This prevents sandwiching null characters * between ascii characters, like Java\0script. * * @access public * @param string * @return string */ function remove_invisible_characters($str) { static $non_displayables; if ( ! isset($non_displayables)) { // every control character except newline (dec 10), carriage return (dec 13), and horizontal tab (dec 09), $non_displayables = array( '/%0[0-8bcef]/', // url encoded 00-08, 11, 12, 14, 15 '/%1[0-9a-f]/', // url encoded 16-31 '/[\x00-\x08]/', // 00-08 '/\x0b/', '/\x0c/', // 11, 12 '/[\x0e-\x1f]/' // 14-31 ); } do { $cleaned = $str; $str = preg_replace($non_displayables, '', $str); } while ($cleaned != $str); return $str; } /* End of file Common.php */ /* Location: ./system/core/Common.php */