This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
forked from Adoream/typecho-plugin-comment2telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
executable file
·153 lines (135 loc) · 7 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
<?php
require_once __DIR__ . '/Bootstrap.php';
/**
* Telegram 推送评论通知
*
* @package TgNotify
* @author BLxcwg666, Sora Jin
* @version 1.3.3
* @link https://blog.xcnya.cn/archives/77.html
*/
class TgNotify_Plugin implements Typecho_Plugin_Interface {
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate() {
if (false == self::isWritable(dirname(__FILE__))) {
throw new Typecho_Plugin_Exception(_t('对不起,插件目录不可写,无法正常使用此功能'));
}
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('TgNotify_Plugin', 'commentSend');
Typecho_Plugin::factory('Widget_Comments_Edit')->finishComment = array('TgNotify_Plugin', 'commentSend');
Helper::addAction("CommentEdit", "TgNotify_Action");
/*Bootstrap::fetch ("https://api.aim.moe/Counter/Plugin", [
'siteName' => $GLOBALS['options']->title,
'siteUrl' => $GLOBALS['options']->siteUrl,
'plugin' => 'TgNotify',
'version' => Plugin_Const::VERSION
], 'POST');*/
return _t('别忘了在插件配置填写相关信息喵~');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate() {
Helper::removeAction("CommentEdit");
/*$data = Bootstrap::fetch ("https://api.aim.moe/Counter/Plugin?siteName=" . $GLOBALS['options']->title . '&siteUrl=' . $GLOBALS['options']->siteUrl . '&plugin=TgNotify');
$data = json_decode ($data, true);
Bootstrap::fetch ("https://api.aim.moe/Counter/Plugin/" . $data[0]->pid, 'DELETE');*/
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config (Typecho_Widget_Helper_Form $form) {
$lversion = json_decode(Bootstrap::fetch (Plugin_Const::GITHUB_REPO_API))->tag_name;
if ($lversion > Plugin_Const::VERSION){
echo '<p style="font-size:18px;">你正在使用 <a>' . Plugin_Const::VERSION . '</a> 版本的 Comment2Telegram,最新版本为 <a style="color:red;">' . $lversion . '</a><a href="https://github.com/MoeLoli/Comment2Telegram"><button type="submit" class="btn btn-warn" style="margin-left:10px;">前往更新</button></a></p>';
} else {
echo '<p style="font-size:18px;">你正在使用最新版的 Comment2Telegram!</p>';
}
$Mode = new Typecho_Widget_Helper_Form_Element_Radio('mode', array ('0' => '由插件处理', '1' => '由外部处理'), 0, '评论回复处理', '建议选择 "插件处理"(好像被我改坏了),如果 Bot 还要实现其他功能请选择 "外部处理"');
$form->addInput($Mode->addRule('enum', _t('必须选择一个模式'), array(0, 1)));
$Token = new Typecho_Widget_Helper_Form_Element_Text('Token', NULL, NULL, _t('Token'), _t('需要输入指定Token'));
$form->addInput($Token->addRule('required', _t('您必须填写一个正确的Token')));
$MasterID = new Typecho_Widget_Helper_Form_Element_Text('MasterID', NULL, NULL, _t('MasterID'), _t('Telegram Master ID'));
$form->addInput($MasterID->addRule('required', _t('您必须填写一个正确的 Telegram ID')));
echo '<style>.typecho-option-submit button[type="submit"]{display:none!important}</style><script>window.onload=function(){$(".typecho-option-submit li").append("<div class=\"description\"><button class=\"btn primary\" id=\"save\">保存设置</button></div>");$("input[name=mode]").change(function(){if($(this).val()==1){$(".description").append("<div class=\"outDeal\">保存后将会往你的 TG 中发送接口信息</div>")}else if($(this).val()==0){if($(".outDeal").length>0){$(".outDeal").remove()}}});$("button#save").click(function(){var b=$(this),a=$(b).text();$(b).attr("disabled","disabled");if($("input[name=Token]").val()==""){$(b).text("请填写Bot Token");setTimeout(function(){$(b).text(a);$(b).removeAttr("disabled")},2000);return}if($("input[name=MasterID]").val()==""){$(b).text("请填写Bot Token");setTimeout(function(){$(b).text(a);$(b).removeAttr("disabled")},2000);return}$.ajax({type:"POST",url:window.location.origin+"/action/CommentEdit?do=setWebhook",dataType:"json",data:{mode:$("input[name=mode]:checked").val(),token:$("input[name=Token]").val(),master:$("input[name=Master]").val()},success:function(d,e,c){if(d.code=="0"){$(b).text("已 Reset Webhook");setTimeout(function(){$(b).text("正在保存设置");$(".typecho-option-submit button[type=\"submit\"]").click()},2000)}else{$(b).text("失败:"+d.msg)}}})})}</script>';
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form) {}
/**
* Telegram 推送
*
* @access public
* @param array $comment 评论结构
* @param Typecho_Widget $post 被评论的文章
* @return void
*/
public static function commentSend($comment, $post) {
// 初始化变量
$_cfg = $GLOBALS['options']->plugin('TgNotify');
$text = "*收到评论!*\n" .
"*文章 >>* $comment->title \n\n" .
"*{$comment->author}*:\n" .
"{$comment->text}\n\n" .
"*IP >>* {$comment->ip}\n" .
"*User-Agent >>* {$comment->agent}\n\n" .
"Cid={$comment->cid}, Coid={$comment->coid}\n" .
"_Tips:回复这条消息来回复评论_";
$button = json_encode([
'inline_keyboard' => [
[
['text' => '垃圾评论', 'callback_data' => 'spam_' . $comment->coid],
['text' => '删除评论', 'callback_data' => 'delete_' . $comment->coid]
],
[
['text' => '直达文章', 'url' => $comment->permalink]
]
]
]);
$GLOBALS['telegramModel']->sendMessage($_cfg->MasterID, $text, NULL, $button);
}
/**
* 检测 是否可写
* @param $file
* @return bool
*/
public static function isWritable($file) {
if (is_dir($file)) {
$dir = $file;
if ($fp = @fopen("$dir/check_writable", 'w')) {
@fclose($fp);
@unlink("$dir/check_writable");
$writeable = true;
} else {
$writeable = false;
}
} else {
if ($fp = @fopen($file, 'a+')) {
@fclose($fp);
$writeable = true;
} else {
$writeable = false;
}
}
return $writeable;
}
}