-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.php
executable file
·245 lines (213 loc) · 8.15 KB
/
functions.php
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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
function themeConfig($form)
{
$logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', null, null, _t('站点LOGO地址'), _t('在这里填入一个图片URL地址, 以在网站标题前加上一个LOGO'));
$form->addInput($logoUrl);
$sidebarBlock = new Typecho_Widget_Helper_Form_Element_Checkbox('sidebarBlock',
array('ShowRecentPosts' => _t('显示最新文章'),
'ShowRecentComments' => _t('显示最近回复'),
'ShowCategory' => _t('显示分类'),
'ShowArchive' => _t('显示归档'),
'ShowOther' => _t('显示其它杂项')),
array('ShowRecentPosts', 'ShowRecentComments', 'ShowCategory', 'ShowArchive', 'ShowOther'), _t('侧边栏显示'));
$form->addInput($sidebarBlock->multiMode());
}
/*
function themeFields($layout) {
$logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('站点LOGO地址'), _t('在这里填入一个图片URL地址, 以在网站标题前加上一个LOGO'));
$layout->addItem($logoUrl);
}
*/
function get_post_img($cid)
{
$db = Typecho_Db::get();
$rs = $db->fetchRow($db->select('table.contents.text')
->from('table.contents')
->where('table.contents.cid=?', $cid)
->order('table.contents.cid', Typecho_Db::SORT_ASC)
->limit(1));
$text = $rs['text'];
preg_match('/!\[[^\]]*]\([^\)]*\.(png|jpeg|jpg|gif|bmp)\)/i', $text, $md_img);
if (empty($md_img)) {
return null;
} else {
$img = substr_between($md_img[0], '(', ')');
return $img;
}
}
function substr_between($string, $start, $end = null)
{
if (($start_pos = strpos($string, $start)) !== false) {
if ($end) {
if (($end_pos = strpos($string, $end, $start_pos + strlen($start))) !== false) {
return substr($string, $start_pos + strlen($start), $end_pos - ($start_pos + strlen($start)));
}
} else {
return substr($string, $start_pos);
}
}
return false;
}
function get_author_info($cont)
{
$db = Typecho_Db::get();
$rs = $db->fetchRow($db->select('*')->from('table.users')
->where('table.users.uid=?', 1));
$array = array('name' => $rs['screenName'],
'mail' => $rs['mail'],
'avatar' => get_avatar($rs['mail'], 120),
'short_name' => 'sora_yakami',
'city'=>'Shanghai'
);
return $array[$cont];
}
function get_avatar($mail, $size)
{
return 'https://cdn.v2ex.com/gravatar/' . md5($mail) . '?s=' . $size . '&r=G&d=';
}
function get_visitor()
{
$db = Typecho_Db::get();
$rs = $db->fetchAll($db->select('author,mail,url')->from('table.comments')
->where('status=? AND authorID=?', 'approved', 0)
->order('coid', Typecho_Db::SORT_DESC));
$array = array_slice(unique_arr($rs), 0, 15);
foreach ($array as $key => $value) {
echo '<li class="ProfileUserList-facepileItem">
<a class="u-block js-tooltip js-nav" href="' . $array[$key]['url'] . '">
<img class="Avatar Avatar--size48" src="' . get_avatar($array[$key]['mail'], 60) . '" alt="' . $array[$key]['author'] . '">
</a></li>';
}
}
function get_commenters($cid, $action = 'list')
{
$db = Typecho_Db::get();
$rs = $db->fetchAll($db->select('author,mail,url')->from('table.comments')
->where('status=? AND cid=? AND authorID=?', 'approved', $cid, 0)
->order('coid', Typecho_Db::SORT_DESC));
if ($rs == null) {
$unique = null;
$array = null;
} else {
$unique = unique_arr($rs);
$array = array_slice($unique, 0, 12);
}
switch ($action) {
case 'list':
if ($array == null) {
return;
}
foreach ($array as $key => $value) {
echo '<a class="js-profile-popup-actionable js-user-tipsy js-tooltip" href="' . $array[$key]['url'] . '">
<img class="avatar size24 js-user-profile-link" src="' . get_avatar($array[$key]['mail'], 60) . '" alt="' . $array[$key]['author'] . '">
</a>' ;
}
break;
case 'comments_count':
echo count($rs);
break;
case 'commenter_count':
echo count($unique);
break;
}
}
function unique_arr($array2D, $stkeep = false, $ndformat = true)
{
// 判断是否保留一级数组键 (一级数组键可以为非数字)
if ($stkeep) {
$stArr = array_keys($array2D);
}
// 判断是否保留二级数组键 (所有二级数组键必须相同)
if ($ndformat) {
$ndArr = array_keys(end($array2D));
}
//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
foreach ($array2D as $v) {
$v = join(",", $v);
$temp[] = $v;
}
//去掉重复的字符串,也就是重复的一维数组
$temp = array_unique($temp);
//再将拆开的数组重新组装
foreach ($temp as $k => $v) {
if ($stkeep) {
$k = $stArr[$k];
}
if ($ndformat) {
$tempArr = explode(",", $v);
foreach ($tempArr as $ndkey => $ndval) {
$output[$k][$ndArr[$ndkey]] = $ndval;
}
} else {
$output[$k] = explode(",", $v);
}
}
return $output;
}
function get_comment_at($coid)
{
$db = Typecho_Db::get();
$prow = $db->fetchRow($db->select('parent')->from('table.comments')
->where('coid = ? AND status = ?', $coid, 'approved'));
$parent = $prow['parent'];
if ($parent != 0) {
$arow = $db->fetchRow($db->select('author')->from('table.comments')
->where('coid = ? AND status = ?', $parent, 'approved'));
$author = $arow['author'];
echo $author;
} else {
echo get_author_info('name');
}
}
function get_filtered_comment($coid){
$db = Typecho_Db::get();
$rs=$db->fetchRow($db->select('text')->from('table.comments')
->where('coid = ? AND status = ?', $coid, 'approved'));
$content=$rs['text'];
echo $content;
}
function get_count($cont)
{
$db = Typecho_Db::get();
switch ($cont) {
case 'article':
$rs = $db->fetchAll($db->select('*')->from('table.contents')
->where('status = ?', 'publish'));
break;
case 'comments':
$rs = $db->fetchAll($db->select('*')->from('table.comments')
->where('status = ?', 'approved'));
break;
case 'tags':
$rs = $db->fetchAll($db->select('*')->from('table.metas'));
break;
}
echo count($rs);
}
function is_mobile()
{
//return true;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_browser = array(
"mqqbrowser", //手机QQ浏览器
"opera mobi", //手机opera
"juc", "iuc", //uc浏览器
"fennec", "ios", "applewebKit/420", "applewebkit/525", "applewebkit/532", "ipad", "iphone", "ipaq", "ipod",
"iemobile", "windows ce", //windows phone
"240x320", "480x640", "acer", "android", "anywhereyougo.com", "asus", "audio", "blackberry",
"blazer", "coolpad", "dopod", "etouch", "hitachi", "htc", "huawei", "jbrowser", "lenovo",
"lg", "lg-", "lge-", "lge", "mobi", "moto", "nokia", "phone", "samsung", "sony",
"symbian", "tablet", "tianyu", "wap", "xda", "xde", "zte",
);
$is_mobile = false;
foreach ($mobile_browser as $device) {
if (stristr($user_agent, $device)) {
$is_mobile = true;
break;
}
}
return $is_mobile;
}