diff --git a/application/libraries/simple_html_dom_lib.php b/application/libraries/simple_html_dom_lib.php new file mode 100644 index 00000000..0e0d7192 --- /dev/null +++ b/application/libraries/simple_html_dom_lib.php @@ -0,0 +1,9 @@ +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 ''; + return FALSE; + } + + //把精简的css添加到head后面 + $html_object = str_replace('', '", $html_object); + +//在最后加载原始css文件和js文件 + $lastload_js = ''; + //把js移动到页面底部 + foreach ($link_js_array as $item) { + $lastload_js.=''; + } + $lastload_js.=$js_content; + + $html_object = str_replace('
+', $lastload_js . '', $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); + } + +} diff --git a/application/third_party/htmlcompressor/views/welcome.php b/application/third_party/htmlcompressor/views/welcome.php new file mode 100644 index 00000000..e18468fd --- /dev/null +++ b/application/third_party/htmlcompressor/views/welcome.php @@ -0,0 +1,22 @@ + + +
+ + + + +
+ + +
+