-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
349 lines (307 loc) · 11.2 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* 腾讯对象储存
*
* @package Cosv5
* @author 南博工作室
* @version 1.0.0
* @link https://github.com/krait-team/Cosv5-typecho
*/
class Cosv5_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return string
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = ['Cosv5_Plugin', 'uploadHandle'];
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = ['Cosv5_Plugin', 'modifyHandle'];
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = ['Cosv5_Plugin', 'deleteHandle'];
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = ['Cosv5_Plugin', 'attachmentHandle'];
Typecho_Plugin::factory('Widget_Upload')->attachmentDataHandle = ['Cosv5_Plugin', 'attachmentDataHandle'];
return _t("Cosv5 插件启动成功");
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return string
*/
public static function deactivate()
{
return _t("Cosv5 已被禁用");
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$secretId = new Typecho_Widget_Helper_Form_Element_Text('secretId',
NULL, NULL,
_t('SecretId'),
_t('<a href="https://console.qcloud.com/cam/capi" target="_blank">获取SecretId</a>'));
$form->addInput($secretId->addRule('required', _t('SecretId不能为空!')));
$secretKey = new Typecho_Widget_Helper_Form_Element_Text('secretKey',
NULL, NULL,
_t('SecretKey'),
_t('<a href="https://console.qcloud.com/cam/capi" target="_blank">获取SecretKey</a>'));
$form->addInput($secretKey->addRule('required', _t('SecretKey不能为空!')));
$region = new Typecho_Widget_Helper_Form_Element_Radio('region', [
'ap-beijing-1' => _t('北京一区(华北)'),
'ap-beijing' => _t('北京'),
'ap-shanghai' => _t('上海(华东)'),
'ap-guangzhou' => _t('广州(华南)'),
'ap-chengdu' => _t('成都(西南)'),
'ap-chongqing' => _t('重庆'),
'ap-singapore' => _t('新加坡'),
'ap-hongkong' => _t('香港'),
'na-toronto' => _t('多伦多'),
'eu-frankfurt' => _t('法兰克福'),
'ap-mumbai' => _t('孟买'),
'ap-seoul' => _t('首尔'),
'na-siliconvalley' => _t('硅谷'),
'na-ashburn' => _t('弗吉尼亚'),
'ap-shenzhen-fsi' => _t('深圳金融'),
'ap-shanghai-fsi' => _t('上海金融'),
'ap-beijing-fsi' => _t('北京金融'),
], 'ap-shanghai', _t('选择bucket节点'));
$form->addInput($region->addRule('required', _t('bucket节点不能为空!')));
$bucket = new Typecho_Widget_Helper_Form_Element_Text('bucket',
NULL, NULL, '存储桶名称',
_t('<a href="https://console.cloud.tencent.com/cos5/bucket" target="_blank">获取存储桶名称</a> 例如: bucket-123456'));
$form->addInput($bucket->addRule('required', _t('Bucket名称不能为空!')));
$uploadDir = new Typecho_Widget_Helper_Form_Element_Text('uploadDir',
NULL, Widget_Upload::UPLOAD_DIR, '存储基础路径',
_t('填写上传的基础路径, 比如:' . Widget_Upload::UPLOAD_DIR));
$form->addInput($uploadDir);
$uploadPath = new Typecho_Widget_Helper_Form_Element_Text('uploadPath',
NULL, '{basedir}/{year}/{month}/{filename}', '存储相对路径',
_t('填写上传的相对路径<br>{basedir} 存储基础路径; {year} 年份; {month} 月份; {filename} 文件名 <br>比如: {basedir}/{year}/{month}/{filename}<br>表示例如 ' . Widget_Upload::UPLOAD_DIR . '/2021/03/03/123456.png'));
$form->addInput($uploadPath);
$uploadFilename = new Typecho_Widget_Helper_Form_Element_Text('uploadFilename',
NULL, '{randname}', '存储文件名字',
_t('填写上传的存储文件名字<br>{randname} 随机数字名字; {basename} 原文件名字'));
$form->addInput($uploadFilename);
$domain = new Typecho_Widget_Helper_Form_Element_Text('domain',
NULL, 'https://',
_t('自定义域名'),
_t('使用的域名,必填,请带上http://或https://,可使用默认域名或自定义域名<br>在bucket中的域名管理, 默认域名形如:http://bucket-123456.cos.ap-shanghai.myqcloud.com<br>自定义域名形如:https://cos.tencent.com'));
$form->addInput($domain->addRule('required', _t('cos域名不能为空!')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
/**
* 上传文件处理函数
*
* @access public
* @param array $file 上传的文件
* @param null $path
* @return mixed
* @throws Typecho_Plugin_Exception
*/
public static function uploadHandle($file, $path = NULL)
{
// check name
// exist in Widget_Upload::uploadHandle
$ext = Cosv5_Plugin::getSafeName($file['name']);
if (!Widget_Upload::checkFileType($ext)) {
return false;
}
// body
if (isset($file['tmp_name'])) {
$tmp_open = fopen($file['tmp_name'], "rb");
$body = fread($tmp_open, $file['size']);
fclose($tmp_open);
} else if (isset($file['bytes'])) {
// check
if (!isset($file['mime'])) {
return false;
}
$body = $file['bytes'];
} else {
return false;
}
$date = new Typecho_Date();
$option = Helper::options()->plugin("Cosv5");
if (empty($path)) {
$randname = sprintf('%u', crc32(uniqid())) . '.' . $ext;
$filename = trim(str_replace(
['{basename}', '{randname}'],
[$file['name'], $randname],
$option->uploadFilename
));
if (empty($filename)) {
$filename = $randname;
}
$path = trim(str_replace(
['{basedir}', '{year}', '{month}', '{filename}'],
[rtrim($option->uploadDir, '/'), $date->year, $date->month, $filename],
$option->uploadPath
));
if (empty($path)) {
$path = (defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : Widget_Upload::UPLOAD_DIR)
. '/' . $date->year . '/' . $date->month . '/' . $filename;
}
}
// client
$client = Cosv5_Plugin::getClient($option);
try {
$client->upload($option->bucket, $path, $body);
} catch (Exception $e) {
return false;
}
if (!isset($file['size'])) {
$file['size'] = filesize($path);
}
if (!isset($file['mime'])) {
$file['mime'] = Typecho_Common::mimeContentType($file['tmp_name']);
}
return [
'name' => $file['name'],
'path' => $path,
'size' => $file['size'],
'type' => $ext,
'mime' => $file['mime']
];
}
/**
* 修改文件处理函数
*
* @access public
* @param array $content 老文件
* @param array $file 新上传的文件
* @return mixed
* @throws Typecho_Exception
*/
public static function modifyHandle($content, $file)
{
// check name
// exist in Widget_Upload::modifyHandle
//获取扩展名
$ext = Cosv5_Plugin::getSafeName($file['name']);
//判定是否是允许的文件类型
if ($content['attachment']->type != $ext) {
return false;
}
// 上传路径
$result = Cosv5_Plugin::uploadHandle($file, $content['attachment']->path);
return [
'name' => $content['attachment']->name,
'path' => $content['attachment']->path,
'size' => $result['size'],
'type' => $content['attachment']->type,
'mime' => $content['attachment']->mime
];
}
/**
* 文件删除
*
* @access public
* @param array $content 当前文件信息
* @return mixed
* @throws Typecho_Exception
*/
public static function deleteHandle($content)
{
// client
$option = Helper::options()->plugin("Cosv5");
$client = Cosv5_Plugin::getClient($option);
try {
$error = $client->deleteObject([
'Bucket' => $option->bucket,
'Key' => $content['attachment']->path
]);
return !$error;
} catch (Exception $e) {
return false;
}
}
/**
* 获取实际文件数据
*
* @access public
* @param array $content
* @return string
* @throws Typecho_Exception
*/
public static function attachmentDataHandle($content)
{
// client
$option = Helper::options()->plugin("Cosv5");
$client = Cosv5_Plugin::getClient($option);
try {
$result = $client->getObject([
'Bucket' => $option->bucket,
'Key' => $content['attachment']->path
]);
return $result['Body'];
} catch (Exception $e) {
return false;
}
}
/**
* 获取实际文件绝对访问路径
*
* @access public
* @param array $content 文件相关信息
* @return string
* @throws Typecho_Exception
*/
public static function attachmentHandle(array $content)
{
$option = Helper::options()->plugin("Cosv5");
if (!preg_match('/http(s)?:\/\/[\w\d\.\-\/]+$/is', $option->domain)) {
return false;
}
return Typecho_Common::url($content['attachment']->path, $option->domain);
}
/**
* 获取安全的文件名
*
* @param string $name
* @static
* @access private
* @return string
*/
private static function getSafeName(&$name)
{
$name = str_replace(array('"', '<', '>'), '', $name);
$name = str_replace('\\', '/', $name);
$name = false === strpos($name, '/') ? ('a' . $name) : str_replace('/', '/a', $name);
$info = pathinfo($name);
$name = substr($info['basename'], 1);
return isset($info['extension']) ? strtolower($info['extension']) : '';
}
/**
* @param $option
* @return \Qcloud\Cos\Client
*/
public static function getClient($option)
{
require_once 'cos-sdk-v5.phar';
return new Qcloud\Cos\Client([
'region' => $option->region,
'schema' => 'https',
'credentials' => [
'secretId' => $option->secretId,
'secretKey' => $option->secretKey
]
]);
}
}