summaryrefslogtreecommitdiffstats
path: root/libs/Utils.php
diff options
context:
space:
mode:
Diffstat (limited to 'libs/Utils.php')
-rw-r--r--libs/Utils.php59
1 files changed, 56 insertions, 3 deletions
diff --git a/libs/Utils.php b/libs/Utils.php
index 541e46d..b78aa06 100644
--- a/libs/Utils.php
+++ b/libs/Utils.php
@@ -157,17 +157,17 @@ class Utils
/**
* 缩略图
*/
- public static function postBanner($post){
+ public static function postBanner($post, $random){
if($post->fields->banner && $post->fields->banner=!''):
$banner = $post->fields->banner;
else:
- if($post->options->randomBanner==''){
+ if($random==''){
$banner = '/usr/themes/Miracles/images/postbg/';
$banner .= srand(mb_strlen($post->title));
$banner .= rand(1,15).'.jpg';
}
else{
- $banner_url = explode(',',$post->options->randomBanner);
+ $banner_url = explode(',',$random);
$banner = $banner_url[mt_rand(0,count($banner_url)-1)].'\"';
}
endif;
@@ -195,4 +195,57 @@ class Utils
echo $banner;
}
+
+ /**
+ * 压缩 HTML
+ * thanks https://www.jiangxu.site/concerning-how-to-typecho-integrated-html-compression.html
+ */
+ public static function compressHtml($html_source) {
+ $chunks = preg_split('/(<!--<nocompress>-->.*?<!--<\/nocompress>-->|<nocompress>.*?<\/nocompress>|<pre.*?\/pre>|<textarea.*?\/textarea>|<script.*?\/script>)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE);
+ $compress = '';
+ foreach ($chunks as $c) {
+ if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') {
+ $c = substr($c, 19, strlen($c) - 19 - 20);
+ $compress .= $c;
+ continue;
+ } else if (strtolower(substr($c, 0, 12)) == '<nocompress>') {
+ $c = substr($c, 12, strlen($c) - 12 - 13);
+ $compress .= $c;
+ continue;
+ } else if (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') {
+ $compress .= $c;
+ continue;
+ } else if (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') != false && (strpos($c, "\r") !== false || strpos($c, "\n") !== false)) {
+ $tmps = preg_split('/(\r|\n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY);
+ $c = '';
+ foreach ($tmps as $tmp) {
+ if (strpos($tmp, '//') !== false) {
+ if (substr(trim($tmp), 0, 2) == '//') {
+ continue;
+ }
+ $chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY);
+ $is_quot = $is_apos = false;
+ foreach ($chars as $key => $char) {
+ if ($char == '"' && $chars[$key - 1] != '\\' && !$is_apos) {
+ $is_quot = !$is_quot;
+ } else if ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) {
+ $is_apos = !$is_apos;
+ } else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) {
+ $tmp = substr($tmp, 0, $key);
+ break;
+ }
+ }
+ }
+ $c .= $tmp;
+ }
+ }
+ $c = preg_replace('/[\\n\\r\\t]+/', ' ', $c);
+ $c = preg_replace('/\\s{2,}/', ' ', $c);
+ $c = preg_replace('/>\\s</', '> <', $c);
+ $c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c);
+ $c = preg_replace('/<!--[^!]*-->/', '', $c);
+ $compress .= $c;
+ }
+ return $compress;
+ }
} \ No newline at end of file