Initial commit
Some checks failed
定时更新GitHub源插件 / 自动更新GitHub插件 (push) Has been cancelled

This commit is contained in:
chorblack
2026-03-07 11:19:25 +08:00
commit e75f275ef4
4484 changed files with 645480 additions and 0 deletions

34
WeiboSync/README.md Normal file
View File

@@ -0,0 +1,34 @@
Typecho新浪微博同步插件
======================================================
## 1 描述
#### 1.1 weibo
用于搭建一个基于新浪微博开放平台`OAuth2`协议的应用服务的程序,它为`WeiboSync`插件提供博客作者当前新浪微博对应的`access_token`30天有效`uid`数据。
#### 1.2 WeiboSync
基于Typecho的插件服务程序可提取Typecho文章图片作配图并同步到新浪微博。同步效果如下图所示
![Typecho同步新浪微博的插件可提取文章图片作配图](https://o3cex9zsl.qnssl.com/2015/08/blog_synchronize_weibo.png "Typecho同步新浪微博的插件可提取文章图片作配图")
## 2 安装方法
:warning:详见`weibo``WeiboSync`这两个目录下的`README`文件。
## 3 版本更新说明
#### 3.1 版本v1.0.0 (2015.12.11)
1、初始化版本。
本版本详细说明:[升级博客文章同步微博的插件PHP正则提取Markdown的图片地址](https://typecodes.com/mix/synweibophpmarkdownimgurl.html '升级博客文章同步微博的插件PHP正则提取Markdown的图片地址')。
#### 3.2 版本1.0.0 (2017.07.04)
1、完善所有的程序和相关文档。
本版本详细说明:[Typecho同步新浪微博的插件可提取文章图片作配图](https://typecodes.com/mix/typechosynweibo.html 'Typecho同步新浪微博的插件可提取文章图片作配图')。
##### 3.3 版本1.0.0 (2017.08.23)
1、同步新浪微博开放平台官方php sdk修改调用接口。
注意新浪微博最新官方要求share接口至少要带上一个【安全域名】下的链接也就是在`WeiboSync\README.md`文件中的2.3小节填写`微博内容`时,必须包含`{link}`参数。

View File

@@ -0,0 +1,259 @@
<?php
/**
* Typecho 自动同步文章至新浪微博
*
* @package WeiboSync
* @author vfhky
* @version 1.0.0
* @update: 2015.12.11
* @link https://typecodes.com
*/
class WeiboSync_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Abstract_Contents')->filter = array('WeiboSync_Plugin', 'TsinaSync');
return _t('请设置WeiboSync的信息以使插件正常使用');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
return _t('插件已禁用成功!');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
self::SinaAuth();
$sina_token = new Typecho_Widget_Helper_Form_Element_Text('sina_token',NULL,NULL,_t('新浪微博Access Token'),_t('填写Sina_weibo_Access_token的值'));
$form->addInput($sina_token);
$sina_uid = new Typecho_Widget_Helper_Form_Element_Text('sina_uid',NULL,NULL,_t('个人微博ID'),_t('填写Sina_weibo_Uid的值'));
$form->addInput($sina_uid);
$sina_mode = new Typecho_Widget_Helper_Form_Element_Radio('sina_mode',array( 0 => '仅新建时同步',1 => '新建和修改时同步'),'0', '选择同步方式');
$form->addInput($sina_mode);
$sina_imgflag = new Typecho_Widget_Helper_Form_Element_Radio('sina_imgflag',array( 1 => '是', 0 => '否' ),'1', '发布微博时是否添加第一张缩略图');
$form->addInput($sina_imgflag);
$sina_format = new Typecho_Widget_Helper_Form_Element_Textarea('sina_format', NULL, _t('我在TypeCodes上发表了一篇文章《{title}》,链接地址:{link}'), _t('微博内容'), _t('可用参数: {title}标题 {link}文章链接 {more}60字内的文章摘要'));
$form->addInput($sina_format);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 是否已经获取到了token未获取则显示获取token的图标否则显示当前登录账号
*
* @access public
* @param
* @return string
*/
public static function SinaAuth()
{
self::getPubFile();
$sina_auth = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
$authurl = $sina_auth->getAuthorizeURL( WB_CALLBACK_URL , 'code');
$img_path = Helper::options()->pluginUrl . '/WeiboSync/weibo.png';
echo $sina_profile = '<ul class="typecho-option"><li><a href="' . $authurl . '"><img src="' . $img_path . '"></a>&nbsp;&nbsp;<b>点击左边图标获取微博Access_token信息</b></li></ul>';
}
/**
* 同步文章到新浪微博
*
* @access public
* @param $content $class
* @return status
*/
public static function TsinaSync($content,$class)
{
//获取插件的配置
$config = self::getWeiboSyncCfg();
//如果不是文章编辑(对象$class属于Widget_Abstract_Contents是否等于对象的值Widget_Contents_Post_Edit它继承Widget_Abstract_Contents对象则直接返回内容
if(!is_a($class,'Widget_Contents_Post_Edit'))
{
return $content;
}
if($config->sina_mode == '1')
{
if( !$class->request->is("do=publish")
|| ($class->request->is("do=publish") && !$class->have()) )
{
return $content;
}
}
else
{
if(!$class->request->is("do=publish")
||($class->request->is("do=publish") && !empty($class->request->cid)))
{
return $content;
}
}
$format = $config->sina_format?$config->sina_format:'我在TypeCodes上发表了一篇文章《{title}》,链接地址{link}';
$title = $content['title'];
$link = self::SinaShortUrl($content['permalink']);
//如果插件配置中出现了{more}标签,即需要提取文章的摘要信息
if( strpos($format,'{more}') !== false )
{
if(strpos($content['text'], '<!--more-->') !== false )
{
$more_t = explode('<!--more-->', $content['text']);
list($more) = $more_t;
$more = Typecho_Common::fixHtml(Typecho_Common::cutParagraph($more));
$more = Typecho_Common::subStr(strip_tags($more), 0, 60, '...');
}
else
{
$more = $content['text'];
$more = Typecho_Common::fixHtml(Typecho_Common::cutParagraph($more));
$more = Typecho_Common::subStr(strip_tags($more), 0, 60, '...');
}
}
else $more = "";
$search = array('{title}','{link}','{more}');
$replace = array($title,$link,$more);
$format = str_replace($search,$replace,$format);
$post_img = '';
if( $config->sina_imgflag )
{
$content_substr = mb_substr( $content['text'], 0, 900, 'utf-8' );
if( preg_match( '/!\[[^\]]*]\((https):\/\/[^\)]*\.(png|jpg)(.*)\)/i', $content_substr, $img_match ) )
{
if( preg_match( '/(https:\/\/)[^>]*?\.(png|jpg)/i',$img_match[0],$img_match_retult ) )
$post_img = $img_match_retult[0];
}
}
self::PostWeibo( $format, $post_img );
return $content;
}
/**
* 调用新浪微博SDK的API生成微博短连接
*
* @access public
* @param $url
* @return string
*/
public static function SinaShortUrl($url)
{
$token = self::GetAuthInfo();
$api_url = "https://api.weibo.com/2/short_url/shorten.json";
$send_url = $api_url . '?access_token='. $token['sina_token'] .'&url_long='.urlencode($url);
$json_result = file_get_contents($send_url);
$short_url = json_decode($json_result);
return $short_url->urls[0]->url_short;
}
/**
* 调用新浪微博SDK的API发送微博
*
* @access public
* @param $post 内容(新浪微博最新官方要求share接口至少要带上一个【安全域名】下的链接。)
* @param $img_url 图片地址
* @return void
*/
public static function PostWeibo( $post, $img_url )
{
$post_token = self::GetAuthInfo();
$sina_auth = $post_token['sina_auth'];
if( empty($img_url) )
$sina_auth->share( $post );
else
$sina_auth->share( $post, $img_url );
}
/**
* 获取WeiboSync插件的配置
*
* @static
* @access public
* @return void
*/
public static function getWeiboSyncCfg()
{
return Typecho_Widget::widget('Widget_Options')->plugin('WeiboSync');
}
/**
* 获取新浪微博SDK文件信息
*
* @static
* @access public
* @return void
*/
public static function getPubFile()
{
require_once 'config.php';
require_once 'saetv2.ex.class.php';
}
/**
* 获取新浪微博的个人授权信息
*
* @access public
* @param
* @return void
*/
public static function GetAuthInfo()
{
self::getPubFile();
$option = self::getWeiboSyncCfg();
$sina_token = $option->sina_token;
$sina_uid = $option->sina_uid;
$sina_auth = new SaeTClientV2( WB_AKEY , WB_SKEY , $sina_token );
$auth = array();
$auth['sina_auth'] = $sina_auth;
$auth['sina_token'] = $sina_token;
$auth['sina_uid'] = $sina_uid;
return $auth;
}
}

View File

@@ -0,0 +1,44 @@
Typecho新浪微博同步插件
======================================================
## 1 描述
基于Typecho的插件服务程序可提取Typecho文章图片作配图并同步到新浪微博。
插件通过新浪微博开放平台最新的`OAuth2`认证方式登录并调用相关的API接口同步Typecho文章。效果如下图所示
![Typecho同步新浪微博的插件可提取文章图片作配图](https://o3cex9zsl.qnssl.com/2015/08/blog_synchronize_weibo.png "Typecho同步新浪微博的插件可提取文章图片作配图")
## 2 安装方法
##### 2.1 修改配置文件
根据新浪微博开放平台的应用的信息修改`config.php`文件中的`WB_AKEY``WB_SKEY``WB_CALLBACK_URL`这三个变量,它们分别代表应用的`App Key``App Secret`和回调地址。
##### 2.2 上传插件程序
把本目录(`WeiboSync`上传至Typecho插件目录默认为`/usr/plugins`)。
##### 2.3 插件设置
进入typecho的后台并启用`WeiboSync`插件,再点击`设置`按钮,进入到`WeiboSync`插件信息的设置页面。
![Typecho同步新浪微博的插件的设置](https://o3cex9zsl.qnssl.com/2015/08/update_weibo_plugin.png "Typecho同步新浪微博的插件的设置")
##### 2.4 注意
为了能正常使用本插件,请根据`weibo`目录下的`README`说明文件搭建一个基于新浪微博开放平台的应用服务程序。该程序可以为本插件提供所需的`access_token`30天有效`uid`数据。
## 3 版本更新说明
##### 3.1 版本v1.0.0 (2015.12.11)
1、初始化版本。
本版本详细说明:[升级博客文章同步微博的插件PHP正则提取Markdown的图片地址](https://typecodes.com/mix/synweibophpmarkdownimgurl.html '升级博客文章同步微博的插件PHP正则提取Markdown的图片地址')。
##### 3.2 版本1.0.0 (2017.07.04)
1、完善所有的程序和相关文档。
本版本详细说明:[Typecho同步新浪微博的插件可提取文章图片作配图](https://typecodes.com/mix/typechosynweibo.html 'Typecho同步新浪微博的插件可提取文章图片作配图')。
##### 3.3 版本1.0.0 (2017.08.23)
1、同步新浪微博开放平台官方php sdk修改调用接口。
注意新浪微博最新官方要求share接口至少要带上一个【安全域名】下的链接也就是在2.3小节填写`微博内容`时,必须包含`{link}`参数。

View File

@@ -0,0 +1,4 @@
<?php
define( 'WB_AKEY' , '1642726886' );
define( 'WB_SKEY' , 'e99a05edc476cf35b4a31b631f54b2a9' );
define( 'WB_CALLBACK_URL' , 'https://typecodes.com/weibo' );

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

38
WeiboSync/weibo/README.md Normal file
View File

@@ -0,0 +1,38 @@
基于新浪微博开放平台OAuth2协议的应用服务的程序
======================================================
## 1 描述
用于搭建一个基于新浪微博开放平台`OAuth2`协议的应用服务的程序,它为`WeiboSync`插件提供博客作者当前新浪微博对应的`access_token`30天有效`uid`数据。
## 2 使用方法
##### 2.1 新浪微博开放平台的应用
用户必须有一个已经审核通过的新浪微博开放平台的应用。申请方法这里不再赘述,详见[官方说明](http://open.weibo.com/authentication/ '微博登录介绍')。
##### 2.2 修改配置文件
根据新浪微博开放平台的应用的信息修改`config.php`文件中的`WB_AKEY``WB_SKEY``WB_CALLBACK_URL`这三个变量,它们分别代表应用的`App Key``App Secret`和回调地址。
![新浪微博开放平台OAuth2.0授权回调地址](https://o3cex9zsl.qnssl.com/2017/07/sinaweiboauthcallback_1.png "新浪微博开放平台OAuth2.0授权回调地址")
##### 2.3 博客搭建回调服务
把本目录(`weibo`)上传至对应的新浪微博应用服务器,如果能通过访问该回调地址(例如`https://typecodes.com/weibo`)则表示服务搭建完毕。
## 3 版本更新说明
##### 3.1 版本v1.0.0 (2015.12.11)
1、初始化版本。
本版本详细说明:[升级博客文章同步微博的插件PHP正则提取Markdown的图片地址](https://typecodes.com/mix/synweibophpmarkdownimgurl.html '升级博客文章同步微博的插件PHP正则提取Markdown的图片地址')。
##### 3.2 版本1.0.0 (2017.07.04)
1、完善所有的程序和相关文档。
本版本详细说明:[Typecho同步新浪微博的插件可提取文章图片作配图](https://typecodes.com/mix/typechosynweibo.html 'Typecho同步新浪微博的插件可提取文章图片作配图')。
##### 3.3 版本1.0.0 (2017.08.23)
1、同步新浪微博开放平台官方php sdk修改调用接口。
## 4 鸣谢
感谢新浪微博开放平台官方提供的`V2版PHP SDK`

View File

@@ -0,0 +1,22 @@
<?php
if( isset($_REQUEST['code']) ) {
include 'config.php';
include 'saetv2.ex.class.php';
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = WB_CALLBACK_URL;
try {
$token = $o->getAccessToken( 'code', $keys ) ;
} catch (OAuthException $e) {
}
if ($token) {
$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token[access_token] );
$uid_get = $c->get_uid();
echo 'Sina_weibo_Access_token = ['. $token['access_token'] . "]<p/>Sina_weibo_Uid = [" . $uid_get['uid'] . ']';
}
}
exit;

View File

@@ -0,0 +1,6 @@
<?php
header('Content-Type: text/html; charset=UTF-8');
define( "WB_AKEY" , '1642726886' );
define( "WB_SKEY" , 'e99a05edc476cf35b4a31b631f54b2a9' );
define( "WB_CALLBACK_URL" , 'https://typecodes.com/weibo' );

22
WeiboSync/weibo/index.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
if( isset($_REQUEST['code']) ) {
include 'config.php';
include 'saetv2.ex.class.php';
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = WB_CALLBACK_URL;
try {
$token = $o->getAccessToken( 'code', $keys ) ;
} catch (OAuthException $e) {
}
if ($token) {
$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token['access_token'] );
$uid_get = $c->get_uid();
echo 'Sina_weibo_Access_token = ['. $token['access_token'] . "]<p/>Sina_weibo_Uid = [" . $uid_get['uid'] . ']';
}
}
exit;

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -0,0 +1,51 @@
<?php
//session_start();
//暂时先直接返回
exit;
include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );
$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
$ms = $c->home_timeline(); // done
$uid_get = $c->get_uid();
$uid = $uid_get['uid'];
$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新浪微博V2接口演示程序-Powered by Sina App Engine</title>
</head>
<body>
<?=$user_message['screen_name']?>,您好!
<h2 align="left">发送新微博</h2>
<form action="" >
<input type="text" name="text" style="width:300px" />
<input type="submit" />
</form>
<?php
if( isset($_REQUEST['text']) ) {
// 注意至少要带上一个链接。
$ret = $c->share( $_REQUEST['text'] ); //发送微博
if ( isset($ret['error_code']) && $ret['error_code'] > 0 ) {
echo "<p>发送失败,错误:{$ret['error_code']}:{$ret['error']}</p>";
} else {
echo "<p>发送成功</p>";
}
}
?>
<?php if( is_array( $ms['statuses'] ) ): ?>
<?php foreach( $ms['statuses'] as $item ): ?>
<div style="padding:10px;margin:5px;border:1px solid #ccc">
<?=$item['text'];?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>