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
|
<?php
/*
* 该文件代码来自 熊猫小A(AlanDecode) 的项目,感谢~
* https://github.com/AlanDecode/typecho-theme-dev-framework
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
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);
}
/**
* 根据邮箱返回 Gravatar 头像链接
*/
public static function gravatar($mail, $size = 64, $d = '')
{
return Typecho_Common::gravatarUrl($mail, $size, '', urlencode($d), true);
}
/**
* 判断插件是否可用(存在且已激活)
*/
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;
}
/**
* 判定电脑端微信内置浏览器
*/
public static function isWeixin()
{
return !self::isMobile() && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
}
/**
* 判定内容是否过时
* $timeout:过期时间,单位为天
*/
public static function isOutdated($archive, $timeout)
{
date_default_timezone_set("Asia/Shanghai");
return round((time()- $archive->created) / 3600 / 24) > $timeout;
}
/**
* 输出建站时间(最早一篇文章的写作时间)
*/
public static function getBuildTime()
{
date_default_timezone_set("Asia/Shanghai");
$db = Typecho_Db::get();
$content = $db->fetchRow($db->select()->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1));
return $content['created'];
}
/**
* 已发布文章数量
*/
public static function getPostNum()
{
$db = Typecho_Db::get();
return $db->fetchObject($db->select(array('COUNT(cid)' => 'num'))
->from('table.contents')
->where('table.contents.type = ?', 'post')
->where('table.contents.status = ?', 'publish'))->num;
}
/**
* 分类数量
*/
public static function getCatNum()
{
$db = Typecho_Db::get();
return $db->fetchObject($db->select(array('COUNT(mid)' => 'num'))
->from('table.metas')
->where('table.metas.type = ?', 'category'))->num;
}
/**
* 标签数量
*/
public static function getTagNum()
{
$db = Typecho_Db::get();
return $db->fetchObject($db->select(array('COUNT(mid)' => 'num'))
->from('table.metas')
->where('table.metas.type = ?', 'tag'))->num;
}
/**
* 总字数
*
* @return int
*/
public static function getWordCount()
{
$db = Typecho_Db::get();
$posts = $db->fetchAll($db->select('table.contents.text')
->from('table.contents')
->where('table.contents.type = ?', 'post')
->where('table.contents.status = ?', 'publish'));
$total = 0;
foreach ($posts as $post) {
$total = $total + mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $post['text']), 'UTF-8');
}
return $total;
}
}
|