forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
157 lines (141 loc) · 4.71 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 用来管理插件,可以下载,安装,卸载插件
*
* @category system
* @package TeStore
* @author zhulin3141
* @version 1.0.0
* @link http://zhulin31410.blog.163.com/
*/
class TeStore_Plugin implements Typecho_Plugin_Interface
{
//默认应用数据来源URL
private static $defaultServer = 'http://teck.vipsinaapp.com/';
//临时目录
private static $tempPath = '/.tmp/';
//数据目录
private static $dataPath = '/data/';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$tempDir = dirname(__FILE__) . self::$tempPath;
$dataDir = dirname(__FILE__) . self::$dataPath;
if ( ! file_exists($tempDir) and ! @mkdir($tempDir) ) {
throw new Typecho_Plugin_Exception('无法创建临时目录.');
}
if( ! self::testWrite($tempDir) ){
throw new Typecho_Plugin_Exception('.tmp目录没有写入的权限');
}
if ( ! file_exists($dataDir) and ! @mkdir($dataDir) ) {
throw new Typecho_Plugin_Exception('无法创建数据目录.');
}
if( ! self::testWrite($dataDir) ){
throw new Typecho_Plugin_Exception('data目录没有写入的权限');
}
Typecho_Plugin::factory('admin/menu.php')->navBar = array('TeStore_Plugin', 'render');
Helper::addPanel(1, 'TeStore/market.php', 'TE应用商店', 'TE应用商店', 'administrator');
Helper::addRoute('te-store_market', __TYPECHO_ADMIN_DIR__ . 'te-store/market', 'TeStore_Action', 'market');
Helper::addRoute('te-store_install', __TYPECHO_ADMIN_DIR__ . 'te-store/install', 'TeStore_Action', 'install');
Helper::addRoute('te-store_uninstall', __TYPECHO_ADMIN_DIR__ . 'te-store/uninstall', 'TeStore_Action', 'uninstall');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){
Helper::removePanel(1, 'TeStore/market.php');
Helper::removeRoute('te-store_market');
Helper::removeRoute('te-store_install');
Helper::removeRoute('te-store_uninstall');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
//应用服务器地址
$server = new Typecho_Widget_Helper_Form_Element_Text('server', NULL, self::$defaultServer, _t('应用服务器地址'));
$server->addRule('required',_t('应用服务器地址不能为空'));
$form->addInput($server);
//缓存设置
$cache = new Typecho_Widget_Helper_Form_Element_Select('cache_time',
array(
'0'=>_t('不缓存'),
'0.5'=>_t('半小时'),
'1'=>_t('1小时'),
'12'=>_t('12小时'),
'24'=>_t('24小时'),
),
'1',
_t('缓存时间'),
'应用数据的缓存时间'
);
$form->addInput($cache);
$showNavMenu = new Typecho_Widget_Helper_Form_Element_Radio(
'showNavMenu' ,
array(
'true' => '是',
'false' => '否',
),
'true' ,
_t('是否显示菜单栏按钮')
);
$form->addInput($showNavMenu);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function render()
{
$options = Helper::options();
$pluginOpts = Typecho_Widget::widget('Widget_Options')->plugin('TeStore');
if( $pluginOpts->showNavMenu == 'true' ){
echo '<a href="';
$options->adminUrl('extending.php?panel=TeStore%2Fmarket.php');
echo '">TE应用商店</a>';
}
}
/**
* 判断目录是否可写
*/
public static function testWrite($dir) {
$testFile = "_test.txt";
$fp = @fopen($dir . "/" . $testFile, "w");
if (!$fp) {
return false;
}
fclose($fp);
$rs = @unlink($dir . "/" . $testFile);
if ($rs) {
return true;
}
return false;
}
}