From 2507154cc0ad564103dfd03270d0d05950481c2a Mon Sep 17 00:00:00 2001 From: "lmr@hainatravel.com" <59361885@qq.com> Date: Thu, 20 Sep 2018 09:04:36 -0500 Subject: [PATCH] fix kcfinder --- .../htmlcompressor/controllers/index.php | 658 +++++++++--------- 1 file changed, 329 insertions(+), 329 deletions(-) diff --git a/application/third_party/htmlcompressor/controllers/index.php b/application/third_party/htmlcompressor/controllers/index.php index 6518d028..999907c2 100644 --- a/application/third_party/htmlcompressor/controllers/index.php +++ b/application/third_party/htmlcompressor/controllers/index.php @@ -1,330 +1,330 @@ -load->library('simple_html_dom_lib'); - } - - public function index() - { - $this->load->view('welcome'); - } - - public function optimize() - { - $htmlsource = $this->input->post('htmlsource'); - $websitehost = $this->input->post('websitehost'); - $template_name = $this->input->post('template_name'); - $create_amp = $this->input->post('create_amp'); - $debug = $this->input->post('debug'); - if (empty($htmlsource) || empty($websitehost)) { - $this->output->set_status_header(500); - echo 'error:htmlsource or websitehost is empty!'; - log_message('error', "htmlsource or websitehost is empty! " . $websitehost); - return false; - } - //域名后面不能有/ - if (substr($websitehost, -1, 1) == '/') { - $websitehost = substr($websitehost, 0, -1); - } - - $html_object = str_get_html($htmlsource); - if (!empty($html_object)) { - -//提取和下载所有CSS样式,包括链接文件和页面样式 - $link_css_array = array(); - $css_content = ''; - foreach ($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 = ''; //删除链接 - } - } - //print_r($link_css_array); - foreach ($link_css_array as $item) { - $get_http_temp = GET_HTTP($this->format_url($item, $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; - } - foreach ($html_object->find('style') as $style_css) { - if ($style_css->type == "text/css") { - $css_content .= $style_css->innertext; - } - } - // echo $css_content; - // echo $html_object;die(); - // -//提取和下载所有JS脚本,包括链接文件和页面脚本 - $link_js_array = array(); - $js_inline_content = ''; - $js_jquery_content = ''; - foreach ($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; - - $html_object = str_replace('', $lastload_js . '', $html_object); - } - - //只返回AMP代码 - if (!empty($create_amp)) { - $amp = $this->create_amp($html_object, $template_name, $websitehost,$debug); - echo json_encode(array('result' => 'ok', 'data' => array('amp' => $amp))); - return; - } - echo json_encode(array('result' => 'ok', 'data' => array('html_object' => $html_object, 'js_content' => $js_content))); - - } - -//格式化url,保证请求的URL有域名,//更换为对应的域名路径 - function format_url($url, $host = '') - { - if (substr($url, 0, 8) == 'https://' || substr($url, 0, 7) == 'http://') { - $url = str_replace('http://', 'https://', $url);//http要改为https - return urldecode($url); - } - - if (substr($url, 0, 2) == '//') { //https或http - return urldecode(str_replace('//', 'https://', $url)); - } - - return urldecode($host . $url); - } - - - -//生成AMP版本 用移动端版本生成 - /* - 1.加载站点AMP模板,模板只有导航、头部、底部 - 2.删除全部js引用和内链代码 - 3.删除全部css引用,内联css放入", $html_object); + //删除多余空格和换行符 + $html_object = str_replace(array(" ", "\t", "\n", "\r"), " ", $html_object); + //循环n次,把双空格替换为一个空格 + for ($i = 0; $i <= 4; $i++) { + $html_object = str_replace(" ", " ", $html_object); + } + +//在最后加载原始css文件和js文件 + //把css移动到页面底部,延迟加载 + $lastload_js = ''; + + $js_content = $js_scr_content . $js_jquery_content; + //延迟加载js,需要把返回的js代码保存到一个文件中,然后替换占位符,以便加载js文件 + $lastload_js .= ''; + $lastload_js .= $js_inline_content; + + $html_object = str_replace('', $lastload_js . '', $html_object); + } + + //只返回AMP代码 + if (!empty($create_amp)) { + $amp = $this->create_amp($html_object, $template_name, $websitehost,$debug); + echo json_encode(array('result' => 'ok', 'data' => array('amp' => $amp))); + return; + } + echo json_encode(array('result' => 'ok', 'data' => array('html_object' => $html_object, 'js_content' => $js_content))); + + } + +//格式化url,保证请求的URL有域名,//更换为对应的域名路径 + function format_url($url, $host = '') + { + if (substr($url, 0, 8) == 'https://' || substr($url, 0, 7) == 'http://') { + $url = str_replace('http://', 'https://', $url);//http要改为https + return urldecode($url); + } + + if (substr($url, 0, 2) == '//') { //https或http + return urldecode(str_replace('//', 'https://', $url)); + } + + return urldecode($host . $url); + } + + + +//生成AMP版本 用移动端版本生成 + /* + 1.加载站点AMP模板,模板只有导航、头部、底部 + 2.删除全部js引用和内链代码 + 3.删除全部css引用,内联css放入