This commit is contained in:
135
SyncPost/Action.php
Normal file
135
SyncPost/Action.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* SyncPost Plugin
|
||||
*
|
||||
* @copyright Copyright (c) 2013 Binjoo (http://binjoo.net)
|
||||
* @license GNU General Public License 2.0
|
||||
*
|
||||
*/
|
||||
require_once 'Constant.php';
|
||||
require_once 'Http.php';
|
||||
|
||||
class SyncPost_Action extends Typecho_Widget implements Widget_Interface_Do
|
||||
{
|
||||
private $_siteUrl;
|
||||
|
||||
public function __construct($request, $response, $params = NULL)
|
||||
{
|
||||
parent::__construct($request, $response, $params);
|
||||
$this->_siteUrl = Helper::options()->siteUrl;
|
||||
}
|
||||
|
||||
public function tqq(){
|
||||
if ($this->request->get("code")) {//已获得code
|
||||
$code = $this->request->get("code");
|
||||
$openid = $this->request->get("openid");
|
||||
$openkey = $this->request->get("openkey");
|
||||
$params = array(
|
||||
'client_id' => TQQ_CLIENT_ID,
|
||||
'client_secret' => TQQ_CLIENT_SECRET,
|
||||
'redirect_uri' => $this->_siteUrl . TQQ_REDIRECT_URI,
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code
|
||||
);
|
||||
parse_str(HTTP::request(TQQ_ACCESS_TOKEN_URL, $params, 'GET'), $result);
|
||||
if(!isset($result['errorCode'])){
|
||||
$set = array(
|
||||
'tqq_access_token' => $result['access_token'],
|
||||
'tqq_expires_in' => $result['expires_in'],
|
||||
'tqq_openid' => $openid,
|
||||
'tqq_openkey' => $openkey,
|
||||
'tqq_last_time' => time() + $result['expires_in']
|
||||
);
|
||||
Widget_Plugins_Edit::configPlugin("SyncPost", $set);
|
||||
$this->widget('Widget_Notice')->set(_t('腾讯微博授权成功!'), 'success');
|
||||
}else{
|
||||
$this->widget('Widget_Notice')->set(_t('腾讯微博授权失败,异常信息:'.$result['errcode'].' - '.$result['errorMsg']), 'error');
|
||||
}
|
||||
header('Location: ' . $this->_siteUrl . __TYPECHO_ADMIN_DIR__ . 'options-plugin.php?config=SyncPost');//刷新页面
|
||||
} else {//获取授权code
|
||||
$params = array(
|
||||
'client_id' => TQQ_CLIENT_ID,
|
||||
'redirect_uri' => $this->_siteUrl . TQQ_REDIRECT_URI,
|
||||
'response_type' => 'code'
|
||||
);
|
||||
header('Location: ' . TQQ_AUTHORIZATION_CODE_URL.'?'.http_build_query($params));//刷新页面
|
||||
}
|
||||
}
|
||||
|
||||
public function sina(){
|
||||
if ($this->request->get("code")) {//已获得code
|
||||
$code = $this->request->get("code");
|
||||
$params = array(
|
||||
'client_id' => SINA_CLIENT_ID,
|
||||
'client_secret' => SINA_CLIENT_SECRET,
|
||||
'redirect_uri' => $this->_siteUrl . SINA_REDIRECT_URI,
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code
|
||||
);
|
||||
$result = json_decode(HTTP::request(SINA_ACCESS_TOKEN_URL, $params, 'POST'));
|
||||
if(!isset($result->error_code)){
|
||||
$set = array(
|
||||
'sina_access_token' => $result->access_token,
|
||||
'sina_expires_in' => $result->expires_in,
|
||||
'sina_last_time' => time() + $result->expires_in
|
||||
);
|
||||
Widget_Plugins_Edit::configPlugin("SyncPost", $set);
|
||||
$this->widget('Widget_Notice')->set(_t('新浪微博授权成功!'), 'success');
|
||||
}else{
|
||||
$this->widget('Widget_Notice')->set(_t('新浪微博授权失败,异常信息:'.$result->error_code.' - '.$result->error_description), 'error');
|
||||
}
|
||||
header('Location: ' . $this->_siteUrl . __TYPECHO_ADMIN_DIR__ . 'options-plugin.php?config=SyncPost');//刷新页面
|
||||
} else {//获取授权code
|
||||
$params = array(
|
||||
'client_id' => SINA_CLIENT_ID,
|
||||
'redirect_uri' => $this->_siteUrl . SINA_REDIRECT_URI,
|
||||
'response_type' => 'code'
|
||||
);
|
||||
header('Location: ' . SINA_AUTHORIZATION_CODE_URL.'?'.http_build_query($params));//刷新页面
|
||||
}
|
||||
}
|
||||
|
||||
public function douban(){
|
||||
if ($this->request->get("code")) {//已获得code
|
||||
$code = $this->request->get("code");
|
||||
$params = array(
|
||||
'client_id' => DOUBAN_CLIENT_ID,
|
||||
'client_secret' => DOUBAN_CLIENT_SECRET,
|
||||
'redirect_uri' => $this->_siteUrl . DOUBAN_REDIRECT_URI,
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code
|
||||
);
|
||||
$result = json_decode(HTTP::request(DOUBAN_ACCESS_TOKEN_URL, $params, 'POST'));
|
||||
if(!isset($result->code)){
|
||||
$set = array(
|
||||
'douban_access_token' => $result->access_token,
|
||||
'douban_refresh_token' => $result->refresh_token,
|
||||
'douban_expires_in' => $result->expires_in,
|
||||
'douban_last_time' => time() + $result->expires_in
|
||||
);
|
||||
Widget_Plugins_Edit::configPlugin("SyncPost", $set);
|
||||
$this->widget('Widget_Notice')->set(_t('豆瓣广播授权成功!'), 'success');
|
||||
}else{
|
||||
$this->widget('Widget_Notice')->set(_t('豆瓣广播授权失败,异常信息:'.$result->code.' - '.$result->msg), 'error');
|
||||
}
|
||||
header('Location: ' . $this->_siteUrl . __TYPECHO_ADMIN_DIR__ . 'options-plugin.php?config=SyncPost');//刷新页面
|
||||
} else {//获取授权code
|
||||
$params = array(
|
||||
'client_id' => DOUBAN_CLIENT_ID,
|
||||
'redirect_uri' => $this->_siteUrl . DOUBAN_REDIRECT_URI,
|
||||
'response_type' => 'code'
|
||||
);
|
||||
header('Location: ' . DOUBAN_AUTHORIZATION_CODE_URL.'?'.http_build_query($params));//刷新页面
|
||||
}
|
||||
}
|
||||
|
||||
public function action(){
|
||||
if(!$this->widget('Widget_User')->pass('administrator')){
|
||||
throw new Typecho_Widget_Exception(_t('禁止访问'), 403);
|
||||
}
|
||||
$this->on($this->request->is('tqq'))->tqq();
|
||||
$this->on($this->request->is('sina'))->sina();
|
||||
$this->on($this->request->is('douban'))->douban();
|
||||
}
|
||||
}
|
||||
?>
|
||||
22
SyncPost/Constant.php
Normal file
22
SyncPost/Constant.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
define("TQQ_CLIENT_ID","801489085");
|
||||
define("TQQ_CLIENT_SECRET","7133c0e58792a223cdde4bd83e6de8d4");
|
||||
define("TQQ_REDIRECT_URI","action/SyncPost?tqq");
|
||||
define("TQQ_AUTHORIZATION_CODE_URL","https://open.t.qq.com/cgi-bin/oauth2/authorize");
|
||||
define("TQQ_ACCESS_TOKEN_URL","https://open.t.qq.com/cgi-bin/oauth2/access_token");
|
||||
define("TQQ_API_URL","https://open.t.qq.com/api/t/add");
|
||||
|
||||
define("SINA_CLIENT_ID","773255307");
|
||||
define("SINA_CLIENT_SECRET","8fc87301c160d2e9dba7f124c3d4bd45");
|
||||
define("SINA_REDIRECT_URI","action/SyncPost?sina");
|
||||
define("SINA_AUTHORIZATION_CODE_URL","https://api.weibo.com/oauth2/authorize");
|
||||
define("SINA_ACCESS_TOKEN_URL","https://api.weibo.com/oauth2/access_token");
|
||||
define("SINA_API_URL","https://api.weibo.com/2/statuses/update.json");
|
||||
|
||||
define("DOUBAN_CLIENT_ID","07132850243491e5153978f06c9db5af");
|
||||
define("DOUBAN_CLIENT_SECRET","a2a357aaafc19903");
|
||||
define("DOUBAN_REDIRECT_URI","action/SyncPost?douban");
|
||||
define("DOUBAN_AUTHORIZATION_CODE_URL","https://www.douban.com/service/auth2/auth");
|
||||
define("DOUBAN_ACCESS_TOKEN_URL","https://www.douban.com/service/auth2/token");
|
||||
define("DOUBAN_API_URL","https://api.douban.com/shuo/v2/statuses/");
|
||||
?>
|
||||
75
SyncPost/Http.php
Normal file
75
SyncPost/Http.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* HTTP请求类
|
||||
* @author xiaopengzhu <xp_zhu@qq.com>
|
||||
* @version 2.0 2012-04-20
|
||||
*/
|
||||
class Http
|
||||
{
|
||||
/**
|
||||
* 发起一个HTTP/HTTPS的请求
|
||||
* @param $url 接口的URL
|
||||
* @param $params 接口参数 array('content'=>'test', 'format'=>'json');
|
||||
* @param $method 请求类型 GET|POST
|
||||
* @param $multi 图片信息
|
||||
* @param $extheaders 扩展的包头信息
|
||||
* @return string
|
||||
*/
|
||||
public static function request( $url , $params = array(), $method = 'GET' , $multi = false, $extheaders = array())
|
||||
{
|
||||
if(!function_exists('curl_init')) exit('Need to open the curl extension');
|
||||
$method = strtoupper($method);
|
||||
$ci = curl_init();
|
||||
curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
|
||||
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
|
||||
$timeout = $multi?30:3;
|
||||
curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
|
||||
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ci, CURLOPT_HEADER, false);
|
||||
$headers = (array)$extheaders;
|
||||
switch ($method)
|
||||
{
|
||||
case 'POST':
|
||||
curl_setopt($ci, CURLOPT_POST, TRUE);
|
||||
if (!empty($params))
|
||||
{
|
||||
if($multi)
|
||||
{
|
||||
foreach($multi as $key => $file)
|
||||
{
|
||||
$params[$key] = '@' . $file;
|
||||
}
|
||||
curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
|
||||
$headers[] = 'Expect: ';
|
||||
}
|
||||
else
|
||||
{
|
||||
curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'DELETE':
|
||||
case 'GET':
|
||||
$method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
||||
if (!empty($params))
|
||||
{
|
||||
$url = $url . (strpos($url, '?') ? '&' : '?')
|
||||
. (is_array($params) ? http_build_query($params) : $params);
|
||||
}
|
||||
break;
|
||||
}
|
||||
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
|
||||
curl_setopt($ci, CURLOPT_URL, $url);
|
||||
if($headers)
|
||||
{
|
||||
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
|
||||
}
|
||||
|
||||
$response = curl_exec($ci);
|
||||
curl_close ($ci);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
?>
|
||||
146
SyncPost/Plugin.php
Normal file
146
SyncPost/Plugin.php
Normal file
@@ -0,0 +1,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;
|
||||
}
|
||||
}
|
||||
21
SyncPost/README.md
Normal file
21
SyncPost/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
## 插件说明 ##
|
||||
|
||||
~~新增、编辑日志或者页面,可以选择是否发送消息至社会化平台。~~
|
||||
|
||||
> :no_entry:不可用:腾讯微博/豆瓣已关闭API,新浪微博已限定回调地址。
|
||||
|
||||
## 使用帮助 ##
|
||||
|
||||
1. 下载插件
|
||||
2. 将插件上传到 `/usr/plugins/` 这个目录下
|
||||
3. 登陆后台,在“控制台”下拉菜单中进入“插件管理”
|
||||
4. 启用相关插件
|
||||
5. 根据相关插件要求更新设置
|
||||
|
||||
## 版本历史 ##
|
||||
|
||||
#### v1.0.0
|
||||
- 加入腾讯微博;
|
||||
- 加入新浪微博;
|
||||
- 加入豆瓣广播;
|
||||
- 可自定义发送内容。
|
||||
Reference in New Issue
Block a user