This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfunctions.php
341 lines (295 loc) · 10.3 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 主题配置
*/
function themeConfig($form)
{
// 头像图
$logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('头像图'), _t('这里填写 URL 地址,最好能走cdn或者oss,毕竟带宽小'));
$form->addInput($logoUrl);
// 你的名字
$yourname = new Typecho_Widget_Helper_Form_Element_Text('yourname', NULL, NULL, _t('你的名字'), _t('在左上角头向下面,太长会自动...省略'));
$form->addInput($yourname);
// ICP备案号
$icpNum = new Typecho_Widget_Helper_Form_Element_Text('icpNum', NULL, NULL, _t('ICP备案号'), _t('管局爸爸很严厉,备案过程中最好关闭站点,如:苏ICP备19008833号'));
$form->addInput($icpNum);
// 座右铭好
$motto = new Typecho_Widget_Helper_Form_Element_Text('motto', NULL, NULL, _t('座右铭'), _t('座右铭会默认显示在首页'));
$form->addInput($motto);
// 友情链接
$guy_link = new Typecho_Widget_Helper_Form_Element_Textarea('guy_link', null, null, _t('友情链接'), _t('格式:{头像url=>名字=>网址=>骚话}'));
$form->addInput($guy_link);
// 百度统计
$baiduAnlysis = new Typecho_Widget_Helper_Form_Element_Text('baiduAnlysis', null, null, _t('百度统计'), _t('注册百度统计账号->新增网站->代码获取->找到 hm.src = "https://hm.baidu.com/hm.js?f045130eebcf5750255929de04bd5bff"; ->复制(问号后面的一串)'));
$form->addInput($baiduAnlysis);
// 实验室
$labs = new Typecho_Widget_Helper_Form_Element_Checkbox(
'labs',
array(
'showCopyright' => _t('文章底部版权'),
'showYiyan' => _t('一言(会替代座右铭显示内容)'),
),
array('showYiyan', 'showCopyright'),
_t('实验室'),
_t('探索未来科技,骚东西,还有Bug')
);
$form->addInput($labs->multiMode());
// 侧边栏
$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);
}
*/
/**
* 文章缩略图
* <?php echo showThumb($this, true, w, h); ?>
*/
function showThumb($obj, $link = false, $w, $h)
{
preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/", $obj->content, $matches);
$thumb = '';
$attach = $obj->attachments(1)->attachment;
if (isset($attach->isImage) && $attach->isImage == 1) {
$thumb = $attach->url; //附件是图片 输出附件
} elseif (isset($matches[1][0])) {
$thumb = $matches[1][0]; //文章内容中抓到了图片 输出链接
}
$thumb = empty($thumb) ? 'https://unsplash.it/' . $w . '/' . $h . '/' : $thumb; //再没有 就用随机图片
if ($link) {
echo $thumb;
} else {
$thumb = '<img src="' . $thumb . '">';
echo $thumb;
}
}
/**
* 文章创建日期转 xx前
*/
function diffTime($time = NULL)
{
$text = '';
$time = $time === NULL || $time > time() ? time() : intval($time);
$t = time() - $time; //时间差 (秒)
$y = date('Y', $time) - date('Y', time()); //是否跨年
switch ($t) {
case $t == 0:
$text = '刚刚';
break;
case $t < 60:
// $text = $t . '秒前'; // 一分钟内
$text = $t . 's'; // 一分钟内
break;
case $t < 60 * 60:
// $text = floor($t / 60) . '分钟前'; //一小时内
$text = floor($t / 60) . 'min'; //一小时内
break;
case $t < 60 * 60 * 24:
// $text = floor($t / (60 * 60)) . '小时前'; // 一天内
$text = floor($t / (60 * 60)) . 'h';
break;
case $t < 60 * 60 * 24 * 30:
// $text = floor($t / 86400) . '天前';
$text = floor($t / 86400) . 'd';
break;
case $t < 60 * 60 * 24 * 365 && $y == 0:
// $text = date('m月d日', $time); //一年内
$text = date('m月', $time); //一年内
break;
default:
// $text = date('Y年m月d日', $time); //一年以前
$text = date('Y年', $time); //一年以前
break;
}
echo $text;
}
/**
* 处理字符串超长问题
*/
function subText($text, $maxlen)
{
if (mb_strlen($text) > $maxlen) {
echo mb_substr($text, 0, $maxlen) . '...';
} else {
echo $text;
}
}
/**
* 字符串截取的百分比
*/
function perText($text, $maxlen)
{
if (mb_strlen($text) > $maxlen) {
echo ceil($maxlen / mb_strlen($text)) . '%';
} else {
echo '100%';
}
}
/**
* 名字首字作头像
*/
function nameToImg($text)
{
echo mb_substr($text, 0, 1);
}
/**
* 文章数量控制
*/
function themeInit($archive)
{
// 分类
if ($archive->is('category', 'default')) { //jobs为你的分类的slug名称
$archive->parameter->pageSize = 10; // 自定义条数
}
// 首页
if ($archive->is('index')) {
$archive->parameter->pageSize = 10;
}
}
/**
* 文章阅读数量
* <?php get_post_view($this) ?>
*/
function get_post_view($archive)
{
$cid = $archive->cid;
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
echo 0;
return;
}
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
if ($archive->is('single')) {
$db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
}
echo $row['views'];
}
/**
* 字数统计
* <?php art_count($this->cid); ?>
*/
function art_count($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));
echo mb_strlen($rs['text'], 'UTF-8');
}
/**
* 预计阅读时间
*/
function readTime($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));
echo ceil(mb_strlen($rs['text'], 'UTF-8') / 300) . '分钟';
}
/**
* ip获得地址
*/
function getLocation($ip)
{
$res = '';
if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
$API = 'http://ip.taobao.com/service/getIpInfo.php?ip=';
$re = json_decode(file_get_contents($API . $ip));
$res = $re->data->country . ' ' . $re->data->region . ' ' . $re->data->city;
} else {
$res = '未知';
}
echo $res;
}
/**
* 修复pjax评论
*/
function fixPjaxComment($archive)
{
echo "<script type=\"text/javascript\">
(function () {
window.TypechoComment = {
dom : function (id) {
return document.getElementById(id);
},
create : function (tag, attr) {
var el = document.createElement(tag);
for (var key in attr) {
el.setAttribute(key, attr[key]);
}
return el;
},
reply : function (cid, coid) {
var comment = this.dom(cid), parent = comment.parentNode,
response = this.dom('" . $archive->respondId . "'), input = this.dom('comment-parent'),
form = 'form' == response.tagName ? response : response.getElementsByTagName('form')[0],
textarea = response.getElementsByTagName('textarea')[0];
if (null == input) {
input = this.create('input', {
'type' : 'hidden',
'name' : 'parent',
'id' : 'comment-parent'
});
form.appendChild(input);
}
input.setAttribute('value', coid);
if (null == this.dom('comment-form-place-holder')) {
var holder = this.create('div', {
'id' : 'comment-form-place-holder'
});
response.parentNode.insertBefore(holder, response);
}
comment.appendChild(response);
this.dom('cancel-comment-reply-link').style.display = '';
if (null != textarea && 'text' == textarea.name) {
textarea.focus();
}
return false;
},
cancelReply : function () {
var response = this.dom('{$archive->respondId}'),
holder = this.dom('comment-form-place-holder'), input = this.dom('comment-parent');
if (null != input) {
input.parentNode.removeChild(input);
}
if (null == holder) {
return true;
}
this.dom('cancel-comment-reply-link').style.display = 'none';
holder.parentNode.insertBefore(response, holder);
return false;
}
};
})();
</script>
";
}
/**
* 格式化友情链接
*/
function formateGuys($str)
{
$res = [];
preg_match_all("/\[(.*)\]/", $str, $match);
if (count($match) > 1) {
foreach ($match[1] as $m) {
$temp = explode("=>", $m);
array_push($res, array('a' => $temp[0], 'b' => $temp[1], "c" => $temp[2], "d" => $temp[3]));
}
}
return $res;
}