summaryrefslogtreecommitdiffstats
path: root/libs/Utils.php
blob: 27d7658b373421a52fd81f3654032c8602b6688f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

/**
 * Utils.php
 * 辅助工具
 * 
 * Author:  Eltrac(BigCoke233)
 * License: MIT
 */

class Utils
{
	
    /**
     * 输出相对首页路由,本方法会自适应伪静态,用于动态文件
     */
    public static function index($path = '')
    {
        Helper::options()->index($path);
    }

    /**
     * 输出相对首页路径,本方法用于静态文件
     */
    public static function indexHome($path = '')
    {
        Helper::options()->siteUrl($path);
    }

    /**
     * 输出相对主题目录路径,用于静态文件
     */
    public static function indexTheme($path = '')
    {
        Helper::options()->themeUrl($path);
    }

    /**
     * 判断插件是否可用(存在且已激活)
     */
    public static function hasPlugin($name) 
    {
        $plugins = Typecho_Plugin::export();
        $plugins = $plugins['activated'];
        return is_array($plugins) && array_key_exists($name, $plugins);
    }

    /**
     * 判断移动端请求
     */
    public static function isMobile()
    { 
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])){
            return TRUE;
        }
        
        if (isset ($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = array ('mobile','nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap'
                ); 
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){
                return TRUE;
            }
        }
        if (isset ($_SERVER['HTTP_ACCEPT'])){
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== FALSE) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === FALSE || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))){
                return TRUE;
            }
        }
        return FALSE;
    }

    /**
     * 判定内容是否过时
     * 
     * @return array
     */
    public static function isOutdated($archive)
    {
        date_default_timezone_set("Asia/Shanghai");
        $created = round((time()- $archive->created) / 3600 / 24);
        $updated = round((time()- $archive->modified) / 3600 / 24);
        return array("is" => $created > 90,
                    "created" => $created,
                    "updated" => $updated);
    }
	
	/**
     * 编辑界面添加内容
     */
    public static function addButton(){
		echo '<link rel="stylesheet" href="/usr/themes/Miracles/assets/css/setting.miracles.css" />';
		echo '<link rel="stylesheet" href="/usr/themes/Miracles/assets/css/owo.min.css" />';
		echo '<script src="/usr/themes/Miracles/assets/js/OwO.min.js"></script>';
		echo '<script src="/usr/themes/Miracles/assets/js/editor.js"></script>';
		echo '<style>#custom-field textarea{width:100%}
        .OwO span{background:none!important;width:unset!important;height:unset!important}
        .OwO .OwO-logo{
            z-index: unset!important;
        }
        .OwO .OwO-body .OwO-items{
            -webkit-overflow-scrolling: touch;
            overflow-x: hidden;
        }
        .OwO .OwO-body .OwO-items-image .OwO-item{
            max-width:-moz-calc(20% - 10px);
            max-width:-webkit-calc(20% - 10px);
            max-width:calc(20% - 10px)
        }
		#wmd-owo-button:hover{
		background:transparent!important}
        @media screen and (max-width:760px){
            .OwO .OwO-body .OwO-items-image .OwO-item{
                max-width:-moz-calc(25% - 10px);
                max-width:-webkit-calc(25% - 10px);
                max-width:calc(25% - 10px)
            }
        }</style>';
    }

    /**
     * 自动生成引用
     */
    public static function addRequires($files,$type,$cdn,$custom) {
        if ($cdn=='1'){
            $path = 'https://cdn.jsdelivr.net/gh/BigCoke233/miracles@'.themeVersion()."/assets/";
	    }
	    elseif ($cdn=='2'){
	        $path = 'https://raw.githack.com/BigCoke233/miracles/master/assets/';
	    }
	    elseif ($cdn=='4'){
		    $path = 'https://rawcdn.githack.com/BigCoke233/miracles/master/assets/';
	    }
	    elseif ($cdn=='5'){
		    $path = 'https://cdn.9jojo.cn/Miracles/'.themeVersion().'/';
	    }
	    elseif ($cdn=='3'){
		    $path = $custom;
	    }else{
		    $path = Helper::options()->themeUrl("","Miracles/assets");
	    }
        foreach ($files as $file) {
            if ($type == "js"){
                echo "<script src=".$path. $type."/".$file.".".$type."></script>";
		    }
            elseif ($type == "css"){
                echo "<link rel=\"stylesheet\" href=".$path. $type."/".$file.".".$type." />";
		    }
        }
    }

    /**
     * 缩略图
     */
    public static function postBanner($post){
		if($post->fields->banner && $post->fields->banner=!''): 
			$banner = $post->fields->banner; 
		else: 
			if($GLOBALS['miraclesOptions_randomBanner']==''){
                $banner = '/usr/themes/Miracles/images/postbg/';
                $banner .= srand(mb_strlen($post->title));
                $banner .= mt_rand(1,15).'.jpg';
		    }
		    else{
		        $banner_url = explode(',', $GLOBALS['miraclesOptions_randomBanner']);
                $banner = $banner_url[mt_rand(0,count($banner_url)-1)];
            }
        endif;
        //使用 TimThumb 剪裁
        if($GLOBALS['miraclesIfTimThumb']=='on') {
            if($GLOBALS['miraclesIfTimThumbSize']=='regular'){
                $banner_size = '&h=336&w=564';
            }
            elseif($GLOBALS['miraclesIfTimThumbSize']=='big'){
                $banner_size = '&h=420&w=705';
            }
            elseif($GLOBALS['miraclesIfTimThumbSize']=='large'){
                $banner_size = '&h=504&w=846';
            }
            elseif($GLOBALS['miraclesIfTimThumbSize']=='huge'){
                $banner_size = '&h=560&w=940';
            }
            else{
                //避免有憨憨打错单词,当成 big 来算
                $banner_size = '&h=420&w=705';
            }
            $banner_url = '/usr/themes/Miracles/libs/TimThumb.php';
            $banner = $banner_url.'?src='.$banner.$banner_size;
        }
        //不知道为什么会有奇怪的空格,所以这里用暴力的方法去掉
        $banner = trim($banner);

        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;
    }
}