-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathfunctions.php
1243 lines (1159 loc) · 44.7 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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Typecho\Plugin;
if (!defined('__TYPECHO_ROOT_DIR__'))
exit;
require_once('api/api.php');
require_once('config/custom_config.php');
// 新文章缩略图
function get_ArticleThumbnail($widget)
{
// 当文章无图片时的随机缩略图
// $rand = mt_rand(1, 26); // 随机 1-9 张缩略图
// // 缩略图加速
// $rand_url;
// if(!empty(Helper::options()->articleImgSpeed)){
// $rand_url = Helper::options()->articleImgSpeed;
// }else {
// $rand_url = $widget->widget('Widget_Options')->themeUrl . '/images/articles/';
// }
// $random = $rand_url . $rand . '.jpg'; // 随机缩略图路径
// $random = 'https://static01.imgkr.com/temp/517e5d14c312427dbf93304563869279.png';
// $attach = $widget->attachments(1)->attachment;
$random = '/usr/themes/butterfly/img/DefualtThumbnail.jpg';
$pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
//如果有自定义缩略图
if ($widget->fields->thumb) {
return $widget->fields->thumb;
} else if (preg_match_all($pattern, $widget->content, $thumbUrl) && strlen($thumbUrl[1][0]) > 7) {
return $thumbUrl[1][0];
} else {
return $random;
}
}
// 主页文章缩略图
function GetRandomThumbnail($widget)
{
// $random = 'https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg';
$random = '/usr/themes/butterfly/img/DefualtThumbnail.jpg';
if (Helper::options()->futureRandom) {
$moszu = explode("\r\n", Helper::options()->futureRandom);
$random = $moszu[array_rand($moszu, 1)] . "?futureRandom=" . mt_rand(0, 1000000);
}
$pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
$patternMD = '/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|jpeg|gif|png|webp))/i';
$patternMDfoot = '/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|jpeg|gif|png|webp))/i';
$t = preg_match_all($pattern, $widget->content, $thumbUrl);
$img = $random;
if ($widget->fields->thumb) {
$img = $widget->fields->thumb;
} elseif ($t) {
$img = $thumbUrl[1][0];
} elseif (preg_match_all($patternMD, $widget->content, $thumbUrl)) {
$img = $thumbUrl[1][0];
} elseif (preg_match_all($patternMDfoot, $widget->content, $thumbUrl)) {
$img = $thumbUrl[1][0];
}
echo $img;
}
// 文章封面缩略图
function GetRandomThumbnailPost($widget)
{
$img = '';
if ($widget->fields->thumb) {
$img = $widget->fields->thumb;
}
echo $img;
}
// 全站字数统计
function allOfCharacters()
{
$showPrivate = 0;
$chars = 0;
$db = Typecho_Db::get();
if ($showPrivate == 0) {
$select = $db->select('text')->from('table.contents')->where('table.contents.status = ?', 'publish');
} else {
$select = $db->select('text')->from('table.contents');
}
$rows = $db->fetchAll($select);
foreach ($rows as $row) {
$chars += mb_strlen($row['text'], 'UTF-8');
}
$unit = '';
if ($chars >= 10000) {
$chars /= 10000;
$unit = 'W';
} else if ($chars >= 1000) {
$chars /= 1000;
$unit = 'K';
}
$out = sprintf('%.2lf %s', $chars, $unit);
echo $out;
}
function thumb($cid)
{
if (empty($imgurl)) {
$rand_num = 10; //随机图片数量,根据图片目录中图片实际数量设置
if ($rand_num == 0) {
$imgurl = "img/0.jpg";
//如果$rand_num = 0,则显示默认图片,须命名为"0.jpg",注意是绝对地址
} else {
$imgurl = "img/" . rand(1, $rand_num) . ".jpg";
//随机图片,须按"1.jpg","2.jpg","3.jpg"...的顺序命名,注意是绝对地址
}
}
$db = Typecho_Db::get();
$rs = $db->fetchRow($db->select('table.contents.text')
->from('table.contents')
->where('table.contents.type = ?', 'attachment')
->where('table.contents.parent= ?', $cid)
->order('table.contents.cid', Typecho_Db::SORT_ASC)
->limit(1));
$img = unserialize($rs['text']);
if (empty($img)) {
echo $imgurl;
} else {
echo '你的博客地址' . $img['path'];
}
}
// 评论时间
function timesince($older_date, $comment_date = false)
{
$chunks = array(
array(86400, '天'),
array(3600, '小时'),
array(60, '分'),
array(1, '秒'),
);
$newer_date = time();
$since = abs($newer_date - $older_date);
if ($since < 2592000) {
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0)
break;
}
$output = $count . $name . '前';
} else {
$output = !$comment_date ? (date('Y-m-j G:i', $older_date)) : (date('Y-m-j', $older_date));
}
return $output;
}
// 文章内获取第一张图做封面
function getPostImg($archive)
{
$img = array();
// 匹配 img 的 src 的正则表达式
preg_match_all("/<img.*?src=\"(.*?)\".*?\/?>/i", $archive->content, $img);
// 判断是否匹配到图片
if (count($img) > 0 && count($img[0]) > 0) {
// 返回图片
return $img[1][0];
} else {
// 如果没有匹配到就返回 none
return 'none';
}
}
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] . ' id="cl-' . $catalog_count . '"><a class="markdownIt-Anchor" href="#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 >' . "\n";
} elseif ($catalog_depth > $prev_depth) {
$to_depth++;
$index .= '<ol class="toc-child">' . "\n";
} 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>' . "\n" . '</ol>' . "\n";
$to_depth--;
}
}
$index .= '</li>';
}
}
$index .= '<li class="toc-item">
<a class="toc-link" href="#cl-' . $catalog_item['count'] . '">
<span class="toc-number"></span>
<span class="toc-text">' . $catalog_item['text'] . '</span>
</a>';
$prev_depth = $catalog_item['depth'];
}
for ($i = 0; $i <= $to_depth; $i++) {
$index .= '</li>' . "\n";
}
// $index = '<div >'."\n".'<div >'."\n"."\n".$index.'</div>'."\n".'</div>'."\n";
}
echo $index;
}
/* 获取懒加载图片 */
function GetLazyLoad()
{
if (Helper::options()->LazyLoad) {
return Helper::options()->LazyLoad;
} else {
return "data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=";
}
}
/* 格式化标签 */
function ParseCode($text)
{
$text = Short_Tabs($text);
$text = Note_Fsm($text);
$text = Note_Ico($text);
$text = Hide_Lnline($text);
$text = Hide_Block($text);
$text = Hide_Toggle($text);
$text = Button($text);
$text = Cheak_Box($text);
$text = inline_Tag($text);
$text = Bf_Radio($text);
$text = Bf_Mark($text);
$text = Font($text);
$text = ArtPlayer($text);
$text = PostImage($text);
return $text;
}
// 标签外挂-Tabs
function Short_Tabs($text)
{
$text = preg_replace_callback('/<p>\[tabs\](.*?)\[\/tabs\]<\/p>/ism', function ($text) {
return '[tabs]' . $text[1] . '[/tabs]';
}, $text);
$text = preg_replace_callback('/\[tabs\](.*?)\[\/tabs\]/ism', function ($text) {
return preg_replace('~<br.*?>~', '', $text[0]);
}, $text);
$text = preg_replace_callback('/\[tabs\](.*?)\[\/tabs\]/ism', function ($text) {
$tabname = '';
preg_match_all('/label=\"(.*?)\"\]/i', $text[1], $tabnamearr);
for ($i = 0; $i < count($tabnamearr[1]); $i++) {
if ($i === 0) {
$tabname .= '<li class="tab active"><button type="button" data-href="' . $i . '">' . $tabnamearr[1][$i] . '</button></li>';
} else {
$tabname .= '<li class="tab" data-href="' . $i . '"><button type="button" data-href="' . $i . '">' . $tabnamearr[1][$i] . '</button></li>';
}
}
$tabcon = '';
preg_match_all('/"\](.*?)\[\//i', $text[1], $tabconarr);
for ($i = 0; $i < count($tabconarr[1]); $i++) {
if ($i === 0) {
$tabcon .= '<div class="tab-item-content active" id="' . $i . '">' . $tabconarr[1][$i] . ' <button type="button" class="tab-to-top" aria-label="scroll to top"><i class="fas fa-arrow-up"></i></button></div>';
} else {
$tabcon .= '<div class="tab-item-content" id="' . $i . '">' . $tabconarr[1][$i] . '<button type="button" class="tab-to-top" aria-label="scroll to top"><i class="fas fa-arrow-up"></i></button></div>';
}
}
return '
<div class="tabs" id="tags"><ul class="nav-tabs">' . $tabname . '</ul><div class="tab-contents">' . $tabcon . '</div></div>';
}, $text);
return $text;
}
// 标签外挂-btn
function Button($text)
{
$text = preg_replace_callback('/\[btn href=\"(.*?)\" type=\"(.*?)\".*?\ ico=\"(.*?)\"](.*?)\[\/btn\]/ism', function ($text) {
return '<a href="' . $text[1] . '" class="btn-beautify button--animated ' . $text[2] . '">
<i class=" ' . $text[3] . '"></i><span>' . $text[4] . '</span></a>';
}, $text);
return $text;
}
// 标签外挂-note
function Note_Fsm($text)
{
$text = preg_replace_callback('/\[note type=\"(.*?)\".*?\](.*?)\[\/note\]/ism', function ($text) {
return '<div class="note ' . $text[1] . '"> <p>' . $text[2] . '</p></div>';
}, $text);
return $text;
}
// 标签外挂-note_ico
function Note_Ico($text)
{
$text = preg_replace_callback('/\[note-ico type=\"(.*?)\".*?\ ico=\"(.*?)\"](.*?)\[\/note-ico\]/ism', function ($text) {
return '<div class="note ' . $text[1] . '"><i class="' . $text[2] . '"></i><p>' . $text[3] . '</p></div>';
}, $text);
return $text;
}
// hide-inline
function Hide_Lnline($text)
{
$text = preg_replace_callback('/\[hide-inline name=\"(.*?)\".*?\](.*?)\[\/hide-inline\]/ism', function ($text) {
return '<span class="hide-inline"><button type="button" class="hide-button button--animated">' . $text[1] . '</button><span class="hide-content">' . $text[2] . '</span></span>';
}, $text);
return $text;
}
// hide-block
function Hide_Block($text)
{
$text = preg_replace_callback('/\[hide-block name=\"(.*?)\".*?\](.*?)\[\/hide-block\]/ism', function ($text) {
return '<div class="hide-block"><button type="button" class="hide-button button--animated">' . $text[1] . '</button><div class="hide-content">' . $text[2] . '</div></div>';
}, $text);
return $text;
}
// hide-toggle
function Hide_Toggle($text)
{
$text = preg_replace_callback('/\[hide-toggle name=\"(.*?)\".*?\](.*?)\[\/hide-toggle\]/ism', function ($text) {
return '<details class="toggle"><summary class="toggle-button">' . $text[1] . '</summary><div class="toggle-content">' . $text[2] . '</div></details>';
}, $text);
return $text;
}
// 复选框
function Cheak_Box($text)
{
$text = preg_replace_callback('/\[cb type=\"(.*?)\".*?\ checked=\"(.*?)"\](.*?)\[\/cb\]/ism', function ($text) {
return '<div class="checkbox ' . $text[1] . ' checked"><input type="checkbox" ' . $text[2] . '>' . $text[3] . '</div>';
}, $text);
return $text;
}
// 行内标签
function inline_Tag($text)
{
$text = preg_replace_callback('/\[in-tag color=\"(.*?)\"](.*?)\[\/in-tag\]/ism', function ($text) {
return '<span class="inline-tag ' . $text[1] . '">' . $text[2] . '</span>';
}, $text);
return $text;
}
// 单选框-radio
function Bf_Radio($text)
{
$text = preg_replace_callback('/\[radio color=\"(.*?)\".*?\ checked=\"(.*?)"\](.*?)\[\/radio\]/ism', function ($text) {
return '<div class="checkbox ' . $text[1] . ' checked"><input type="radio" ' . $text[2] . '>' . $text[3] . '</div>';
}, $text);
return $text;
}
function Bf_Mark($text)
{
$text = preg_replace_callback('/\[label color=\"(.*?)\".*?\](.*?)\[\/label\]/ism', function ($text) {
return '<mark class="hl-label ' . $text[1] . '">' . $text[2] . '</mark>';
}, $text);
return $text;
}
function Font($text)
{
$text = preg_replace_callback('/\[font size=\"(.*?)"\ color=\"(.*?)"\](.*?)\[\/font\]/ism', function ($text) {
return '<font style="font-size: ' . $text[1] . 'px;color:' . $text[2] . '">' . $text[3] . '</font>';
}, $text);
return $text;
}
function ArtPlayer($text)
{
$text = preg_replace_callback('/\[video title=\"(.*?)"\ url=\"(.*?)"\ container=\"(.*?)"\ subtitle=\"(.*?)"\ poster=\"(.*?)"\](.*?)\[\/video\]/ism', function ($text) {
$t = explode("<br>", $text[6]);
for ($i = 0; $i < count($t); $i++) {
$a[] = explode("|", $t[$i]);
}
for ($i = 0; $i < count($a); $i++) {
$cut[$i]['time'] = isset($a[$i][0]) ? (int) $a[$i][0] : 0;
$cut[$i]['text'] = isset($a[$i][1]) ? $a[$i][1] : '';
unset($cut[$i][0]);
unset($cut[$i][1]);
}
$cut[0]['time'] == null ? $highlight = '[]' : $highlight = json_encode($cut);
$text[4] == ' ' ? $tooltip = '无字幕' : $tooltip = '默认字幕';
return '
<div class="iframe_video artplayer artplayer-' . $text[3] . '"></div>
<script>
var ' . $text[3] . ' = new Artplayer({
container: ".artplayer-' . $text[3] . '",
url: "' . $text[2] . '",
title: "' . $text[1] . '",
poster: "' . $text[5] . '",
subtitle: {
url: "' . $text[4] . '",
},
volume: 0.5,
muted: false,
autoplay: false,
pip: true,
autoSize: true,
autoMini: false,
screenshot: true,
setting: true,
loop: true,
flip: true,
playbackRate: true,
aspectRatio: true,
fullscreen: true,
fullscreenWeb: true,
subtitleOffset: true,
miniProgressBar: true,
mutex: true,
backdrop: true,
playsInline: true,
autoPlayback: true,
theme: "#23ade5",
lang: navigator.language.toLowerCase(),
whitelist: ["*"],
moreVideoAttr: {
crossOrigin: "anonymous",
},
settings: [{
width: 200,
html: "字幕",
tooltip: "' . $tooltip . '",
selector: [{
html: "Display",
tooltip: "显示",
switch: true,
onSwitch: function (item) {
item.tooltip = item.switch ? "关闭" : "显示";
' . $text[3] . '.subtitle.show = !item.switch;
return !item.switch;
},
}],
onSelect: function(item) {
art.subtitle.switch(item.url, {
name: item.html,
});
return item.html;
},
}, ],
highlight: ' . $highlight . '
});
</script>';
}, $text);
return $text;
}
// 重写文章图片加载
function PostImage($text)
{
$pattern = '/<img[^>]*src="([^"]+)"[^>]*alt="([^"]+)"[^>]*(style="[^"]+")[^>]*>/i';
$replacement = '<img title="$2" alt="$2" data-lazy-src="$1" $3 src="' . GetLazyLoad() . '">';
$text = preg_replace($pattern, $replacement, $text);
return $text;
}
/**
* 判断时间区间
*
* 使用方法 if(timeZone($this->date->timeStamp)) echo 'ok';
*/
function timeZone($from)
{
$now = new Typecho_Date(Typecho_Date::gmtTime());
return $now->timeStamp - $from < 24 * 60 * 60 ? true : false;
}
/**
* 获取标签数目
*
* 语法: <?php echo tagsNum(); ?>
*
* @access protected
* @return integer
*/
function tagsNum($display = true)
{
$db = Typecho_Db::get();
$total_tags = $db->fetchObject($db->select(array('COUNT(mid)' => 'num'))
->from('table.metas')
->where('table.metas.type = ?', 'tag'))->num;
if ($display) {
echo $total_tags;
} else {
return $total_tags;
}
}
//获取Gravatar头像 QQ邮箱取用qq头像
function getGravatar($email, $name, $comments_a, $s = 96, $d = 'mp', $r = 'g')
{
preg_match_all('/((\d)*)@qq.com/', $email, $vai);
if (empty($vai['1']['0'])) {
$url = Helper::options()->GravatarSelect;
$url .= md5(strtolower(trim($email)));
$url .= "?s=$s&d=$d&r=$r";
$imga = '<img ' . $comments_a . ' src="' . GetLazyLoad() . '" data-lazy-src="' . $url . '" >';
} else {
$url = 'https://cravatar.cn/avatar/'.md5(strtolower(trim($email)));
$imga = '<img ' . $comments_a . ' src="' . GetLazyLoad() . '" data-lazy-src="' . $url . '" >';
}
return $imga;
}
// 获取浏览器信息
function getBrowser($agent)
{
$browsers = [
'/MSIE\s([^\s|;]+)/i' => ['label' => 'Internet Explorer', 'icon' => 'fab fa-internet-explorer'],
'/FireFox\/([^\s]+)/i' => ['label' => 'FireFox', 'icon' => 'fab fa-firefox-browser'],
'/Maxthon([\d]*)\/([^\s]+)/i' => ['label' => '遨游', 'icon' => 'iconfont icon-maxthon'],
'#360([a-zA-Z0-9.]+)#i' => ['label' => '360极速浏览器', 'icon' => 'iconfont icon-chrome'],
'/Edg([\d]*)\/([^\s]+)/i' => ['label' => 'Microsoft Edge', 'icon' => 'fab fa-edge'],
'/UC/i' => ['label' => 'UC浏览器', 'icon' => 'iconfont icon-UCliulanqi'],
'/QQ/i' => ['label' => 'QQ浏览器', 'icon' => 'iconfont icon-QQliulanqi'],
'/QQBrowser\/([^\s]+)/i' => ['label' => 'QQ浏览器', 'icon' => 'iconfont icon-QQliulanqi'],
'/UBrowser/i' => ['label' => 'UC浏览器', 'icon' => 'iconfont icon-UCliulanqi'],
'/Opera[\s|\/]([^\s]+)|OPR/i' => ['label' => 'Opera', 'icon' => 'fab fa-opera'],
'/YaBrowser/i' => ['label' => 'Yandex', 'icon' => 'fab fa-yandex-international'],
'/Quark/i' => ['label' => 'Quark', 'icon' => 'iconfont icon-quark'],
'/XiaoMi/i' => ['label' => '小米浏览器', 'icon' => 'iconfont icon-XiaoMi'],
'/Chrome([\d]*)\/([^\s]+)/i' => ['label' => 'Google Chrome', 'icon' => 'fab fa-chrome'],
'/safari\/([^\s]+)/i' => ['label' => 'Safari', 'icon' => 'fab fa-safari'],
];
$defaultBrowser = ['label' => 'Google Chrome', 'icon' => 'fab fa-chrome'];
foreach ($browsers as $pattern => $info) {
if (preg_match($pattern, $agent, $regs)) {
echo "<i class='{$info['icon']}'></i> {$info['label']}";
return;
}
}
echo "<i class='{$defaultBrowser['icon']}'></i> {$defaultBrowser['label']}";
}
// 获取操作系统信息
function getOs($agent) {
$osData = [
'Windows Vista' => ['/nt 6.0/i', 'iconfont icon-windows'],
'Windows 7' => ['/nt 6.1/i', 'iconfont icon-windows'],
'Windows 8' => ['/nt 6.2/i', 'fab fa-windows'],
'Windows 8.1' => ['/nt 6.3/i', 'fab fa-windows'],
'Windows XP' => ['/nt 5.1/i', 'iconfont icon-windows'],
'Windows 10' => ['/nt 10.0/i', 'fab fa-windows'],
'Windows 11' => ['/nt 11.0/i', 'fab fa-windows'],
'Android Pie' => ['/android 9/i', 'fab fa-android'],
'Android ICS' => ['/android 4/i', 'fab fa-android'],
'Android Lollipop' => ['/android 5/i', 'fab fa-android'],
'Android M' => ['/android 6/i', 'fab fa-android'],
'Android Nougat' => ['/android 7/i', 'fab fa-android'],
'Android Oreo' => ['/android 8/i', 'fab fa-android'],
'Android Q' => ['/android 10/i', 'fab fa-android'],
'Android 11' => ['/android 11/i', 'fab fa-android'],
'Android 12' => ['/android 12/i', 'fab fa-android'],
'Android 13' => ['/android 13/i', 'fab fa-android'],
'Ubuntu' => ['/ubuntu/i', 'fab fa-ubuntu'],
'Arch Linux' => ['/Arch/i', 'iconfont icon-Arch-Linux'],
'Manjaro' => ['/manjaro/i', 'iconfont icon-manjaro'],
'Debian' => ['/debian/i', 'iconfont icon-debianos'],
'Linux' => ['/linux/i', 'fab fa-linux'],
'iOS(iPad)' => ['/iPad/i', 'fab fa-apple'],
'iOS(iPhone)' => ['/iPhone/i', 'fab fa-apple'],
'MacOS' => ['/mac/i', 'fab fa-apple'],
'Android' => ['/fusion/i', 'fab fa-android'],
];
foreach ($osData as $osName => list($pattern, $iconClass)) {
if (preg_match($pattern, $agent)) {
echo ' <i class="' . $iconClass . '"></i> ' . $osName . ' / ';
return;
}
}
// Default case
echo ' <i class="fab fa-linux"></i> Linux / ';
}
function commentRank($widget, $email = NULL)
{
if (empty($email))
return;
$txt = Helper::options()->CustomAuthenticated;
if ($txt == "") {
$txt = 'x||x';
}
$string_arr = explode("\r\n", $txt);
$long = count($string_arr);
for ($i = 0; $i < $long; $i++) {
$mailList[] = explode("||", $string_arr[$i])[0];
$authList[] = explode("||", $string_arr[$i])[1];
}
$all = array_combine($mailList, $authList);
if ($widget->authorId == $widget->ownerId) {
echo '<span class="vtag vmaster">博主</span>';
} else if (in_array($email, $mailList)) {
echo '<span class="vtag vauth">' . $all[$email] . '</span>';
} else {
echo '<span class="vtag vvisitor">访客</span>';
}
}
//获取评论的锚点链接
function get_comment_at($coid)
{
$db = Typecho_Db::get();
$prow = $db->fetchRow($db->select('parent,status')->from('table.comments')
->where('coid = ?', $coid)); //当前评论
$mail = "";
$parent = @$prow['parent'];
if ($parent != "0") { //子评论
$arow = $db->fetchRow($db->select('author,status,mail')->from('table.comments')
->where('coid = ?', $parent)); //查询该条评论的父评论的信息
@$author = @$arow['author']; //作者名称
$mail = @$arow['mail'];
if (@$author && $arow['status'] == "approved") { //父评论作者存在且父评论已经审核通过
if (@$prow['status'] == "waiting") {
echo '<p class="commentReview">(评论审核中))</p>';
}
echo '<a onclick="b(this);return false;" href="#comment-' . $parent . '">@' . $author . '</a>';
} else { //父评论作者不存在或者父评论没有审核通过
if (@$prow['status'] == "waiting") {
echo '<p class="commentReview">(评论审核中))</p>';
} else {
echo '';
}
}
} else { //母评论,无需输出锚点链接
if (@$prow['status'] == "waiting") {
echo '<p class="commentReview">(评论审核中))</p>';
} else {
echo '';
}
}
}
/**
* 重写评论显示函数
*/
function threadedComments($comments, $options)
{
$commentClass = '';
if ($comments->authorId) {
if ($comments->authorId == $comments->ownerId) {
$commentClass .= ' comment-by-author';
} else {
$commentClass .= ' comment-by-user';
}
}
$commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent';
?>
<li id="li-<?php $comments->theId(); ?>" class="comment-body<?php
if ($comments->levels > 0) {
echo ' comment-child';
$comments->levelsAlt(' comment-level-odd', ' comment-level-even');
} else {
echo ' comment-parent';
}
$comments->alt(' comment-odd', ' comment-even');
echo $commentClass;
?>">
<div id="<?php $comments->theId(); ?>">
<div class="comment-author">
<?php $email = $comments->mail;
$name = $comments->author;
$comments_a = 'class="vimg" style="border-radius: 50%;"';
echo getGravatar($email, $name, $comments_a); ?>
<div class="vuser">
<cite class="vnick" title="<?php $comments->author; ?>">
<?php $comments->author(); ?>
</cite>
<?php commentRank($comments, $comments->mail); ?>
</div>
</div>
<div class="vhead">
<b>
<?php $parentMail = get_comment_at($comments->coid) ?>
<?php echo $parentMail; ?>
</b>
<a class="vtime" href="<?php $comments->permalink(); ?>"><?php $comments->date('Y-m-d H:i'); ?></a>
<?php if (Helper::options()->CloseComments == 'off'): ?>
<span class="comment-reply">
<?php $comments->reply(); ?>
</span>
<?php endif ?>
</div>
<div class="comment-content">
<?php $comments->content(); ?>
</div>
<span class="comment-ua">
<?php getOs($comments->agent); ?>
<?php getBrowser($comments->agent); ?>
</span>
</div>
<?php if ($comments->children) { ?>
<div class="comment-children">
<?php $comments->threadedComments($options); ?>
</div>
<?php } ?>
</li>
<?php }
// 主页封面
function img_postthemb($thiz, $default_img)
{
$content = $thiz->content;
$ret = preg_match("/\<img.*?src\=\"(.*?)\"[^>]*>/i", $content, $thumbUrl);
if ($ret === 1 && count($thumbUrl) == 2) {
return $thumbUrl[1];
} else {
return $default_img = "https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg";
}
}
// 输出标签
function printTag($that)
{ ?>
<?php if (count($that->tags) > 0): ?>
<?php foreach ($that->tags as $tags): ?>
<a href="<?php print($tags['permalink']) ?>" class="post-meta__tags"><span>
<?php print($tags['name']) ?>
</span></a>
<?php endforeach; ?>
<?php else: ?>
<a class="post-meta__tags"><span>无标签</span></a>
<?php endif; ?>
<?php }
//当前人数
function onlinePeople()
{
$online_log = "usr/themes/butterfly/online.dat"; //保存人数的文件到根目录,
$timeout = 30; //30秒内没动作者,认为掉线
if (!file_exists($online_log)) {
fopen($online_log, "w");
}
$entries = file($online_log);
$temp = array();
for ($i = 0; $i < count($entries); $i++) {
$entry = explode(",", trim($entries[$i]));
if (($entry[0] != getenv('REMOTE_ADDR')) && ($entry[1] > time())) {
array_push($temp, $entry[0] . "," . $entry[1] . "\n"); //取出其他浏览者的信息,并去掉超时者,保存进$temp
}
}
array_push($temp, getenv('REMOTE_ADDR') . "," . (time() + ($timeout)) . "\n"); //更新浏览者的时间
$slzxrs = count($temp); //计算在线人数
$entries = implode("", $temp);
//写入文件
$fp = fopen($online_log, "w");
flock($fp, LOCK_EX); //flock() 不能在NFS以及其他的一些网络文件系统中正常工作
fputs($fp, $entries);
flock($fp, LOCK_UN);
fclose($fp);
echo $slzxrs;
}
function only_get_post_view($archive)
{
$db = Typecho_Db::get();
$cid = $archive->cid;
$exist = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid))['views'];
if ($exist >= 10000) {
$out = sprintf('%.2f W', $exist / 10000);
} else {
$out = sprintf('%d', $exist);
}
echo $out;
}
//总访问量
function theAllViews()
{
$db = Typecho_Db::get();
$row = $db->fetchAll($db->select('SUM(views)')->from('table.contents'));
echo array_values($row[0])[0];
}
// 回复可见
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('myyodux', 'one');
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('myyodux', 'one');
class myyodux
{
public static function one($con, $obj, $text)
{
$text = empty($text) ? $con : $text;
if (!$obj->is('single')) {
$text = preg_replace("/\[hide\](.*?)\[\/hide\]/sm", '', $text);
// $text = preg_replace("/\n\s*){3,}/sm",' ',$text);
}
return $text;
}
}
/**
* 显示上一篇
*
* 如果没有下一篇,返回null
*/
function thePrevCid($widget, $default = NULL)
{
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created < ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_DESC)
->limit(1);
$content = $db->fetchRow($sql);
if ($content) {
return $content["cid"];
} else {
return $default;
}
}
/**
* 获取下一篇文章mid
*
* 如果没有下一篇,返回null
*/
function theNextCid($widget, $default = NULL)
{
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created > ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1);
$content = $db->fetchRow($sql);
if ($content) {
return $content["cid"];
} else {
return $default;
}
}
//调用博主最近文章更新时间
function get_last_update()
{
$num = '1';
$type = 'post';
$status = 'publish';
$now = time();
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$create = $db->fetchRow($db->select('created')->from('table.contents')->where('table.contents.type=? and status=?', $type, $status)->order('created', Typecho_Db::SORT_DESC)->limit($num));
$update = $db->fetchRow($db->select('modified')->from('table.contents')->where('table.contents.type=? and status=?', $type, $status)->order('modified', Typecho_Db::SORT_DESC)->limit($num));
if ($create >= $update) {
echo Typecho_I18n::dateWord(isset($create['created']), $now);
} else {
$lastModified = $now - $update['modified'];
$timeIntervals = [
31536000 => '年',
2592000 => '个月',
86400 => '天',
3600 => '小时',
60 => '分钟',
1 => '秒'
];
foreach ($timeIntervals as $interval => $label) {
if ($lastModified > $interval) {
$value = floor($lastModified / $interval);
echo $value . ' ' . $label . '前';
break;
}
}
}
}
// 自定义编辑器
Typecho_Plugin::factory('admin/write-post.php')->bottom = array('editor', 'reset');
Typecho_Plugin::factory('admin/write-page.php')->bottom = array('editor', 'reset');
class editor
{
public static function reset()
{
echo "<script src='" . Helper::options()->themeUrl . '/edit/extend.js?v1.7.6' . "'></script>";
echo "<link rel='stylesheet' href='" . Helper::options()->themeUrl . '/edit/edit.css?v1.6.3' . "'>";
}
}
/* 判断是否是移动端 */
function isMobile()
{
if (isset($_SERVER['HTTP_X_WAP_PROFILE']))
return true;
if (isset($_SERVER['HTTP_VIA'])) {
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array('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', 'mobile');
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;
}
function RunTime()
{
$site_create_time = strtotime(Helper::options()->buildtime);
$time = time() - $site_create_time;
if (is_numeric($time)) {
if ($time >= 86400) {
$days = floor($time / 86400);
$time = ($time % 86400);
echo $days . ' 天';
} else {
echo '1 天';
}
} else {
echo '';
}
}
function RecapOutPut($login)
{
$siteKey = Helper::options()->siteKey;
$secretKey = Helper::options()->secretKey;
if (!empty($siteKey) && !empty($secretKey) && !$login) {
echo '<script src="https://recaptcha.net/recaptcha/api.js" async defer data-no-instant></script>
<div class="g-recaptcha" data-sitekey=' . $siteKey . '></div>';
}
if (!empty(Helper::options()->hcaptchaSecretKey) && !empty(Helper::options()->hcaptchaAPIKey) && !$login) {
echo '
<div id="h-captcha" class="h-captcha" data-sitekey=' . Helper::options()->hcaptchaSecretKey . '></div>';
}
}
function comments_filter($comment)
{
if (isset($_REQUEST['text']) != null) {
if ($_POST['g-recaptcha-response'] == null) {
throw new Typecho_Widget_Exception(_t('人机验证失败,确认你加载了谷歌人机验证并通过验证'));
} else {
$siteKey = Helper::options()->siteKey;
$secretKey = Helper::options()->secretKey;
function getCaptcha($recaptcha_response, $secretKey)
{
$response = file_get_contents("https://recaptcha.net/recaptcha/api/siteverify?secret=" . $secretKey . "&response=" . $recaptcha_response);
$response = json_decode($response);
return $response;
}
$resp = getCaptcha($_POST['g-recaptcha-response'], $secretKey);
if ($resp->success == true) {
return $comments;
} else {
switch ($resp->error - codes) {
case '{[0] => "timeout-or-duplicate"}':
throw new Typecho_Widget_Exception(_t('验证时间超过2分钟或连续重复发言!'));
break;
case '{[0] => "invalid-input-secret"}':
throw new Typecho_Widget_Exception(_t('博主填了无效的siteKey或者secretKey...'));
break;
case '{[0] => "bad-request"}':
throw new Typecho_Widget_Exception(_t('请求错误!请检查网络'));
break;
default:
throw new Typecho_Widget_Exception(_t('很遗憾,您被当成了机器人...'));
}
}
}
}
return $comment;
}
function hcaptcha_filter($comment)
{
if (isset($_REQUEST['text']) != null) {
if ($_POST['h-captcha-response'] == null) {
throw new Typecho_Widget_Exception(_t('人机验证失败,确认你加载了hcaptcha人机验证并通过验证'));