You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/core/MY_Output.php

180 lines
4.2 KiB
PHTML

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Output extends CI_Output
{
// --------------------------------------------------------------------
/**
* Write a Cache File
*
* @access public
* @param string
* @return void
*/
function _write_cache($output)
{
$CI =& get_instance();
$path = $CI->config->item('cache_path');
$cache_path = ($path == '') ? APPPATH.'cache/' : $path;
if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
{
log_message('error', "Unable to write cache file: ".$cache_path);
return;
}
$uri = $CI->config->item('base_url').
$CI->config->item('index_page').
$CI->uri->uri_string();
///////<2F>ijɰ<C4B3><C9B0><EFBFBD>URL<52><4C><EFBFBD><EFBFBD><E6BBBA>
//$cache_path .= md5($uri);
//<2F><><EFBFBD>ɻ<EFBFBD><C9BB><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>@cache@refresh<73><68>ʶȥ<CAB6><C8A5>
$origin_url=$this->get_origin_url($CI->config->item('index_page'));
$cache_path=$cache_path.$_SERVER['HTTP_HOST'].'/'.$origin_url;
create_folder_by_path(dirname($cache_path));
///////
if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
{
log_message('error', "Unable to write cache file: ".$cache_path);
return;
}
$expire = time() + ($this->cache_expiration * 60);
if (flock($fp, LOCK_EX))
{
fwrite($fp, $expire.'TS--->'.$output);
fseek($fp,0,SEEK_END);
fwrite($fp, '<!-- Generated by '.$_SERVER['HTTP_HOST'].' ('.date('c').') -->');
flock($fp, LOCK_UN);
}
else
{
log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
return;
}
fclose($fp);
@chmod($cache_path, FILE_WRITE_MODE);
log_message('debug', "Cache file written: ".$cache_path);
}
// --------------------------------------------------------------------
/**
* Update/serve a cached file
*
* @access public
* @param object config class
* @param object uri class
* @return void
*/
function _display_cache(&$CFG, &$URI)
{
$cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
///////<2F>ijɰ<C4B3><C9B0><EFBFBD>URL<52><4C><EFBFBD><EFBFBD><E6BBBA>
//$filepath = $cache_path.md5($uri);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@cache@refresh<73><68>ʶ<EFBFBD>򷵻<EFBFBD>false<73><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
$origin_url=$this->get_origin_url($CFG->item('index_page'));
$filepath=$cache_path.$_SERVER['HTTP_HOST'].'/'.$origin_url;
if($this->is_has_cache_mark())
{
@unlink($filepath);
return false;
}
///////
if ( ! @file_exists($filepath))
{
return FALSE;
}
if ( ! $fp = @fopen($filepath, FOPEN_READ))
{
return FALSE;
}
flock($fp, LOCK_SH);
$cache = '';
if (filesize($filepath) > 0)
{
$cache = fread($fp, filesize($filepath));
}
flock($fp, LOCK_UN);
fclose($fp);
// Strip out the embedded timestamp
if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
{
return FALSE;
}
// Has the file expired? If so we'll delete it.
if (time() >= trim(str_replace('TS--->', '', $match['1'])))
{
if (is_really_writable($cache_path))
{
@unlink($filepath);
log_message('debug', "Cache file has expired. File deleted");
return FALSE;
}
}
// Display the cache
$this->_display(str_replace($match['0'], '', $cache));
log_message('debug', "Cache file is current. Sending it to browser.");
return TRUE;
}
/**
* <20><>ȡiis rewrite֮ǰ<D6AE><C7B0>ԭʼurl
*/
function get_origin_url($index_page) {
if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
$origin_url = str_replace($index_page,'/',$_SERVER['HTTP_X_REWRITE_URL']);
} else {
$origin_url = str_replace($index_page,'/',$_SERVER['REQUEST_URI']);
}
$origin_url=str_replace(array('///','//','@cache@refresh'),array('/','/',''),urldecode($origin_url));
if(mb_substr($origin_url,-1,1)=='/')
{
$origin_url.='index.htm';
}
return $origin_url;
}
/*
* <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>и<EFBFBD><D0B8>±<EFBFBD>ʶ@cache@refresh
*/
function is_has_cache_mark()
{
//HTTP_X_REWRITE_URL <20>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>@cache@refresh<73><68>ʶ<EFBFBD>ģ<EFBFBD><C4A3>жϵ<D0B6>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>url
if (!isset($_SERVER['HTTP_X_REWRITE_URL']))
{
if(strpos($_SERVER['REQUEST_URI'],'@cache@refresh'))
{
return true;
}
}
else
{
if (strpos($_SERVER['HTTP_X_REWRITE_URL'],'@cache@refresh'))
{
return true;
}
}
return false;
}
}