|
|
|
@ -0,0 +1,107 @@
|
|
|
|
|
<?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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$cache_path = APPPATH.'cache/static_html';
|
|
|
|
|
|
|
|
|
|
if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
|
|
|
|
|
{
|
|
|
|
|
log_message('error', "Unable to write cache file: ".$cache_path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////改成按照URL保存缓存
|
|
|
|
|
//优先使用提交过来的static_html_url做文件名,这是主动生成静态文件用的参数
|
|
|
|
|
if(empty($_GET['static_html_url']))
|
|
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
$cache_path=$cache_path.$_GET['static_html_url'];
|
|
|
|
|
if(mb_substr($cache_path,-1,1)=='/')
|
|
|
|
|
{
|
|
|
|
|
$cache_path.='index.htm';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->create_folder_by_path(dirname($cache_path));
|
|
|
|
|
|
|
|
|
|
//如果文件存在,先判断是否为缓存文件,防止覆盖原始程序文件
|
|
|
|
|
if (@file_exists($cache_path))
|
|
|
|
|
{
|
|
|
|
|
if ( ! $fp_read = @fopen($cache_path, FOPEN_READ))
|
|
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flock($fp_read, LOCK_SH);
|
|
|
|
|
|
|
|
|
|
$cache = '';
|
|
|
|
|
if (filesize($cache_path) > 0)
|
|
|
|
|
{
|
|
|
|
|
$cache = fread($fp_read, filesize($cache_path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flock($fp_read, LOCK_UN);
|
|
|
|
|
fclose($fp_read);
|
|
|
|
|
if(strpos($cache,'<!-- Generated by ')===false)
|
|
|
|
|
{
|
|
|
|
|
log_message('error', "is not cache file.".$cache_path);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////
|
|
|
|
|
|
|
|
|
|
if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
|
|
|
|
|
{
|
|
|
|
|
log_message('error', "Unable to write cache file: ".$cache_path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flock($fp, LOCK_EX))
|
|
|
|
|
{
|
|
|
|
|
fwrite($fp, $output.'<!-- 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 递归创建文件夹
|
|
|
|
|
*/
|
|
|
|
|
function create_folder_by_path($dir, $mode = 0777)
|
|
|
|
|
{
|
|
|
|
|
if (!is_dir($dir))
|
|
|
|
|
{
|
|
|
|
|
return @mkdir($dir, $mode, true);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|