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

117 lines
4.4 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
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的内容
$js_content.= $link_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('</title>', '</title><style type="text/css">' . $optimize_css . "</style>", $html_object);
//在最后加载原始css文件和js文件
$lastload_js = '<script>';
//把css移动到页面底部
foreach ($link_css_array as $item) {
$lastload_js.='var elem=document.createElement("link");elem.rel="stylesheet";elem.type="text/css";elem.href="' . $item . '";document.body.appendChild(elem);';
}
$lastload_js.='</script>';
//把js移动到页面底部
foreach ($link_js_array as $item) {
$lastload_js.='<script src="' . $item . '"></script>';
}
$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);
}
}