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.

251 lines
10 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';
$optimize_js_path = 'd:/Dropbox/wwwcache/asiahighlights.com/js';
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) && filesize($cache_path) > 0) {
if (!$fp_read = @fopen($cache_path, FOPEN_READ)) {
return FALSE;
}
flock($fp_read, LOCK_SH);
$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');
//$clean_html = GET_HTTP('http://202.103.68.110:8001/info.php/apps/htmlcompressor/index/optimize', 'websitehost=' . urlencode('https://data.asiahighlights.com') . '&htmlsource=' . urlencode($output), 'POST');
if (!empty($clean_html)) {
$clean_html_object = json_decode($clean_html);
if (!empty($clean_html_object) && $clean_html_object->result == 'ok') {
$output = $clean_html_object->data->html_object;
$output_js = $clean_html_object->data->js_content;
$output_js_name = '/' . md5($cache_path) . '.js';
file_put_contents($optimize_js_path . $output_js_name, $output_js);
$time_mark= time();//添加时间戳用来更新CDN
$output = str_replace('##DEFERSCRIPTDEFER##', 'https://data.asiahighlights.com/optimizejs' . $output_js_name.'?'.$time_mark, $output);
}
}
}
}
//解析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 || strpos($image->src, '/image/') !== false) {
$image->src = 'https://data.asiahighlights.com' . $image->src;
}
}
if(empty($image->loader) || $image->loader!=="nolazy") {//如果没有禁止懒加载的标识符则给图片添加懒加载。一般首页logo和banner图片不需要懒加载
$image->originalsrc = $image->src;
$image->loader = "lazy";
$image->src = "https://data.asiahighlights.com/image/grey.gif";
}
}
//保存PC端代码
$output = $html_object->save();
//生成移动版本
foreach ($html_object->find('.hidden-xs') as $hidden_item) {
$hidden_item->outertext = '';
}
//生成移动端用小图
foreach ($html_object->find('img') as $image) {
if(substr($image->originalsrc,-4)=='.jpg'){//只有jpg可以处理
if (strpos($image->originalsrc, 'https://data.asiahighlights.com/') !== false) {//当查找的字符串在开始位置则返回0所以一定要写===false
$source_image = str_replace('https://data.asiahighlights.com', '', $image->originalsrc); //原始图片URL
$image->originalsrc = 'https://data.asiahighlights.com/mobile' . $source_image;
}
}
}
$html_object->save($cache_path . '.mobile.htm');
}
//////
////// 生成AMP版本
///
//用移动端版本生成
/*
1.加载站点AMP模板模板只有导航、头部、底部
2.删除全部js引用和内链代码
3.删除全部css引用内联css放入<style amp-custom>
4.图片全部替换成amp-img设置宽高设置layout="responsive"
5.去掉所有元素中的style
6.去掉样式表中的!important
7.替换读取源文件title description keywords canonical
8.todo:增加application/ld+json
*/
if (!empty($_GET['amp_optimize'])) {//生成AMP版本
//当前文件夹下的AMP模板
$amp_template=file_get_contents(dirname(__FILE__).'/amp-template.html');
foreach ($html_object->find('title') as $title) {
$amp_template = str_replace('<!--@TITLE@-->', $title->innertext, $amp_template);
}
foreach ($html_object->find('meta') as $meta) {
if($meta->name=='description'){
$amp_template = str_replace('<!--@DESCRIPTION@-->', $meta->content, $amp_template);
}
if($meta->name=='keywords'){
$amp_template = str_replace('<!--@KEYWORDS@-->', $meta->content, $amp_template);
}
}
foreach ($html_object->find('script') as $script) {
$script->outertext = '';
}
foreach ($html_object->find('link') as $link) {
if ($link->rel == 'stylesheet' && !empty($link->href)) {
$link->outertext = '';
}
if ($link->rel == 'canonical' && !empty($link->href)) {
$amp_template = str_replace('<!--@CANONICAL@-->', $link->href, $amp_template);
}
}
$style_content = '';
foreach ($html_object->find('style') as $style) {
$style_content .= $style->innertext;
$style->outertext = '';
}
$style_content = str_replace('!important', '', $style_content);
$amp_template = str_replace('/*@CUSTOM-CSS@*/', $style_content, $amp_template);
//获取图片尺寸AMP要求必须填写width和height
//拼接请求的图片url如 https://data.asiahighlights.com/imagesize.php?photo=/pic/logo-ah.png,/pic/ah-slide-logo.png
$request_size=array();
foreach ($html_object->find('img') as $image) {
$img_src = $image->src;
if (!empty($image->originalsrc)) {
$img_src = $image->originalsrc;
}
$request_size[] = str_replace('https://data.asiahighlights.com', '', $img_src);
}
//请求图片尺寸
$image_sizes=array();
$parse_url='https://data.asiahighlights.com/imagesize.php?photo='.implode(',',$request_size);
$size_data=GET_HTTP($parse_url);
if(!empty($size_data)){
$size_data=json_decode($size_data);
foreach ($size_data as $item){
$item->photo='https://data.asiahighlights.com'.$item->photo;
$image_sizes[$item->photo]=$item;
}
}
//获取图片尺寸 end
foreach ($html_object->find('img') as $image) {
$img_src = $image->src;
if (!empty($image->originalsrc)) {
$img_src = $image->originalsrc;
}
$img_alt = $image->alt;
$img_class = $image->class;
$img_size=$image_sizes[$img_src];
if(!empty($img_size)){
$image->outertext = " <amp-img layout=\"responsive\" class=\"{$img_class}\" alt=\"{$img_alt}\" src=\"{$img_src}\" width=\"{$img_size->width}\" height=\"{$img_size->height}\"></amp-img>";
}
}
foreach ($html_object->find('#content_main') as $content_main) {
$amp_template = str_replace('<!--@CUSTOM-CONENT@-->', $content_main, $amp_template);
}
//替换掉写在组件上的样式
$amp_template = preg_replace('/style=.+?[\'|"]/i', '', $amp_template);
file_put_contents($cache_path . '.amp.htm',$amp_template);
}
////// 生成AMP版本 end
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;
}
}