-
Notifications
You must be signed in to change notification settings - Fork 59
/
functions.php
530 lines (499 loc) · 22.8 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
function themeConfig($form) {
$logoimg = new Typecho_Widget_Helper_Form_Element_Text('logoimg', NULL, NULL, _t('页头logo地址'), _t('一般为http://www.yourblog.com/image.png,支持 https:// 或 //,留空则使用默认图片'));
$form->addInput($logoimg->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$favicon = new Typecho_Widget_Helper_Form_Element_Text('favicon', NULL, NULL, _t('favicon地址'), _t('一般为http://www.yourblog.com/image.ico,支持 https:// 或 //,留空则不设置favicon'));
$form->addInput($favicon->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$appleicon = new Typecho_Widget_Helper_Form_Element_Text('appleicon', NULL, NULL, _t('apple touch icon地址'), _t('一般为http://www.yourblog.com/image.png,支持 https:// 或 //,留空则不设置Apple Touch Icon'));
$form->addInput($appleicon->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$email = new Typecho_Widget_Helper_Form_Element_Text('email', NULL, NULL, _t('Email地址'), _t('邮箱地址 ,留空则不设置Email地址'));
$form->addInput($email->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$github = new Typecho_Widget_Helper_Form_Element_Text('github', NULL, NULL, _t('Github地址'), _t('一般为https://github.com/Seevil ,留空则不设置Github地址'));
$form->addInput($github->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$twitter = new Typecho_Widget_Helper_Form_Element_Text('twitter', NULL, NULL, _t('twitter地址'), _t('一般为https://twitter.com/skyurl ,留空则不设置twitter地址'));
$form->addInput($twitter->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$weibo = new Typecho_Widget_Helper_Form_Element_Text('weibo', NULL, NULL, _t('Weibo地址'), _t('一般为http://www.weibo.com/xxx ,留空则不设置Weibo地址'));
$form->addInput($weibo->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$writing = new Typecho_Widget_Helper_Form_Element_Text('writing', NULL, NULL, _t('首页writing地址'), _t('一般设置为你的归档地址 ,留空则默认为/archives.html'));
$form->addInput($writing->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$Projectsurl = new Typecho_Widget_Helper_Form_Element_Text('Projectsurl', NULL, NULL, _t('首页Projects地址'), _t('一般为http://www.url.com/ ,留空则默认为#地址'));
$form->addInput($Projectsurl->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$urldiy = new Typecho_Widget_Helper_Form_Element_Text('urldiy', NULL, NULL, _t('主页自定义地址'), _t('注意该设置会直接输出设置内容,建议格式为《a class="icon" href="https://twitter.com/skyurl" target="_blank" title="twitter"》《i class="fa fa-twitter"》《/i》《/a》 支持 FontAwesome 图标 ,留空则不显示'));
$form->addInput($urldiy->addRule('xssCheck', _t('请不要在链接中使用特殊字符')));
$beian = new Typecho_Widget_Helper_Form_Element_Text('beian', NULL, NULL, _t('备案号设置'), _t('直接填写备案号即可如:京ICP备888888号'));
$form->addInput($beian->addRule('xssCheck', _t('请不要在图片链接中使用特殊字符')));
$sticky = new Typecho_Widget_Helper_Form_Element_Text('sticky', NULL,NULL, _t('文章置顶'), _t('置顶的文章cid,按照排序输入, 请以半角逗号或空格分隔'));
$form->addInput($sticky);
$Projects = new Typecho_Widget_Helper_Form_Element_Textarea('Projects', NULL, NULL, _t('首页 Projects 作品链接 设置(注意:切换主题会被清空,注意备份!)'), _t('按照格式输入链接信息,格式:<br><strong>链接名称(必须)|链接地址(必须)|链接描述</strong><br>不同信息之间用英文竖线“|”分隔,例如:<br><strong>XDE|https://www.krsay.com/|可说欢喜</strong><br>若中间有暂时不想填的信息,请留空,例如暂时不想填写链接描述:<br><strong>XDE|https://www.krsay.com||</strong><br>多个链接换行即可,一行一个'));
$form->addInput($Projects);
$catalog = new Typecho_Widget_Helper_Form_Element_Radio('catalog',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('文章目录设置'), _t('默认显示随机文章,启用则显示文章目录'));
$form->addInput($catalog);
$Emoji = new Typecho_Widget_Helper_Form_Element_Radio('Emoji',
array('able' => _t('启用'),
'disable' => _t('禁止'),
),
'disable', _t('Emoji表情设置'), _t('默认显示Emoji表情,如果你的数据库charset配置不是utf8mb4请禁用'));
$form->addInput($Emoji);
}
function themeInit($archive) {
if ($archive->is('index')) {
$archive->parameter->pageSize = 9; // 自定义条数
}
if ($archive->is('single')) {
$archive->content = createCatalog($archive->content);//文章锚点实现
}
@$comment = spam_protection_pre($comment,$post, $result);//数字验证码
}
function parseContent($obj){
$options = Typecho_Widget::widget('Widget_Options');
if(!empty($options->src_add) && !empty($options->cdn_add)){
$obj->content = str_ireplace($options->src_add,$options->cdn_add,$obj->content);
}
$obj->content = preg_replace("/<a href=\"([^\"]*)\">/i", "<a href=\"\\1\" target=\"_blank\" rel=\"nofollow\">", $obj->content); //新标签页打开连接
echo trim($obj->content);
}
/**项目展示
<?php Projects(); ?>
*/
function Projects($sorts = NULL) {
$options = Typecho_Widget::widget('Widget_Options');
$Project = NULL;
if ($options->Projects) {
$list = explode("\r\n", $options->Projects);
foreach ($list as $val) {
list($name, $url, $description, $sort) = explode("|", $val);
if ($sorts) {
$arr = explode("|", $sorts);
if ($sort && in_array($sort, $arr)) {
$Project .= $url ? '<li class="project-item"><a href="'.$url.'" target="_blank">'.$name.'</a>: '.$description.'</li>' : '<li class="project-item">'.$name.': '.$description.'</li>';
}
} else {
$Project .= $url ? '<li class="project-item"><a href="'.$url.'" target="_blank">'.$name.'</a>: '.$description.'</li>' : '<li class="project-item">'.$name.': '.$description.'</li>';
}
}
}
echo $Project ? $Project : '世间无限丹青手,一片伤心画不成。';
}
/**阅读浏览次数
<?php Postviews($this); ?>
*/
function Postviews($archive) {
$db = Typecho_Db::get();
$cid = $archive->cid;
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `'.$db->getPrefix().'contents` ADD `views` INT(10) DEFAULT 0;');
}
$exist = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid))['views'];
if ($archive->is('single')) {
$cookie = Typecho_Cookie::get('contents_views');
$cookie = $cookie ? explode(',', $cookie) : array();
if (!in_array($cid, $cookie)) {
$db->query($db->update('table.contents')
->rows(array('views' => (int)$exist+1))
->where('cid = ?', $cid));
$exist = (int)$exist+1;
array_push($cookie, $cid);
$cookie = implode(',', $cookie);
Typecho_Cookie::set('contents_views', $cookie);
}
}
echo $exist == 0 ? : $exist;
}
function theme_random_posts(){
$defaults = array(
'number' => 6,
'xformat' => '<li><a href="{permalink}">{title}</a></li>'
);
$db = Typecho_Db::get();
$adapterName = $db->getAdapterName();//兼容非MySQL数据库
if($adapterName == 'pgsql' || $adapterName == 'Pdo_Pgsql' || $adapterName == 'Pdo_SQLite' || $adapterName == 'SQLite'){
$order_by = 'RANDOM()';
}else{
$order_by = 'RAND()';
}
$sql = $db->select()->from('table.contents')
->where('status = ?','publish')
->where('type = ?', 'post')
->where('created <= unix_timestamp(now())', 'post') //添加这一句避免未达到时间的文章提前曝光
->limit($defaults['number'])
->order($order_by);
$result = $db->fetchAll($sql);
foreach($result as $val){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val);
echo str_replace(array('{permalink}', '{title}'),array($val['permalink'], $val['title']), $defaults['xformat']);
}
}
/** 获取浏览器信息 <?php echo getBrowser($comments->agent); ?> */
function getBrowser($agent)
{ $outputer = false;
if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) {
$outputer = 'IE Browser';
} else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) {
$str1 = explode('Firefox/', $regs[0]);
$FireFox_vern = explode('.', $str1[1]);
$outputer = 'Firefox Browser '. $FireFox_vern[0];
} else if (preg_match('/Maxthon([\d]*)\/([^\s]+)/i', $agent, $regs)) {
$str1 = explode('Maxthon/', $agent);
$Maxthon_vern = explode('.', $str1[1]);
$outputer = 'Maxthon Browser '.$Maxthon_vern[0];
} else if (preg_match('#SE 2([a-zA-Z0-9.]+)#i', $agent, $regs)) {
$outputer = 'Sogo Browser';
} else if (preg_match('#360([a-zA-Z0-9.]+)#i', $agent, $regs)) {
$outputer = '360 Browser';
} else if (preg_match('/Edge([\d]*)\/([^\s]+)/i', $agent, $regs)) {
$str1 = explode('Edge/', $regs[0]);
$Edge_vern = explode('.', $str1[1]);
$outputer = 'Edge '.$Edge_vern[0];
} else if (preg_match('/EdgiOS([\d]*)\/([^\s]+)/i', $agent, $regs)) {
$str1 = explode('EdgiOS/', $regs[0]);
$outputer = 'Edge';
} else if (preg_match('/UC/i', $agent)) {
$str1 = explode('rowser/', $agent);
$UCBrowser_vern = explode('.', $str1[1]);
$outputer = 'UC Browser '.$UCBrowser_vern[0];
}else if (preg_match('/OPR/i', $agent)) {
$str1 = explode('OPR/', $agent);
$opr_vern = explode('.', $str1[1]);
$outputer = 'Open Browser '.$opr_vern[0];
} else if (preg_match('/MicroMesseng/i', $agent, $regs)) {
$outputer = 'Weixin Browser';
} else if (preg_match('/WeiBo/i', $agent, $regs)) {
$outputer = 'WeiBo Browser';
} else if (preg_match('/QQ/i', $agent, $regs)||preg_match('/QQ Browser\/([^\s]+)/i', $agent, $regs)) {
$str1 = explode('rowser/', $agent);
$QQ_vern = explode('.', $str1[1]);
$outputer = 'QQ Browser '.$QQ_vern[0];
} else if (preg_match('/MQBHD/i', $agent, $regs)) {
$str1 = explode('MQBHD/', $agent);
$QQ_vern = explode('.', $str1[1]);
$outputer = 'QQ Browser '.$QQ_vern[0];
} else if (preg_match('/BIDU/i', $agent, $regs)) {
$outputer = 'Baidu Browser';
} else if (preg_match('/LBBROWSER/i', $agent, $regs)) {
$outputer = 'KS Browser';
} else if (preg_match('/TheWorld/i', $agent, $regs)) {
$outputer = 'TheWorld Browser';
} else if (preg_match('/XiaoMi/i', $agent, $regs)) {
$outputer = 'XiaoMi Browser';
} else if (preg_match('/UBrowser/i', $agent, $regs)) {
$str1 = explode('rowser/', $agent);
$UCBrowser_vern = explode('.', $str1[1]);
$outputer = 'UCBrowser '.$UCBrowser_vern[0];
} else if (preg_match('/mailapp/i', $agent, $regs)) {
$outputer = 'Email Browser';
} else if (preg_match('/2345Explorer/i', $agent, $regs)) {
$outputer = '2345 Browser';
} else if (preg_match('/Sleipnir/i', $agent, $regs)) {
$outputer = 'Sleipnir Browser';
} else if (preg_match('/YaBrowser/i', $agent, $regs)) {
$outputer = 'Yandex Browser';
} else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) {
$outputer = 'Opera Browser';
} else if (preg_match('/MZBrowser/i', $agent, $regs)) {
$outputer = 'MZ Browser';
} else if (preg_match('/VivoBrowser/i', $agent, $regs)) {
$outputer = 'Vivo Browser';
} else if (preg_match('/Quark/i', $agent, $regs)) {
$outputer = 'Quark Browser';
} else if (preg_match('/mixia/i', $agent, $regs)) {
$outputer = 'Mixia Browser';
}else if (preg_match('/fusion/i', $agent, $regs)) {
$outputer = 'Fusion';
} else if (preg_match('/CoolMarket/i', $agent, $regs)) {
$outputer = 'CoolMarket Browser';
} else if (preg_match('/Thunder/i', $agent, $regs)) {
$outputer = 'Thunder Browser';
} else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs)) {
$str1 = explode('Chrome/', $agent);
$chrome_vern = explode('.', $str1[1]);
$outputer = 'Chrome '.$chrome_vern[0];
} else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) {
$str1 = explode('Version/', $agent);
$safari_vern = explode('.', $str1[1]);
$outputer = 'Safari '.$safari_vern[0];
} else{
return false;
}
return $outputer;
}
/** 获取操作系统信息 <?php echo getOs($comments->agent); ?>*/
function getOs($agent)
{
$os = false;
if (preg_match('/win/i', $agent)) {
if (preg_match('/nt 10.0/i', $agent)) {
$os = 'Windows 10';
} else if (preg_match('/nt 6.1/i', $agent)) {
$os = 'Windows 7';
} else if (preg_match('/nt 6.2/i', $agent)) {
$os = 'Windows 8';
} else if(preg_match('/nt 6.3/i', $agent)) {
$os = 'Windows 8.1';
} else if(preg_match('/nt 5.1/i', $agent)) {
$os = 'Windows XP';
} else if (preg_match('/nt 6.0/i', $agent)) {
$os = 'Windows Vista';
} else{
$os = 'Windows 11';
}
} else if (preg_match('/android/i', $agent)) {
if (preg_match('/android 11/i', $agent)) {
$os = 'Android R';
}
else if (preg_match('/android 12/i', $agent)) {
$os = 'Android 12';
}
else if (preg_match('/android 10/i', $agent)) {
$os = 'Android Q';
}
else if (preg_match('/android 9/i', $agent)) {
$os = 'Android P';
}
else if (preg_match('/android 8/i', $agent)) {
$os = 'Android O';
}
else{
$os = 'Android';
}
}
else if (preg_match('/ubuntu/i', $agent)) {
$os = 'ubuntu';
} else if (preg_match('/linux/i', $agent)) {
$os = 'Linux';
} else if (preg_match('/iPhone/i', $agent)) {
$os = 'iPhone';
} else if (preg_match('/iPad/i', $agent)) {
$os = 'iPad';
} else if (preg_match('/mac/i', $agent)) {
$os = 'OSX';
}else if (preg_match('/cros/i', $agent)) {
$os = 'Chrome os';
}else {
return false;
}
return $os;
}
//html压缩
/***<?php $html_source = ob_get_contents(); ob_clean(); print compressHtml($html_source); ob_end_flush(); ?>**/
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;
}
//为文章标题添加锚点
function createCatalog($obj) {
global $catalog;
global $catalog_count;
$catalog = array();
$catalog_count = 0;
$obj = preg_replace_callback('/<h([1-6])(.*?)>(.*?)<\/h\1>/i', function($obj) {
global $catalog;
global $catalog_count;
$catalog_count ++;
$catalog[] = array('text' => trim(strip_tags($obj[3])), 'depth' => $obj[1], 'count' => $catalog_count);
return '<h'.$obj[1].$obj[2].'><a name="cl-'.$catalog_count.'"></a>'.$obj[3].'</h'.$obj[1].'>';
}, $obj);
return $obj;
}
//输出文章目录容器
function getCatalog() {
global $catalog;
$index = '';
if ($catalog) {
$prev_depth = '';
$to_depth = 0;
foreach($catalog as $catalog_item) {
$catalog_depth = $catalog_item['depth'];
if ($prev_depth) {
if ($catalog_depth == $prev_depth) {
$index .= '</li>';
} elseif ($catalog_depth > $prev_depth) {
$to_depth++;
} else {
$to_depth2 = ($to_depth > ($prev_depth - $catalog_depth)) ? ($prev_depth - $catalog_depth) : $to_depth;
if ($to_depth2) {
for ($i=0; $i<$to_depth2; $i++) {
$index .= '</li>';
$to_depth--;
}
}
$index .= '</li>';
}
}
$index .= '<li><a href="#cl-'.$catalog_item['count'].'">'.$catalog_item['text'].'</a>';
$prev_depth = $catalog_item['depth'];
}
for ($i=0; $i<=$to_depth; $i++) {
$index .= '</li>';
}
}
echo $index;
}
/**
* 根据$coid获取链接
*/
function getPermalinkFromCoid($coid)
{
$db = Typecho_Db::get();
$options = Helper::options();
$contents = Typecho_Widget::widget('Widget_Abstract_Contents');
$row = $db->fetchRow($db->select('cid, type, author, text')->from('table.comments')->where('coid = ? AND status = ?', $coid, 'approved'));
if (empty($row)) {
return 'Comment not found!';
}
$cid = $row['cid'];
$select = $db->select('coid, parent')->from('table.comments')->where('cid = ? AND status = ?', $cid, 'approved')->order('coid');
if ($options->commentsShowCommentOnly) {
$select->where('type = ?', 'comment');
}
$comments = $db->fetchAll($select);
if ($options->commentsOrder == 'DESC') {
$comments = array_reverse($comments);
}
foreach ($comments as $key => $val) {
$array[$val['coid']] = $val['parent'];
}
$i = $coid;
while ($i != 0) {
$break = $i;
$i = $array[$i];
}
$count = 0;
foreach ($array as $key => $val) {
if ($val == 0) {
$count++;
}
if ($key == $break) {
break;
}
}
$parentContent = $contents->push($db->fetchRow($contents->select()->where('table.contents.cid = ?', $cid)));
$permalink = rtrim($parentContent['permalink'], '/');
$page = ($options->commentsPageBreak) ? '/comment-page-' . ceil($count / $options->commentsPageSize) : (substr($permalink, -5, 5) == '.html' ? '' : '/');
return array("author" => $row['author'], "text" => $row['text'], "href" => "{$permalink}{$page}#{$row['type']}-{$coid}");
}
/**
* 输出评论回复内容,配合 commentAtContent($coid)一起使用
* <?php showCommentContent($comments->coid); ?>
*/
function showCommentContent($coid)
{
$db = Typecho_Db::get();
$result = $db->fetchRow($db->select('text')->from('table.comments')->where('coid = ? AND (status = ? OR status = ?)', $coid, 'approved','waiting'));
$text = $result['text'];
$atStr = commentAtContent($coid);
$_content = Markdown::convert($text);
//<p>
if ($atStr !== '') {
$content = substr_replace($_content, $atStr, 0, 3);
} else {
$content = $_content;
}
echo $content;
}
/**
* 评论回复加@
*/
function commentAtContent($coid)
{
$db = Typecho_Db::get();
$prow = $db->fetchRow($db->select('parent')->from('table.comments')->where('coid = ? AND (status = ? OR status = ?)', $coid, 'approved','waiting'));
$parent = $prow['parent'];
if ($parent != "0") {
$arow = $db->fetchRow($db->select('author')->from('table.comments')
->where('coid = ? AND (status = ? OR status = ?)', $parent, 'approved','waiting'));
$author = $arow['author'];
$href = '<p><a href="#comment-' . $parent . '">@' . $author . '</a> ';
return $href;
} else {
return '';
}
}
//算术验证评论
function spam_protection_math(){
$num1=1;
$num2=rand(1,9);
echo "$num1 + $num2 = ";
echo "<input type=\"text\" name=\"sum\" class=\"vnick vinput\" value=\"\" size=\"25\" tabindex=\"4\" style=\" width:70px;\" placeholder=\"计算结果\">\n";
echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">";
}
function spam_protection_pre($comment, $post, $result){
if(isset($_POST['sum'])){$sum=$_POST['sum'];}
switch($sum){
case $_POST['num1']+$_POST['num2']:
break;
case null:
throw new Typecho_Widget_Exception(_t('抱歉:请输入验证码','评论失败'));
break;
default:
throw new Typecho_Widget_Exception(_t('抱歉:验证码错误,请返回重试','评论失败'));
};
return $comment;
}
/**
* 获取扇贝单词每日一句
*/
function today(){
//删除之前的图片和故事
for ($i=1; $i <=30 ; $i++) {
@unlink(date('Ymd',time()-24*3600*$i).'.json');
}
$coverstory = date('Ymd').'.json'; //每日故事 json格式
if (!file_exists($coverstory)) {
$http = Typecho_Http_Client::get();
$http->Send('https://apiv3.shanbay.com/weapps/dailyquote/quote/');
$shanbaytoday = $http->getResponseBody();
file_put_contents($coverstory,$shanbaytoday); //写入文本
}
$coverstory = json_decode(file_get_contents($coverstory),true);
return $coverstory;
}