Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
265 lines (242 loc) · 9.32 KB
/
Copy pathPlugin.php
File metadata and controls
265 lines (242 loc) · 9.32 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 读取Github上维护的专用表格实现插件仓库各项功能
*
* @package TeStore
* @author 羽中, zhulin3141, Ryan
* @version 1.1.7
* @dependence 13.12.12-*
* @link https://www.yzmb.me/archives/net/testore-for-typecho
* @copyright Copyright (c) 2014-2020 Yuzhong Zheng (jzwalk)
* @license MIT
*/
class TeStore_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$tempDir = __TYPECHO_ROOT_DIR__.__TYPECHO_PLUGIN_DIR__.'/TeStore/.tmp';
$dataDir = __TYPECHO_ROOT_DIR__.__TYPECHO_PLUGIN_DIR__.'/TeStore/data';
if (!class_exists('ZipArchive')) {
return _t('主机未安装ZipArchive扩展, 无法安装插件');
}
if (!is_dir($tempDir) && !@mkdir($tempDir)) {
throw new Typecho_Plugin_Exception('无法创建临时目录.');
}
if(!self::testWrite($tempDir)){
throw new Typecho_Plugin_Exception('.tmp目录没有写入权限');
}
Typecho_Plugin::factory('admin/menu.php')->navBar = array('TeStore_Plugin','render');
Helper::addPanel(1,'TeStore/market.php',_t('TE插件仓库'),_t('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)
{
$settings = NULL;
try {
$settings = Helper::options()->plugin('TeStore');
} catch (Exception $e) {
// 首次启用时配置记录尚未创建,使用表单默认值。
}
$mirrorType = '';
$mirrorEndpoint = 'https://cdn.jsdelivr.net';
$mirrorCustom = '';
if (isset($settings->mirror_type) || isset($settings->mirror_endpoint) || isset($settings->mirror_custom)) {
$mirrorType = $settings->mirror_type;
$mirrorEndpoint = isset($settings->mirror_endpoint) ? $settings->mirror_endpoint : $mirrorEndpoint;
$mirrorCustom = isset($settings->mirror_custom) ? $settings->mirror_custom : '';
} elseif (isset($settings->proxy) && $settings->proxy) {
if ($settings->proxy === 'cdn.jsdelivr.net/gh') {
$mirrorType = 'cdn';
} elseif ($settings->proxy === 'jsd.onmicrosoft.cn/gh') {
$mirrorType = 'cdn';
$mirrorEndpoint = 'https://cdn.jsdmirror.cn';
} else {
$mirrorType = 'proxy';
$mirrorEndpoint = 'custom';
$mirrorCustom = rtrim($settings->proxy, '/');
}
}
$source = new Typecho_Widget_Helper_Form_Element_Textarea('source',
NULL,'https://github.com/typecho-fans/plugins/blob/master/TESTORE.md'.PHP_EOL.'https://github.com/typecho-fans/plugins/blob/master/README.md',_t('插件信息来源'),
_t('应为可公开访问且包含符合本插件规定表格内容的页面地址, 每行一个, 默认: ').'<br/>
<strong><a href="https://github.com/typecho-fans/plugins/blob/master/README.md">https://github.com/typecho-fans/plugins/blob/master/README.md</a> - <span class="warning">'._t('Typecho-Fans内部插件索引(社区维护版列表)').'</span><br/>
<a href="https://github.com/typecho-fans/plugins/blob/master/TESTORE.md">https://github.com/typecho-fans/plugins/blob/master/TESTORE.md</a> - <span class="warning">'._t('Typecho-Fans外部插件登记表(TeStore专用)').'</span></strong><br/>
'._t('以上Markdown语法文件在Github上由多人共同维护, 参与方式详见文件说明'));
$source->addRule('required',_t('文件地址不能为空'));
$form->addInput($source);
$cache = new Typecho_Widget_Helper_Form_Element_Select('cache_time',
array(
'0'=>_t('不缓存'),
'6'=>_t('6小时'),
'12'=>_t('12小时'),
'24'=>_t('1天'),
'72'=>_t('3天'),
'168'=>_t('1周')
),
'24',_t('数据缓存时限'),_t('设置本地缓存数据时间'));
$form->addInput($cache);
$mirrorTypeInput = new Typecho_Widget_Helper_Form_Element_Radio('mirror_type',
array(
''=>_t('关闭'),
'cdn'=>_t('jsDelivr兼容CDN'),
'proxy'=>_t('URL前缀代理')
),
$mirrorType,_t('镜像类型'),_t('CDN仅加速GitHub仓库文件;URL前缀代理可加速所有HTTP(S)信息源和插件下载'));
$form->addInput($mirrorTypeInput);
$mirrorEndpointInput = new Typecho_Widget_Helper_Form_Element_Select('mirror_endpoint',
array(
'https://cdn.jsdelivr.net'=>_t('CDN - cdn.jsdelivr.net'),
'https://gcore.jsdelivr.net'=>_t('CDN - gcore.jsdelivr.net'),
'https://cdn.jsdmirror.cn'=>_t('CDN - cdn.jsdmirror.cn'),
'https://cdn.jsdmirror.com'=>_t('CDN - cdn.jsdmirror.com'),
'https://gh-proxy.org'=>_t('代理 - gh-proxy.org'),
'https://cdn.gh-proxy.org'=>_t('代理 - cdn.gh-proxy.org'),
'https://box.w0x7ce.eu/proxy'=>_t('代理 - box.w0x7ce.eu/proxy'),
'custom'=>_t('自定义地址')
),
$mirrorEndpoint,_t('镜像节点'),_t('请选择与镜像类型一致的节点;请求失败时自动回退原地址'));
$form->addInput($mirrorEndpointInput);
$mirrorCustomInput = new Typecho_Widget_Helper_Form_Element_Text('mirror_custom',
NULL,$mirrorCustom,_t('自定义镜像地址'),_t('仅在镜像节点选择“自定义地址”时生效,必须使用HTTPS;CDN地址可带或不带/gh'));
$form->addInput($mirrorCustomInput);
// 保留旧字段供Typecho回填,保存时由configHandle移除。
$form->addInput(new Typecho_Widget_Helper_Form_Element_Hidden('proxy', NULL, ''));
$curl = new Typecho_Widget_Helper_Form_Element_Checkbox('curl',
array(1=>'是'),0, _t('cURL方式下载'),_t('默认方式无效时可尝试'));
$form->addInput($curl);
$showNavMenu = new Typecho_Widget_Helper_Form_Element_Radio('showNavMenu',
array(1=>_t('显示'),0=>_t('关闭')),1,_t('导航快捷按钮'));
$form->addInput($showNavMenu);
}
/**
* 检查cURL支持
*
* @param array $settings
* @return string
*/
public static function configCheck(array $settings)
{
if (!class_exists('ZipArchive')) {
return _t('主机未安装ZipArchive扩展, 无法安装插件');
}
if (!empty($settings['curl']) && !extension_loaded('curl')) {
return _t('主机未安装cURL扩展, 无法使用此方式下载');
}
$mirrorType = isset($settings['mirror_type']) ? $settings['mirror_type'] : '';
$endpoint = isset($settings['mirror_endpoint']) ? $settings['mirror_endpoint'] : '';
$custom = isset($settings['mirror_custom']) ? trim($settings['mirror_custom']) : '';
$cdnEndpoints = array(
'https://cdn.jsdelivr.net',
'https://gcore.jsdelivr.net',
'https://cdn.jsdmirror.cn',
'https://cdn.jsdmirror.com'
);
$proxyEndpoints = array(
'https://gh-proxy.org',
'https://cdn.gh-proxy.org',
'https://box.w0x7ce.eu/proxy'
);
if ($mirrorType !== '' && $mirrorType !== 'cdn' && $mirrorType !== 'proxy') {
return _t('镜像类型无效');
}
if ($mirrorType === 'cdn' && $endpoint !== 'custom' && !in_array($endpoint, $cdnEndpoints, true)) {
return _t('请选择jsDelivr兼容CDN节点');
}
if ($mirrorType === 'proxy' && $endpoint !== 'custom' && !in_array($endpoint, $proxyEndpoints, true)) {
return _t('请选择URL前缀代理节点');
}
if ($mirrorType && $endpoint === 'custom') {
$parts = @parse_url($custom);
if (!$parts || !isset($parts['scheme']) || strtolower($parts['scheme']) !== 'https'
|| empty($parts['host']) || isset($parts['query']) || isset($parts['fragment'])
|| isset($parts['user']) || isset($parts['pass'])) {
return _t('自定义镜像地址必须是无查询参数的HTTPS地址');
}
}
}
/**
* 保存配置并清理旧版 proxy 字段
*
* @param array $settings
* @param boolean $isInit
* @return void
*/
public static function configHandle(array $settings, $isInit)
{
unset($settings['proxy']);
Helper::configPlugin('TeStore', $settings);
}
/**
* 个人用户的配置面板
*
* @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();
if ($options->plugin('TeStore')->showNavMenu && Typecho_Widget::widget('Widget_User')->pass('administrator',true)){
echo '<a href="'.$options->adminUrl.'extending.php?panel=TeStore%2Fmarket.php"><span class="message notice"><i class="mime-script"></i>'._t('TE插件仓库').'</span></a>';
}
}
/**
* 判断目录可写
*
* @access public
* @return boolean
*/
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;
}
}