forked from baiwulin/ThirdLogin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
199 lines (190 loc) · 7.17 KB
/
Plugin.php
File metadata and controls
199 lines (190 loc) · 7.17 KB
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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* Typecho第三方账号登录插件
*
* @package ThirdLogin
* @author 白雾林
* @version 1.0
* @link https://www.baiwulin.com/
*/
class ThirdLogin_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$info = self::installDb();
//建立oauth路由
Helper::addRoute('oauth', '/oauth', 'ThirdLogin_Widget', 'oauth');
Helper::addRoute('oauth_callback', '/oauth_callback', 'ThirdLogin_Widget', 'callback');
return _t($info);
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeRoute('oauth');
Helper::removeRoute('oauth_callback');
//删除数据表
return self::removeTable();
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$config = require_once 'config.php';
$text = $html ='';
$text.= "互联配置示例 | 网站回调域 | 平台名称"."\r\n";
$text.= "-|-|-"."\r\n";
$num = 0;
foreach ($config as $k => $v) {
$num++;
$type = strtolower(substr($k, 10));
$text.= $type.':APP_KEY,APP_SECRET,'.$v['NAME'].' | '.$v['CALLBACK'].' | '.$v['NAME']."\r\n";
}
$html = Markdown::convert($text);
//互联配置
$connect = new Typecho_Widget_Helper_Form_Element_Textarea('connect', null, null, _t('互联配置'), _t('文本形式,一行一个账号系统配置,目前共支持'.$num.'种第三方登录!<br/>
您可以复制对应的互联配置示例,把<strong class="warning">APP_KEY</strong>和<strong class="warning">APP_SECRET</strong>改成您申请的参数,粘贴到上方配置框。<br/>
最后,复制对应的网站回调域,粘贴到第三方开发平台的网站回调域设置中。'.$html));
$form->addInput($connect);
//强制绑定
$custom = new Typecho_Widget_Helper_Form_Element_Radio('custom', array(1=>_t('是'),0=>'否'), 0, _t('是否需要完善资料'), _t('用户使用社会化登录后,是否需要完善昵称、邮箱等信息;选择不需要完善资料则直接使用获取到的昵称。'));
$form->addInput($custom);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
/**
* 安装数据库
*/
public static function installDb()
{
try {
return self::addTable();
} catch (Typecho_Db_Exception $e) {
if ('42S01' == $e->getCode()) {
$msg = '数据表third_login已存在!';
return $msg;
}
}
}
//添加数据表
public static function addTable()
{
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if ("Pdo_Mysql" === $db->getAdapterName() || "Mysql" === $db->getAdapterName()) {
$sql = "CREATE TABLE IF NOT EXISTS `{$prefix}third_login` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
`uuid` int(10) unsigned NOT NULL DEFAULT '0',
`type` char(32) NOT NULL,
`openid` char(50) NOT NULL,
`access_token` varchar(255) NOT NULL DEFAULT '0' COMMENT '用户对应access_token',
`expires_in` int(10) unsigned NOT NULL DEFAULT '0',
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后登录',
`name` varchar(38) NOT NULL DEFAULT '0',
`nickname` varchar(38) NOT NULL DEFAULT '0',
`gender` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '性别0未知,1男,2女',
`head_img` varchar(255) NOT NULL DEFAULT '0' COMMENT '头像',
`refresh_token` varchar(255) NOT NULL DEFAULT '0' COMMENT '刷新有效期token',
PRIMARY KEY (`id`),
KEY `uuid` (`uuid`),
KEY `uid` (`uid`),
KEY `type` (`type`),
KEY `openid` (`openid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
$db->query($sql);
} else {
throw new Typecho_Plugin_Exception(_t('对不起, 本插件仅支持MySQL数据库。'));
}
return "数据表third_login安装成功!";
}
//删除数据表
public static function removeTable()
{
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
try {
$db->query("DROP TABLE `" . $prefix . "third_login`", Typecho_Db::WRITE);
} catch (Typecho_Exception $e) {
return "删除third_login表失败!";
}
return "删除third_login表成功!";
}
//在前端调用显示登录按钮
public static function show($text = false)
{
if ($text) {
//文本样式
$format= '<a href="{url}" title="{title}">{title}</a>';
} else {
//登录按钮样式
$format= '<a href="{url}"><img src="/usr/plugins/ThirdLogin/login_ico/{type}.png" alt="{type}-{title}" style="margin-top: 0.8em;"></a>';
}
$list = self::options();
if (empty($list)) {
return '';
}
$html = '';
foreach ($list as $type=>$v) {
$url = Typecho_Common::url('/oauth?type='.$type, Typecho_Widget::Widget('Widget_Options')->index);
$html .= str_replace(
array('{type}','{title}','{url}'),
array($type,$v['title'],$url),
$format
);
}
echo $html;
}
//读取插件配置,返回数组
public static function options($type='')
{
static $options = array();
if (empty($options)) {
$connect = Typecho_Widget::Widget('Widget_Options')->plugin('ThirdLogin')->connect;
$connect = preg_split('/[;\r\n]+/', trim($connect, ",;\r\n"));
foreach ($connect as $v) {
$v = explode(':', $v);
if (isset($v[1])) {
$tmp = explode(',', $v[1]);
}
if (isset($tmp[1])) {
$options[strtolower($v[0])] = array(
'id'=>trim($tmp[0]),
'key'=>trim($tmp[1]),
'title'=>isset($tmp[2]) ? $tmp[2] : $v[0]
);
}
}
}
return empty($type) ? $options : (isset($options[$type]) ? $options[$type] : array());
}
}