-
Notifications
You must be signed in to change notification settings - Fork 16
/
Plugin.php
904 lines (818 loc) · 34 KB
/
Plugin.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
<?php
namespace TypechoPlugin\CommentNotifier;
use Typecho\Plugin\PluginInterface;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Layout;
use Widget\Options;
use Widget\Base\Comments;
use Typecho\Db;
use Typecho\Date;
use Utils\Helper;
use Widget\Feedback;
use Widget\Service;
use Widget\Comments\Edit;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require dirname(__FILE__) . '/PHPMailer/PHPMailer.php';
require dirname(__FILE__) . '/PHPMailer/SMTP.php';
require dirname(__FILE__) . '/PHPMailer/Exception.php';
/**
* typecho 评论通过时发送邮件提醒,要求typecho1.2.0及以上
*
* @package CommentNotifier
* @author 泽泽社长
* @version 1.8.0
* @link https://github.com/jrotty/CommentNotifier
*/
class Plugin implements PluginInterface
{
/** @var string 控制菜单链接 */
public static $panel = 'CommentNotifier/console.php';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return string
*/
public static function activate()
{
Feedback::pluginHandle()->finishComment = __CLASS__ . '::refinishComment'; // 前台提交评论完成接口
Edit::pluginHandle()->finishComment = __CLASS__ . '::refinishComment'; // 后台操作评论完成接口
Service::pluginHandle()->send = __CLASS__ . '::send';//异步接口
Edit::pluginHandle()->mark = __CLASS__ . '::mark'; // 后台标记评论状态完成接口
Helper::addPanel(1, self::$panel, '评论邮件提醒外观', '评论邮件提醒主题列表', 'administrator');
Helper::addRoute("zemail","/zemail","CommentNotifier_Action",'action');
return _t('请配置邮箱SMTP选项!');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
*/
public static function deactivate()
{
Helper::removePanel(1, self::$panel);
Helper::removeRoute("zemail");
}
/**
* 获取插件配置面板
*
* @access public
* @param Form $form 配置面板
* @return void
*/
public static function config(Form $form)
{
?>
<style>
.aliyun,.smtp,.api{display:none;}
</style>
<script>
window.onload = function () {
$('.'+$("#tuisongtype :radio:checked").val()).show();
$('#tuisongtype input').click(function(){
$('.smtp').hide();
$('.aliyun').hide();
$('.api').hide();
$('.'+$("#tuisongtype :radio:checked").val()).show();
});
}
</script>
<?php
// 记录log
$log = new Form\Element\Checkbox('log', array('ok' => _t('记录日志')), [], _t('记录日志'), _t('启用后将当前目录生成一个log.txt 注:目录需有写入权限'));
$form->addInput($log->multiMode());
$yibu = new Form\Element\Radio('yibu', array('0' => _t('不启用'), '1' => _t('启用'),), '0', _t('异步提交'), _t('异步回调优点就是减小对博客评论提交速度的影响'));
$form->addInput($yibu);
// 发信方式
$tuisongtype = new Form\Element\Radio('tuisongtype', array('smtp' => _t('SMTP'), 'aliyun' => _t('阿里云推送'), 'api' => _t('通过api')), 'smtp', _t('邮件推送方式'));
$form->addInput($tuisongtype);
$tuisongtype->setAttribute('id', 'tuisongtype');
$stmplayout = new Layout();
$stmplayout->html(_t('<h3>邮件SMTP服务配置:</h3>'));
$form->addItem($stmplayout);
// SMTP服务地址
$STMPHost = new Form\Element\Text('STMPHost', NULL, 'smtp.qq.com', _t('SMTP服务器地址'), _t('如:smtp.163.com,smtp.gmail.com,smtp.exmail.qq.com,smtp.sohu.com,smtp.sina.com'));
$form->addInput($STMPHost);
// SMTP用户名
$SMTPUserName = new Form\Element\Text('SMTPUserName', NULL, NULL, _t('SMTP登录用户'), _t('SMTP登录用户名,一般为邮箱地址'));
$form->addInput($SMTPUserName);
// 发件邮箱
$from = new Form\Element\Text('from', NULL, NULL, _t('SMTP邮箱地址'), _t('请填写用于发送邮件的邮箱,一般与SMTP登录用户名一致'));
$form->addInput($from);
// SMTP密码
$description = _t('一般为邮箱登录密码, 有特殊如: QQ邮箱有独立的SMTP密码. 可参考: ');
$description .= '<a href="https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001256&&id=28" target="_blank">QQ邮箱</a> ';
$description .= '<a href="https://mailhelp.aliyun.com/freemail/detail.vm?knoId=6521875" target="_blank">阿里邮箱</a> ';
$description .= '<a href="https://support.office.com/zh-cn/article/outlook-com-%E7%9A%84-pop%E3%80%81imap-%E5%92%8C-smtp-%E8%AE%BE%E7%BD%AE-d088b986-291d-42b8-9564-9c414e2aa040?ui=zh-CN&rs=zh-CN&ad=CN" target="_blank">Outlook邮箱</a> ';
$description .= '<a href="http://help.sina.com.cn/comquestiondetail/view/160/" target="_blank">新浪邮箱</a> ';
$SMTPPassword = new Form\Element\Text('SMTPPassword', NULL, NULL, _t('SMTP登录密码'), $description);
$form->addInput($SMTPPassword);
// 服务器安全模式
$SMTPSecure = new Form\Element\Radio('SMTPSecure', array('' => _t('无安全加密'), 'ssl' => _t('SSL加密'), 'tls' => _t('TLS加密')), '', _t('SMTP加密模式'));
$form->addInput($SMTPSecure);
// SMTP server port
$SMTPPort = new Form\Element\Text('SMTPPort', NULL, '25', _t('SMTP服务端口'), _t('默认25 SSL为465 TLS为587'));
$form->addInput($SMTPPort);
$stmplayout->setAttribute('class', 'typecho-option smtp');
$STMPHost->setAttribute('class', 'typecho-option smtp');
$SMTPUserName->setAttribute('class', 'typecho-option smtp');
$from->setAttribute('class', 'typecho-option smtp');
$SMTPPassword->setAttribute('class', 'typecho-option smtp');
$SMTPSecure->setAttribute('class', 'typecho-option smtp');
$SMTPPort->setAttribute('class', 'typecho-option smtp');
// 阿里云推送区块
$ali_section = new Layout();
// 区块标题
$ali_section->html('<h2>阿里云推送邮件发送设置</h2>');
$form->addItem($ali_section);
// 发件邮箱
$ali_from = new Form\Element\Text('ali_from', NULL, NULL, _t('阿里云邮箱地址'), _t('请填写用于发送邮件的邮箱'));
$form->addInput($ali_from);
// 地域选择
$ali_region = new Form\Element\Select('ali_region', array('hangzhou' => _t('华东1(杭州)'), 'singapore' => _t('亚太东南1(新加坡)'), 'sydney' => _t('亚太东南2(悉尼)')), NULL, _t('DM接入区域'), _t('请选择您的邮件推送所在服务器区域,请务必选择正确'));
$form->addInput($ali_region);
// AccessKey ID
$ali_accesskey_id = new Form\Element\Text('ali_accesskey_id', NULL, NULL, _t('AccessKey ID'), _t('请填入在阿里云生成的AccessKey ID'));
$form->addInput($ali_accesskey_id);
// Access Key Secret
$ali_accesskey_secret = new Form\Element\Text('ali_accesskey_secret', NULL, NULL, _t('Access Key Secret'), _t('请填入在阿里云生成的Access Key Secret'));
$form->addInput($ali_accesskey_secret);
$ali_section->setAttribute('class', 'typecho-option aliyun');
$ali_region->setAttribute('class', 'typecho-option aliyun');
$ali_from->setAttribute('class', 'typecho-option aliyun');
$ali_accesskey_id->setAttribute('class', 'typecho-option aliyun');
$ali_accesskey_secret->setAttribute('class', 'typecho-option aliyun');
// api推送区块
$api_section = new Layout();
// 区块标题
$api_section->html('<h2>API发送设置</h2>');
$form->addItem($ali_section);
// 发件api
$api_url = new Form\Element\Text('api_url', NULL, NULL, _t('api地址'), _t('请填写用于发送的api链接,需要服务器支持curl函数,部分虚拟主机可能并不能用curl,自己api将插件目录下的zemailapi文件夹放到用于构建api的服务器,然后配置config.php文件即可'));
$form->addInput($api_url);
$api_url->setAttribute('class', 'typecho-option api');
$layout = new Layout();
$layout->html(_t('<h3>其他配置:</h3>'));
$form->addItem($layout);
// 发件人姓名
$fromName = new Form\Element\Text('fromName', NULL, NULL, _t('发件人昵称'), _t('发件人昵称'));
$form->addInput($fromName->addRule('required', _t('发件人昵称必填!')));
// 收件邮箱
$adminfrom = new Form\Element\Text('adminfrom', NULL, NULL, _t('站长收件邮箱'), _t('遇到待审核评论或文章作者邮箱为空时,评论提醒会发送到此邮箱地址!'));
$form->addInput($adminfrom->addRule('required', _t('收件邮箱必填!')));
$zznotice = new Form\Element\Radio('zznotice', array('0' => _t('通知'), '1' => _t('不通知'),), '0', _t('是否通知站长'), _t('因为站长可能有其他接受评论通知的方式,不想在重复接受邮件通知可选择不通知'));
$form->addInput($zznotice);
$rewrite='';if(Helper::options()->rewrite==0){$rewrite='index.php/';}
$apiurl=Helper::options()->siteUrl.$rewrite.'zemail';
$testurl='#';
if(empty($_GET['activate'])){
$plugin = Helper::options()->plugin('CommentNotifier');
$testurl=$apiurl.'?subject=标题&html=测试内容&to='.$plugin->adminfrom.'&fromName='.$plugin->fromName.'&auth='.$plugin->auth;
}
// smtp发信方案
$smtptype = new Form\Element\Radio('smtptype', array('curl' => _t('默认Curl方案'), 'go' => _t('传统直发方案')), 'curl', _t('发信方案'), _t('传统方案兼容性好,适合用虚拟主机的用户'));
$form->addInput($smtptype);
$smtptype->setAttribute('class', 'typecho-option smtp');
// 表情重载函数
$biaoqing = new Form\Element\Text('biaoqing', NULL, NULL, _t('表情重载'), _t('请填写您博客主题评论表情函数名,如:parseBiaoQing(我的Plain,Sinner,Dinner,Store主题),Mirages::parseBiaoqing(Mirages主题),(此项非必填项具体函数名请咨询主题作者,填写后邮件提醒将支持显示表情,更换主题后请同步更换此项内容或者删除此项内容)<p class="smtp">设置好插件所有设置参数并保存设置后,可以点击下方链接进行测试邮件是否发信正常<br><a href="'.$testurl.'" target="_blank" rel="noopener noreferrer">点击测试邮件发信是否正常【仅适用于SMTP模式】</a></p>'));
$form->addInput($biaoqing);
// 模板
$template = new Form\Element\Text('template', NULL, 'default', _t('邮件模板选择'), _t('该项请不要在插件设置里填写,请到邮件模板列表页面选择模板启动!'));
$template->setAttribute('class', 'hidden');
$form->addInput($template);
$t = new Form\Element\Text(
'auth',
null,
\Typecho\Common::randString(32),
_t('* 接口保护'),
_t('加盐保护 API 接口不被滥用,自动生成禁止自行设置。')
);
$t->setAttribute('class', 'hidden');
$form->addInput($t);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Form $form
* @return void
*/
public static function personalConfig(Form $form)
{
}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function render()
{
}
/**
* @param $comment
* @return array
* @throws Typecho_Db_Exception
* 获取上级评论人
*/
public static function getParent($comment): array
{
$recipients = [];
$parent = Helper::widgetById('comments', $comment->parent);
$recipients = [
'name' => $parent->author,
'mail' => $parent->mail,
];
return $recipients;
}
/**
* @param $comment
* @return array
* @throws Typecho_Db_Exception
* 获取文章作者邮箱
*/
public static function getAuthor($comment): array
{
$plugin = Options::alloc()->plugin('CommentNotifier');
$recipients = [];
$db = Db::get();
$ae = $db->fetchRow($db->select()->from('table.users')->where('table.users.uid=?', $comment->ownerId));
if (empty($ae['mail'])) {
$ae['mail'] = $plugin->adminfrom;
}
$recipients = [
'name' => $ae['screenName'],
'mail' => $ae['mail'],
];
// 查询
return $recipients;
}
/**
* @param $comment
* @param Widget_Comments_Edit $edit
* @param $status
* @throws Typecho_Db_Exception
* @throws Typecho_Plugin_Exception
* 在后台标记评论状态时的回调
*/
public static function mark($comment, $edit, $status)
{
$recipients = [];
$plugin = Options::alloc()->plugin('CommentNotifier');
$from = $plugin->adminfrom; // 站长邮箱
// 在后台标记评论状态为[approved 审核通过]时, 发信给上级评论人或作者
if ($status == 'approved') {
$type = 0;
// 如果有上级
if ($edit->parent > 0) {
$recipients[] = self::getParent($edit);//获取上级评论信息
$type = 1;
} else {
$recipients[] = self::getAuthor($edit);//获取作者信息
}
// 如果自己回复自己的评论, 不做任何操作
if ($recipients[0]['mail'] == $edit->mail) {
return;
}
// 如果上级是博主, 不做任何操作
if ($recipients[0]['mail'] == $from) {
return;
}
//邮箱为空时就不发邮件
if (empty($recipients[0]['mail'])) {
return;
}
self::sendMail($edit, $recipients, $type);
}
}
/**
* @param Widget_Comments_Edit|Widget_Feedback $comment
* @throws Typecho_Db_Exception
* @throws Typecho_Plugin_Exception
* 评论/回复时的回调
*/
public static function refinishComment($comment)
{
$plugin = Options::alloc()->plugin('CommentNotifier');
$from = $plugin->adminfrom; // 站长邮箱
$fromName = $plugin->fromName; // 发件人
$recipients = [];
// 审核通过
if ($comment->status == 'approved') {
$type = 0;//0为无父级评论
$author=self::getAuthor($comment);//获取文章作者信息
// 不需要发信给博主
if ($comment->authorId != $comment->ownerId && $comment->mail != $author['mail']) {
$recipients[] = $author;
}
// 如果有上级
if ($comment->parent) {
$type = 1;//1为有父级评论
// 查询上级评论人
$parent = self::getParent($comment);//获取上级评论者邮箱
// 如果上级是博主和自己回复自己, 不需要发信
if ($parent['mail'] != $from && $parent['mail'] != $comment->mail) {
$recipients[] = $parent;
}
}
self::sendMail($comment, $recipients, $type);
} else {
// 如果所有评论必须经过审核, 通知博主审核评论
$recipients[] = ['name' => $fromName, 'mail' => $from];
self::sendMail($comment, $recipients, 2);//2为待审核评论
}
}
/**
* @param Widget_Comments_Edit|Widget_Feedback $comment
* @param array $recipients
* @param $type
*/
private static function sendMail($comment, array $recipients, $type)
{
if (empty($recipients)) return; // 没有收信人
// 获取系统配置选项
$options = Options::alloc();
$plugin = $options->plugin('CommentNotifier');
if ($type == 1) {
$Subject = '你在[' . $comment->title . ']的评论有了新的回复';
} elseif ($type == 2) {
$Subject = '文章《' . $comment->title . '》有条待审评论';
} else {
$Subject = '你的《' . $comment->title . '》文章有了新的评论';
}
foreach ($recipients as $recipient) {
$param['to']=$recipient['mail']; // 收件地址
$param['fromName']=$recipient['name']; // 收件人名称
$param['subject']=$Subject; // 邮件标题
$param['html']=self::mailBody($comment, $options, $type); // 邮件内容
self::resendMail($param);
}
}
public static function resendMail($param)
{
// 获取系统配置选项
$options = Options::alloc();
$plugin = $options->plugin('CommentNotifier');
if($plugin->zznotice==1&&$param['to']==$plugin->adminfrom){return;}//不通知站长邮箱
if($plugin->yibu==1){
Helper::requestService('send', $param);
}else{
self::send($param);
}
}
public static function send($param){
// 获取系统配置选项
$options = Options::alloc();
$plugin = $options->plugin('CommentNotifier');
// 消除下方注释后这里会睡眠10秒,可测试异步提交是否真正提速了
//sleep(10);
if($plugin->tuisongtype=='aliyun'){
self::aliyun($param);
}else{
self::zemail($param);
}
}
public static function zemail($param)
{ // 获取系统配置选项
$options = Options::alloc();
// 获取插件配置
$plugin = $options->plugin('CommentNotifier');
if($plugin->tuisongtype=='api'&&$plugin->api_url){
$apiurl=$plugin->api_url;
$plugin->smtptype='curl';
}else{
//api地址
$rewrite='';if(Helper::options()->rewrite==0){$rewrite='index.php/';}
$apiurl=Helper::options()->siteUrl.$rewrite.'zemail';
$param['auth']=$plugin->auth;//密钥
}
if($plugin->smtptype=="go"){
try {
$from = $plugin->from; // 发件邮箱
$fromName = $plugin->fromName; // 发件人
// Server settings
$mail = new PHPMailer(false);
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->Encoding = PHPMailer::ENCODING_BASE64;
$mail->isSMTP();
$mail->Host = $plugin->STMPHost; // SMTP 服务地址
$mail->SMTPAuth = true; // 开启认证
$mail->Username = $plugin->SMTPUserName; // SMTP 用户名
$mail->Password = $plugin->SMTPPassword; // SMTP 密码
$mail->SMTPSecure = $plugin->SMTPSecure; // SMTP 加密类型 'ssl' or 'tls'.
$mail->Port = $plugin->SMTPPort; // SMTP 端口
$mail->setFrom($from, $fromName);
$mail->addAddress($param['to'], $paramT['fromName']); // 收件人
$mail->Subject =$param['subject'];
$mail->isHTML(); // 邮件为HTML格式
// 邮件内容
$mail->Body = $param['html'];
$mail->send();
// 记录日志
if ($plugin->log) {
$at = date('Y-m-d H:i:s');
if ($mail->isError()) {
$data = $at . ' ' . $mail->ErrorInfo; // 记录发信失败的日志
} else { // 记录发信成功的日志
$data = PHP_EOL . $at . ' 发送成功! ';
$data .= ' 发件人:' . $fromName;
$data .= ' 发件邮箱:' . $from;
$data .= ' 接收人:' . $paramT['fromName'];
$data .= ' 接收邮箱:' . $param['to'] . PHP_EOL;
}
$fileName = dirname(__FILE__) . '/log.txt';
file_put_contents($fileName, $data, FILE_APPEND);
}
} catch (Exception $e) {
if ($plugin->log) {
$fileName = dirname(__FILE__) . '/log.txt';
$str = "\nerror time: " . date('Y-m-d H:i:s') . "\n";
file_put_contents($fileName, $str, FILE_APPEND);
file_put_contents($fileName, $e, FILE_APPEND);
}
}
}else{
// 初始化Curl
$ch = curl_init();
// 设置为POST请求
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// 请求地址
curl_setopt($ch, CURLOPT_URL, $apiurl);
// 开启非阻塞模式
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
// 返回数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// 提交参数
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
// 关闭ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// 执行请求
$result = curl_exec($ch);
// 获取错误代码
$errno = curl_errno($ch);
// 获取错误信息
$error = curl_error($ch);
// 获取返回状态码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭请求
curl_close($ch);
// 成功标识
$flag = TRUE;
// 如果开启了Debug
if ($plugin->log) {
// 记录时间
$log = '[Zemail] ' . date('Y-m-d H:i:s') . ': ' . PHP_EOL;
// 如果失败
if ( $errno ) {
// 设置失败
$flag = FALSE;
$log .= _t('邮件发送失败, 错误代码:' . $errno . ',错误提示: ' . $error . PHP_EOL);
}
// 如果失败
if ( 400 <= $httpCode ) {
// 设置失败
$flag = FALSE;
// 尝试转换json
if ( $json = json_decode($result) ) {
$log .= _t('邮件发送失败,错误代码:' . $json->Code . ',错误提示:' . $json->Message . PHP_EOL);
} else {
$log .= _t('邮件发送失败, 请求返回HTTP Code:' . $httpCode . PHP_EOL);
}
}
// 记录返回值
$log .= _t('邮件发送返回数据:' . serialize($result) . PHP_EOL);
// 输出分隔
$log .= '-------------------------------------------' . PHP_EOL;
// 写入文件
file_put_contents(dirname(__FILE__) . '/log.txt', "\n".$log."\n", FILE_APPEND);
}
}
// 返回结果
return $flag;
}
/**
* 阿里云邮件发送
*
* @static
* @access public
*
* @param array $param 公共参数
*
* @return bool|string
* @throws Typecho_Plugin_Exception
*/
public static function aliyun($param)
{ // 获取系统配置选项
$options = Options::alloc();
// 获取插件配置
$plugin = $options->plugin('CommentNotifier');
// 判断当前请求区域
switch ( $plugin->ali_region ) {
case 'hangzhou': // 杭州
// API地址
$param['api'] = 'https://dm.aliyuncs.com/';
// API版本号
$param['version'] = '2015-11-23';
// 机房信息
$param['region'] = 'cn-hangzhou';
break;
case 'singapore': // 新加坡
// API地址
$param['api'] = 'https://dm.ap-southeast-1.aliyuncs.com/';
// API版本号
$param['version'] = '2017-06-22';
// 机房信息
$param['region'] = 'ap-southeast-1';
break;
case 'sydney': // 悉尼
// API地址
$param['api'] = 'https://dm.ap-southeast-2.aliyuncs.com/';
// API版本号
$param['version'] = '2017-06-22';
// 机房信息
$param['region'] = 'ap-southeast-2';
break;
}
// 重新组合为阿里云所使用的参数
$data = array(
'Action' => 'SingleSendMail', // 操作接口名
'AccountName' => $plugin->ali_from, // 发件地址
'ReplyToAddress' => "true", // 回信地址
'AddressType' => 1, // 地址类型
'ToAddress' => $param['to'], // 收件地址
'FromAlias' => $param['fromName'], // 发件人名称
'Subject' => $param['subject'], // 邮件标题
'HtmlBody' => $param['html'], // 邮件内容
'Format' => 'JSON', // 返回JSON
'Version' => $param['version'], // API版本号
'AccessKeyId' => $plugin->ali_accesskey_id, // Access Key ID
'SignatureMethod' => 'HMAC-SHA1', // 签名方式
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'), // 请求时间
'SignatureVersion' => '1.0', // 签名算法版本
'SignatureNonce' => md5(time()), // 唯一随机数
'RegionId' => $param['region'] // 机房信息
);
// 请求签名
$data['Signature'] = self::sign($data, $plugin->ali_accesskey_secret);
// 初始化Curl
$ch = curl_init();
// 设置为POST请求
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// 请求地址
curl_setopt($ch, CURLOPT_URL, $param['api']);
// 开启非阻塞模式
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
// 返回数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// 提交参数
curl_setopt($ch, CURLOPT_POSTFIELDS, self::getPostHttpBody($data));
// 关闭ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// 执行请求
$result = curl_exec($ch);
// 获取错误代码
$errno = curl_errno($ch);
// 获取错误信息
$error = curl_error($ch);
// 获取返回状态码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭请求
curl_close($ch);
// 成功标识
$flag = TRUE;
// 如果开启了Debug
if ($plugin->log) {
// 记录时间
$log = '[Aliyun] ' . date('Y-m-d H:i:s') . ': ' . PHP_EOL;
// 如果失败
if ( $errno ) {
// 设置失败
$flag = FALSE;
$log .= _t('邮件发送失败, 错误代码:' . $errno . ',错误提示: ' . $error . PHP_EOL);
}
// 如果失败
if ( 400 <= $httpCode ) {
// 设置失败
$flag = FALSE;
// 尝试转换json
if ( $json = json_decode($result) ) {
$log .= _t('邮件发送失败,错误代码:' . $json->Code . ',错误提示:' . $json->Message . PHP_EOL);
} else {
$log .= _t('邮件发送失败, 请求返回HTTP Code:' . $httpCode . PHP_EOL);
}
}
// 记录返回值
$log .= _t('邮件发送返回数据:' . serialize($result) . PHP_EOL);
// 输出分隔
$log .= '-------------------------------------------' . PHP_EOL;
// 写入文件
file_put_contents(dirname(__FILE__) . '/log.txt', "\n".$log."\n", FILE_APPEND);
}
// 返回结果
return $flag;
}
/**
* @param $comment
* @param $options
* @param $type
* @return string
* 很朴素的邮件风格
*/
private static function mailBody($comment, $options, $type): string
{
$plugin = Options::alloc()->plugin('CommentNotifier');
$commentAt = new Date($comment->created);
$commentAt = $commentAt->format('Y-m-d H:i:s');
$commentText = $comment->content;
$html = 'owner';
if ($type == 1) {
$html = 'guest';
} elseif ($type == 2) {
$html = 'notice';
}
$Pmail = '';
$Pname = '';
$Ptext = '';
$Pmd5 = '';
if ($comment->parent) {
$parent = Helper::widgetById('comments', $comment->parent);
$Pname = $parent->author;
$Ptext = $parent->content;
$Pmail = $parent->mail;
$Pmd5 = md5($parent->mail);
}
$post=Helper::widgetById('Contents', $comment->cid);
if($plugin->biaoqing&&is_callable($plugin->biaoqing)){//表情函数重载
$parseBiaoQing = $plugin->biaoqing;
$commentText = $parseBiaoQing($commentText);
$Ptext = $parseBiaoQing($Ptext);
}
$style='style="display: inline-block;vertical-align: bottom;margin: 0;" width="30"';//限制表情尺寸
$commentText=str_replace('class="biaoqing',$style.' class="biaoqing',$commentText);
$Ptext=str_replace('class="biaoqing',$style.' class="biaoqing',$Ptext);
$content = self::getTemplate($html);
$content=preg_replace('#<\?php#', '<!--', $content);
$content=preg_replace('#\?>#', '-->', $content);
$template = Options::alloc()->plugin('CommentNotifier')->template;
$status = array(
"approved" => '通过',
"waiting" => '待审',
"spam" => '垃圾',
);
$search = array(
'{title}',//文章标题
'{PostAuthor}',//文章作者昵称
'{time}',//评论发出时间
'{commentText}',//评论内容
'{author}',//评论人昵称
'{mail}',//评论者邮箱
'{md5}',//评论者邮箱
'{ip}',//评论者ip
'{permalink}',//评论楼层链接
'{siteUrl}',//网站地址
'{siteTitle}',//网站标题
'{Pname}',//父级评论昵称
'{Ptext}',//父级评论内容
'{Pmail}',//父级评论邮箱
'{Pmd5}',//父级评论邮箱md5
'{url}',//当前模板文件夹路径
'{manageurl}',//后台管理评论的入口链接
'{status}', //评论状态
);
$replace = array(
$comment->title,
$post->author->screenName,
$commentAt,
$commentText,
$comment->author,
$comment->mail,
md5($comment->mail),
$comment->ip,
$comment->permalink,
$options->siteUrl,
$options->title,
$Pname,
$Ptext,
$Pmail,
$Pmd5,
Options::alloc()->pluginUrl . '/CommentNotifier/template/' . $template,
Options::alloc()->adminUrl . '/manage-comments.php',
$status[$comment->status]
);
return str_replace($search, $replace, $content);
}
/**
* 获取评论模板
*
* @param template owner 为博主 guest 为访客
* @return false|string
*/
private static function getTemplate($template = 'owner')
{
$template .= '.html';
$templateDir = self::configStr('template', 'default');
$filePath = dirname(__FILE__) . '/template/' . $templateDir . '/' . $template;
if (!file_exists($filePath)) {//如果模板文件缺失就调用根目录下的default文件夹中用于垫底的模板
$filePath = dirname(__FILE__) . 'template/default/' . $template;
}
return file_get_contents($filePath);
}
/**
* 从 Widget_Options 对象获取 Typecho 选项值(文本型)
* @param string $key 选项 Key
* @param mixed $default 默认值
* @param string $method 测空值方法
* @return string
*/
public static function configStr(string $key, $default = '', string $method = 'empty'): string
{
$value = Helper::options()->plugin('CommentNotifier')->$key;
if ($method === 'empty') {
return empty($value) ? $default : $value;
} else {
return call_user_func($method, $value) ? $default : $value;
}
}
/**
* 阿里云签名
*
* @static
* @access private
*
* @param array $param 签名参数
* @param string $accesssecret 秘钥
*
* @return string
*/
private static function sign($param, $accesssecret)
{
// 参数排序
ksort($param);
// 组合基础
$stringToSign = 'POST&' . self::percentEncode('/') . '&';
// 临时变量
$tmp = '';
// 循环参数列表
foreach ( $param as $k => $v ) {
// 组合参数
$tmp .= '&' . self::percentEncode($k) . '=' . self::percentEncode($v);
}
// 去除最后一个&
$tmp = trim($tmp, '&');
// 组合签名参数
$stringToSign = $stringToSign . self::percentEncode($tmp);
// 数据签名
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accesssecret . '&', TRUE));
// 返回签名
return $signature;
}
/**
* 阿里云签名编码转换
*
* @static
* @access private
*
* @param string $val 要转换的编码
*
* @return string|string[]|null
*/
private static function percentEncode($val)
{
// URL编码
$res = urlencode($val);
// 加号转换为%20
$res = preg_replace('/\+/', '%20', $res);
// 星号转换为%2A
$res = preg_replace('/\*/', '%2A', $res);
// %7E转换为~
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
/**
* 阿里云请求参数组合
*
* @static
* @access private
*
* @param array $param 发送参数
*
* @return bool|string
*/
private static function getPostHttpBody($param)
{
// 空字符串
$str = "";
// 循环参数
foreach ( $param as $k => $v ) {
// 组合参数
$str .= $k . '=' . urlencode($v) . '&';
}
// 去除第一个&
return substr($str, 0, -1);
}
}