├── CHANGELOG.md ├── Plugin.php └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | Version 1.0.0.1 *(2016-12-31)* 5 | ---------------------------- 6 | 7 | * 兼容FileCache插件,请安装本插件再安装FileCache插件 8 | -------------------------------------------------------------------------------- /Plugin.php: -------------------------------------------------------------------------------- 1 | begin = array('CompressHTML_Plugin', 'Widget_index_begin'); 23 | //Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('CompressHTML_Plugin', 'Widget_Archive_beforeRender'); 24 | } 25 | 26 | /** 27 | * 禁用插件方法,如果禁用失败,直接抛出异常 28 | * 29 | * @static 30 | * @access public 31 | * @return void 32 | * @throws Typecho_Plugin_Exception 33 | */ 34 | public static function deactivate() { 35 | } 36 | 37 | /** 38 | * 获取插件配置面板 39 | * 40 | * @access public 41 | * @param Typecho_Widget_Helper_Form $form 配置面板 42 | * @return void 43 | */ 44 | public static function config(Typecho_Widget_Helper_Form $form) { 45 | $compression_open = new Typecho_Widget_Helper_Form_Element_Checkbox('compression_open', array('compression_open' => '开启gzip'), null, _t('是否开启gzip')); 46 | $form->addInput($compression_open); 47 | 48 | $compression_level = new Typecho_Widget_Helper_Form_Element_Select('compression_level', array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9'), '5', _t('gzip压缩级别:'), _t('这个参数值范围是0-9,0表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU。*推荐:5')); 49 | $form->addInput($compression_level); 50 | 51 | $compress_html = new Typecho_Widget_Helper_Form_Element_Checkbox('compress_html', array('compress_html' => '开启压缩HTML'), null, _t('是否开启压缩HTML'), _t('当开启后页面与原来页面不一致时请关闭')); 52 | $form->addInput($compress_html); 53 | 54 | $keyword_replace = new Typecho_Widget_Helper_Form_Element_Textarea('keyword_replace', null, null, _t('HTML关键词替换'), _t('作用:主要把附件的内容转到七牛。一行一个。格式:关键词=替换关键词')); 55 | $form->addInput($keyword_replace); 56 | } 57 | 58 | /** 59 | * 个人用户的配置面板 60 | * 61 | * @access public 62 | * @param Typecho_Widget_Helper_Form $form 63 | * @return void 64 | */ 65 | public static function personalConfig(Typecho_Widget_Helper_Form $form) { 66 | } 67 | 68 | public static function Widget_index_begin() { 69 | ob_start('CompressHTML_Plugin::qlwz_ob_handler'); 70 | } 71 | 72 | public static function Widget_Archive_beforeRender() { 73 | ob_start('CompressHTML_Plugin::qlwz_ob_handler'); 74 | } 75 | 76 | public static function qlwz_ob_handler($buffer) { 77 | $settings = Helper::options()->plugin('CompressHTML'); 78 | 79 | if ($settings->keyword_replace) { 80 | $list = explode("\r\n", $settings->keyword_replace); 81 | foreach ($list as $tmp) { 82 | list($old, $new) = explode('=', $tmp); 83 | $buffer = str_replace($old, $new, $buffer); 84 | } 85 | } 86 | 87 | if ($settings->compress_html) { 88 | $buffer = self::qlwz_compress_html($buffer); 89 | } 90 | 91 | if ($settings->compression_open) { 92 | $buffer = self::ob_gzip($buffer, $settings->compression_level); 93 | } else { 94 | if (ini_get('zlib.output_compression')) { 95 | ini_set('zlib.output_compression', 'Off'); 96 | } 97 | } 98 | return $buffer; 99 | } 100 | 101 | public static function ob_gzip($buffer, $level) { 102 | if (ini_get('zlib.output_compression')) { 103 | if (ini_get('zlib.output_compression_level') != $level) { 104 | ini_set('zlib.output_compression_level', $level); 105 | } 106 | return $buffer; 107 | } 108 | if (headers_sent() || !extension_loaded('zlib') || !strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) { 109 | return $buffer; 110 | } 111 | 112 | $out_buffer = gzencode($buffer, $level); 113 | if (strlen($out_buffer) < strlen($buffer)) { 114 | header('Content-Encoding: gzip'); 115 | header('Vary: Accept-Encoding'); 116 | header('Content-Length: ' . strlen($out_buffer)); 117 | } else { 118 | $out_buffer = $buffer; 119 | } 120 | return $out_buffer; 121 | } 122 | 123 | /** 124 | * 压缩HTML代码 125 | * 126 | * @author 情留メ蚊子 127 | * @version 1.0.0.0 By 2016-11-23 128 | * @param string $html_source HTML源码 129 | * @return string 压缩后的代码 130 | */ 131 | public static function qlwz_compress_html($html_source) { 132 | $chunks = preg_split('/(.*?|.*?<\/nocompress>|||)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE); 133 | $compress = ''; 134 | foreach ($chunks as $c) { 135 | if (strtolower(substr($c, 0, 19)) == '') { 136 | $c = substr($c, 19, strlen($c) - 19 - 20); 137 | $compress .= $c; 138 | continue; 139 | } else if (strtolower(substr($c, 0, 12)) == '') { 140 | $c = substr($c, 12, strlen($c) - 12 - 13); 141 | $compress .= $c; 142 | continue; 143 | } else if (strtolower(substr($c, 0, 4)) == ' $char) { 157 | if ($char == '"' && $chars[$key - 1] != '\\' && !$is_apos) { 158 | $is_quot = !$is_quot; 159 | } else if ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) { 160 | $is_apos = !$is_apos; 161 | } else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) { 162 | $tmp = substr($tmp, 0, $key); // 不是字符串内的就是注释 163 | break; 164 | } 165 | } 166 | } 167 | $c .= $tmp; 168 | } 169 | } 170 | $c = preg_replace('/[\\n\\r\\t]+/', ' ', $c); // 清除换行符,清除制表符 171 | $c = preg_replace('/\\s{2,}/', ' ', $c); // 清除额外的空格 172 | $c = preg_replace('/>\\s <', $c); // 清除标签间的空格 173 | $c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c); // 清除 CSS & JS 的注释 174 | $c = preg_replace('//', '', $c); // 清除 HTML 的注释 175 | $compress .= $c; 176 | } 177 | return $compress; 178 | } 179 | 180 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CompressHTML For Typecho 2 | 3 | Typecho插件:该插件支持开启gzip、压缩HTML、HTML关键词替换 4 | 5 | 开发者:情留メ蚊子 6 | 7 | 插件地址:[http://www.94qing.com/compresshtml-for-typecho.html](http://www.94qing.com/compresshtml-for-typecho.html) 8 | 9 | 最新版本:1.0.0.1 10 | 11 | 更新日期:2016-12-23 --------------------------------------------------------------------------------