forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.php
306 lines (266 loc) · 12 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
<?php
/**
* 为评论提供当前页面@ 功能
*
* @package At
* @author 公子
* @version 0.1.1
* @link http://zh.eming.li
*/
class At_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->footer = array('At_Plugin', 'footer');
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('At_Plugin', 'toMail');
return _t('主人我还需要您到配置页面中手动初始化一下才能正常工作');
}
/**
* 插件实现方法
*
* @access public
* @param $widget
* @return false
*/
public static function footer($widget)
{
//~ 非post, page页以及不允许评论页插件不做处理
if(!$widget->is('post') && !$widget->is('page')) return false;
//~ 获取插件路径
$options = Helper::options();
$baseUrl = $options->pluginUrl;
//~ 获取当前页面所有评论并格式化输出
$db = Typecho_Db::get();
$comments = $db->fetchAll( $db->select('coid', 'author', 'text')->from('table.comments')->where('cid = ?', $widget->cid));
$data = array();
foreach($comments as $comment)
{
$text = mb_strimwidth(strip_tags($comment['text']), 0, 23, '...','UTF-8');
$text = str_replace(array("\r\n", "\n", "\r"), ' ', $text);
$data[] = array('id'=>$comment['coid'], 'name'=>$comment['author'], 'text'=>$comment['author'].$text);
}
if($options->plugin('At')->jquery) {
echo '<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>';
}
?>
<link rel="stylesheet" type="text/css" href="<?php echo $baseUrl; ?>/At/res/css/jquery.atwho.css"/>
<script type="text/javascript" src="<?php echo $baseUrl; ?>/At/res/js/jquery.atwho.min.js"></script>
<script>
//<![CDATA[
TypechoComment.reply = function (cid, coid) {
var comment = TypechoComment.dom(cid), parent = comment.parentNode,
response = TypechoComment.dom('respond-post-<?php echo $widget->cid; ?>'), input = TypechoComment.dom('comment-parent'),
form = 'form' == response.tagName ? response : response.getElementsByTagName('form')[0],
textarea = response.getElementsByTagName('textarea')[0];
if (null == input) {
input = TypechoComment.create('input', {
'type' : 'hidden',
'name' : 'parent',
'id' : 'comment-parent'
});
form.appendChild(input);
}
input.setAttribute('value', coid);
if (null == TypechoComment.dom('comment-form-place-holder')) {
var holder = TypechoComment.create('div', {
'id' : 'comment-form-place-holder'
});
response.parentNode.insertBefore(holder, response);
}
comment.appendChild(response);
TypechoComment.dom('cancel-comment-reply-link').style.display = '';
if (null != textarea && 'text' == textarea.name) {
textarea.focus();
var res = {
'id': response.parentNode.id,
'name':response.parentNode.getElementsByClassName('fn')[0].innerText
}
textarea.innerHTML = '@<a href="#'+res.id+'">'+res.name+'</a>'+textarea.innerHTML;
}
return false;
}
//]]>
$(function() {
var data = <?php echo json_encode($data); ?>;
$('textarea').atwho('run').atwho({
at: "@",
data: data,
max_len: 8,
search_key: 'text',
tpl: '<li data-value=\'@<a href=\"#comment-${id}\">${name}</a>\'>${text}</li>'
});
});
</script>
<?php
}
/**
* 插件实现方法
*
* @access public
* @param $comment
* @return false
*/
public static function toMail($post) {
/**
* 可自定义的全局变量
* 标签 含义 说明
* {site} 博客站点名称 在博客后台设置中设定的
* {title} 评论文章标题
* {author} 作者名称
* {mail} 评论者的邮箱
* {permalink} 评论链接
* {text} 发评论的正文
* {time} 发布评论时间
* {author_m} 被提及的用户名称
* {text_m} 被提及的用户评论
*
*/
/* 获取$preg */
$preg = array('/{site}/', '/{title}/', '/{author}/', '/{mail}/', '/{permalink}/', '/{text}/', '/{time}/', '/{author_m}/', '/{text_,}/');
/* 获取$settings */
$config = Helper::options()->plugin('At');
$host = $config->host;
$port = $config->port;
$user = $config->user;
$password = $config->pass;
$validate = $config->validate;
$sender = $config->sender;
$settings = compact('config', 'host', 'port', 'user', 'password', 'validate', 'sender');
require '.'. __TYPECHO_PLUGIN_DIR__.'/At/PHPMailer/PHPMailerAutoload.php';
/* 获取$replace */
$options = Typecho_Widget::widget('Widget_Options');
$site = $options->title;
$settings['site'] = $site;
$title = $post->title;
$author = $post->author;
$mail = $post->mail;
$permalink = $post->permalink;
$text = $post->text;
$time = date($options->timezone, $post->created);
$replace = compact('site', 'title', 'author', 'mail', 'permalink', 'text', 'time');
$mentions = self::match($text);
$db = Typecho_Db::get();
foreach($mentions as $id => $user) {
$res = $db->fetchRow( $db->select('*')->from('table.comments')->where('coid = ?', $id) );
$replace['author_m'] = $user;
$replace['text_m'] = $res['text'];
$settings['mentioned'] = $user;
$settings['address'] = $res['mail'];
$settings['subject'] = preg_replace($preg, $replace, $config->subject);
$settings['body'] = preg_replace($preg, $replace, $config->body);
self::send($settings);
}
}
public static function match($text) {
$preg = '|@<a href="(.*?)">(.*?)</a>|i';
preg_match_all($preg, $text, $match);
$arr = array();
foreach($match[0] as $k => $v) {
$id = explode('-', $match[1][$k]);
$id = end($id);
$name = $match[2][$k];
$arr[$id] = $name;
}
return array_unique($arr, SORT_STRING);
}
public static function send($settings) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->CharSet = "UTF-8";
$mail->Host = $settings['host'];
$mail->port = $settings['port'];
$mail->SMTPAuth = ( array_search('validate', $settings['validate']) === false ) ? false : true;
if( !( array_search('ssl', $settings['validate']) === false) ) $mail->SMTPSecure = 'ssl';
$mail->Username = $settings['user'];
$mail->Password = $settings['password'];
$mail->From = $settings['user'];
$mail->FromName = "=?utf-8?B?".base64_encode($settings['site'])."?=";
$mail->addAddress($settings['address'], "=?utf-8?B?".base64_encode(strip_tags($settings['mentioned']))."?=");
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "=?utf-8?B?".base64_encode($settings['subject'])."?=";
$mail->Body = $settings['body'];
$mail->AltBody = strip_tags($settings['body']);
if(!$mail->send()) {
$file = '.'.__TYPECHO_PLUGIN_DIR__.'/At/error_log.txt';
$fp = @fopen($file,'a+');
fwrite($fp,date('Y-m-d H:i:s').' '.$mail->ErrorInfo.PHP_EOL);
fclose($fp);
exit;
}
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){
/**
* 可自定义的全局变量
* 标签 含义 说明
* {site} 博客站点名称 在博客后台设置中设定的
* {title} 评论文章标题
* {author} 作者名称
* {mail} 评论者的邮箱
* {permalink} 评论链接
* {text} 发评论的正文
* {time} 发布评论时间
* {author_m} 被提及的用户名称
* {text_m} 被提及的用户评论
*
*/
$text = "<p>{author}在<a href=\"{permalink}\">《{title}》</a>中发布的评论提到了你,以下是他的评论正文:</p>
<blockquote>{text}</blockquote>
<p>快去看看到底是怎么回事吧!</p>";
$jquery = new Typecho_Widget_Helper_Form_Element_Radio('jquery',
array('0' => _t('不加载'), '1' => _t('加载')),
'1', _t('是否加载外部jQuery库文件'), _t('插件需要jQuery库文件的支持,如果主题已经加载了可以选择不加载'));
$form->addInput($jquery);
$host = new Typecho_Widget_Helper_Form_Element_Text('host', NULL, 'smtp.', _t('SMTP地址'), _t('请填写 SMTP 服务器地址'));
$form->addInput($host->addRule('required', _t('必须填写一个SMTP服务器地址')));
$port = new Typecho_Widget_Helper_Form_Element_Text('port', NULL, '25', _t('SMTP端口'), _t('SMTP服务端口,一般为25。'));
$port->input->setAttribute('class', 'mini');
$form->addInput($port->addRule('required', _t('必须填写SMTP服务端口'))->addRule('isInteger', _t('端口号必须是纯数字')));
$user = new Typecho_Widget_Helper_Form_Element_Text('user', NULL, NULL, _t('SMTP用户'),_t('SMTP服务验证用户名,一般为邮箱名如:youname@domain.com'));
$form->addInput($user->addRule('required', _t('SMTP服务验证用户名')));
$pass = new Typecho_Widget_Helper_Form_Element_Password('pass', NULL, NULL, _t('SMTP密码'));
$form->addInput($pass->addRule('required', _t('SMTP服务验证密码')));
$validate=new Typecho_Widget_Helper_Form_Element_Checkbox('validate', array('validate'=>'服务器需要验证', 'ssl'=>'ssl加密'), array('validate'),'SMTP验证');
$form->addInput($validate);
/*
$limit = new Typecho_Widget_Helper_Form_Element_Text('limit', NULL, '5', _t('最大单次提及邮件提醒数'), _t('一次@最多能发送多少封邮件提醒,默认为5封'));
$limit->input->setAttribute('class', 'mini');
$form->addInput($limit->addRule('required', _t('请设置一个上限数字'))->addRule('isInteger', _t('上限必须是纯数字')));
*/
$subject = new Typecho_Widget_Helper_Form_Element_Text('subject', NULL, _t('有人在《{title}》中召唤你'), _t('提醒邮件标题'));
$form->addInput($subject->addRule('required', _t('提醒邮件的标题是必须要设置的')));
$body = new Typecho_Widget_Helper_Form_Element_Textarea('body', NULL, $text, _t('提醒邮件正文'), _t('可使用变量:{site}, {title}, {author}, {mail}, {permalink}, {text}, {time}, {author_m}, {text_m}'));
$form->addInput($body->addRule('required', _t('提醒邮件的正文是必须要设置的')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
}