-
Notifications
You must be signed in to change notification settings - Fork 593
/
Plugin.php
244 lines (226 loc) · 7.63 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
<?php
// +----------------------------------------------------------------------
// | LoveKKWeiBo [ Plugin.php ]
// +----------------------------------------------------------------------
// | Create: 08/24/2018 15:06:00
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://www.lovekk.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://opensource.org/licenses/MIT )
// +----------------------------------------------------------------------
// | Author: 康粑粑 <[email protected]>
// +----------------------------------------------------------------------
/**
* 把新浪微博作为附件图床 for Typecho
*
* @package LoveKKWeiBo
* @author 康粑粑
* @version 1.0.1
* @link https://www.lovekk.org
*/
if (!defined('__TYPECHO_ROOT_DIR__'))
exit;
class LoveKKWeiBo_Plugin implements Typecho_Plugin_Interface
{
// 默认上传目录
const UPLOAD_DIR = '/usr/uploads';
/**
* 插件激活
*
* @static
* @access public
*/
static public function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array(__CLASS__, 'uploadHandle');
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array(__CLASS__, 'modifyHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array(__CLASS__, 'attachmentHandle');
}
static public function deactivate()
{
}
/**
* 插件配置
*
* @static
* @access public
*
* @param Typecho_Widget_Helper_Form $form
*/
static public function config(Typecho_Widget_Helper_Form $form)
{
$weiboUser = new Typecho_Widget_Helper_Form_Element_Text('weiboUser', NULL, '', _t('微博账号'), _t('建议尽量使用平时不用的小号'));
$form->addInput($weiboUser->addRule('required', _t('微博账号必须填写')));
$weiboPass = new Typecho_Widget_Helper_Form_Element_Password('weiboPass', NULL, '', _t('登录密码'));
$form->addInput($weiboPass->addRule('required', _t('登录密码必须填写')));
}
static public function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
/**
* 图片上传方法
*
* @static
* @access public
*
* @param $file
*
* @return array|bool
* @throws Typecho_Exception
* @throws Typecho_Plugin_Exception
*/
static public function uploadHandle($file)
{
// 检查上传文件名
if (empty($file['name'])) {
return FALSE;
}
// 获取扩展名
$ext = self::getSafeName($file['name']);
// 检查文件类型
if (!Widget_Upload::checkFileType($ext)) {
return FALSE;
}
// 获取插件配置
$plugin = Helper::options()->plugin('LoveKKWeiBo');
// 验证临时文件名
if (!isset($file['tmp_name'])) {
return FALSE;
}
// 是否有文件大小
if (!isset($file['size'])) {
$file['size'] = filesize($file['tmp_name']);
}
// 复制一份数据
$files = $file['tmp_name'];
// 附件地址
$uri = '';
// 是否为图片文件
if (in_array($ext, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
// 是否载入了
if (!class_exists('Consatan\Weibo\ImageUploader\Client')) {
require __DIR__ . '/vendor/autoload.php';
}
// 初始化类
$weibo = new Consatan\Weibo\ImageUploader\Client();
// 上传图片
$uri = $weibo->upload($files, $plugin->weiboUser, $plugin->weiboPass);
} else {
// 初始化一个时间对象
$date = new Typecho_Date();
// 初始化保存路径
$path = Typecho_Common::url(defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR, defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__) . '/' . $date->year . '/' . $date->month;
// 目录是否存在
if (!is_dir($path)) {
// 创建目录
if (!self::makeUploadDir($path)) {
return FALSE;
}
}
// 生成存储文件名
$fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
// 组合路径
$path = $path . '/' . $fileName;
// 移动并保存文件
if (!@move_uploaded_file($file['tmp_name'], $path)) {
return FALSE;
}
// 附件地址
$uri = (defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR) . '/' . $date->year . '/' . $date->month . '/' . $fileName;
// 实际地址
$files = $path;
}
return array(
'name' => $file['name'],
'path' => $uri,
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($files)
);
}
/**
* 图片修改
*
* @static
* @access public
*
* @param $content
* @param $file
*
* @return array|bool
* @throws Typecho_Exception
* @throws Typecho_Plugin_Exception
*/
static public function modifyHandle($content, $file)
{
return self::uploadHandle($file);
}
/**
* 获取附件路径
*
* @param array $content
*
* @return string
* @throws Typecho_Exception
*/
static public function attachmentHandle(array $content)
{
// 获取附件地址
$path = $content['attachment']->path;
// 是否为远程附件
if ('http://' == substr($path, 0, 7) || 'https://' == substr($path, 0, 8)) {
return $path;
}
// 获取系统配置
$options = Typecho_Widget::widget('Widget_Options');
// 返回附件路径
return Typecho_Common::url($content['attachment']->path, defined('__TYPECHO_UPLOAD_URL__') ? __TYPECHO_UPLOAD_URL__ : $options->siteUrl);
}
/**
* 获取安全的文件名
*
* @param string $name
*
* @static
* @access private
* @return string
*/
static private 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']) : '';
}
/**
* 创建上传路径
*
* @access private
*
* @param string $path 路径
*
* @return boolean
*/
static private function makeUploadDir($path)
{
$path = preg_replace("/\\\+/", '/', $path);
$current = rtrim($path, '/');
$last = $current;
while (!is_dir($current) && FALSE !== strpos($path, '/')) {
$last = $current;
$current = dirname($current);
}
if ($last == $current) {
return TRUE;
}
if (!@mkdir($last)) {
return FALSE;
}
$stat = @stat($last);
$perms = $stat['mode'] & 0007777;
@chmod($last, $perms);
return self::makeUploadDir($path);
}
}