forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
146 lines (142 loc) · 7.62 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
<?php
/**
* Sync Post
*
* @package SyncPost
* @author 冰剑
* @version 1.0.0
* @link http://www.binjoo.net
*/
require_once 'Constant.php';
require_once 'Http.php';
class SyncPost_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('admin/write-post.php')->option = array('SyncPost_Plugin', 'render');
Typecho_Plugin::factory('admin/write-page.php')->option = array('SyncPost_Plugin', 'render');
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->write = array('SyncPost_Plugin', 'postRender');
Typecho_Plugin::factory('Widget_Contents_Page_Edit')->write = array('SyncPost_Plugin', 'postRender');
Helper::addAction('SyncPost', 'SyncPost_Action');
}
public static function deactivate(){
Helper::removeAction('SyncPost');
}
public static function config(Typecho_Widget_Helper_Form $form){
$options = '';
$siteUrl = Helper::options()->siteUrl;
try {
$options = Helper::options()->plugin('SyncPost');
} catch (Exception $e) {
}
echo '<style type="text/css">form{width: 480px}.rigthUl{float: right;margin-left: 10px;width: 260px}</style>';
echo '<div class="rigthUl">';
echo '<ul class="typecho-option"><li><label class="typecho-label">腾讯微博</label><p class="description">';
echo '授权状态:';
if($options->tqq_access_token){
if($options->tqq_last_time < time()){
echo '<span style="color: #0000FF">已过期</span>(<a href="'.$siteUrl.'/action/SyncPost?tqq">重新授权</a>)';
}else{
echo '<span style="color: #BD6800">已授权</span>';
}
echo '<br />有效时间:'. date("Y-m-d H:i", $options->tqq_last_time);
}else{
echo '<span style="color: #FF0000">未授权</span>(<a href="'.$siteUrl.'action/SyncPost?tqq">申请授权</a>)';
}
echo '</p></li></ul>';
echo '<ul class="typecho-option"><li><label class="typecho-label">新浪微博</label><p class="description">';
echo '授权状态:';
if($options->sina_access_token){
if($options->sina_last_time < time()){
echo '<span style="color: #0000FF">已过期</span>(<a href="'.$siteUrl.'/action/SyncPost?sina">重新授权</a>)';
}else{
echo '<span style="color: #BD6800">已授权</span>';
}
echo '<br />有效时间:'. date("Y-m-d H:i", $options->sina_last_time);
}else{
echo '<span style="color: #FF0000">未授权</span>(<a href="'.$siteUrl.'action/SyncPost?sina">申请授权</a>)';
}
echo '</p></li></ul>';
echo '<ul class="typecho-option"><li><label class="typecho-label">豆瓣广播</label><p class="description">';
echo '授权状态:';
if($options->douban_access_token){
if($options->douban_last_time < time()){
echo '<span style="color: #0000FF">已过期</span>(<a href="'.$siteUrl.'/action/SyncPost?douban">重新授权</a>)';
}else{
echo '<span style="color: #BD6800">已授权</span>';
}
echo '<br />有效时间:'. date("Y-m-d H:i", $options->douban_last_time);
}else{
echo '<span style="color: #FF0000">未授权</span>(<a href="'.$siteUrl.'action/SyncPost?douban">申请授权</a>)';
}
echo '</p></li></ul>';
echo '</div>';
$postContent = new Typecho_Widget_Helper_Form_Element_Textarea('postContent', NULL, '我发表了一篇新的日志《%title%》,地址是:%permalink%,快来坐沙发吧!', _t('发送内容'), _t('标题:%title%<br />地址:%permalink%'));
$form->addInput($postContent);
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('tqq_access_token'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('tqq_expires_in'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('tqq_openid'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('tqq_openkey'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('tqq_last_time'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('sina_access_token'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('sina_expires_in'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('sina_last_time'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('douban_access_token'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('douban_refresh_token'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('douban_expires_in'));
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('douban_last_time'));
}
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
public static function render()
{
echo '<section class="typecho-post-option category-option"><label class="typecho-label">Sync Post</label>
<ul><li><input type="checkbox" id="syncpost-tqq" value="syncpost-tqq" name="syncpost[]">
<label for="syncpost-tqq">腾讯微博</label></li>
<li><input type="checkbox" id="syncpost-sina" value="syncpost-sina" name="syncpost[]">
<label for="syncpost-sina">新浪微博</label></li>
<li><input type="checkbox" id="syncpost-douban" value="syncpost-douban" name="syncpost[]">
<label for="syncpost-douban">豆瓣广播</label></li></ul></section>';
}
public static function postRender($contents, $class){
//echo '<xmp>';
//var_dump($contents);
//exit();
if($class->request->is('do=publish')/* && !$class->request->get('cid')*/){
$opstions = Helper::options()->plugin('SyncPost');
$syncpost = $class->request->get('syncpost');
foreach ((array)$syncpost as $key => $val) {
if($val == 'syncpost-tqq'){
$params = array(
'oauth_consumer_key' => TQQ_CLIENT_ID,
'access_token' => $opstions->tqq_access_token,
'openid' => $opstions->tqq_openid,
'oauth_version' => '2.a',
'format' => 'json',
'content' => str_replace(array('%title%', '%permalink%'), array($contents['title'], $class->permalink), $opstions->postContent)
);
$result = HTTP::request(TQQ_API_URL, $params, 'POST');
}else if($val == 'syncpost-sina'){
$params = array(
'access_token' => $opstions->sina_access_token,
'status' => str_replace(array('%title%', '%permalink%'), array($contents['title'], $class->permalink), $opstions->postContent)
);
$result = HTTP::request(SINA_API_URL, $params, 'POST');
}else if($val == 'syncpost-douban'){
$params = array(
'source' => $opstions->douban_access_token,
'text' => str_replace(array('%title%', '%permalink%'), array($contents['title'], $class->permalink), $opstions->postContent)
);
$result = HTTP::request(DOUBAN_API_URL, $params, 'POST', false, array('Authorization: Bearer ' .$opstions->douban_access_token));
}
}
}
return $contents;
}
}