-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPlugin.php
More file actions
223 lines (198 loc) · 6.89 KB
/
Copy pathPlugin.php
File metadata and controls
223 lines (198 loc) · 6.89 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
use Typecho\Plugin\PluginInterface;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Options;
use Typecho\Common;
use Typecho\Widget\Helper\Layout;
/**
* S3 协议上传插件
*
* @package S3Upload
* @author 老孙
* @version 1.3.3
* @link https://www.imsun.org
* @dependence 1.3-*
*/
class S3Upload_Plugin implements PluginInterface
{
/**
* 激活插件方法
*/
public static function activate()
{
// 检查依赖
$warnings = self::checkDependencies();
// 注册钩子 - 使用新的Typecho 1.3.0方式
\Typecho\Plugin::factory('Widget\Upload')->uploadHandle = ['S3Upload_FileHandler', 'uploadHandle'];
\Typecho\Plugin::factory('Widget\Upload')->modifyHandle = ['S3Upload_FileHandler', 'modifyHandle'];
\Typecho\Plugin::factory('Widget\Upload')->deleteHandle = ['S3Upload_FileHandler', 'deleteHandle'];
\Typecho\Plugin::factory('Widget\Upload')->attachmentHandle = ['S3Upload_FileHandler', 'attachmentHandle'];
\Typecho\Plugin::factory('Widget\Upload')->attachmentDataHandle = ['S3Upload_FileHandler', 'attachmentDataHandle'];
$message = _t('插件已经激活,请设置 S3 配置信息');
if (!empty($warnings)) {
$message .= '<br/>' . implode('<br/>', $warnings);
}
return $message;
}
/**
* 检查依赖
*/
private static function checkDependencies()
{
if (!extension_loaded('curl')) {
throw new \Typecho\Plugin\Exception(_t('PHP cURL 扩展未安装,插件无法上传文件,请先安装并启用 cURL 扩展'));
}
$warnings = [];
if (!extension_loaded('gd')) {
$warnings[] = _t('提醒:PHP GD 扩展未安装,图片压缩功能将不可用');
}
return $warnings;
}
/**
* 禁用插件方法
*/
public static function deactivate()
{
return _t('插件已被禁用');
}
/**
* 获取插件配置面板
*/
public static function config(Form $form)
{
// S3基本设置
$endpoint = new \Typecho\Widget\Helper\Form\Element\Text(
'endpoint',
null,
's3.amazonaws.com',
_t('S3 Endpoint'),
_t('S3 服务器地址,例如:s3.amazonaws.com')
);
$form->addInput($endpoint->addRule('required', _t('必须填写 Endpoint')));
$bucket = new \Typecho\Widget\Helper\Form\Element\Text(
'bucket',
null,
'',
_t('Bucket'),
_t('存储桶名称')
);
$form->addInput($bucket->addRule('required', _t('必须填写 Bucket')));
$region = new \Typecho\Widget\Helper\Form\Element\Text(
'region',
null,
'us-east-1',
_t('Region'),
_t('区域,例如:us-east-1')
);
$form->addInput($region->addRule('required', _t('必须填写 Region')));
$accessKey = new \Typecho\Widget\Helper\Form\Element\Text(
'accessKey',
null,
'',
_t('Access Key'),
_t('访问密钥 ID')
);
$form->addInput($accessKey->addRule('required', _t('必须填写 Access Key')));
$secretKey = new \Typecho\Widget\Helper\Form\Element\Text(
'secretKey',
null,
'',
_t('Secret Key'),
_t('访问密钥密码')
);
$form->addInput($secretKey->addRule('required', _t('必须填写 Secret Key')));
// CDN设置
$customDomain = new \Typecho\Widget\Helper\Form\Element\Text(
'customDomain',
null,
'',
_t('自定义域名'),
_t('设置自定义域名,例如:cdn.example.com(不要包含 http:// 或 https://)')
);
$form->addInput($customDomain);
$useHttps = new \Typecho\Widget\Helper\Form\Element\Radio(
'useHttps',
[
'true' => _t('使用'),
'false' => _t('不使用'),
],
'true',
_t('使用HTTPS'),
_t('是否使用HTTPS协议')
);
$form->addInput($useHttps);
// 高级设置
$customPath = new \Typecho\Widget\Helper\Form\Element\Text(
'customPath',
null,
'/',
_t('自定义路径前缀'),
_t('设置文件存储路径前缀,例如:uploads/(以/结尾)')
);
$form->addInput($customPath);
$saveLocal = new \Typecho\Widget\Helper\Form\Element\Radio(
'saveLocal',
[
'true' => _t('保存'),
'false' => _t('不保存'),
],
'false',
_t('保存本地备份'),
_t('是否在本地保存文件备份')
);
$form->addInput($saveLocal);
$urlStyle = new \Typecho\Widget\Helper\Form\Element\Radio(
'urlStyle',
[
'path' => _t('路径形式'),
'virtual' => _t('虚拟主机形式'),
],
'path',
_t('URL访问方式'),
_t('路径形式:http(s)://endpoint/bucket/object<br/>虚拟主机形式:http(s)://bucket.endpoint/object')
);
$form->addInput($urlStyle);
// 图片压缩设置
$compressImages = new \Typecho\Widget\Helper\Form\Element\Radio(
'compressImages',
[
'1' => _t('启用'),
'0' => _t('禁用'),
],
'0',
_t('图片压缩'),
_t('是否对上传的图片进行自动压缩')
);
$form->addInput($compressImages);
$compressQuality = new \Typecho\Widget\Helper\Form\Element\Text(
'compressQuality',
null,
'85',
_t('压缩质量'),
_t('图片压缩质量 (1-100),数值越大质量越好但文件越大')
);
$compressQuality->addRule('isInteger', _t('请输入整数'));
$compressQuality->addRule('min', _t('请输入不小于1的数字'), 1);
$compressQuality->addRule('max', _t('请输入不大于100的数字'), 100);
$form->addInput($compressQuality);
// SSL证书验证设置
$sslVerify = new \Typecho\Widget\Helper\Form\Element\Radio(
'sslVerify',
[
'true' => _t('启用'),
'false' => _t('禁用'),
],
'false',
_t('SSL证书验证'),
_t('是否验证S3服务器的SSL证书。如果上传失败且服务器SSL证书配置有问题,可以尝试禁用此选项')
);
$form->addInput($sslVerify);
}
/**
* 个人用户的配置面板
*/
public static function personalConfig(Form $form)
{
}
}