-
Notifications
You must be signed in to change notification settings - Fork 4
/
Plugin.php
executable file
·174 lines (139 loc) · 6.24 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
<?php
/**
* 将 Typecho 的附件上传至七牛云存储中。该插件仅为满足个人需求而制作,如有考虑不周的地方,可自行修改。<a href="https://github.com/abelyao/Typecho-QiniuFile" target="_blank">源代码参考</a> & <a href="https://portal.qiniu.com/signup?code=3li4q4loavdxu" target="_blank">注册七牛</a>
*
* @package Qiniu File
* @author abelyao
* @version 1.2.0
* @link http://www.abelyao.com/
* @date 2014-02-22
*/
class QiniuFile_Plugin implements Typecho_Plugin_Interface
{
// 激活插件
public static function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array('QiniuFile_Plugin', 'uploadHandle');
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array('QiniuFile_Plugin', 'modifyHandle');
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array('QiniuFile_Plugin', 'deleteHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array('QiniuFile_Plugin', 'attachmentHandle');
return _t('插件已经激活,需先配置七牛的信息!');
}
// 禁用插件
public static function deactivate()
{
return _t('插件已被禁用');
}
// 插件配置面板
public static function config(Typecho_Widget_Helper_Form $form)
{
$bucket = new Typecho_Widget_Helper_Form_Element_Text('bucket', null, null, _t('空间名称:'));
$form->addInput($bucket->addRule('required', _t('“空间名称”不能为空!')));
$accesskey = new Typecho_Widget_Helper_Form_Element_Text('accesskey', null, null, _t('AccessKey:'));
$form->addInput($accesskey->addRule('required', _t('AccessKey 不能为空!')));
$sercetkey = new Typecho_Widget_Helper_Form_Element_Text('sercetkey', null, null, _t('SecretKey:'));
$form->addInput($sercetkey->addRule('required', _t('SecretKey 不能为空!')));
$domain = new Typecho_Widget_Helper_Form_Element_Text('domain', null, 'http://', _t('绑定域名:'), _t('以 http:// 开头,结尾不要加 / !'));
$form->addInput($domain->addRule('required', _t('请填写空间绑定的域名!'))->addRule('url', _t('您输入的域名格式错误!')));
$savepath = new Typecho_Widget_Helper_Form_Element_Text('savepath', null, '{year}/{month}/', _t('保存路径格式:'), _t('附件保存路径的格式,默认为 Typecho 的 {year}/{month}/ 格式,注意<strong style="color:#C33;">前面不要加 / </strong>!<br />可选参数:{year} 年份、{month} 月份、{day} 日期'));
$form->addInput($savepath->addRule('required', _t('请填写保存路径格式!')));
}
// 个人用户配置面板
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
// 获得插件配置信息
public static function getConfig()
{
return Typecho_Widget::widget('Widget_Options')->plugin('QiniuFile');
}
// 初始化七牛SDK
public static function initSDK($accesskey, $sercetkey)
{
require_once 'sdk/io.php';
require_once 'sdk/rs.php';
Qiniu_SetKeys($accesskey, $sercetkey);
}
public static function init($accesskey, $sercetkey)
{
require_once 'vendor/autoload.php';
header('Access-Control-Allow-Origin:*');
return new Qiniu\Auth($accesskey, $sercetkey);
}
// 删除文件
public static function deleteFile($filepath)
{
// 获取插件配置
$option = self::getConfig();
// 初始化 SDK
self::initSDK($option->accesskey, $option->sercetkey);
// 删除
$client = new Qiniu_MacHttpClient(null);
return Qiniu_RS_Delete($client, $option->bucket, $filepath);
}
// 上传文件
public static function uploadFile($file, $content = null)
{
// 获取上传文件
if (empty($file['name'])) return false;
// 校验扩展名
$part = explode('.', $file['name']);
$ext = (($length = count($part)) > 1) ? strtolower($part[$length-1]) : '';
if (!Widget_Upload::checkFileType($ext)) return false;
// 获取插件配置
$option = self::getConfig();
$date = new Typecho_Date(Typecho_Widget::widget('Widget_Options')->gmtTime);
// 保存位置
$savepath = preg_replace(array('/\{year\}/', '/\{month\}/', '/\{day\}/'), array($date->year, $date->month, $date->day), $option->savepath);
$savename = $savepath . sprintf('%u', crc32(uniqid())) . '.' . $ext;
if (isset($content))
{
$savename = $content['attachment']->path;
self::deleteFile($savename);
}
// 上传文件
$filename = $file['tmp_name'];
if (!isset($filename)) return false;
// 初始化 SDK
$token = self::init($option->accesskey, $option->sercetkey)->uploadToken($option->bucket);
// 上传
$uploadMgr = new Qiniu\Storage\UploadManager();
list($result, $error) = $uploadMgr->putFile($token, $savename, $filename);
if ($error == null)
{
return array
(
'name' => $file['name'],
'path' => $savename,
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($savename)
);
}
else {
var_dump($error);
return false;
}
}
// 上传文件处理函数
public static function uploadHandle($file)
{
return self::uploadFile($file);
}
// 修改文件处理函数
public static function modifyHandle($content, $file)
{
return self::uploadFile($file, $content);
}
// 删除文件
public static function deleteHandle(array $content)
{
self::deleteFile($content['attachment']->path);
}
// 获取实际文件绝对访问路径
public static function attachmentHandle(array $content)
{
$option = self::getConfig();
return Typecho_Common::url($content['attachment']->path, $option->domain);
}
}