图片CDN分发,压缩html

master
ycc 9 years ago
parent 2f7bad2649
commit 5371548663

@ -1,107 +1,111 @@
<?php <?php
//加载第三方用于解析html的类
require '/lib/simple_html_dom.php';
if (!defined('BASEPATH')) if (!defined('BASEPATH'))
exit('No direct script access allowed'); exit('No direct script access allowed');
class MY_Output extends CI_Output class MY_Output extends CI_Output {
{ // --------------------------------------------------------------------
// -------------------------------------------------------------------- /**
* Write a Cache File
/** *
* Write a Cache File * @access public
* * @param string
* @access public * @return void
* @param string */
* @return void function _write_cache($output) {
*/ $cache_path = 'd:/Dropbox/wwwcache/asiahighlights.com';
function _write_cache($output)
{ if (!is_dir($cache_path) OR ! is_really_writable($cache_path)) {
log_message('error', "Unable to write cache file: " . $cache_path);
return;
$cache_path = 'd:/Dropbox/wwwcache/asiahighlights.com'; }
if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) ///////改成按照URL保存缓存
{ //优先使用提交过来的static_html_url做文件名这是主动生成静态文件用的参数
log_message('error', "Unable to write cache file: ".$cache_path); if (empty($_GET['static_html_url'])) {
return; return FALSE;
} }
$cache_path = $cache_path . $_GET['static_html_url'];
///////改成按照URL保存缓存 if (mb_substr($cache_path, -1, 1) == '/') {
//优先使用提交过来的static_html_url做文件名这是主动生成静态文件用的参数 $cache_path.='index.htm';
if(empty($_GET['static_html_url'])) }
{
return FALSE; $this->create_folder_by_path(dirname($cache_path));
}
$cache_path=$cache_path.$_GET['static_html_url']; //如果文件存在,先判断是否为缓存文件,防止覆盖原始程序文件
if(mb_substr($cache_path,-1,1)=='/') if (@file_exists($cache_path)) {
{ if (!$fp_read = @fopen($cache_path, FOPEN_READ)) {
$cache_path.='index.htm'; return FALSE;
} }
$this->create_folder_by_path(dirname($cache_path)); flock($fp_read, LOCK_SH);
//如果文件存在,先判断是否为缓存文件,防止覆盖原始程序文件 $cache = '';
if (@file_exists($cache_path)) if (filesize($cache_path) > 0) {
{ $cache = fread($fp_read, filesize($cache_path));
if ( ! $fp_read = @fopen($cache_path, FOPEN_READ)) }
{
return FALSE; flock($fp_read, LOCK_UN);
} fclose($fp_read);
if (strpos($cache, '<!-- Generated by ') === false) {
flock($fp_read, LOCK_SH); log_message('error', "is not cache file." . $cache_path);
return FALSE;
$cache = ''; }
if (filesize($cache_path) > 0) }
{
$cache = fread($fp_read, filesize($cache_path)); ///////
}
if (!$fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) {
flock($fp_read, LOCK_UN); log_message('error', "Unable to write cache file: " . $cache_path);
fclose($fp_read); return;
if(strpos($cache,'<!-- Generated by ')===false) }
{
log_message('error', "is not cache file.".$cache_path); //解析html生成不同版本的静态文件
return FALSE; //替换图片地址用CDN分发
} $html_object = str_get_html($output);
} if (!empty($html_object)) {
foreach ($html_object->find('img') as $image) {
/////// if (strpos($image->src, '/pic/') !== false) {
$image->src = 'https://data.asiahighlights.com' . $image->src;
if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) } else if (strpos($image->src, '/image/') !== false) {
{ $image->src = 'https://data.asiahighlights.com' . $image->src;
log_message('error', "Unable to write cache file: ".$cache_path); }
return; }
} $output = $html_object->save();
}
if (flock($fp, LOCK_EX)) //删除多余空格和换行符
{ $output = str_replace(array(" ", "\t", "\n", "\r"), " ", $output);
fwrite($fp, $output.'<!-- Generated by '.$_SERVER['HTTP_HOST'].' ('.date('c').') -->'); $output = str_replace(" ", " ", $output);
flock($fp, LOCK_UN); //////
}
else
{ if (flock($fp, LOCK_EX)) {
log_message('error', "Unable to secure a file lock for file at: ".$cache_path); fwrite($fp, $output . '<!-- Generated by ' . $_SERVER['HTTP_HOST'] . ' (' . date('c') . ') -->');
return; flock($fp, LOCK_UN);
} } else {
fclose($fp); log_message('error', "Unable to secure a file lock for file at: " . $cache_path);
@chmod($cache_path, FILE_WRITE_MODE); return;
}
log_message('debug', "Cache file written: ".$cache_path); 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;
}
} // --------------------------------------------------------------------
/*
* 递归创建文件夹
*/
function create_folder_by_path($dir, $mode = 0777) {
if (!is_dir($dir)) {
return @mkdir($dir, $mode, true);
}
return;
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save