-
Notifications
You must be signed in to change notification settings - Fork 589
/
Plugin.php
357 lines (281 loc) · 14.4 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* WeChat Share for Typecho
*
* @package WeChatShare
* @author Fuzqing
* @version 1.0.6
* @link https://huangweitong.com
*
*
* version 1.0.6 at 2018-12-19
* 删除在线更新
* 删除自定义字段
*
*/
class WeChatShare_Plugin implements Typecho_Plugin_Interface
{
/**
* 插件版本号
* @var string
*/
const _VERSION = '1.0.6';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
*/
public static Function activate()
{
$info = WeChatShare_Plugin::wechatShareInstall();
Helper::addAction('wx-share', 'WeChatShare_Action');
Typecho_Plugin::factory('admin/write-post.php')->bottom = array('WeChatShare_Plugin', 'render');
Typecho_Plugin::factory('admin/write-page.php')->bottom = array('WeChatShare_Plugin', 'render');
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish = array('WeChatShare_Plugin','updateWxShare');
Typecho_Plugin::factory('Widget_Contents_Page_Edit')->finishPublish = array('WeChatShare_Plugin','updateWxShare');
Typecho_Plugin::factory('Widget_Archive')->footer = array('WeChatShare_Plugin','addWxShareScript');
return $info;
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
*/
public static function deactivate(){
Helper::removeAction('wx-share');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$options = Typecho_Widget::widget('Widget_Options');
$up_action_url = Typecho_Common::url('/index.php/action/wx-share?do=update-plugin', $options->siteUrl);
/** 公众号配置 */
$wx_AppID = new Typecho_Widget_Helper_Form_Element_Text('wx_AppID', NULL, NULL, _t('APPID'),'请登录微信公众号获取');
$form->addInput($wx_AppID);
$wx_AppSecret = new Typecho_Widget_Helper_Form_Element_Text('wx_AppSecret', NULL, NULL, _t('密钥'),'请登录微信公众号获取');
$form->addInput($wx_AppSecret);
$wx_image = new Typecho_Widget_Helper_Form_Element_Text('wx_image', NULL, NULL, _t('默认图标URL'),'请注意图标大小不要超过32KB');
$form->addInput($wx_image);
$access_token_expire_time = new Typecho_Widget_Helper_Form_Element_Hidden('access_token_expire_time', NULL, NULL, _t('AccessToken 过期时间'),'隐藏');
$form->addInput($access_token_expire_time);
$access_token = new Typecho_Widget_Helper_Form_Element_Hidden('access_token', NULL, NULL, _t('AccessToken'),'隐藏');
$form->addInput($access_token);
$jsapi_ticket_expire_time = new Typecho_Widget_Helper_Form_Element_Hidden('jsapi_ticket_expire_time', NULL, NULL, _t('JsapiTicket 过期时间'),'隐藏');
$form->addInput($jsapi_ticket_expire_time);
$jsapi_ticket = new Typecho_Widget_Helper_Form_Element_Text('jsapi_ticket', NULL, NULL, _t('JsapiTicket'),'方便出错调试,不用填写自动更新');
$form->addInput($jsapi_ticket);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 添加微信分享信息输入框到编辑文章/页面的页面,添加自定义摘要自定义字段
*/
public static function render()
{
$options = Typecho_Widget::widget('Widget_Options');
$wx_action_url = Typecho_Common::url('/index.php/action/wx-share?do=insert', $options->siteUrl);
$cid_res = preg_match('/cid=(\d+)/',$_SERVER['REQUEST_URI'],$match);
$description = '';$wx_title = '';$wx_description = '';$wx_url = '';$wx_image = '';
if($cid_res) {
/** 取出数据 */
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$desc_res= $db->fetchAll($db->select()->from($prefix.'fields')->where('cid = ?', $match['1'])->where('name = ?', 'description'));
$wx_share= $db->fetchAll($db->select()->from($prefix.'wx_share')->where('cid = ?', $match['1']));
if($desc_res) {
$description = $desc_res[0]['str_value'];
}
if($wx_share) {
$wx_title = $wx_share[0]['wx_title'];
$wx_description = $wx_share[0]['wx_description'];
$wx_url = $wx_share[0]['wx_url'];
$wx_image = $wx_share[0]['wx_image'];
}
}
$data = '<style>:-moz-placeholder {color: #E0E0E0; opacity:1;}::-moz-placeholder {color: #E0E0E0;opacity:1;}input:-ms-input-placeholder{color: #E0E0E0;opacity:1;}input::-webkit-input-placeholder{color: #E0E0E0;opacity:1;}</style><form id="wx_share" ><fieldset><legend>微信分享</legend><ol style="list-style-type:none;><li style="padding-bottom: 5px;"><label for="wx_title">标题:</label><input style="width: 80%" type="text" class="wx_title" value="'.$wx_title.'" name="wx_title" ></li><li style="padding-bottom: 5px;"><label for="wx_url">链接:</label><input type="text" style="width: 80%" value="'.$wx_url.'" name="wx_url"></li><li style="padding-bottom: 5px;display:block;"><span style="float:left" for="wx_describe">摘要:</span><textarea rows="4" class="wx_describe" name="wx_description" style="width: 80%" >'.$wx_description.'</textarea></li><li style="padding-bottom: 5px;"><label for="wx_image">图标:</label><input type="text" class="wx_image" value="'.$wx_image.'" style="width: 80%" name="wx_image"></li></ol></fieldset><input type="hidden" name="cid" value="'.$match['1'].'"></form>';
?>
<script>
$(document).ready(function(){
//添加微信分享
$("#custom-field").after('<?php echo $data ?>');
//监控文章的输入,同步更新微信标题
$("#title").bind(' input propertychange ',function () {
var wx_title = $(".wx_title").val();
if($("#title").val()) {
if (wx_title !== null || wx_title !== undefined || wx_title !== '') {
$(".wx_title").val($("#title").val());
}
}
});
$("#text").blur(function(){
var text = $("#text").val();
var images = text.match(/http.*?(png|jpg|jpeg|gif)$/gi);
if(images){
var wx_image = $(".wx_image").val();
if (wx_image !== null || wx_image !== undefined || wx_image !== '') {
$(".wx_image").val(images[0]);
}
}
});
});
$("#btn-submit").click(function () {
var wx_data = $("#wx_share").serialize();
$.post("<?php echo $wx_action_url;?>", wx_data,success,"");
function success(data){
//console.log(data);
}
});
</script>
<?php
}
/**
* 更新wx_share的wx_url字段
*/
public static function updateWxShare($contents,$class)
{
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$wx_share = $db->fetchAll($db->select('wx_id,wx_url')->from('table.wx_share')->order('wx_id',Typecho_Db::SORT_DESC)->limit(1));
$wx_id = $wx_share[0]['wx_id'];
!empty($wx_share[0]['wx_url']) || $data['wx_url'] = $class->permalink;
$data['cid'] = $class->cid;
//更新数据,执行后,返回收影响的行数。
$db->query($db->update($prefix.'wx_share')->rows($data)->where('wx_id = ?',$wx_id));
}
/**
* 将微信分享的需要的信息写入wx_share.js文件中
*/
public static function addWxShareScript($archive)
{
echo '<script type="text/javascript" src="//qzonestyle.gtimg.cn/qzone/qzact/common/share/share.js"></script>';
$options = Typecho_Widget::widget('Widget_Options');
$ajax_wx_share_url = Typecho_Common::url('/index.php/action/wx-share?do=ajax-get', $options->siteUrl);
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$signature_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// pjax
$signature_url = preg_replace('/\?_pjax=.*/','',$signature_url);
$version = self::_VERSION;
$wx_script = <<<SCRIPT
WX_Custom_Share = function(){
var xhr = null;
var url = '{$ajax_wx_share_url}';
var formData = {
title: '{$archive->title}',
parameter_type: '{$archive->parameter->type}',
cid: '{$archive->cid}',
signature_url: '{$signature_url}'
};
this.init = function(){
if( window.XMLHttpRequest ){
xhr = new XMLHttpRequest();
}
else if( window.ActiveXObject ){
xhr = new ActiveXObject('Microsoft.XMLHTTP');
};
get_share_info();
};
function formatPostData( obj ){
var arr = new Array();
for (var attr in obj ){
arr.push( encodeURIComponent( attr ) + '=' + encodeURIComponent( obj[attr] ) );
};
return arr.join( '&' );
};
function get_share_info(){
if( xhr == null ) return;
xhr.onreadystatechange = function(){
if( xhr.readyState == 4 && xhr.status == 200 ){
var data = eval('(' + xhr.responseText + ')');
if( data == null ){
return;
}
//console.log(data);
var info = {
title: data.wx_title,
summary: data.wx_description,
pic: data.wx_image,
url: data.wx_url
};
//info.url = data.wx_url;
if( data.error ){
console.error( data.error );
} else if( data.appId ){
info.WXconfig = {
swapTitleInWX: true,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature
};
};
setShareInfo( info );
}
};
xhr.open( 'POST', url, true);
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
xhr.send( formatPostData( formData ) );
};
};
new WX_Custom_Share().init();
console.log("%c", "padding:100px 200px;line-height:220px;background:url('https://hiphotos.baidu.com/feed/pic/item/b999a9014c086e06606a9d0009087bf40bd1cbbf.jpg') no-repeat;");
console.log("%c WeChatShare v{$version} %c By Fuzqing https://huangweitong.com ","color:#444;background:#eee;padding:5px 0;","color:#eee;background:#444;padding:5px 0;");
SCRIPT;
file_put_contents('usr/plugins/WeChatShare/wx_share.js',$wx_script);
$wx_script_js = Typecho_Common::url('/usr/plugins/WeChatShare/wx_share.js', $options->siteUrl);
echo '<script type="text/javascript">'.$wx_script.'</script>';
}
/*
* WeChatShare插件安装方法
*/
public static function wechatShareInstall()
{
$installDb = Typecho_Db::get();
$type = explode('_', $installDb->getAdapterName());
$type = array_pop($type);
$prefix = $installDb->getPrefix();
$scripts = file_get_contents('usr/plugins/WeChatShare/'.$type.'.sql');
$scripts = str_replace('typecho_', $prefix, $scripts);
$scripts = str_replace('%charset%', 'utf8', $scripts);
$scripts = explode(';', $scripts);
try {
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {
$installDb->query($script, Typecho_Db::WRITE);
}
}
return '建立微信分享数据表,插件启用成功,请去配置页面填写微信公众号APPID、密钥和默认图标url';
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
if(('Mysql' == $type && 1050 == $code) || ('Mysql' == $type && '42S01' == $code) ||
('SQLite' == $type && ('HY000' == $code || 1 == $code))) {
try {
$script = 'SELECT `wx_id`, `wx_title`, `wx_url`, `wx_image`, `wx_description` ,`cid` from `' . $prefix . 'wx_share`';
$installDb->query($script, Typecho_Db::READ);
return '检测到微信分享数据表,微信分享插件启用成功,请去配置页面填写微信公众号APPID、密钥和默认图标url';
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
throw new Typecho_Plugin_Exception('数据表检测失败,微信分享插件启用失败。错误号:'.$code);
}
} else {
throw new Typecho_Plugin_Exception('数据表建立失败,微信分享插件启用失败。错误号:'.$code);
}
}
}
}