-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy pathPlugin.php
More file actions
1377 lines (1273 loc) · 54.5 KB
/
Copy pathPlugin.php
File metadata and controls
1377 lines (1273 loc) · 54.5 KB
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
/**
* 友情链接插件 [TF 社区维护版本]
* @package Links Plus
* @author LHL,HANNY
* @version 1.3.1
* @dependence 14.10.10-*
* @link https://github.com/typecho-fans/plugins/tree/master/Links
*
* version 1.3.1 at 2025-02-09 by LHL
* 优化 一些细节
*
* version 1.3.0 at 2025-02-09 by LHL
* 优化 UI - Material Design 3
* 添加 Links Plus到菜单,更加方便操作
* 添加 模板,能够更灵活地自定义输出结构,支持 CSS/JS 注入
* 添加 正文重写,免去修改/重新开发主题的步骤
* 移除 懵仙兔兔 的广告内容(一键添加TA的友链)
*
* version 1.2.7 at 2024-06-21 by 泽泽社长
* 解决php8.2一处报错问题
*
* version 1.2.6 at 2023-05-15 by 泽泽社长
* 支持主题作者自定义友链 html 结构
*
* version 1.2.5 at 2023-03-27 by 懵仙兔兔
* 友链添加 noopener 外链属性
* 内置友链邮箱解析头像链接 api 接口调整为仅内部调用
* Action 和内置友链邮箱解析头像链接 api 接口使用加盐地址
* 文本字段入库过滤 XSS
* 增加图片尺寸参数支持
* 增加规则和默认图片尺寸设置选项
* 修复历史遗留问题更新 lid 导致报错
*
* version 1.2.3 at 2023-03-26 by 懵仙兔兔
* 修复没有一条友链时,Typecho 1.2 友链设置界面报错问题(虽然报错不影响功能)
* 调整表格间距
* 删除失效链接,隐藏界面多余 input 标签
* 修复友链邮箱解析头像链接功能,内置 api 接口
*
* version 1.2.2 at 2020-03-11 by 懵仙兔兔
* 修复一个小 BUG
*
* version 1.2.1 at 2020-03-03 by 懵仙兔兔
* 修复邮箱头像解析问题
* 优化逻辑问题
*
* version 1.2.0 at 2020-02-16 by 懵仙兔兔
* 增加友链禁用功能
* 增加友链邮箱功能
* 增加友链邮箱解析头像链接功能
* 修正数据表的占用大小问题
*
* 历史版本 by 懵仙兔兔(第三方维护者)
*
* version 1.1.3 at 2020-02-08 by 懵仙兔兔
* 修复已存在表激活失败、表检测失败
*
* version 1.1.2 at 2019-08-26 by 泽泽社长
* 修复越权漏洞
*
* version 1.1.1 at 2014-12-14
* 修改支持 Typecho 1.0
* 修正 Typecho 1.0 下不能删除的 BUG
*
* 历史版本 by Hanny(原作者)
*
* version 1.1.0 at 2013-12-08
* 修改支持 Typecho 0.9
*
* version 1.0.4 at 2010-06-30
* 修正数据表的前缀问题
* 在 Pattern 里加上所有的数据表字段
*
* version 1.0.3 at 2010-06-20
* 修改友链图片的支持方式。
* 增加友链分类功能
* 增加自定义字段,以便用户自定义扩展
* 增加多种友链输出方式。
* 增加较详细的帮助文档
* 增加在自定义页面引用标签,方便友情链接页面的引用
*
* version 1.0.2 at 2010-05-16
* 增加SQLite支持
*
* version 1.0.1 at 2009-12-27
* 增加显示友链描述
* 增加首页友链数量限制功能
* 增加友链图片功能
*
* version 1.0.0 at 2009-12-12
* 实现友情链接的基本功能
* 包括: 添加 删除 修改 排序
*/
class Links_Plugin implements Typecho_Plugin_Interface
{
/**
* 固定占位符(写入文章正文中用于替换)
*/
const REWRITE_PLACEHOLDER = '{{links_plus}}';
/**
* 重写块标记(用于二次重写时定位并替换旧内容)
*/
const REWRITE_BLOCK_START = '<!-- LINKS_PLUS_START -->';
const REWRITE_BLOCK_END = '<!-- LINKS_PLUS_END -->';
/** 模板目录(相对插件目录) */
const TEMPLATE_DIR = 'templates';
/**
* 获取插件绝对路径
*/
public static function getPluginDir()
{
return dirname(__FILE__);
}
/**
* 获取模板根目录绝对路径
*/
public static function getTemplateRoot()
{
return self::getPluginDir() . DIRECTORY_SEPARATOR . self::TEMPLATE_DIR;
}
/**
* 列出所有文件模板(读取 templates/<name>/manifest.json)
*
* @return array<string,array>
*/
public static function listTemplates()
{
$root = self::getTemplateRoot();
$list = array();
if (!is_dir($root)) {
return $list;
}
$dirs = glob($root . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
if (!$dirs) {
return $list;
}
foreach ($dirs as $dir) {
$manifestFile = $dir . DIRECTORY_SEPARATOR . 'manifest.json';
if (!is_file($manifestFile)) {
continue;
}
$json = @file_get_contents($manifestFile);
if (!$json) {
continue;
}
$manifest = @json_decode($json, true);
if (!is_array($manifest)) {
continue;
}
$name = isset($manifest['name']) ? (string)$manifest['name'] : basename($dir);
if ($name === '') {
continue;
}
$manifest['_dir'] = $dir;
$list[$name] = $manifest;
}
return $list;
}
/**
* 读取模板文件内容
*/
public static function readTemplateFile($templateName, $file)
{
$templateName = trim((string)$templateName);
$file = trim((string)$file);
if ($templateName === '' || $file === '') {
return null;
}
// 简单防穿越:只允许 [A-Za-z0-9_-]
if (!preg_match('/^[A-Za-z0-9_-]+$/', $templateName)) {
return null;
}
$path = self::getTemplateRoot() . DIRECTORY_SEPARATOR . $templateName . DIRECTORY_SEPARATOR . $file;
if (!is_file($path)) {
return null;
}
return file_get_contents($path);
}
/**
* 注入模板 CSS/JS(同一模板同一请求只注入一次)
*/
public static function injectTemplateAssetsOnce($templateName, array $manifest)
{
static $injected = array();
$key = 'tpl:' . $templateName;
if (isset($injected[$key])) {
return;
}
$injected[$key] = true;
$inject = isset($manifest['inject']) && is_array($manifest['inject']) ? $manifest['inject'] : array();
$injectCss = !empty($inject['css']);
$injectJs = !empty($inject['js']);
if ($injectCss) {
$css = self::readTemplateFile($templateName, 'style.css');
if ($css && trim($css) !== '') {
echo '<style id="links-plus-tpl-' . htmlspecialchars($templateName, ENT_QUOTES, 'UTF-8') . '">' . $css . '</style>';
}
}
if ($injectJs) {
$js = self::readTemplateFile($templateName, 'script.js');
if ($js && trim($js) !== '') {
echo '<script id="links-plus-tpl-' . htmlspecialchars($templateName, ENT_QUOTES, 'UTF-8') . '">' . $js . '</script>';
}
}
}
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return string
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$info = Links_Plugin::linksInstall();
try {
$menuIndex = Helper::addMenu('Links Plus');
Helper::addPanel($menuIndex, 'Links/manage-links.php', _t('友情链接'), _t('管理友情链接'), 'administrator');
} catch (Exception $e) {
Helper::addPanel(3, 'Links/manage-links.php', _t('友情链接'), _t('管理友情链接'), 'administrator');
} catch (Throwable $e) {
Helper::addPanel(3, 'Links/manage-links.php', _t('友情链接'), _t('管理友情链接'), 'administrator');
}
Helper::addAction('links-edit', 'Links_Action');
// Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Links_Plugin', 'parse');
// Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('Links_Plugin', 'parse');
// Typecho_Plugin::factory('Widget_Abstract_Comments')->contentEx = array('Links_Plugin', 'parse');
// Typecho_Plugin::factory('Widget_Archive')->callLinks = array('Links_Plugin', 'output_str');
return _t($info);
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeAction('links-edit');
try {
$menuIndex = Helper::removeMenu('Links Plus');
if ($menuIndex !== null) {
Helper::removePanel($menuIndex, 'Links/manage-links.php');
}
} catch (Exception $e) {
// ignore
} catch (Throwable $e) {
// ignore
}
// 兼容旧注册方式
Helper::removePanel(3, 'Links/manage-links.php');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
echo
<style>
:root {
--md-primary: #0061a4;
--md-on-primary: #ffffff;
--md-primary-container: #d1e4ff;
--md-on-primary-container: #001d36;
--md-surface: #fdfcff;
--md-surface-variant: #e1e2ec;
--md-surface-container: #f3f4f7;
--md-outline: #74777f;
--md-outline-variant: rgba(0,0,0,.12);
--md-radius: 12px;
}
.md3-wrap {
max-width: 1080px;
}
.md3-card {
background: var(--md-surface);
border-radius: var(--md-radius);
padding: 24px;
box-shadow: 0 1px 2px rgba(0,0,0,.08), 0 1px 3px rgba(0,0,0,.12);
margin-bottom: 24px;
border: 1px solid var(--md-outline-variant);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
.md3-title {
font-size: 1.1rem;
font-weight: 600;
color: var(--md-on-primary-container);
margin-bottom: 16px;
padding-left: 12px;
border-left: 4px solid var(--md-primary);
line-height: 1.2;
}
.md3-subtitle {
font-size: .95rem;
font-weight: 600;
color: #374151;
margin: 18px 0 10px;
}
.md3-body {
color: #58606b;
line-height: 1.75;
}
.md3-grid {
display: grid;
grid-template-columns: 1fr;
gap: 16px;
}
@media (min-width: 980px) {
.md3-grid.two {
grid-template-columns: 1fr 1fr;
}
}
.md3-header-actions {
display: flex;
gap: 16px;
margin-top: 16px;
flex-wrap: wrap;
}
.md3-btn-text {
color: var(--md-primary);
text-decoration: none;
font-weight: 500;
padding: 8px 16px;
border-radius: 20px;
background-color: var(--md-primary-container);
transition: opacity 0.2s;
}
.md3-btn-text:hover {
opacity: 0.9;
color: var(--md-primary);
text-decoration: none;
}
.lp-update-note {
margin-top: 14px;
padding: 12px 14px;
border-radius: 12px;
border: 1px solid var(--md-outline-variant);
background: var(--md-surface-container);
color: #374151;
font-size: 13px;
line-height: 1.65;
}
.lp-update-note a { color: var(--md-primary); text-decoration: none; font-weight: 600; }
.lp-update-note a:hover { text-decoration: underline; }
.lp-update-note.is-ok { border-color: rgba(0,97,164,.25); }
.lp-update-note.is-warn { border-color: rgba(245,158,11,.35); }
.lp-update-note.is-err { border-color: rgba(239,68,68,.35); }
.lp-update-note .lp-update-title { font-weight: 700; margin-bottom: 4px; }
.md3-btn-text.is-disabled { opacity: .55; pointer-events: none; }
.md3-chip {
display: inline-flex;
align-items: center;
height: 28px;
padding: 0 10px;
border-radius: 999px;
background: var(--md-surface-container);
border: 1px solid var(--md-outline-variant);
color: #374151;
font-size: 12px;
gap: 6px;
}
.md3-table {
width: 100%;
border-collapse: collapse;
font-size: 14px;
}
.md3-table th {
text-align: left;
padding: 12px 16px;
color: #555;
background-color: var(--md-surface-variant);
font-weight: 600;
border-radius: 4px;
}
.md3-table td {
padding: 12px 16px;
border-bottom: 1px solid #eee;
color: #333;
}
.md3-table tr:last-child td {
border-bottom: none;
}
.field-tag {
display: inline-block;
padding: 2px 8px;
background: #f0f0f0;
border-radius: 4px;
font-family: monospace;
color: #d63384;
}
/* 覆盖 Typecho 原生表单样式 */
.typecho-option-submit button {
background-color: var(--md-primary) !important;
border-radius: 20px !important;
padding: 0 24px !important;
height: 40px !important;
}
textarea, input[type="text"], select {
border: 1px solid var(--md-outline-variant);
border-radius: 12px;
padding: 10px 12px;
height:auto;
transition: box-shadow .15s, border-color .15s;
background: #fff;
}
textarea:focus, input[type="text"]:focus, select:focus {
border-color: rgba(0, 97, 164, .45);
box-shadow: 0 0 0 3px rgba(0, 97, 164, 0.18);
outline: none;
}
.typecho-option {
border-bottom: 1px dashed rgba(0,0,0,.06);
}
.typecho-option:last-child { border-bottom: 0; }
.typecho-option label {
font-weight: 600;
color: #1f2937;
}
.description {
color: #6b7280;
}
</style>
'
.
<script>
(function(){
var REPO = "lhl77/Typecho-Plugin-LinksPlus";
// 当前版本(按 tag 口径对比)
var CURRENT = "v1.3.1";
function normalizeTag(tag){
tag = (tag || "").toString().trim();
if(!tag) return "";
return tag.replace(/^refs\/tags\//, "");
}
function tagToVersion(tag){
tag = normalizeTag(tag);
return tag.replace(/^[vV]/, "");
}
function cmp(a, b){
a = (a || "").toString();
b = (b || "").toString();
var as = a.split('.');
var bs = b.split('.');
var n = Math.max(as.length, bs.length);
for(var i=0;i<n;i++){
var ai = as[i] || "0";
var bi = bs[i] || "0";
var an = /^\d+$/.test(ai) ? parseInt(ai, 10) : null;
var bn = /^\d+$/.test(bi) ? parseInt(bi, 10) : null;
if(an !== null && bn !== null){
if(an > bn) return 1;
if(an < bn) return -1;
} else {
if(ai > bi) return 1;
if(ai < bi) return -1;
}
}
return 0;
}
function fetchJson(url, cb){
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("Accept", "application/vnd.github+json");
xhr.onreadystatechange = function(){
if(xhr.readyState !== 4) return;
if(xhr.status >= 200 && xhr.status < 300){
try { cb(null, JSON.parse(xhr.responseText)); } catch(e){ cb(e); }
} else {
cb(new Error("HTTP " + xhr.status));
}
};
xhr.send(null);
}
function setBtnText(btn, text){
if(btn){ btn.textContent = text; }
}
function setBusy(btn, busy){
if(!btn) return;
busy = !!busy;
btn.setAttribute('aria-disabled', busy ? 'true' : 'false');
if(busy){
btn.classList.add('is-disabled');
btn.dataset.busy = '1';
} else {
btn.classList.remove('is-disabled');
btn.dataset.busy = '';
}
}
function renderNote(host, type, title, html){
if(!host) return;
var cls = 'lp-update-note';
if(type === 'ok') cls += ' is-ok';
if(type === 'warn') cls += ' is-warn';
if(type === 'err') cls += ' is-err';
host.innerHTML = '<div class="' + cls + '">' +
'<div class="lp-update-title">' + title + '</div>' +
'<div class="lp-update-body">' + html + '</div>' +
'</div>';
}
document.addEventListener("DOMContentLoaded", function(){
var btn = document.getElementById("links-plus-check-update");
if(!btn) return;
var card = btn.closest ? btn.closest('.md3-card') : null;
var out = card ? card.querySelector('.lp-update-out') : null;
btn.addEventListener("click", function(e){
e.preventDefault();
if(btn.dataset && btn.dataset.busy === '1') return;
setBusy(btn, true);
var api = "https://api.github.com/repos/" + REPO + "/tags?per_page=100";
var oldText = btn.textContent;
setBtnText(btn, "检查中...");
renderNote(out, 'ok', '检查更新', '正在查询 GitHub tags…');
fetchJson(api, function(err, tags){
setBtnText(btn, oldText || "检查更新");
setBusy(btn, false);
if(err || !Array.isArray(tags)){
renderNote(
out,
'err',
'检查失败',
'原因:' + (err ? err.message : '响应异常') + '<br>' +
'说明:此方案直接从浏览器访问 GitHub API,可能会受网络或 CORS 限制。'
);
return;
}
var latestTag = "";
var latestVer = "";
for(var i=0;i<tags.length;i++){
var name = tags[i] && tags[i].name ? tags[i].name : "";
var tag = normalizeTag(name);
var ver = tagToVersion(tag);
if(!ver) continue;
if(!latestVer || cmp(ver, latestVer) > 0){
latestVer = ver;
latestTag = tag;
}
}
if(!latestTag){
renderNote(out, 'err', '检查失败', '未发现可用的版本 tag。');
return;
}
var curVer = tagToVersion(CURRENT);
var hasUpdate = cmp(latestVer, curVer) > 0;
var url = "https://github.com/" + REPO + "/releases/tag/" + encodeURIComponent(latestTag);
if(hasUpdate){
renderNote(
out,
'warn',
'发现新版本:' + latestTag,
'当前版本:<code>' + CURRENT + '</code><br>' +
'最新版本:<code>' + latestTag + '</code><br>' +
'<a href="' + url + '" target="_blank" rel="noopener">打开 GitHub 查看</a>'
);
} else {
renderNote(
out,
'ok',
'已是最新版本',
'当前版本:<code>' + CURRENT + '</code><br>' +
'最新版本:<code>' + latestTag + '</code>'
);
}
});
});
});
})();
</script>
LINKS_PLUS_UPDATE_JS
.
<div class="md3-wrap">
<div class="md3-card">
<div class="md3-title">友情链接插件 (Links Plus)</div>
<div class="md3-body">
<p>欢迎使用 Links Plus 增强版。您可以在“管理”菜单下找到“友情链接”进行日常操作。</p>
<p>本插件支持多种输出模式(文字、图片、图文混合),并支持自定义字段扩展。</p>
</div>
<div class="lp-update-out"></div>
<div class="md3-header-actions">
<a href="' . Typecho_Common::url('extending.php?panel=Links%2Fmanage-links.php', Helper::options()->adminUrl) . '" class="md3-btn-text">管理友链</a>
<a href="https://github.com/lhl77/Typecho-Plugin-LinksPlus" target="_blank" class="md3-btn-text">GitHub</a>
<a id="links-plus-check-update" href="#" class="md3-btn-text">检查更新</a>
<a href="https://blog.lhl.one/artical/902.html" target="_blank" class="md3-btn-text">帮助文档</a>
<a href="https://github.com/lhl77/Typecho-Plugin-LinksPlus/issues" target="_blank" class="md3-btn-text">反馈</a>
</div>
</div>
<div class="md3-card">
<div class="md3-title">模式字符串变量说明</div>
<div style="overflow-x: auto;">
<table class="md3-table">
<thead>
<tr>
<th style="border-radius:5px 0px 0px 5px;" width="30%">变量占位符</th>
<th style="border-radius:0px 5px 5px 0px">说明</th>
</tr>
</thead>
<tbody>
<tr><td><span class="field-tag">{url}</span></td><td>友链的 URL 地址</td></tr>
<tr><td><span class="field-tag">{name}</span></td><td>友链显示的名称</td></tr>
<tr><td><span class="field-tag">{description}</span></td><td>友链的描述</td></tr>
<tr><td><span class="field-tag">{image}</span></td><td>图片地址 (Logo/头像)</td></tr>
<tr><td><span class="field-tag">{size}</span></td><td>在调用中设置的图片尺寸值 (数字)</td></tr>
<tr><td><span class="field-tag">{sort}</span></td><td>分类名称</td></tr>
<tr><td><span class="field-tag">{user}</span></td><td>自定义扩展数据</td></tr>
<tr><td><span class="field-tag">{lid}</span></td><td>数据库内的 ID 编号</td></tr>
</tbody>
</table>
</div>
</div>
</div>
';
// 模板选择(文件模板 / 保留高级自定义 textarea 作为兼容)
$templates = self::listTemplates();
$tplOptions = array(
'' => _t('(不使用模板,沿用下方自定义规则/旧配置)'),
);
if (!empty($templates)) {
foreach ($templates as $name => $manifest) {
$title = isset($manifest['title']) ? (string)$manifest['title'] : $name;
$tplOptions[$name] = $title;
}
}
$selectedText = new Typecho_Widget_Helper_Form_Element_Select(
'template_text',
$tplOptions,
'default-text',
_t('SHOW_TEXT 使用的模板'),
_t('选择一个文件模板(templates 目录)。选择后,前台调用 SHOW_TEXT 会优先使用该模板渲染。')
);
$form->addInput($selectedText);
$selectedImg = new Typecho_Widget_Helper_Form_Element_Select(
'template_img',
$tplOptions,
'default-img',
_t('SHOW_IMG 使用的模板'),
_t('选择一个文件模板(templates 目录)。选择后,前台调用 SHOW_IMG 会优先使用该模板渲染。')
);
$form->addInput($selectedImg);
$selectedMix = new Typecho_Widget_Helper_Form_Element_Select(
'template_mix',
$tplOptions,
'default-mix',
_t('SHOW_MIX 使用的模板'),
_t('选择一个文件模板(templates 目录)。选择后,前台调用 SHOW_MIX 会优先使用该模板渲染。')
);
$form->addInput($selectedMix);
$advHelp = new Typecho_Widget_Helper_Layout('div', array('class' => 'md3-card'));
$advHelp->html(
'<div class="md3-title">高级:自定义源码规则(兼容旧版本)</div>' .
'<div class="md3-body">' .
'<p>当你不想用模板,或需要更细粒度的自定义时,再使用下面三段规则(旧版配置项)。</p>' .
'</div>'
);
$form->addItem($advHelp);
$pattern_text = new Typecho_Widget_Helper_Form_Element_Textarea(
'pattern_text',
null,
'<li><a href="{url}" title="{title}" target="_blank" rel="noopener">{name}</a></li>',
_t('SHOW_TEXT 模式源码规则(高级)'),
_t('当未选择模板时生效。使用 SHOW_TEXT(仅文字) 模式输出时的源码,可按上表规则替换其中字段')
);
$form->addInput($pattern_text);
$pattern_img = new Typecho_Widget_Helper_Form_Element_Textarea(
'pattern_img',
null,
'<li><a href="{url}" title="{title}" target="_blank" rel="noopener"><img src="{image}" alt="{name}" width="{size}" height="{size}" /></a></li>',
_t('SHOW_IMG 模式源码规则(高级)'),
_t('当未选择模板时生效。使用 SHOW_IMG(仅图片) 模式输出时的源码,可按上表规则替换其中字段')
);
$form->addInput($pattern_img);
$pattern_mix = new Typecho_Widget_Helper_Form_Element_Textarea(
'pattern_mix',
null,
'<li><a href="{url}" title="{title}" target="_blank" rel="noopener"><img src="{image}" alt="{name}" width="{size}" height="{size}" /><span>{name}</span></a></li>',
_t('SHOW_MIX 模式源码规则(高级)'),
_t('当未选择模板时生效。使用 SHOW_MIX(图文混合) 模式输出时的源码,可按上表规则替换其中字段')
);
$form->addInput($pattern_mix);
$dsize = new Typecho_Widget_Helper_Form_Element_Text(
'dsize',
NULL,
'32',
_t('默认输出图片尺寸'),
_t('调用时如果未指定尺寸参数默认输出的图片大小(单位px不用填写)')
);
$dsize->input->setAttribute('class', 'w-10');
$form->addInput($dsize->addRule('isInteger', _t('请填写整数数字')));
$temHelp = new Typecho_Widget_Helper_Layout('div', array('class' => 'md3-card'));
$temHelp->html(
'<div class="md3-title">正文重写</div>
<div class="md3-body">
<p>当主题没有通过 <code>$this->content()</code> 输出正文,导致 <code><links>...</links></code> 不解析时,可用“正文重写工具”把正文中的占位符替换为友链 HTML。</p>
<p>固定占位符:<span class="md3-chip" style="font-weight:bold;">' . self::REWRITE_PLACEHOLDER . '</span></p>
<span class="md3-chip">建议</span>
<span style="margin-left:8px">优先使用文件模板(<code>templates/</code>)来管理输出结构;旧版“源码规则”保留兼容。</span><br><br>
<a id="links-plus-get-templates" href="' . Helper::security()->getIndex('/action/links-edit?do=update_templates') . '" class="md3-btn-text">获取最新主题</a>
<a href="https://blog.lhl.one/artical/902.html#主题" target="_blank" class="md3-btn-text">查看全部主题/开发文档</a>
<div class="lp-update-out" style="margin-top:12px"></div>
</div>'
);
$form->addItem($temHelp);
// 前端脚本:获取最新主题按钮行为 — 调用后由服务器端执行下载并覆盖 templates(会备份原有 templates)
$script = <<<'SCRIPT'
<script>
document.addEventListener('DOMContentLoaded', function(){
var btn = document.getElementById('links-plus-get-templates');
if(!btn) return;
btn.addEventListener('click', function(e){
e.preventDefault();
if(!confirm('将从 GitHub 下载并覆盖本插件的 templates 目录(会先备份原有 templates),确定继续?')) return;
var host = btn.closest ? btn.closest('.md3-card') : null;
var out = host ? host.querySelector('.lp-update-out') : null;
if(out){ out.innerHTML = '<div class="lp-update-note is-working"><div class="lp-update-title">更新中</div><div class="lp-update-body">正在从 GitHub 下载并更新模板,页面将跳转,请稍候…</div></div>'; }
try{ btn.setAttribute('aria-disabled','true'); btn.classList.add('is-disabled'); }catch(e){}
// 导航到 action 链接,服务器端处理下载/解压/覆盖逻辑
window.location.href = btn.getAttribute('href');
}, false);
});
</script>
SCRIPT;
echo $script;
/**
* 按 cid 重写正文输出(绕过主题不走 contentEx 的情况)
*/
// 说明已在上方 intro card 给出,这里不再重复插入大段说明卡片,避免页面过长。
$rewriteCid = new Typecho_Widget_Helper_Form_Element_Text(
'rewrite_cids',
null,
'',
_t('需要重写的 cid(可多个)'),
_t('填写文章/页面 cid,多个用英文逗号分隔,例如:12,34,56')
);
$rewriteCid->input->setAttribute('class', 'w-50');
$form->addInput($rewriteCid);
$rewriteModeOptions = array(
);
if (!empty($templates)) {
foreach ($templates as $name => $manifest) {
$title = isset($manifest['title']) ? (string)$manifest['title'] : $name;
$rewriteModeOptions['TPL:' . $name] = $title ;
}
}
$rewritePattern = new Typecho_Widget_Helper_Form_Element_Select(
'rewrite_pattern',
$rewriteModeOptions,
'SHOW_TEXT',
_t('重写输出主题'),
_t('把占位符替换成哪种模式输出,也可以直接选择某个文件模板。')
);
$form->addInput($rewritePattern);
$rewriteNum = new Typecho_Widget_Helper_Form_Element_Text(
'rewrite_num',
null,
'0',
_t('重写输出数量'),
_t('0 表示全部')
);
$rewriteNum->input->setAttribute('class', 'w-10');
$form->addInput($rewriteNum->addRule('isInteger', _t('请填写整数数字')));
// 开发中,小子别看了,一起开发提PR吧
// 是否启用短代码 [links_plus]
// $enableShortcode = new Typecho_Widget_Helper_Form_Element_Checkbox(
// 'enable_shortcodes',
// array('links_plus' => _t('启用短代码 [links_plus](前端调用,免重写)')),
// array(),
// _t('短代码支持'),
// _t('开启后,文章正文中使用 [links_plus] 将被动态替换为友链 HTML(不写回正文)。')
// );
// $form->addInput($enableShortcode);
$rewriteSort = new Typecho_Widget_Helper_Form_Element_Text(
'rewrite_sort',
null,
'',
_t('重写分类(可选)'),
_t('只输出指定分类 sort;留空为全部')
);
$rewriteSort->input->setAttribute('class', 'w-20');
$form->addInput($rewriteSort);
$rewriteSize = new Typecho_Widget_Helper_Form_Element_Text(
'rewrite_size',
null,
'0',
_t('重写图片尺寸(可选)'),
_t('0 表示使用插件默认尺寸')
);
$rewriteSize->input->setAttribute('class', 'w-10');
$form->addInput($rewriteSize->addRule('isInteger', _t('请填写整数数字')));
$rewriteWrapBang = new Typecho_Widget_Helper_Form_Element_Radio(
'rewrite_wrap_bang',
array(
'0' => _t('不包裹'),
'1' => _t('使用 !!! !!! 包裹(部分主题需要)'),
),
'0',
_t('重写输出 HTML'),
_t('有些主题/渲染器不支持直接输出 HTML,需要用 “!!!” 包裹整段 HTML 才会被当作原始 HTML 渲染。')
);
$form->addInput($rewriteWrapBang);
// //重写按钮(GET,走 Action,带 CSRF),这里是测试的时候用的
// $sec = Helper::security();
// $rewriteUrl = $sec->getIndex('/action/links-edit?do=rewrite');
// $rewriteBtn = new Typecho_Widget_Helper_Layout('p', array('class' => 'typecho-option'));
// $rewriteBtn->html(
// '<a class="btn primary" href="' . $rewriteUrl . '" ' .
// 'onclick="return confirm(\'确认要对配置的 cid 执行重写吗?该操作会直接修改文章/页面正文内容。\');">' .
// '执行重写</a>'
// );
// $form->addItem($rewriteBtn);
}
/**
* 生成用于“重写正文”的 HTML 字符串
*/
public static function buildRewriteHtml()
{
$options = Typecho_Widget::widget('Widget_Options');
$settings = $options->plugin('Links');
$pattern = isset($settings->rewrite_pattern) && $settings->rewrite_pattern ? $settings->rewrite_pattern : 'SHOW_TEXT';
$num = isset($settings->rewrite_num) ? (int)$settings->rewrite_num : 0;
$sort = isset($settings->rewrite_sort) && $settings->rewrite_sort !== '' ? (string)$settings->rewrite_sort : null;
// size=0 表示使用插件默认尺寸(dsize)
$size = isset($settings->rewrite_size) ? (int)$settings->rewrite_size : 0;
if ($size <= 0) {
$size = (int)$settings->dsize;
}
// 如果重写模式选择了文件模板(TPL:xxx),则把模板的 CSS/JS 一并内联写入正文,
// 以适配“主题不加载 head 注入 / 仅渲染正文”的场景。
$assetCss = '';
$assetJs = '';
if (is_string($pattern) && stripos($pattern, 'TPL:') === 0) {
$tplName = trim(substr($pattern, 4));
$templates = self::listTemplates();
if ($tplName !== '' && isset($templates[$tplName])) {
$manifest = $templates[$tplName];
$inject = isset($manifest['inject']) && is_array($manifest['inject']) ? $manifest['inject'] : array();
if (!empty($inject['css'])) {
$css = self::readTemplateFile($tplName, 'style.css');
if ($css && trim($css) !== '') {
$assetCss = "<style>\n" . $css . "\n</style>\n";
}
}
if (!empty($inject['js'])) {
$js = self::readTemplateFile($tplName, 'script.js');
if ($js && trim($js) !== '') {
$assetJs = "<script>\n" . $js . "\n</script>\n";
}
}
}
}
$html = Links_Plugin::output_str('', array($pattern, $num, $sort, $size, 'HTML'));
// 资产写在正文前,避免部分主题/解析器只截取首段导致样式丢失
if ($assetCss !== '' || $assetJs !== '') {
$html = $assetCss . $assetJs . (string)$html;
}
$wrap = isset($settings->rewrite_wrap_bang) ? (string)$settings->rewrite_wrap_bang : '0';
if ($wrap === '1') {
// Trim 只去两端空白,避免破坏内部格式
$html = trim((string)$html);
if ($html !== '') {
$html = "!!!\n" . $html . "\n!!!";
}
}
return $html;
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
public static function linksInstall()
{
$installDb = Typecho_Db::get();
$type = explode('_', $installDb->getAdapterName());
$type = array_pop($type);
$prefix = $installDb->getPrefix();
$scripts = file_get_contents('usr/plugins/Links/' . $type . '.sql');
$scripts = str_replace('typecho_', $prefix, $scripts);
$scripts = str_replace('%charset%', 'utf8', $scripts);
$scripts = explode(';', $scripts);
try {
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {
$installDb->query($script, Typecho_Db::WRITE);
}
}
return _t('建立友情链接数据表,插件启用成功');
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
if (('Mysql' == $type && (1050 == $code || '42S01' == $code)) ||
('SQLite' == $type && ('HY000' == $code || 1 == $code))
) {
try {
$script = 'SELECT `lid`, `name`, `url`, `sort`, `email`, `image`, `description`, `user`, `state`, `order` from `' . $prefix . 'links`';
$installDb->query($script, Typecho_Db::READ);
return _t('检测到友情链接数据表,友情链接插件启用成功');
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
if (('Mysql' == $type && (1054 == $code || '42S22' == $code)) ||
('SQLite' == $type && ('HY000' == $code || 1 == $code))
) {
return Links_Plugin::linksUpdate($installDb, $type, $prefix);
}
throw new Typecho_Plugin_Exception(_t('数据表检测失败,友情链接插件启用失败。错误号:') . $code);
}
} else {
throw new Typecho_Plugin_Exception(_t('数据表建立失败,友情链接插件启用失败。错误号:') . $code);
}
}
}
public static function linksUpdate($installDb, $type, $prefix)
{
$scripts = file_get_contents('usr/plugins/Links/Update_' . $type . '.sql');
$scripts = str_replace('typecho_', $prefix, $scripts);
$scripts = str_replace('%charset%', 'utf8', $scripts);
$scripts = explode(';', $scripts);
try {
foreach ($scripts as $script) {
$script = trim($script);