-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlugin.php
executable file
·167 lines (158 loc) · 6.48 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
//如果没有json库,加载兼容包
! extension_loaded('json') and include('libs/compat_json.php');
/**
* 黑客派社区实时同步插件
*
* @package B3log for HacPai
* @author DT27
* @version 1.0.1
* @link https://dt27.org/B3log-for-HacPai/
*/
class B3logForHacPai_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish =
array('B3logForHacPai_Plugin', 'finishPublish');
Typecho_Plugin::factory('Widget_Feedback')->finishComment =
array('B3logForHacPai_Plugin', 'finishComment');
// 创建路由
// from HacPai
Helper::addRoute('b3log.hacpai.article', '/b3log-hacpai/article', 'B3logForHacPai_Action', 'articleReceiver');
Helper::addRoute('b3log.hacpai.comment', '/b3log-hacpai/comment', 'B3logForHacPai_Action', 'commentReceiver');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){
Helper::removeRoute('b3log.hacpai.article');
Helper::removeRoute('b3log.hacpai.comment');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$b3logKey = new Typecho_Widget_Helper_Form_Element_Text('b3logKey', NULL, NULL,
_t('B3log Key'), _t('请填写黑客派社区中设置的 B3log Key,并在社区中设置接收接口。<a href="https://hacpai.com/settings#soloKey" target="_blank">点此设置</a><br>
客户端收文及更新接口:<strong style="color: red;">'.Helper::options()->siteUrl.'b3log-hacpai/article</strong><br>客户端收评接口:<strong style="color: red;">'.Helper::options()->siteUrl.'b3log-hacpai/comment</strong>'));
$form->addInput($b3logKey->addRule('required', _t('必须填写 B3log Key')));
$b3logTitle = new Typecho_Widget_Helper_Form_Element_Text('b3logTitle', NULL, Helper::options()->title,
_t('博客标题'), _t('请填写本博客标题'));
$form->addInput($b3logTitle);
$b3logHost = new Typecho_Widget_Helper_Form_Element_Text('b3logHost', NULL, Helper::options()->siteUrl,
_t('博客地址'), _t('请填写本博客地址,需包括 http 且末尾无斜杠,例如:<strong style="color: red;">https://dt27.org</strong>'));
$form->addInput($b3logHost);
Typecho_Widget::widget('Widget_User')->to($user);
$b3logEmail = new Typecho_Widget_Helper_Form_Element_Text('b3logEmail', NULL, $user->mail,
_t('博客邮箱'), _t('请填写本博客邮箱'));
$form->addInput($b3logEmail);
$isHacPai = new Typecho_Widget_Helper_Form_Element_Radio('isHacPai',
array(
'1' => '是',
'0' => '否',
),'1', _t('是否启用同步功能'), NULL);
$form->addInput($isHacPai);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 发布文章
*
* @access public
* @return void
*/
public static function finishPublish($contents, $edit)
{
$b3log = Typecho_Widget::widget('Widget_Options')->plugin('B3logForHacPai');
if($b3log->isHacPai == 1) {
$postData = array(
"article" => array(
"id" => $edit->cid,
"title" => $contents['title'],
"permalink" => substr($edit->permalink,strlen($b3log->b3logHost)),//substr($str,4) [article.permalink] should start with /, for example, /hello-world
"tags" => $contents['tags'],
"content" => $contents['text'],
),
"client" => array(
"title" => $b3log->b3logTitle,
"host" => $b3log->b3logHost,
"email" => $b3log->b3logEmail,
"key" => $b3log->b3logKey,
));
$postString = json_encode($postData);
$ch = curl_init('http://rhythm.b3log.org/api/article');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($postString))
);
$result = curl_exec($ch);
}
return $contents;
}
/**
* 发布评论
*
* @access public
* @return void
*/
public static function finishComment($comment)
{
$b3log = Typecho_Widget::widget('Widget_Options')->plugin('B3logForHacPai');
if ($b3log->isHacPai == 1) {
$postData = array(
"comment" => array(
"id" => $comment->coid,
"articleId" => $comment->cid,
"content" => $comment->text,
"authorName" => $comment->author,
"authorEmail" => $comment->mail,
),
"client" => array(
"title" => $b3log->b3logTitle,
"host" => $b3log->b3logHost,
"email" => $b3log->b3logEmail,
"key" => $b3log->b3logKey,
));
$postString = json_encode($postData);
$ch = curl_init('http://rhythm.b3log.org/api/comment');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content -Length: ' . strlen($postString))
);
$result = curl_exec($ch);
//print_r($result);exit;
}
return $comment;
}
}