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.
information-system/application/third_party/htmlcompressor/controllers/index.php

133 lines
5.9 KiB
PHTML

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Index extends CI_Controller {
public function __construct() {
parent::__construct();
$this->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');
if (empty($htmlsource) || empty($websitehost)) {
$this->output->set_status_header(500);
echo 'error:htmlsource or websitehost is empty!';
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) {
$css_content.= GET_HTTP($this->format_url($item, $websitehost));
}
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_content = '';
foreach ($html_object->find('script') as $link_script) {
if (!empty($link_script->src)) {
$link_js_array[] = $link_script->src;
$link_script->outertext = ''; //删除链接,移动到页底
} else {
//网页内的js不需要提取
//$js_content.= $link_script->innertext;//js的内容
8 years ago
// $js_content.= $link_script;//js的内容包含<script>
8 years ago
//$link_script_content = $link_script;
8 years ago
//含有$表示调用了jquery的函数添加延迟加载defer
// if (strpos($link_script_content, '$') !== FALSE) {
// $link_script_content = str_replace('defer', 'defer', $link_script_content);
// $link_script_content = str_replace('<script', '<script defer', $link_script_content);
// $js_content.= $link_script_content;
// } else {
8 years ago
$js_content.= $link_script; //js的内容包含<script>
// }
$link_script->outertext = ''; //删除js移动到页底
}
}
foreach ($link_js_array as $item) {
//$js_content.= GET_HTTP($this->format_url($item, $websitehost));
}
//echo $js_content;
//把网页内容和css提交到purifycss处理
$optimize_css = GET_HTTP('http://184.172.113.219:33033/', 'html_source=' . urlencode($htmlsource) . '&html_css=' . urlencode($css_content), 'POST');
if (empty($optimize_css)) {
$this->output->set_status_header(500);
echo 'css精简错误';
echo '<!-- ' . $css_content . ' -->';
return FALSE;
}
//把精简的css添加到head前面
$html_object = str_replace('</head>', '<style type="text/css">' . $optimize_css . "</style></head>", $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移动到页面底部延迟加载
8 years ago
$lastload_js = '<noscript id="deferred-styles">';
foreach ($link_css_array as $item) {
8 years ago
$lastload_js.='<link rel="stylesheet" type="text/css" href="' . $item . '"/>';
}
8 years ago
$lastload_js.='</noscript><script>var loadDeferredStyles=function(){var addStylesNode=document.getElementById("deferred-styles");var replacement=document.createElement("div");replacement.innerHTML=addStylesNode.textContent;document.body.appendChild(replacement);addStylesNode.parentElement.removeChild(addStylesNode)};var raf=requestAnimationFrame||mozRequestAnimationFrame||webkitRequestAnimationFrame||msRequestAnimationFrame;if(raf){raf(function(){window.setTimeout(loadDeferredStyles,0)})}else{window.addEventListener("load",loadDeferredStyles)};</script>';
//把js移动到页面底部
foreach ($link_js_array as $item) {
$lastload_js.='<script src="' . $item . '"></script>';
}
8 years ago
$lastload_js.=$js_content;
$html_object = str_replace('</body>', $lastload_js . '</body>', $html_object);
}
echo $html_object;
}
//格式化url保证请求的URL有域名//更换为对应的域名路径
function format_url($url, $host = '') {
if (substr($url, 0, 8) == 'https://' || substr($url, 0, 7) == 'http://') {
return urldecode($url);
}
if (substr($url, 0, 2) == '//') { //https或http
return urldecode(str_replace('//', 'http://', $url));
}
return urldecode($host . $url);
}
}