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.

173 lines
6.8 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
//加载第三方用于解析html的类
require '/lib/simple_html_dom.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 = 'd:/Dropbox/wwwcache/asiahighlights.com';
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;
}
//精简html和css代码
if (!empty($_GET['static_html_optimize'])) {//设置优化开关,暂时手动优化
$optimize_html = $_GET['static_html_optimize'];
if ($optimize_html === 'comeon') {
$clean_html = GET_HTTP('http://cht.mycht.cn/info.php/apps/htmlcompressor/index/optimize', 'websitehost=' . urlencode('https://data.asiahighlights.com') . '&htmlsource=' . urlencode($output), 'POST');
if (!empty($clean_html)) {
$output = $clean_html;
}
}
}
//解析html生成不同版本的静态文件
$html_object = str_get_html($output);
if (!empty($html_object)) {
//替换图片地址用CDN分发
foreach ($html_object->find('img') as $image) {
if (strpos($image->src, 'https://data.asiahighlights.com/') === false) {//当查找的字符串在开始位置则返回0所以一定要写===false
if (strpos($image->src, '/pic/') !== false) {
$image->src = 'https://data.asiahighlights.com' . $image->src;
} else if (strpos($image->src, '/image/') !== false) {
$image->src = 'https://data.asiahighlights.com' . $image->src;
}
}
}
//保存PC端代码
$output = $html_object->save();
//生成移动版本
foreach ($html_object->find('.hidden-xs') as $hidden_item) {
$hidden_item->outertext = '';
}
//生成移动端用小图
$CI = & get_instance();
$CI->load->library('image_lib');
$mobile_image_path = 'd:/Dropbox/wwwroot/asiahighlights.com/mobile'; //小图的存放路径
foreach ($html_object->find('img') as $image) {
$source_image = str_replace('https://data.asiahighlights.com', '', $image->src); //原始图片URL
$source_image_path = $_SERVER['DOCUMENT_ROOT'] . $source_image; //原始图片绝对路径
$new_image_path = $mobile_image_path . $source_image; //小图绝对路径
if (is_file($source_image_path)) {
$CI->image_lib->clear();
//$config['image_library'] = 'GD2';
$config['image_library'] = 'ImageMagick';
$config['library_path'] = 'c:/ImageMagick/';
$config['source_image'] = $source_image_path;
$config['new_image'] = $new_image_path;
$config['maintain_ratio'] = true;
$config['create_thumb'] = false;
$config['quality'] = 95;
$config['width'] = 414; //Iphone的屏幕宽度
$config['height'] = 10240; //设置一个大数,让程序以宽度来缩放
$CI->image_lib->initialize($config);
if ($CI->image_lib->orig_width > 414) {
$this->create_folder_by_path(dirname($new_image_path));
//比较文件时间,没新文件则不生成,但是文本还是要替换的
if (is_file($new_image_path)) {
if (filemtime($new_image_path) > filemtime($source_image_path)) {
$image->src = 'https://data.asiahighlights.com/mobile' . $source_image;
continue;
}
}
//生成小图,并替换引用文件
if ($CI->image_lib->resize()) {
$image->src = 'https://data.asiahighlights.com/mobile' . $source_image;
} else {
log_message('error', "my_output image resize :" . $CI->image_lib->display_errors());
}
}
$CI->image_lib->clear();
}
}
$html_object->save($cache_path . '.mobile.htm');
}
//////
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;
}
}