-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
623 lines (549 loc) · 18.8 KB
/
Copy pathfunctions.php
File metadata and controls
623 lines (549 loc) · 18.8 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
function themeConfig($form)
{
// 置顶栏内容
$topPost = new \Typecho\Widget\Helper\Form\Element\Textarea(
'topPost',
NULL,
NULL,
_t('<h2 style="margin: 1em 0;">【首页置顶栏设置】</h2>内容'),
_t('可以在这里填入置顶栏内容,留空则不显示置顶栏。')
);
$form->addInput($topPost);
// 置顶栏头像
$topPostAvatar = new \Typecho\Widget\Helper\Form\Element\Text(
'topPostAvatar',
NULL,
NULL,
_t('头像'),
_t('可以在这里填入置顶栏头像的 url 地址。')
);
$form->addInput($topPostAvatar);
// 置顶栏头像旁名字
$topPostAuthor = new \Typecho\Widget\Helper\Form\Element\Text(
'topPostAuthor',
NULL,
NULL,
_t('作者名字'),
_t('可以在这里填入置顶栏作者名字。')
);
$form->addInput($topPostAuthor);
// 置顶栏左下角定位文本
$topPostPositionText = new \Typecho\Widget\Helper\Form\Element\Text(
'topPostPositionText',
NULL,
NULL,
_t('定位(文本)'),
_t('可以在这里填入置顶栏左下角定位的文本。默认不显示。')
);
$form->addInput($topPostPositionText);
// 置顶栏左下角定位
$topPostPositionUrl = new \Typecho\Widget\Helper\Form\Element\Text(
'topPostPositionUrl',
NULL,
NULL,
_t('定位(跳转链接)'),
_t('可以在这里填入置顶栏左下角定位的 URL 跳转地址,设置了上方文本才生效。')
);
$form->addInput($topPostPositionUrl);
// 置顶栏右下角时间
$topPostDate = new \Typecho\Widget\Helper\Form\Element\Text(
'topPostDate',
NULL,
NULL,
_t('日期时间'),
_t('可以在这里填入置顶栏右下角的日期时间。默认不显示。<hr style="border: none; margin-bottom: 5em;">')
);
$form->addInput($topPostDate);
// 顶部高度
$topHeight = new \Typecho\Widget\Helper\Form\Element\Text(
'topHeight',
NULL,
NULL,
_t('<h2 style="margin: 1em 0;">【顶部区域设置】</h2>高度'),
_t('在这里填入顶部区域的高度值,例如 9rem 或者 180px 等等
<br>此处留空则顶部区域不显示。')
);
$form->addInput($topHeight);
// 顶部区域颜色
$topColor = new \Typecho\Widget\Helper\Form\Element\Text(
'topColor',
NULL,
NULL,
_t('颜色'),
_t('在这里设置顶部区域的颜色(纯色),例如 #FFF ')
);
$form->addInput($topColor);
// 顶部区域渐变色
$topGradientColor = new \Typecho\Widget\Helper\Form\Element\Text(
'topGradientColor',
NULL,
NULL,
_t('渐变色'),
_t('在这里设置顶部区域的渐变色(可以一个或多个,优先级高于上方纯色),例如
<br>linear-gradient(217deg, rgba(255, 0, 0, 0.8), rgba(255, 0, 0, 0) 70.71%),
<br>linear-gradient(127deg, rgba(0, 255, 0, 0.8), rgba(0, 255, 0, 0) 70.71%),
<br>linear-gradient(336deg, rgba(0, 0, 255, 0.8), rgba(0, 0, 255, 0) 70.71%) ')
);
$form->addInput($topGradientColor);
// 顶部区域背景图片
$topImage = new \Typecho\Widget\Helper\Form\Element\Text(
'topImage',
NULL,
NULL,
_t('背景图'),
_t('在这里填入顶部区域的背景图片 URL(支持Bing每日一图API接口),会覆盖在顶部颜色(纯色)上,会替换顶部渐变色。')
);
$form->addInput($topImage);
// 顶部区域 Logo
$logoUrl = new \Typecho\Widget\Helper\Form\Element\Text(
'logoUrl',
NULL,
NULL,
_t('Logo'),
_t('在这里填入顶部区域的站点 Logo 图片的 URL')
);
$form->addInput($logoUrl);
$logoSize = new \Typecho\Widget\Helper\Form\Element\Text(
'logoSize',
NULL,
NULL,
_t('Logo 大小'),
_t('在这里填入顶部区域的 Logo 图片的尺寸')
);
$form->addInput($logoSize);
// 顶部区域 Logo 点击跳转链接
$logoLink = new \Typecho\Widget\Helper\Form\Element\Text(
'logoLink',
NULL,
NULL,
_t('Logo 跳转链接'),
_t('在这里填入顶部区域 Logo 点击后的跳转地址,例如 /archives.html 留空则默认跳转首页。')
);
$form->addInput($logoLink);
// 顶部区域 Logo 左边的文本框
$topTitle = new \Typecho\Widget\Helper\Form\Element\Textarea(
'topTitle',
NULL,
NULL,
_t('站点标题'),
_t('可以在这里填入站点标题(Logo 左边的文本框),留空则不显示。')
);
$form->addInput($topTitle);
$topTitleDark = new \Typecho\Widget\Helper\Form\Element\Radio(
"topTitleDark",
[
"1" => _t("是"),
"0" => _t("不是"),
],
"0",
_t("站点标题是否显示为暗色"),
_t('默认亮色')
);
$form->addInput($topTitleDark);
// 顶部区域 Logo 下面的文本框
$topDescription = new \Typecho\Widget\Helper\Form\Element\Textarea(
'topDescription',
NULL,
NULL,
_t('站点描述'),
_t('可以在这里填入站点描述(Logo 下面的文本框),留空则不显示。<hr style="border: none; margin-bottom: 5em;">')
);
$form->addInput($topDescription);
// 返回按钮的位置
$backButtonPosition = new Typecho_Widget_Helper_Form_Element_Radio(
"backButtonPosition",
array(
"left" => _t('左上角'),
"right" => _t('右下角'),
"hide" => _t('不显示')
),
"hide", // 默认不显示
_t('<h2 style="margin: 1em 0;">【返回按钮设置】</h2>返回按钮的位置'),
_t('默认不显示返回按钮')
);
$form->addInput($backButtonPosition);
// 返回按钮的点击结果
$backButtonType = new Typecho_Widget_Helper_Form_Element_Radio(
"backButtonType",
array(
"home" => _t('返回首页'),
"history" => _t('返回上页'),
),
"history", // 默认不显示
_t('返回按钮的点击结果'),
_t('默认是回到上一页。')
);
$form->addInput($backButtonType);
// 顶部左上角的返回按钮
$backButtonText = new \Typecho\Widget\Helper\Form\Element\Text(
'backButtonText',
NULL,
NULL,
_t('左上角返回按钮的文本'),
_t('可以自定义左上角返回按钮的文本<hr style="border: none; margin-bottom: 5em;">')
);
$form->addInput($backButtonText);
// 站点底部版权信息
$copyrightStart = new \Typecho\Widget\Helper\Form\Element\Text(
'copyrightStart',
NULL,
NULL,
_t('<h2 style="margin: 1em 0;">【页脚设置】</h2>版权信息:开始的年份'),
_t('在这里填入底部的版权信息:开始的年份。')
);
$form->addInput($copyrightStart);
// 站点底部版权信息
$copyrightOwner = new \Typecho\Widget\Helper\Form\Element\Text(
'copyrightOwner',
NULL,
NULL,
_t('版权所有者的姓名'),
_t('在这里填入底部的版权信息:作者的姓名')
);
$form->addInput($copyrightOwner);
// 站点底部备案信息
$beianInfo = new \Typecho\Widget\Helper\Form\Element\Text(
'beianInfo',
NULL,
NULL,
_t('站点底部ICP备案'),
_t('在这里填入底部的备案号。<hr style="border: none; margin-bottom: 5em;">')
);
$form->addInput($beianInfo);
// 站点 Favicon
$siteFavicon = new \Typecho\Widget\Helper\Form\Element\Text(
'siteFavicon',
NULL,
NULL,
_t('<h2 style="margin: 1em 0;">【杂项】</h2>站点 Favicon'),
_t('在这里填入站点 Favicon 的 URL 地址,留空则默认为顶部区域 Logo。')
);
$form->addInput($siteFavicon);
// 自定义头部link
$customLink = new \Typecho\Widget\Helper\Form\Element\Textarea(
'customLink',
NULL,
NULL,
_t('自定义头部Link'),
_t('在这里填入放在<head>区域的<link>')
);
$form->addInput($customLink);
// 自定义CSS
$customCss = new \Typecho\Widget\Helper\Form\Element\Textarea(
'customCss',
NULL,
NULL,
_t('自定义CSS'),
_t('在这里填入自定义CSS代码')
);
$form->addInput($customCss);
// 自定义JavaScript
$customJs = new \Typecho\Widget\Helper\Form\Element\Textarea(
'customJs',
NULL,
NULL,
_t('自定义JS'),
_t('在这里填入自定义JavaScript代码<hr style="border: none; margin-bottom: 5em;">')
);
$form->addInput($customJs);
// Gravatar 头像加速地址
$gravatarUrl = new \Typecho\Widget\Helper\Form\Element\Text(
'gravatarUrl',
NULL,
NULL,
_t('Gravatar 头像加速地址'),
_t('用于替换 Gravatar 官方地址 http://www.gravatar.com ,提升国内访问速度
<br>可选服务:
<br>(WeAvatar)https://weavatar.com
<br>(Cravatar)https://cravatar.cn
<br>(Cravatar)https://cn.cravatar.com
<br>……')
);
$form->addInput($gravatarUrl);
}
function themeFields($layout)
{
echo '<style>
textarea {
width: 100%;
height: 8em;
}
input[type=text] {
width: 100%;
}
.field .description {
margin: 1em;
font-size: 1rem;
}
</style>';
$position = new \Typecho\Widget\Helper\Form\Element\Text(
'position',
null,
null,
_t('左下角显示定位'),
_t('<span>在这里填定位名称(例:成都市·天府广场)</span>')
);
$position->input->setAttribute('class', 't-default-find');
$layout->addItem($position);
$positionUrl = new \Typecho\Widget\Helper\Form\Element\Text(
'positionUrl',
null,
null,
_t('定位跳转URL')
);
$positionUrl->input->setAttribute('class', 't-default-find');
$layout->addItem($positionUrl);
$isAdvertise = new \Typecho\Widget\Helper\Form\Element\Radio(
"isAdvertise",
[
"1" => _t("是"),
"0" => _t("不是"),
],
"0",
_t("是否是广告"),
_t('<span>默认不是</span>')
);
$isAdvertise->input->setAttribute('class', 't-default-find');
$layout->addItem($isAdvertise);
}
/**
* 获取 Gravatar 头像 URL(支持加速地址)
*/
function getGravatarUrl($email)
{
$options = \Widget\Options::alloc();
// 获取配置的 Gravatar 加速地址,如果未配置则使用默认值
$gravatarBase = $options->gravatarUrl;
// if (empty($gravatarBase)) {
// $gravatarBase = 'https://weavatar.com';
// }
// 移除末尾的斜杠
$gravatarBase = rtrim($gravatarBase, '/');
// 生成头像 URL - 使用SHA256代替MD5
$hash = hash('sha256', strtolower(trim($email)));
$url = $gravatarBase . '/avatar/' . $hash;
return $url;
}
/**
* 获取文章自定义字段
* @param int $cid 文章ID
* @param string $name 字段名
* @return array 字段数据
*/
function getArticleFieldsByCid($cid, $name)
{
$db = Typecho\Db::get();
return $db->fetchAll(
$db->select('*')
->from('table.fields')
->where('cid = ?', $cid)
->where('name = ?', $name)
);
}
/**
* 自动为外链添加安全属性,并为图片添加灯箱功能
*/
function addAttributesForLinksAndImgs($content)
{
if (empty($content)) {
return $content;
}
$siteHost = parse_url(Utils\Helper::options()->siteUrl, PHP_URL_HOST);
// 为图片添加灯箱类
$content = preg_replace_callback(
'/<img(?!.*?class="[^"]*simple-lightbox[^"]*")([^>]*)>/i',
function ($matches) {
return '<img class="simple-lightbox" loading="lazy" ' . $matches[1] . '>';
},
$content
);
// 为外链添加安全属性
$content = preg_replace_callback(
'/<a\s+([^>]*?)href=["\'](https?:\/\/[^"\']+)["\']([^>]*?)>/i',
function ($matches) use ($siteHost) {
$linkHost = parse_url($matches[2], PHP_URL_HOST);
$attrs = $matches[1] . $matches[3];
// 如果是外链
if ($linkHost && $linkHost !== $siteHost) {
// 检查是否已有target属性
if (!preg_match('/\starget\s*=/i', $attrs)) {
$attrs .= ' target="_blank"';
}
// 检查是否已有rel属性
if (!preg_match('/\srel\s*=/i', $attrs)) {
$attrs .= ' rel="noopener noreferrer"';
} else {
// 确保rel包含必要的值
$attrs = preg_replace(
'/\srel\s*=\s*["\']([^"\']*)["\']/i',
' rel="$1 noopener noreferrer"',
$attrs
);
}
}
// 确保URL经过转义
$url = htmlspecialchars($matches[2], ENT_QUOTES, 'UTF-8');
return '<a ' . trim($attrs) . ' href="' . $url . '">';
},
$content
);
return $content;
}
/**
* 带HTML标签的内容截取
* @param string $html HTML内容
* @param int $length 截取长度
* @return string 截取后的内容
*/
function truncateHtmlWithTags($html, $length)
{
$text_length = 0;
$output = '';
$tag_stack = [];
$i = 0;
$html_length = mb_strlen($html, 'UTF-8');
while ($i < $html_length && $text_length < $length) {
$char = mb_substr($html, $i, 1, 'UTF-8');
if ($char === '<') {
$tag_end = mb_strpos($html, '>', $i);
if ($tag_end !== false) {
$tag_content = mb_substr($html, $i, $tag_end - $i + 1, 'UTF-8');
// 跳过 <img> 标签,摘要不输出图片
if (preg_match('/<img/i', $tag_content)) {
$i = $tag_end + 1;
continue;
}
$output .= $tag_content;
if (preg_match('/<(\w+)(?:\s|>)/i', $tag_content, $matches)) {
$tag_name = $matches[1];
if (!preg_match('/<\w+[^>]*\/>/i', $tag_content) && !preg_match('/<\/\w+>/i', $tag_content)) {
array_push($tag_stack, $tag_name);
}
}
if (preg_match('/<\/(\w+)>/i', $tag_content, $matches)) {
$tag_name = $matches[1];
$key = array_search($tag_name, array_reverse($tag_stack, true));
if ($key !== false) {
array_splice($tag_stack, count($tag_stack) - 1 - $key, 1);
}
}
$i = $tag_end + 1;
continue;
}
}
$output .= $char;
$text_length++;
$i++;
}
while (!empty($tag_stack)) {
$output .= '</' . array_pop($tag_stack) . '>';
}
return preg_replace('/(<br\s*\/?>|\s| |<p>\s*<\/p>)+$/i', '', $output);
}
/**
* 文章摘要按字数截取
*/
function smartExcerpt($widget, $length = 140, $suffix = '...')
{
$content = $widget->content;
// 优先检查 <!--more--> 标签
if (strpos($content, '<!--more-->') !== false) {
return $widget->content($suffix);
}
$content = addAttributesForLinksAndImgs($content);
$plain_text = trim(preg_replace('/\s+/', ' ', strip_tags($content)));
$is_truncated = mb_strlen($plain_text, 'UTF-8') > $length;
if ($is_truncated) {
$excerpt = truncateHtmlWithTags($content, $length) . $suffix;
} else {
$excerpt = $content;
}
return $excerpt;
}
/**
* 获取纯文本摘要
* @param string $content 内容
* @param int $length 摘要长度
* @param string $suffix 后缀
* @return string 纯文本摘要
*/
function getPlainTextExcerpt($content, $length = 100, $suffix = '...')
{
$length = (int)$length;
// 总是尝试转换 Markdown 以处理语法标记
$content = \Utils\Markdown::convert($content);
$plain_text = trim(preg_replace('/\s+/', ' ', strip_tags($content)));
$is_truncated = mb_strlen($plain_text, 'UTF-8') > $length;
if ($is_truncated) {
return mb_substr($plain_text, 0, $length) . $suffix;
} else {
return $plain_text;
}
}
/**
* 获取微信朋友圈风格的时间轴归档
* @param int $summary_length 摘要长度
* @param int $page 页码,从1开始
* @param int $perPage 每页数量
* @return string HTML内容
*/
function getArchiveTimelineMoments($summary_length = 100, $page = 1, $perPage = 10)
{
$db = Typecho\Db::get();
$offset = ($page - 1) * $perPage;
$posts = $db->fetchAll(
$db->select('cid', 'title', 'created', 'slug', 'text')
->from('table.contents')
->where('status = ? AND type = ?', 'publish', 'post')
->order('created', Typecho\Db::SORT_DESC)
->limit($perPage)
->offset($offset)
);
if (empty($posts)) {
if ($page == 1) {
return '<div class="moments-empty">
<div class="empty-icon">📝</div>
<p>还没有发布日志</p>
</div>';
} else {
return ''; // 没有更多内容
}
}
$moments = '';
$currentDate = '';
foreach ($posts as $post) {
$year = date('Y', $post['created']);
if ($currentDate !== $year) {
$currentDate = $year;
$moments .= '<div><span style="font-weight:bold;font-size:1.4em;">'
. $currentDate . '</span>年</div>';
}
$excerpt = getPlainTextExcerpt($post['text'], $summary_length);
$options = Typecho\Widget::widget('Widget_Options');
$postUrl = Typecho\Router::url('post', array(
'cid' => $post['cid'],
'slug' => $post['slug'],
'year' => date('Y', $post['created']),
'month' => date('m', $post['created']),
'day' => date('d', $post['created'])
), $options->index);
$moments .= '<div class="moments-group">
<div class="moments-date">
<date>' . date('m月', $post['created']) . '</date>
<span class="mg-day">' . date('d', $post['created']) . '</span>
</div>
<div class="moment-body">
<a href="' . htmlspecialchars($postUrl) . '">
' . $excerpt . '
</a>
</div>
</div>';
}
if ($page == 1) {
return '<div class="moments-container">' . $moments . '</div>';
} else {
return $moments;
}
}