-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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'])))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
## typecho 用户个性签名插件 | ||
|
||
### 安装方法: | ||
在插件目录下面建立UserSign文件夹,把Plugin.php复制进去 | ||
|
||
**注意:由于typecho的问题,插件的用户配置在读取的时候会自动读取在配置表里面的插件数据。如果要让用户的设置正常运行请修改 '/var/Widget/Users/Profile.php'。修改下面这个函数。不修改的话用户的个人页面里面显示用户的当前签名** | ||
|
||
/** | ||
* 输出自定义设置选项 | ||
* | ||
* @access public | ||
* @param string $pluginName 插件名称 | ||
* @param string $className 类名称 | ||
* @param string $pluginFileName 插件文件名 | ||
* @param string $group 用户组 | ||
* @return Typecho_Widget_Helper_Form | ||
*/ | ||
public function personalForm($pluginName, $className, $pluginFileName, &$group) | ||
{ | ||
/** 构建表格 */ | ||
$form = new Typecho_Widget_Helper_Form($this->security->getIndex('/action/users-profile'), | ||
Typecho_Widget_Helper_Form::POST_METHOD); | ||
$form->setAttribute('name', $pluginName); | ||
$form->setAttribute('id', $pluginName); | ||
|
||
require_once $pluginFileName; | ||
$group = call_user_func(array($className, 'personalConfig'), $form); | ||
$group = $group ? $group : 'subscriber'; | ||
|
||
$options = $this->options->personalPlugin($pluginName); | ||
|
||
if (!empty($options)) { | ||
foreach ($options as $key => $val) { | ||
if(!isset($form->getInput($key)->value)) | ||
$form->getInput($key)->value($val); | ||
} | ||
} | ||
|
||
$form->addItem(new Typecho_Widget_Helper_Form_Element_Hidden('do', NULL, 'personal')); | ||
$form->addItem(new Typecho_Widget_Helper_Form_Element_Hidden('plugin', NULL, $pluginName)); | ||
$submit = new Typecho_Widget_Helper_Form_Element_Submit(NULL, NULL, _t('保存设置')); | ||
$submit->input->setAttribute('class', 'btn primary'); | ||
$form->addItem($submit); | ||
return $form; | ||
} | ||
|
||
###使用方法 | ||
|
||
由于已经在users表里面插入了userSign这个字段了,这个字段会自动的被系统读取。只需要在需要的地方输出$user->userSign就可以了 |