<?php
namespace addons\speed\controller;
use think\Controller;
use app\index\model\Plug;
class Main extends Controller{
private $plug;
public function _initialize(){
$this->plug = Plug::where(['only'=>'speed'])->find();
$this->check_cache_file();
}
public function ViewFilter(&$content){
$nod=compress_html($content);
$css_regular = '/<link[^>]+href=(?>\'|")(.*?\.css)(?>\'|").*?>/i';
<link开头|匹配首个非>(闭合标记)|匹配href=|匹配单双引号|匹配CSS路径|匹配单双引号|匹配任意字符|匹配闭合标签>|i不区分大小
preg_match_all($css_regular,$nod,$match_css) if(!empty($match_css[1])){
$exclude_css=$this->plug['config']['exclude_css'] $diff_css=array_diff($match_css[1],$exclude_css) if(!empty($diff_css)){
$css_file_name=substr(md5(implode('|', $diff_css)),8,16).'.css' $css_file_nod=DS.'skin'.DS.'speed'.DS.'css'.DS.$css_file_name $css_cache=file_exists(ROOT_PATH.$css_file_nod)?true:false $css_cache||($css_file_info="") foreach ($diff_css as $diff_css_key=>$diff_css_vo) {
$css_cache||($css_file_path=strpos($diff_css_vo, $css_cache||($css_file_info.=compress_css(file_get_contents($css_file_path))) $nod=str_replace($match_css[0][$diff_css_key],'',$nod) }
$css_cache||(file_put_contents(ROOT_PATH.$css_file_nod,$css_file_info)) $nod=str_replace('</head>','<link rel="stylesheet" href="'.$css_file_nod.'" media="all"/></head>',$nod) }
}
$js_regular = '/<script[^>]+src=(?>\'|")(.*?\.js)(?>\'|").*?<\/script>/i';
<script开头|匹配首个非>(闭合标记)|匹配src=|匹配单双引号|匹配JS路径|匹配单双引号|匹配任意字符|匹配闭合标签</script>|i不区分大小
preg_match_all($js_regular,$nod,$match_js) if(!empty($match_js[1])){
$exclude_js=$this->plug['config']['exclude_js'] $diff_js=array_diff($match_js[1],$exclude_js) if(!empty($diff_js)){
$js_file_name=substr(md5(implode('|', $diff_js)),8,16).'.js' $js_file_nod=DS.'skin'.DS.'speed'.DS.'js'.DS.$js_file_name $js_cache=file_exists(rtrim(ROOT_PATH,DS).$js_file_nod)?true:false $js_cache||($js_file_info="") foreach ($diff_js as $diff_js_key=>$diff_js_vo) {
$js_cache||($js_file_path=strpos($diff_js_vo, if(!$js_cache){
$jsmin=new \org\jsmin();
$js_file_info.=';'.$jsmin::minify(file_get_contents($js_file_path));
}
$nod=str_replace($match_js[0][$diff_js_key],'',$nod) }
$js_cache||(file_put_contents(rtrim(ROOT_PATH,DS).$js_file_nod,$js_file_info)) $nod=str_replace('</body>','<script src="'.$js_file_nod.'" type="text/javascript" charset="utf-8"></script></body>',$nod) }
}
$content=$nod;
}
public function check_cache_file(){
$cache_time=$this->plug['config']['cache_time'] $css_path=ROOT_PATH.'skin'.DS.'speed'.DS.'css'.DS;
$css_list=scandir($css_path);
foreach ($css_list as $css_list_vo) {
if(!in_array($css_list_vo,['.','..'])){
$create_time=filectime($css_path.$css_list_vo) if($create_time+$cache_time*3600<time()){
@unlink($css_path.$css_list_vo) }
}
}
$js_path=ROOT_PATH.'skin'.DS.'speed'.DS.'js'.DS;
$js_list=scandir($js_path);
foreach ($js_list as $js_list_vo) {
if(!in_array($js_list_vo,['.','..'])){
$create_time=filectime($js_path.$js_list_vo) if($create_time+$cache_time*3600<time()){
@unlink($js_path.$js_list_vo) }
}
}
}
public function save(){
$input=input('post.',null,'html_entity_decode') if(isset_full($input,'cache_time')){
Plug::where(['only'=>'speed'])->update(['config'=>json_encode($input)]);
return json (['state'=>'success']);
}else{
return json(['state'=>'error','info'=>'传入参数不完整!']);
}
}
}