forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Plugin.php
126 lines (110 loc) · 3.62 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
<?php
/**
* 用户个性签名
*
* @package UserSign
* @author hmoe
* @version 0.0.1
* @dependence 10.8.15-*
* @link https://github.com/hmoe/typechoSignPlugin
*
*/
class UserSign_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$info = UserSign_Plugin::sqlInstall();
// Typecho_Plugin::factory('Widget_Users_Profile')->personalConfigHandle = array('UserSign_Plugin', 'personalConfigHandle');
// Typecho_Plugin::factory('Widget_Archive')->select = array('UserSign_Plugin', 'selectHandle');
return _t($info);
}
//SQL创建
public static function sqlInstall()
{
$db = Typecho_Db::get();
$type = explode('_', $db->getAdapterName());
$type = array_pop($type);
$prefix = $db->getPrefix();
try {
$select = $db->select('table.users.userSign')->from('table.users');
$db->query($select);
return '检测到个性签名字段,插件启用成功';
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
if(('Mysql' == $type && (0 == $code ||1054 == $code || $code == '42S22')) ||
('SQLite' == $type && ('HY000' == $code || 1 == $code))) {
try {
if ('Mysql' == $type) {
$db->query("ALTER TABLE `".$prefix."users` ADD `userSign` VARCHAR( 255 ) DEFAULT '' COMMENT '用户个性签名';");
} else if ('SQLite' == $type) {
$db->query("ALTER TABLE `".$prefix."users` ADD `userSign` VARCHAR( 10 ) DEFAULT ''");
} else {
throw new Typecho_Plugin_Exception('不支持的数据库类型:'.$type);
}
return '建立个性签名字段,插件启用成功';
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
if(('Mysql' == $type && 1060 == $code) ) {
return '个性签名已经存在,插件启用成功';
}
throw new Typecho_Plugin_Exception('个性签名插件启用失败。错误号:'.$code);
}
}
throw new Typecho_Plugin_Exception('数据表检测失败,个性签名插件启用失败。错误号:'.$code);
}
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){
$db = Typecho_Db::get();
$user = Typecho_Widget::widget('Widget_User');
$user->execute();
$res = $db->fetchRow($db->select('table.users.userSign')->from('table.users')->where('uid = ?', $user->uid));
$sign = new Typecho_Widget_Helper_Form_Element_Text('userSign', NULL, $res['userSign'], _t('个性签名'));
$form->addInput($sign);
}
public static function personalConfigHandle($settings,$isSetup){
$db = Typecho_Db::get();
if($isSetup)
{
Typecho_Widget::widget('Widget_Abstract_Options')->insert(array(
'name' => '_plugin:UserSign',
'value' => serialize($settings),
'user' => 0
));
}
$user = Typecho_Widget::widget('Widget_User');
$user->execute();
$db->query($db->sql()->where('uid = ?', $user->uid)->update('table.users')->rows(array('userSign'=>Typecho_Common::removeXSS($settings['userSign']))));
}
}