From 8a2bf3298ad5a1feab6b6f1d09be26feda649769 Mon Sep 17 00:00:00 2001
From: cyc
Date: Thu, 20 Feb 2020 14:58:46 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96AH=20=20amp=E8=BD=AC=E6=8D=A2?=
=?UTF-8?q?=E7=A8=8B=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
application/libraries/Amplib.php | 2 +
.../htmlcompressor/controllers/index_ah.php | 391 ++++++++++++++++++
.../htmlcompressor/controllers/index_gm.php | 5 +-
.../htmlcompressor/views/amp-template/ah.php | 81 +---
4 files changed, 396 insertions(+), 83 deletions(-)
create mode 100644 application/third_party/htmlcompressor/controllers/index_ah.php
diff --git a/application/libraries/Amplib.php b/application/libraries/Amplib.php
index 80283aca..0b5929d9 100644
--- a/application/libraries/Amplib.php
+++ b/application/libraries/Amplib.php
@@ -79,6 +79,8 @@ class Amplib {
}else{
return GET_HTTP(site_url('/apps/htmlcompressor/index_gm/choose_way'), $post_data, 'POST');
}
+ }elseif($site_code == 'ah'){
+ return GET_HTTP(site_url('/apps/htmlcompressor/index_ah/choose_way'), $post_data, 'POST');
}else{
return GET_HTTP(site_url('/apps/htmlcompressor/index/optimize'), $post_data, 'POST');
}
diff --git a/application/third_party/htmlcompressor/controllers/index_ah.php b/application/third_party/htmlcompressor/controllers/index_ah.php
new file mode 100644
index 00000000..e4d40dca
--- /dev/null
+++ b/application/third_party/htmlcompressor/controllers/index_ah.php
@@ -0,0 +1,391 @@
+load->library('simple_html_dom_lib');
+ }
+
+ public function index(){
+ $this->load->view('welcome');
+ }
+
+ //选择处理方式
+ public function choose_way(){
+ $this->htmlsource = $this->input->post('htmlsource');
+ $this->websitehost = $this->input->post('websitehost');
+ $this->template_name = $this->input->post('template_name');
+ $this->create_amp = $this->input->post('create_amp');
+ $this->debug = $this->input->post('debug');
+
+ /*$this->htmlsource = file_get_contents('aa.htm');
+ $this->websitehost = 'https://data.chinarundreisen.com';
+ $this->create_amp = 'true';
+ $this->template_name = 'gm';*/
+
+ if (empty($this->htmlsource) || empty($this->websitehost)) {
+ $this->output->set_status_header(500);
+ echo 'error:htmlsource or websitehost is empty!';
+ log_message('error', "htmlsource or websitehost is empty! " . $this->websitehost);
+ return false;
+ }
+
+ //域名后面不能有/
+ if (substr($this->websitehost, -1, 1) == '/') {
+ $this->websitehost = substr($this->websitehost, 0, -1);
+ }
+
+ //进行页面解析
+ $this->html_object = str_get_html($this->htmlsource);
+
+ //PC或AMP的构造方式
+ if (!empty($this->create_amp)) {
+ echo $this->create_amp();
+ return;
+ }else{
+ echo $this->optimize();
+ return;
+ }
+
+ }
+
+ //html优化处理
+ function optimize(){
+ //提取css样式地址
+ $link_css_array = array();
+ $css_content = '';
+ foreach ($this->html_object->find('link') as $link_css) {
+ //将所有的样式链接存储为一个数组
+ if ($link_css->rel == 'stylesheet' && !empty($link_css->href)) {
+ $link_css_array[] = $link_css->href;
+ $link_css->outertext = ''; //删除链接
+ }
+ }
+
+ //循环下载所有的css样式,拼接成字符串
+ foreach ($link_css_array as $item) {
+ $get_http_temp = GET_HTTP($this->format_url($item, $this->websitehost));
+ if ($get_http_temp == false) {
+ $this->output->set_status_header(404);
+ echo 'CSS文件下载错误';
+ log_message('error', "optimize CSS文件下载错误! " . $item);
+ return FALSE;
+ }
+ $css_content .= $get_http_temp;
+ }
+
+ //找出所有style写的内置样式
+ foreach ($this->html_object->find('style') as $style_css) {
+ if ($style_css->type == "text/css") {
+ $css_content .= $style_css->innertext;
+ }
+ }
+
+ //提取和下载所有JS脚本,包括链接文件和页面脚本
+ $link_js_array = array();
+ $js_inline_content = '';
+ $js_jquery_content = '';
+ foreach ($this->html_object->find('script') as $link_script) {
+ if (!empty($link_script->src)) {
+ if (empty($link_script->defer) && empty($link_script->async)) { //没有defer标记的才处理,否则让js延后加载
+ $link_js_array[] = $link_script->src;
+ $link_script->outertext = ''; //删除链接,移动到页底
+ }
+ } else {
+ //网页内的js不需要提取
+ //$js_content.= $link_script->innertext;//js的内容
+ // $js_content.= $link_script;//js的内容,包含';
+
+ $js_content = $js_scr_content . $js_jquery_content;
+ //延迟加载js,需要把返回的js代码保存到一个文件中,然后替换占位符,以便加载js文件
+ $lastload_js .= '';
+ $lastload_js .= $js_inline_content;
+
+ $this->html_object = str_replace('
-
-
-
-
-
-
-
-
-
-
-
-
+@AMP-COMMON-HEADER@
+@AMP-COMMON-FOOTER@
-
@AMP-GOOGLE-ANALYTICS@