forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
129 lines (108 loc) · 4.65 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
<?php
/**
* Sina 微博登陆插件
*
* @package Sinauth
* @author jimmy chaw
* @version 1.0.0 Beta
* @link http://x3d.cnblogs.com
*/
class Sinauth_Plugin implements Typecho_Plugin_Interface
{
private static $pluginName = 'Sinauth';
private static $tableName = 'users_oauth';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$meg = self::install();
Typecho_Plugin::factory('Widget_User')->___sinauthAuthorizeIcon = array('Sinauth_Plugin', 'authorizeIcon');
Helper::addAction('sinauthAuthorize', 'Sinauth_AuthorizeAction');
Helper::addRoute('sinauthAuthorize', '/sinauthAuthorize/', 'Sinauth_AuthorizeAction', 'action');
Helper::addRoute('sinauthCallback', '/sinauthCallback/', 'Sinauth_AuthorizeAction', 'callback');
Helper::addPanel(1, 'Sinauth/panel.php', 'Sinauth', 'Sinauth用户管理', 'administrator');
return _t($meg.'。请进行<a href="options-plugin.php?config='.self::$pluginName.'">初始化设置</a>');
}
public static function install()
{
$installDb = Typecho_Db::get();
$type = array_pop(explode('_',$installDb->getAdapterName()));
$prefix = $installDb->getPrefix();
$oauthTable = $prefix. self::$tableName;
try {
$installDb->query("CREATE TABLE `$oauthTable` (
`moid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`plateform` varchar(45) NOT NULL DEFAULT 'sina',
`uid` int(10) unsigned NOT NULL,
`openid` varchar(80) NOT NULL,
`bind_time` int(10) unsigned NOT NULL,
`expires_in` int(10) unsigned DEFAULT NULL,
`refresh_token` varchar(300) DEFAULT NULL,
PRIMARY KEY (`moid`),
KEY `uid` (`uid`),
KEY `plateform` (`plateform`),
KEY `openid` (`openid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");
return('表创建成功, 插件已经被激活!');
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
if(('Mysql' == $type && (1050||'42S01') == $code)) {
$script = 'SELECT `moid` from `' . $oauthTable . '`';
$installDb->query($script, Typecho_Db::READ);
return '数据表已存在,插件启用成功';
} else {
throw new Typecho_Plugin_Exception('数据表'.$oauthTable.'建立失败,插件启用失败。错误号:'.$code);
}
}
}
//在前台登陆页面增加oauth跳转图标
public static function authorizeIcon() {
return '<a href="' . Helper::options()->index . '/sinauthAuthorize">新浪登陆</a>';
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){
Helper::removeRoute('sinauthAuthorize');
Helper::removeRoute('sinauthCallback');
Helper::removeAction('sinauthAuthorize');
Helper::removePanel(1, 'Sinauth/panel.php');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$client_id = new Typecho_Widget_Helper_Form_Element_Text('client_id', NULL,'', _t('App Key'),'请在微博开放平台查看http://open.weibo.com');
$form->addInput($client_id);
$client_secret = new Typecho_Widget_Helper_Form_Element_Text('client_secret', NULL,'', _t('App Secret'),'请在微博开放平台查看http://open.weibo.com');
$form->addInput($client_secret);
$callback_url = new Typecho_Widget_Helper_Form_Element_Text('callback_url', NULL,'http://', _t('回调地址'),'请与微博开放平台中设置一致');
$form->addInput($callback_url);
//$callback_url = new Typecho_Widget_Helper_Form_Element_Text('email_domain', NULL,'v.sina.com', _t('虚拟email后缀'),'创建用户帐号时构造一个虚拟email,如[email protected]');
//$form->addInput($callback_url);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){
}
}