forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
377 lines (306 loc) · 15.5 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
<?php
/**
* 评论通知推送多服务
*
* @package CommentPush
* @author 高彬展,奥秘Sir
* @version 1.7.0
* @link https://github.com/gaobinzhan/CommentPush
* @blog https://blog.gaobinzhan.com
*/
require 'lib/QQService.php';
require 'lib/WeChatService.php';
require 'lib/AliYunEmailService.php';
require 'lib/SmtpService.php';
require 'lib/DingTalkBotService.php';
require 'lib/EnterpriseWeChatService.php';
require 'lib/OfficialAccountService.php';
class CommentPush_Plugin implements Typecho_Plugin_Interface
{
protected static $comment;
protected static $active;
/**
* @return string|void
* @throws Typecho_Db_Exception
*/
public static function activate()
{
self::addTable();
Typecho_Plugin::factory('Widget_Feedback')->comment = [__CLASS__, 'pushServiceReady'];
Typecho_Plugin::factory('Widget_Feedback')->finishComment = [__CLASS__, 'pushServiceGo'];
Helper::addRoute('CommentPushAction','/CommentPush/officialAccount','CommentPush_Action','officialAccount');
Helper::addPanel(1, 'CommentPush/Logs.php', 'CommentPush日志', 'CommentPush日志', 'administrator');
return _t('CommentPush插件启用成功');
}
/**
* @throws Typecho_Db_Exception
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeRoute('CommentPushAction');
Helper::removePanel(1, 'CommentPush/Logs.php');
if (Helper::options()->plugin('CommentPush')->isDelete == 1) {
self::removeTable();
}
}
/**
* @throws Typecho_Db_Exception
*/
private static function addTable()
{
$db = Typecho_Db::get();
$sql = self::getSql($db, 'install');
$db->query($sql);
}
/**
* @param $db
* @param string $path
* @return string|string[]
*/
private static function getSql($db, $path = 'install')
{
$adapter = $db->getAdapterName();
$prefix = $db->getPrefix();
if ($adapter === 'Pdo_Mysql' || $adapter === 'Mysql' || $adapter === 'Mysqli') {
$sqlTemplate = file_get_contents(__DIR__ . '/sql/' . $path . '/Mysql.sql');
}
if ($adapter === 'Pdo_SQLite') {
$sqlTemplate = file_get_contents(__DIR__ . '/sql/' . $path . '/SQLite.sql');
}
if ($adapter === 'Pdo_Pgsql') {
$sqlTemplate = file_get_contents(__DIR__ . '/sql/' . $path . '/Pgsql.sql');
}
if (empty($sqlTemplate)) throw new \Exception('暂不支持你的数据库');
$sql = str_replace('{prefix}', $prefix, $sqlTemplate);
return $sql;
}
/**
* @return string
* @throws Typecho_Db_Exception
*/
private static function removeTable()
{
$db = Typecho_Db::get();
$sql = self::getSql($db, 'uninstall');
try {
$db->query($sql, Typecho_Db::WRITE);
} catch (Typecho_Exception $e) {
return "删除CommentPush日志表失败!";
}
return "删除CommentPush日志表成功!";
}
/**
* @param Typecho_Widget_Helper_Form $form
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$serviceTitle = new Typecho_Widget_Helper_Layout('div', array('class=' => 'typecho-page-title'));
$serviceTitle->html('<h2>推送服务配置</h2>');
$form->addItem($serviceTitle);
$services = new Typecho_Widget_Helper_Form_Element_Checkbox('services', [
"QQService" => _t('Qmsg酱'),
"WeChatService" => _t('Server酱'),
"AliYunEmailService" => _t('阿里云邮件'),
"SmtpService" => _t('SMTP'),
"DingTalkBotService" => _t('钉钉机器人'),
"EnterpriseWeChatService" => _t('企业微信机器人'),
"OfficialAccountService" => _t('微信公众号')
], 'services', _t('推送服务 多选同时推送'), _t('插件作者:<a href="https://blog.gaobinzhan.com">高彬展</a> <a href="https://blog.say521.cn/">奥秘Sir</a>'));
$form->addInput($services->addRule('required', _t('必须选择一项推送服务')));
$isPushBlogger = new Typecho_Widget_Helper_Form_Element_Radio('isPushBlogger', [
1 => '是',
0 => '否'
], 1, _t('当评论者为博主本人不推送'), _t('如果选择“是”,博主本人写的评论将不推送'));
$form->addInput($isPushBlogger);
$isPushCommentReply = new Typecho_Widget_Helper_Form_Element_Radio('isPushCommentReply', [
1 => '是',
0 => '否'
], 1, _t('当作者回复评论向对方发送邮件'), _t('如果选择“否”,将不推送'));
$form->addInput($isPushCommentReply);
$isDelete = new Typecho_Widget_Helper_Form_Element_Radio('isDelete', [0 => '不删除', 1 => '删除'], 1, _t('卸载是否删除数据表'));
$form->addInput($isDelete);
self::qqService($form);
self::weChatService($form);
self::aliYunMailService($form);
self::smtpService($form);
self::DingTalkBotService($form);
self::EnterpriseWeChatService($form);
self::officialAccount($form);
}
/**
* Qmsg酱配置面板
* @param Typecho_Widget_Helper_Form $form
*/
private static function qqService(Typecho_Widget_Helper_Form $form)
{
$qqServiceTitle = new Typecho_Widget_Helper_Layout('div', ['class=' => 'typecho-page-title']);
$qqServiceTitle->html('<h2>Qmsg酱配置</h2>');
$form->addItem($qqServiceTitle);
$qqApiUrl = new Typecho_Widget_Helper_Form_Element_Text('qqApiUrl', NULL, NULL, _t('Qmsg酱接口'), _t("当选择Qmsg酱必须填写"));
$form->addInput($qqApiUrl);
$receiveQq = new Typecho_Widget_Helper_Form_Element_Text('receiveQq', NULL, NULL, _t('接收消息的QQ,可以添加多个,以英文逗号分割'), _t("当选择Qmsg酱必须填写(指定的QQ必须在您的QQ号列表中)"));
$form->addInput($receiveQq);
}
/**
* Server酱配置面板
* @param Typecho_Widget_Helper_Form $form
*/
private static function weChatService(Typecho_Widget_Helper_Form $form)
{
$weChatServiceTitle = new Typecho_Widget_Helper_Layout('div', ['class=' => 'typecho-page-title']);
$weChatServiceTitle->html('<h2>Server酱配置</h2>');
$form->addItem($weChatServiceTitle);
$weChatScKey = new Typecho_Widget_Helper_Form_Element_Text('weChatScKey', NULL, NULL, _t('Server酱 SCKEY'), _t("当选择Server酱必须填写"));
$form->addInput($weChatScKey);
}
/**
* 阿里云邮件配置面板
* @param Typecho_Widget_Helper_Form $form
*/
private static function aliYunMailService(Typecho_Widget_Helper_Form $form)
{
$aliYunEmailServiceTitle = new Typecho_Widget_Helper_Layout('div', ['class=' => 'typecho-page-title']);
$aliYunEmailServiceTitle->html('<h2>阿里云邮件配置</h2>');
$form->addItem($aliYunEmailServiceTitle);
$aliYunRegion = new Typecho_Widget_Helper_Form_Element_Select('regionId', [
AliYunEmailService::HANGZHOU => _t('华东1(杭州)'),
AliYunEmailService::SINGAPORE => _t('亚太东南1(新加坡)'),
AliYunEmailService::SYDNEY => _t('亚太东南2(悉尼)')
], NULL, _t('服务地址'), _t('选择邮件推送所在服务器区域'));
$form->addInput($aliYunRegion);
$aliYunAccessKeyId = new Typecho_Widget_Helper_Form_Element_Text('accessKeyId', NULL, NULL, _t('AccessKey ID'), _t('请填入在阿里云生成的AccessKey ID'));
$form->addInput($aliYunAccessKeyId);
$aliYunAccessKeySecret = new Typecho_Widget_Helper_Form_Element_Text('accessKeySecret', NULL, NULL, _t('Access Key Secret'), _t('请填入在阿里云生成的Access Key Secret'));
$form->addInput($aliYunAccessKeySecret);
$aliYunFromAlias = new Typecho_Widget_Helper_Form_Element_Text('fromAlias', NULL, NULL, _t('发件人名称'), _t('邮件中显示的发信人名称,留空为博客名称'));
$form->addInput($aliYunFromAlias);
$aliYunAccountName = new Typecho_Widget_Helper_Form_Element_Text('accountName', NULL, NULL, _t('发件邮箱地址'), _t('邮件中显示的发信地址'));
$form->addInput($aliYunAccountName->addRule('email', _t('请输入正确的邮箱地址')));
}
/**
* SMTP配置面板
* @param Typecho_Widget_Helper_Form $form
*/
private static function smtpService(Typecho_Widget_Helper_Form $form)
{
$smtpServiceTitle = new Typecho_Widget_Helper_Layout('div', ['class=' => 'typecho-page-title']);
$smtpServiceTitle->html('<h2>SMTP配置</h2>');
$form->addItem($smtpServiceTitle);
$smtpHost = new Typecho_Widget_Helper_Form_Element_Text('smtpHost', NULL, NULL, _t('SMTP地址'), _t('SMTP服务器连接地址'));
$form->addInput($smtpHost);
$smtpPort = new Typecho_Widget_Helper_Form_Element_Text('smtpPort', NULL, NULL, _t('SMTP端口'), _t('SMTP服务器连接端口'));
$form->addInput($smtpPort);
$smtpFromAlias = new Typecho_Widget_Helper_Form_Element_Text('smtpFromAlias', NULL, NULL, _t('发件人名称'), _t('邮件中显示的发信人名称,留空为博客名称'));
$form->addInput($smtpFromAlias);
$smtpUser = new Typecho_Widget_Helper_Form_Element_Text('smtpUser', NULL, NULL, _t('SMTP登录用户'), _t('SMTP登录用户名,一般为邮箱地址'));
$form->addInput($smtpUser);
$smtpPass = new Typecho_Widget_Helper_Form_Element_Text('smtpPass', NULL, NULL, _t('SMTP登录密码'), _t('一般为邮箱密码,但某些服务商需要生成特定密码'));
$form->addInput($smtpPass);
$smtpAuth = new Typecho_Widget_Helper_Form_Element_Checkbox('smtpAuth', ['enable' => _t('服务器需要验证')], ['enable'], _t('SMTP验证模式'));
$form->addInput($smtpAuth);
$smtpSecure = new Typecho_Widget_Helper_Form_Element_Radio('smtpSecure', ['false' => _t('无安全加密'), 'ssl' => _t('SSL加密'), 'tls' => _t('TLS加密')], 'false', _t('SMTP加密模式'));
$form->addInput($smtpSecure);
$template = new Typecho_Widget_Helper_Layout('div', ['class=' => 'typecho-page-title']);
$template->html('<h2>邮件自定义模版</h2>');
$form->addItem($template);
$authorTemplate = new Typecho_Widget_Helper_Form_Element_Textarea('authorTemplate', NULL, NULL, _t('向博主发信内容模板(为空即默认模版)'),
_t("可选参数:
'{blogUrl}',
'{blogName}',
'{author}',
'{permalink}',
'{title}',
'{text}',
'{ip}'
<br>" . '写法:' . htmlspecialchars('<h1>{title}</h1>')));
$form->addInput($authorTemplate);
$replyTemplate = new Typecho_Widget_Helper_Form_Element_Textarea('replyTemplate', NULL, NULL, _t('向访客发信内容模板(为空即默认模版)'),
_t("可选参数:
'{blogUrl}',
'{blogName}',
'{author}',
'{permalink}',
'{title}',
'{text}',
'{replyAuthor}',
'{replyText}',
'{commentUrl}'
<br>" . '写法:' . htmlspecialchars('<h1>{title}</h1>')));
$form->addInput($replyTemplate);
}
/**
* 钉钉机器人配置面板
* @param Typecho_Widget_Helper_Form $form
*/
private static function DingTalkBotService(Typecho_Widget_Helper_Form $form)
{
$DingTalkBotServiceTitle = new Typecho_Widget_Helper_Layout('div', ['class=' => 'typecho-page-title']);
$DingTalkBotServiceTitle->html('<h2>钉钉机器人配置</h2>');
$form->addItem($DingTalkBotServiceTitle);
$DingTalkWebhook = new Typecho_Widget_Helper_Form_Element_Text('DingTalkWebhook', NULL, NULL, _t('钉钉 Webhook 地址'), _t("当选择钉钉机器人必须填写"));
$form->addInput($DingTalkWebhook);
$DingTalkSecret = new Typecho_Widget_Helper_Form_Element_Text('DingTalkSecret', NULL, NULL, _t('安全设置加签密钥'), _t("当选择钉钉机器人必须填写(安全设置:加签)"));
$form->addInput($DingTalkSecret);
}
/**
* 企业微信机器人配置面板
* @param Typecho_Widget_Helper_Form $form
*/
private static function EnterpriseWeChatService(Typecho_Widget_Helper_Form $form)
{
$EnterpriseWeChatServiceTitle = new Typecho_Widget_Helper_Layout('div', ['class=' => 'typecho-page-title']);
$EnterpriseWeChatServiceTitle->html('<h2>企业微信机器人配置</h2>');
$form->addItem($EnterpriseWeChatServiceTitle);
$EnterpriseWeChatWebhook = new Typecho_Widget_Helper_Form_Element_Text('EnterpriseWeChatWebhook', NULL, NULL, _t('企业微信 Webhook 地址'), _t("当选择企业微信机器人必须填写"));
$form->addInput($EnterpriseWeChatWebhook);
}
private static function officialAccount(Typecho_Widget_Helper_Form $form){
$officialAccountTitle = new Typecho_Widget_Helper_Layout('div',['class' => 'typecho-page-title']);
$officialAccountTitle->html('<h2>微信公众号</h2>');
$form->addItem($officialAccountTitle);
$token = new Typecho_Widget_Helper_Form_Element_Text('officialAccountToken', null, null, _t('接口配置信息 Token'), '接口配置信息 Token,Url为:博客地址/CommentPush/officialAccount');
$form->addInput($token);
$appId = new Typecho_Widget_Helper_Form_Element_Text('officialAccountAppId', null, null, _t('appId'), '微信公众号 appID');
$form->addInput($appId);
$appSecret = new Typecho_Widget_Helper_Form_Element_Text('officialAccountAppSecret', null, null, _t('appSecret'), '微信公众号 appSecret');
$form->addInput($appSecret);
$openId = new Typecho_Widget_Helper_Form_Element_Text('officialAccountOpenid', null, null, _t('openid'), '接收信息的微信号 openid');
$form->addInput($openId);
$templateId = new Typecho_Widget_Helper_Form_Element_Text('officialAccountTemplateId', null, null, _t('templateId'), "消息模版 templateId 可选参数:'{{title.DATA}}','{{user.DATA}}','{{ip.DATA}}','{{content.DATA}}'<br>示例(将例子复制粘贴到微信公众号模版内容即可):
标题:{{title.DATA}}
评论人:{{user.DATA}}
IP:{{ip.DATA}}
评论内容:{{content.DATA}}");
$form->addInput($templateId);
}
/**
* @param Typecho_Widget_Helper_Form $form
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
// TODO: Implement personalConfig() method.
}
public static function pushServiceReady($comment, $active)
{
self::$comment = $comment;
self::$active = $active;
return $comment;
}
/**
* @modify 小码农 <[email protected]> 增加实例化方法
*/
public static function pushServiceGo($comment)
{
$options = Helper::options();
$plugin = $options->plugin('CommentPush');
$services = $plugin->services;
if (!$services || $services == 'services') return false;
self::$comment['coid'] = $comment->coid;
/** @var QQService | WeChatService | AliYunEmailService | SmtpService | DingTalkBotService | EnterpriseWeChatService $service */
foreach ($services as $service){
$inst=call_user_func([$service,'create']);
call_user_func([$inst, '__handler'], self::$active, self::$comment, $plugin);
}
}
}