-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfunctions.php
704 lines (641 loc) · 25.5 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
define('CREAMy_VERSION', '1.0.7');
define('__TYPECHO_GRAVATAR_PREFIX__', Helper::options()->Gravatar ? Helper::options()->Gravatar : '//cdn.v2ex.com/gravatar/');
require_once 'lib/Utils.php';
require_once 'lib/Comments.php';
if (!empty(Helper::options()->cdn)) {
define('__TYPECHO_UPLOAD_URL__', $_SERVER['REQUEST_SCHEME'] . '://' . Helper::options()->cdn);
}
function themeConfig($form)
{
echo '<script>var CREAMy_VERSION = "' . CREAMy_VERSION . '";</script>';
?>
<style>form {
position: relative;
max-width: 100%
}
form input:not([type]), form input[type="date"], form input[type="datetime-local"], form input[type="email"], form input[type="number"], form input[type="password"], form input[type="search"], form input[type="tel"], form input[type="time"], form input[type="text"], form input[type="file"], form input[type="url"] {
font-family: 'Lato', 'Helvetica Neue', Arial, Helvetica, sans-serif;
margin: 0em;
outline: none;
-webkit-appearance: none;
tap-highlight-color: rgba(255, 255, 255, 0);
line-height: 1.21428571em;
padding: 0.67857143em 1em;
font-size: 1em;
background: #FFFFFF;
border: 1px solid rgba(34, 36, 38, 0.15);
color: rgba(0, 0, 0, 0.87);
border-radius: 0.28571429rem;
-webkit-box-shadow: 0em 0em 0em 0em transparent inset;
box-shadow: 0em 0em 0em 0em transparent inset;
-webkit-transition: color 0.5s ease, border-color 0.5s ease;
transition: color 0.5s ease, border-color 0.5s ease
}
form textarea {
margin: 0em;
-webkit-appearance: none;
tap-highlight-color: rgba(255, 255, 255, 0);
padding: 0.78571429em 1em;
background: #FFFFFF;
border: 1px solid rgba(34, 36, 38, 0.15);
outline: none;
color: rgba(0, 0, 0, 0.87);
border-radius: 0.28571429rem;
-webkit-box-shadow: 0em 0em 0em 0em transparent inset;
box-shadow: 0em 0em 0em 0em transparent inset;
-webkit-transition: color 0.1s ease, border-color 0.5s ease;
transition: color 0.1s ease, border-color 0.5s ease;
font-size: 1em;
line-height: 1.2857;
resize: vertical
}
form textarea:not([rows]) {
height: 12em;
min-height: 8em;
max-height: 24em
}
form textarea, form input[type="checkbox"] {
vertical-align: top
}
form textarea:focus, form input:focus {
color: rgba(0, 0, 0, 0.95);
border-color: #85B7D9;
border-radius: 0.28571429rem;
background: #FFFFFF;
-webkit-box-shadow: 0px 0em 0em 0em rgba(34, 36, 38, 0.35) inset;
box-shadow: 0px 0em 0em 0em rgba(34, 36, 38, 0.35) inset;
-webkit-appearance: none
}
.tip {
max-width: 100%;
position: relative;
min-height: 1em;
margin: 0 10px;
background: #F8F8F9;
padding: 1em 1.5em;
line-height: 1.4285em;
color: rgba(0, 0, 0, 0.87);
-webkit-transition: opacity 0.1s ease, color 0.1s ease, background 0.1s ease, -webkit-box-shadow 0.1s ease;
transition: opacity 0.1s ease, color 0.1s ease, background 0.1s ease, -webkit-box-shadow 0.1s ease;
transition: opacity 0.1s ease, color 0.1s ease, background 0.1s ease, box-shadow 0.1s ease;
transition: opacity 0.1s ease, color 0.1s ease, background 0.1s ease, box-shadow 0.1s ease, -webkit-box-shadow 0.1s ease;
border-radius: 0.28571429rem;
-webkit-box-shadow: 0 0 0 1px rgba(34, 36, 38, .22) inset, 0 2px 4px 0 rgba(34, 36, 38, .12), 0 2px 10px 0 rgba(34, 36, 38, .15);
box-shadow: 0 0 0 1px rgba(34, 36, 38, .22) inset, 0 2px 4px 0 rgba(34, 36, 38, .12), 0 2px 10px 0 rgba(34, 36, 38, .15)
}
.tip-header {
text-align: center;
margin: 10px auto 20px auto;
color: #444;
text-shadow: 0 0 2px #c2c2c2
}
.current-ver {
position: relative;
border-color: #b21e1e !important;
background-color: #DB2828 !important;
color: #FFF !important;
left: -37px;
padding-left: 1rem;
border-bottom-right-radius: 5px;
padding-right: 1.2em
}
.current-ver:after {
position: absolute;
content: '';
top: 100%;
left: 0;
background-color: transparent !important;
border-style: solid;
border-width: 0 1.2em 1.2em 0;
border-color: transparent;
border-right-color: inherit;
width: 0;
height: 0
}
.btn.primary {
cursor: pointer;
display: inline-block;
background: #E0E1E2 none;
color: rgba(0, 0, 0, 0.6);
padding: 0 1.5em;
border-radius: 0.28571429rem;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
outline: none;
-webkit-transition: opacity 0.5s ease, background-color 0.5s ease, color 0.5s ease, background 0.5s ease, -webkit-box-shadow 0.5s ease;
transition: opacity 0.5s ease, background-color 0.5s ease, color 0.5s ease, background 0.5s ease, -webkit-box-shadow 0.5s ease;
transition: opacity 0.5s ease, background-color 0.5s ease, color 0.5s ease, box-shadow 0.5s ease, background 0.5s ease;
transition: opacity 0.5s ease, background-color 0.5s ease, color 0.5s ease, box-shadow 0.5s ease, background 0.5s ease, -webkit-box-shadow 0.5s ease;
-webkit-tap-highlight-color: transparent
}
.btn.primary:hover {
background-color: #CACBCD;
color: rgba(0, 0, 0, 0.8)
}
.btn.primary[type="submit"] {
position: fixed;
right: 100px;
bottom: 100px
}
.btn.confirm {
background-color: #95f798 !important
}
.btn.alert {
background-color: #fa9492 !important
}
i.confirm {
position: absolute;
left: .5em
}
i.confirm:after, i.confirm:before {
content: "";
background: green;
display: block;
position: absolute;
width: 3px;
border-radius: 3px
}
i.confirm:after {
height: 6px;
transform: rotate(-45deg);
top: 9px;
left: 6px
}
i.confirm:before {
height: 11px;
transform: rotate(45deg);
top: 5px;
left: 10px
}
i.alert {
position: absolute;
left: .5em
}
i.alert:after, i.alert:before {
content: "";
background: red;
display: block;
position: absolute;
width: 3px;
border-radius: 3px;
left: 9px
}
i.alert:after {
height: 3px;
top: 14px
}
i.alert:before {
height: 8px;
top: 4px
}
.multiline {
position: relative;
display: inline-block;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
outline: none;
vertical-align: baseline;
font-style: normal;
min-height: 17px;
font-size: 1rem;
line-height: 17px;
min-width: 17px
}
.multiline input[type="checkbox"], .multiline input[type="radio"] {
cursor: pointer;
position: absolute;
top: 0px;
left: 0px;
opacity: 0 !important;
outline: none;
z-index: 3;
width: 17px;
height: 17px
}
.multiline {
min-height: 1.5rem
}
.multiline input {
width: 3.5rem;
height: 1.5rem
}
.multiline .box, .multiline label {
min-height: 1.5rem;
padding-left: 4.5rem;
color: rgba(0, 0, 0, 0.87)
}
.multiline label {
padding-top: 0.15em
}
.multiline .box:before, .multiline label:before {
cursor: pointer;
display: block;
position: absolute;
content: '';
z-index: 1;
-webkit-transform: none;
transform: none;
border: none;
top: 0rem;
background: rgba(0, 0, 0, 0.05);
-webkit-box-shadow: none;
box-shadow: none;
width: 3.5rem;
height: 1rem;
border-radius: 500rem
}
.multiline .box:after, .multiline label:after {
cursor: pointer;
background: #FFFFFF -webkit-gradient(linear, left top, left bottom, from(transparent), to(rgba(0, 0, 0, 0.05)));
background: #FFFFFF -webkit-linear-gradient(transparent, rgba(0, 0, 0, 0.05));
background: #FFFFFF linear-gradient(transparent, rgba(0, 0, 0, 0.05));
position: absolute;
content: '' !important;
opacity: 1;
z-index: 2;
border: none;
-webkit-box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15), 0px 0px 0px 1px rgba(34, 36, 38, 0.15) inset;
box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15), 0px 0px 0px 1px rgba(34, 36, 38, 0.15) inset;
width: 1.2rem;
height: 1.2rem;
top: -.1rem;
left: 0em;
border-radius: 500rem;
-webkit-transition: background 0.3s ease, left 0.3s ease;
transition: background 0.3s ease, left 0.3s ease
}
.multiline input ~ .box:after, .multiline input ~ label:after {
left: -0.05rem;
-webkit-box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15), 0px 0px 0px 1px rgba(34, 36, 38, 0.15) inset;
box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15), 0px 0px 0px 1px rgba(34, 36, 38, 0.15) inset
}
.multiline input:focus ~ .box:before, .multiline input:focus ~ label:before {
background-color: rgba(0, 0, 0, 0.15);
border: none
}
.multiline .box:hover::before, .multiline label:hover::before {
background-color: rgba(0, 0, 0, 0.15);
border: none
}
.multiline input:checked ~ .box, .multiline input:checked ~ label {
color: rgba(0, 0, 0, 0.95) !important
}
.multiline input:checked ~ .box:before, .multiline input:checked ~ label:before {
background-color: #2185D0 !important
}
.multiline input:checked ~ .box:after, .multiline input:checked ~ label:after {
left: 2.3rem;
-webkit-box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15), 0px 0px 0px 1px rgba(34, 36, 38, 0.15) inset;
box-shadow: 0px 1px 2px 0 rgba(34, 36, 38, 0.15), 0px 0px 0px 1px rgba(34, 36, 38, 0.15) inset
}
.multiline input:focus:checked ~ .box, .multiline input:focus:checked ~ label {
color: rgba(0, 0, 0, 0.95) !important
}
.multiline input:focus:checked ~ .box:before, .multiline input:focus:checked ~ label:before {
background-color: #0d71bb !important
}
#typecho-option-item-MathJaxConfig-15 {
display: none
}
</style>
<?php
echo '<div class="tip"><span class="current-ver"><strong><code>Ver ' . CREAMy_VERSION . '</code></strong></span>
<div class="tip-header"><h1>Theme-CREAMy</h1></div>
<p>感谢选择使用 <code>CREAMy</code> </p>
<p>查看<a href="//iobiji.com/zi-ji-xie-de-di-yi-kuan-typechozhu-ti-jaydenforu/">帮助手册</a> <a href="https://github.com/JaydenForYou/CREAMy/issues">issue</a></p>
</div>';
$logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('站点 LOGO 地址'), _t('在这里填入一个图片 URL 地址, 以在网站标题前加上一个 LOGO'));
$bgUrl = new Typecho_Widget_Helper_Form_Element_Text('bgUrl', NULL, NULL, _t('首页背景图片'), _t('在这里填入一个图片 URL 地址'));
$Gravatar = new Typecho_Widget_Helper_Form_Element_Text('Gravatar', NULL, NULL, _t('自定义 Gravatar 源'), _t('输入Gravatar源,如https://cdn.v2ex.com/gravatar/'));
$cdn = new Typecho_Widget_Helper_Form_Element_Text('cdn', NULL, NULL, _t('使用自己的静态存储'), _t('输入静态存储链接(不需要带http/https)'));
$APPID = new Typecho_Widget_Helper_Form_Element_Text('APPID', NULL, NULL, _t('APP ID'), _t('输入在Valine获取的APP ID'));
$APPKEY = new Typecho_Widget_Helper_Form_Element_Text('APPKEY', NULL, NULL, _t('APP KEY'), _t('输入在Valine获取的APP KEY'));
$weibo = new Typecho_Widget_Helper_Form_Element_Text('weibo', NULL, NULL, _t('微博地址'), _t('输入微博地址'));
$twitter = new Typecho_Widget_Helper_Form_Element_Text('twitter', NULL, NULL, _t('推特地址'), _t('输入推特地址'));
$tongji = new Typecho_Widget_Helper_Form_Element_Text('tongji', NULL, NULL, _t('百度统计URL'), _t('输入百度统计URL'));
$tips = new Typecho_Widget_Helper_Form_Element_Text('tips', NULL, NULL, _t('前台公告'), _t(''));
$beian = new Typecho_Widget_Helper_Form_Element_Text('beian', NULL, NULL, _t('ICP备案号'), _t(''));
$Subtitle = new Typecho_Widget_Helper_Form_Element_Text('Subtitle', NULL, NULL, _t('站点副标题'), _t(''));
$alipay = new Typecho_Widget_Helper_Form_Element_Text('alipay', NULL, NULL, _t('支付宝收款二维码链接'), _t(''));
$wpay = new Typecho_Widget_Helper_Form_Element_Text('wpay', NULL, NULL, _t('微信收款二维码链接'), _t(''));
$qiniu = new Typecho_Widget_Helper_Form_Element_Text('qiniu', NULL, NULL, _t('七牛云替换全站镜像'), _t('需要带http/https'));
$form->addInput($logoUrl);
$form->addInput($bgUrl);
$form->addInput($tips);
$form->addInput($Subtitle);
$form->addInput($beian);
$form->addInput($qiniu);
$form->addInput($tongji);
$form->addInput($weibo);
$form->addInput($twitter);
$form->addInput($Gravatar);
$form->addInput($cdn);
$form->addInput($APPID);
$form->addInput($APPKEY);
$form->addInput($alipay);
$form->addInput($wpay);
$JConfig = new Typecho_Widget_Helper_Form_Element_Checkbox('JConfig',
array(
'enableLazyload' => '开启图片懒加载<a href="https://appelsiini.net/projects/lazyload" target="_blank">lazyload</a>',
'enableComments' => '开启主题自带评论系统',
'enablePJAX' => '开启全站PJAX'
),
[],
'开关设置'
);
$form->addInput($JConfig->multiMode());
}
function themeFields($layout)
{
//$showTOC = new Typecho_Widget_Helper_Form_Element_Radio('showTOC', array(true => _t('开启'), false => _t('关闭')), false, _t('文章目录'), _t('仅会解析h2和h3标题,最多解析两层'));
$previewContent = new Typecho_Widget_Helper_Form_Element_Text('previewContent', NULL, NULL, _t('文章摘要'), _t('设置文章的预览内容,留空自动截取文章前55个字。'));
$thumbnail = new Typecho_Widget_Helper_Form_Element_Text('thumbnail', NULL, NULL, _t('文章/页面缩略图Url'), _t('需要带上http(s)://'));
$layout->addItem($thumbnail);
$layout->addItem($previewContent);
//$layout->addItem($showTOC);
}
function get_post_view($archive)
{
$cid = $archive->cid;
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
echo 0;
return;
}
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
if ($archive->is('single')) {
$views = Typecho_Cookie::get('extend_contents_views');
if (empty($views)) {
$views = array();
} else {
$views = explode(',', $views);
}
if (!in_array($cid, $views)) {
$db->query($db->update('table.contents')->rows(array('views' => (int)$row['views'] + 1))->where('cid = ?', $cid));
array_push($views, $cid);
$views = implode(',', $views);
Typecho_Cookie::set('extend_contents_views', $views); //记录查看cookie
}
}
echo $row['views'];
}
function themeInit($archive)
{
// 判断是否是添加评论的操作
// 为文章或页面、post操作,且包含参数`themeAction=comment`(自定义)
if ($archive->is('single') && $archive->request->isPost() && $archive->request->is('Ajax=comment')) {
// 为添加评论的操作时
ajaxComment($archive);
}
}
/**
* ajaxComment
* 实现Ajax评论的方法(实现feedback中的comment功能)
* @param Widget_Archive $archive
* @return void
*/
function ajaxComment($archive)
{
$options = Helper::options();
$user = Typecho_Widget::widget('Widget_User');
$db = Typecho_Db::get();
// Security 验证不通过时会直接跳转,所以需要自己进行判断
// 需要开启反垃圾保护,此时将不验证来源
if ($archive->request->get('_') != Helper::security()->getToken($archive->request->getReferer())) {
$archive->response->throwJson(array('status' => 0, 'msg' => _t('非法请求')));
}
/** 评论关闭 */
if (!$archive->allow('comment')) {
$archive->response->throwJson(array('status' => 0, 'msg' => _t('评论已关闭')));
}
/** 检查ip评论间隔 */
if (!$user->pass('editor', true) && $archive->authorId != $user->uid &&
$options->commentsPostIntervalEnable) {
$latestComment = $db->fetchRow($db->select('created')->from('table.comments')
->where('cid = ?', $archive->cid)
->where('ip = ?', $archive->request->getIp())
->order('created', Typecho_Db::SORT_DESC)
->limit(1));
if ($latestComment && ($options->gmtTime - $latestComment['created'] > 0 &&
$options->gmtTime - $latestComment['created'] < $options->commentsPostInterval)) {
$archive->response->throwJson(array('status' => 0, 'msg' => _t('对不起, 您的发言过于频繁, 请稍侯再次发布')));
}
}
$comment = array(
'cid' => $archive->cid,
'created' => $options->gmtTime,
'agent' => $archive->request->getAgent(),
'ip' => $archive->request->getIp(),
'ownerId' => $archive->author->uid,
'type' => 'comment',
'status' => !$archive->allow('edit') && $options->commentsRequireModeration ? 'waiting' : 'approved'
);
/** 判断父节点 */
if ($parentId = $archive->request->filter('int')->get('parent')) {
if ($options->commentsThreaded && ($parent = $db->fetchRow($db->select('coid', 'cid')->from('table.comments')
->where('coid = ?', $parentId))) && $archive->cid == $parent['cid']) {
$comment['parent'] = $parentId;
} else {
$archive->response->throwJson(array('status' => 0, 'msg' => _t('父级评论不存在')));
}
}
$feedback = Typecho_Widget::widget('Widget_Feedback');
//检验格式
$validator = new Typecho_Validate();
$validator->addRule('author', 'required', _t('必须填写用户名'));
$validator->addRule('author', 'xssCheck', _t('请不要在用户名中使用特殊字符'));
$validator->addRule('author', array($feedback, 'requireUserLogin'), _t('您所使用的用户名已经被注册,请登录后再次提交'));
$validator->addRule('author', 'maxLength', _t('用户名最多包含200个字符'), 200);
if ($options->commentsRequireMail && !$user->hasLogin()) {
$validator->addRule('mail', 'required', _t('必须填写电子邮箱地址'));
}
$validator->addRule('mail', 'email', _t('邮箱地址不合法'));
$validator->addRule('mail', 'maxLength', _t('电子邮箱最多包含200个字符'), 200);
if ($options->commentsRequireUrl && !$user->hasLogin()) {
$validator->addRule('url', 'required', _t('必须填写个人主页'));
}
$validator->addRule('url', 'url', _t('个人主页地址格式错误'));
$validator->addRule('url', 'maxLength', _t('个人主页地址最多包含200个字符'), 200);
$validator->addRule('text', 'required', _t('必须填写评论内容'));
$comment['text'] = $archive->request->text;
/** 对一般匿名访问者,将用户数据保存一个月 */
if (!$user->hasLogin()) {
/** Anti-XSS */
$comment['author'] = $archive->request->filter('trim')->author;
$comment['mail'] = $archive->request->filter('trim')->mail;
$comment['url'] = $archive->request->filter('trim')->url;
/** 修正用户提交的url */
if (!empty($comment['url'])) {
$urlParams = parse_url($comment['url']);
if (!isset($urlParams['scheme'])) {
$comment['url'] = 'http://' . $comment['url'];
}
}
$expire = $options->gmtTime + $options->timezone + 30 * 24 * 3600;
Typecho_Cookie::set('__typecho_remember_author', $comment['author'], $expire);
Typecho_Cookie::set('__typecho_remember_mail', $comment['mail'], $expire);
Typecho_Cookie::set('__typecho_remember_url', $comment['url'], $expire);
} else {
$comment['author'] = $user->screenName;
$comment['mail'] = $user->mail;
$comment['url'] = $user->url;
/** 记录登录用户的id */
$comment['authorId'] = $user->uid;
}
/** 评论者之前须有评论通过了审核 */
if (!$options->commentsRequireModeration && $options->commentsWhitelist) {
if ($feedback->size($feedback->select()->where('author = ? AND mail = ? AND status = ?', $comment['author'], $comment['mail'], 'approved'))) {
$comment['status'] = 'approved';
} else {
$comment['status'] = 'waiting';
}
}
if ($error = $validator->run($comment)) {
$archive->response->throwJson(array('status' => 0, 'msg' => implode(';', $error)));
}
/** 添加评论 */
if (preg_match("/[\x{4e00}-\x{9fa5}]/u", $comment['text']) == 0) {
$archive->response->throwJson(array('status' => 0, 'msg' => _t('评论内容请不少于一个中文汉字')));
}
$commentId = $feedback->insert($comment);
if (!$commentId) {
$archive->response->throwJson(array('status' => 0, 'msg' => _t('评论失败')));
}
Typecho_Cookie::delete('__typecho_remember_text');
$db->fetchRow($feedback->select()->where('coid = ?', $commentId)
->limit(1), array($feedback, 'push'));
$feedback->pluginHandle()->finishComment($feedback);
// 返回评论数据
$data = array(
'cid' => $feedback->cid,
'coid' => $feedback->coid,
'parent' => $feedback->parent,
'mail' => $feedback->mail,
'url' => $feedback->url,
'ip' => $feedback->ip,
'agent' => parseUA($feedback->agent),
'author' => $feedback->author,
'authorId' => $feedback->authorId,
'permalink' => $feedback->permalink,
'created' => $feedback->created,
'datetime' => $feedback->date->format('Y-m-d H:i:s'),
'status' => $feedback->status,
);
// 评论内容
ob_start();
$feedback->content();
$data['content'] = ob_get_clean();
$data['avatar'] = Typecho_Common::gravatarUrl($data['mail'], 48, Helper::options()->commentsAvatarRating, NULL, $archive->request->isSecure());
$archive->response->throwJson(array('status' => 1, 'comment' => $data));
}
function parseUA($ua)
{
// 解析操作系统
$htmlTag = "";
$os = null;
$fontClass = null;
if (preg_match('/Windows NT 6.0/i', $ua)) {
$os = "Windows Vista";
} elseif (preg_match('/Windows NT 6.1/i', $ua)) {
$os = "Windows 7";
} elseif (preg_match('/Windows NT 6.2/i', $ua)) {
$os = "Windows 8";
} elseif (preg_match('/Windows NT 6.3/i', $ua)) {
$os = "Windows 8.1";
} elseif (preg_match('/Windows NT 10.0/i', $ua)) {
$os = "Windows 10";
} elseif (preg_match('/Windows NT 5.1/i', $ua)) {
$os = "Windows XP";
} elseif (preg_match('/Windows NT 5.2/i', $ua) && preg_match('/Win64/i', $ua)) {
$os = "Windows XP 64 bit";
} elseif (preg_match('/Android ([0-9.]+)/i', $ua, $matches)) {
$os = "Android " . $matches[1];
} elseif (preg_match('/iPhone OS ([_0-9]+)/i', $ua, $matches)) {
$os = 'iPhone ' . $matches[1];
} elseif (preg_match('/iPad/i', $ua)) {
$os = "iPad";
} elseif (preg_match('/Mac OS X ([_0-9]+)/i', $ua, $matches)) {
$os = 'Mac OS X ' . $matches[1];
} elseif (preg_match('/Gentoo/i', $ua)) {
$os = 'Gentoo Linux';
} elseif (preg_match('/Ubuntu/i', $ua)) {
$os = 'Ubuntu Linux';
} elseif (preg_match('/Debian/i', $ua)) {
$os = 'Debian Linux';
} elseif (preg_match('/X11; FreeBSD/i', $ua)) {
$os = 'FreeBSD';
} elseif (preg_match('/X11; Linux/i', $ua)) {
$os = 'Linux';
} else {
$os = 'unknown os';
}
$htmlTag = '<span class="vsys">' . $os . '</span>';
$browser = null;
//解析浏览器
if (preg_match('#SE 2([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Sogou browser';
} elseif (preg_match('#360([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = '360 browser ';
} elseif (preg_match('#Maxthon( |\/)([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Maxthon ';
} elseif (preg_match('#Edge( |\/)([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Edge ';
} elseif (preg_match('#MicroMessenger/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Wechat ';
} elseif (preg_match('#QQ/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'QQ Mobile ';
} elseif (preg_match('#Chrome/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Chrome ';
} elseif (preg_match('#CriOS/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Chrome ';
} elseif (preg_match('#Chromium/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Chromium ';
} elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Safari ';
} elseif (preg_match('#opera mini#i', $ua)) {
preg_match('#Opera/([a-zA-Z0-9.]+)#i', $ua, $matches);
$browser = 'Opera Mini ';
} elseif (preg_match('#Opera.([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Opera ';
} elseif (preg_match('#QQBrowser ([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'QQ browser ';
} elseif (preg_match('#UCWEB([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'UCWEB ';
} elseif (preg_match('#MSIE ([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Internet Explorer ';
} elseif (preg_match('#Trident/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Internet Explorer 11';
} elseif (preg_match('#(Firefox|Phoenix|Firebird|BonEcho|GranParadiso|Minefield|Iceweasel)/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$browser = 'Firefox ';
} else {
$browser = 'unknown br';
}
$htmlTag .= " ";
$htmlTag .= '<span class="vsys">' . $browser . '</span>';
return $htmlTag;
}
function getCommentAt($coid)
{
$db = Typecho_Db::get();
$prow = $db->fetchRow($db->select('parent')
->from('table.comments')
->where('coid = ? AND status = ?', $coid, 'approved'));
$parent = $prow['parent'];
if ($parent != "0") {
$arow = $db->fetchRow($db->select('author')
->from('table.comments')
->where('coid = ? AND status = ?', $parent, 'approved'));
$author = $arow['author'];
$href = '<a href="#comment-' . $parent . '" class="cute atreply">@' . $author . '</a>';
//$href = '@'.$author;
return $href;
} else {
return '';
}
}