forked from monkey-wenjun/upyun_plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupyun.class.php
495 lines (435 loc) · 15.3 KB
/
upyun.class.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
class UpYun {
const VERSION = '2.0';
/*{{{*/
const ED_AUTO = 'v0.api.upyun.com';
const ED_TELECOM = 'v1.api.upyun.com';
const ED_CNC = 'v2.api.upyun.com';
const ED_CTT = 'v3.api.upyun.com';
const CONTENT_TYPE = 'Content-Type';
const CONTENT_MD5 = 'Content-MD5';
const CONTENT_SECRET = 'Content-Secret';
// 缩略图
const X_GMKERL_THUMBNAIL = 'x-gmkerl-thumbnail';
const X_GMKERL_TYPE = 'x-gmkerl-type';
const X_GMKERL_VALUE = 'x-gmkerl-value';
const X_GMKERL_QUALITY = 'xgmkerl-quality';
const X_GMKERL_UNSHARP = 'xgmkerl-unsharp';
/*}}}*/
private $_bucket_name;
private $_username;
private $_password;
private $_timeout = 100000;
/**
* @deprecated
*/
private $_content_md5 = NULL;
/**
* @deprecated
*/
private $_file_secret = NULL;
/**
* @deprecated
*/
private $_file_infos= NULL;
protected $endpoint;
private $error = NULL;
/**
* 初始化 UpYun 存储接口
* @param $bucketname 空间名称
* @param $username 操作员名称
* @param $password 密码
*
* @return object
*/
public function __construct($bucketname, $username, $password, $endpoint = NULL, $timeout = 100000) {/*{{{*/
//$this->error = new WP_Error();
$this->_bucketname = $bucketname;
$this->_username = $username;
$this->_password = md5($password);
$this->_timeout = $timeout;
$this->endpoint = is_null($endpoint) ? self::ED_AUTO : $endpoint;
}/*}}}*/
/**
* 获取当前SDK版本号
*/
public function version() {
return self::VERSION;
}
/**
* 创建目录
* @param $path 路径
* @param $auto_mkdir 是否自动创建父级目录,最多10层次
*
* @return void
*/
public function makeDir($path, $auto_mkdir = false) {/*{{{*/
$headers = array('Folder' => 'true');
if ($auto_mkdir) $headers['Mkdir'] = 'true';
return $this->_do_request('PUT', $path, $headers);
}/*}}}*/
/**
* 删除目录和文件
* @param string $path 路径
*
* @return boolean
*/
public function delete($path) {/*{{{*/
return $this->_do_request('DELETE', $path);
}/*}}}*/
/**
* 上传文件
* @param string $path 存储路径
* @param mixed $file 需要上传的文件,可以是文件流或者文件内容
* @param boolean $auto_mkdir 自动创建目录
* @param array $opts 可选参数
*/
public function writeFile($path, $file, $auto_mkdir = False, $opts = NULL) {/*{{{*/
if (is_null($opts)) $opts = array();
if (!is_null($this->_content_md5) || !is_null($this->_file_secret)) {
//if (!is_null($this->_content_md5)) array_push($opts, self::CONTENT_MD5 . ": {$this->_content_md5}");
//if (!is_null($this->_file_secret)) array_push($opts, self::CONTENT_SECRET . ": {$this->_file_secret}");
if (!is_null($this->_content_md5)) $opts[self::CONTENT_MD5] = $this->_content_md5;
if (!is_null($this->_file_secret)) $opts[self::CONTENT_SECRET] = $this->_file_secret;
}
// 如果设置了缩略版本或者缩略图类型,则添加默认压缩质量和锐化参数
//if (isset($opts[self::X_GMKERL_THUMBNAIL]) || isset($opts[self::X_GMKERL_TYPE])) {
// if (!isset($opts[self::X_GMKERL_QUALITY])) $opts[self::X_GMKERL_QUALITY] = 95;
// if (!isset($opts[self::X_GMKERL_UNSHARP])) $opts[self::X_GMKERL_UNSHARP] = 'true';
//}
if ($auto_mkdir === True) $opts['Mkdir'] = 'true';
$this->_file_infos = $this->_do_request('PUT', $path, $opts, $file);
return $this->_file_infos;
}/*}}}*/
/**
* 下载文件
* @param string $path 文件路径
* @param mixed $file_handle
*
* @return mixed
*/
public function readFile($path, $file_handle = NULL) {/*{{{*/
return $this->_do_request('GET', $path, NULL, NULL, $file_handle);
}/*}}}*/
/**
* 获取目录文件列表
*
* @param string $path 查询路径
*
* @return mixed
*/
public function getList($path = '/') {/*{{{*/
$rsp = $this->_do_request('GET', $path);
$list = array();
if ($rsp) {
$rsp = explode("\n", $rsp);
foreach($rsp as $item) {
@list($name, $type, $size, $time) = explode("\t", trim($item));
if (!empty($time)) {
$type = $type == 'N' ? 'file' : 'folder';
}
$item = array(
'name' => $name,
'type' => $type,
'size' => intval($size),
'time' => intval($time),
);
array_push($list, $item);
}
}
return $list;
}/*}}}*/
/**
* 获取目录空间使用情况
*
* @param string $path 目录路径
*
* @return mixed
*/
public function getFolderUsage($path) {/*{{{*/
$rsp = $this->_do_request('GET', $path . '?usage');
if($rsp !== false){
return strval(floatval($rsp));
}
return false;
}/*}}}*/
/**
* 获取文件、目录信息
*
* @param string $path 路径
*
* @return mixed
*/
public function getFileInfo($path) {/*{{{*/
$rsp = $this->_do_request('HEAD', $path);
return $rsp;
}/*}}}*/
/**
* 连接签名方法
* @param $method 请求方式 {GET, POST, PUT, DELETE}
* return 签名字符串
*/
private function sign($method, $uri, $date, $length){/*{{{*/
//$uri = urlencode($uri);
$sign = "{$method}&{$uri}&{$date}&{$length}&{$this->_password}";
return 'UpYun '.$this->_username.':'.md5($sign);
}/*}}}*/
/**
* HTTP REQUEST 封装
* @param string $method HTTP REQUEST方法,包括PUT、POST、GET、OPTIONS、DELETE
* @param string $path 除Bucketname之外的请求路径,包括get参数
* @param array $headers 请求需要的特殊HTTP HEADERS
* @param array $body 需要POST发送的数据
*
* @return mixed
*/
protected function _do_request($method, $path, $headers = NULL, $body= NULL, $file_handle= NULL) {/*{{{*/
$uri = "/{$this->_bucketname}{$path}";
$ch = curl_init("https://{$this->endpoint}{$uri}");
$_headers = array('Expect:');
if (!is_null($headers) && is_array($headers)){
foreach($headers as $k => $v) {
array_push($_headers, "{$k}: {$v}");
}
}
$length = 0;
$date = gmdate('D, d M Y H:i:s \G\M\T');
if (!is_null($body)) {
if(is_resource($body)){
fseek($body, 0, SEEK_END);
$length = ftell($body);
fseek($body, 0);
array_push($_headers, "Content-Length: {$length}");
curl_setopt($ch, CURLOPT_INFILE, $body);
curl_setopt($ch, CURLOPT_INFILESIZE, $length);
}
else {
$length = @strlen($body);
array_push($_headers, "Content-Length: {$length}");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
}
else {
array_push($_headers, "Content-Length: {$length}");
}
array_push($_headers, "Authorization: {$this->sign($method, $uri, $date, $length)}");
array_push($_headers, "Date: {$date}");
curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($method == 'PUT' || $method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
}
else {
curl_setopt($ch, CURLOPT_POST, 0);
}
if ($method == 'GET' && is_resource($file_handle)) {
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILE, $file_handle);
}
if ($method == 'HEAD') {
curl_setopt($ch, CURLOPT_NOBODY, true);
}
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 0){
return $this->set_error_msg('CURL连接失败');
};
curl_close($ch);
$header_string = '';
$body = '';
if ($method == 'GET' && is_resource($file_handle)) {
$header_string = '';
$body = $response;
}
else {
list($header_string, $body) = explode("\r\n\r\n", $response, 2);
}
//var_dump($http_code);
if ($http_code == 200) {
if ($method == 'GET' && is_null($file_handle)) {
return $body;
}
else {
$data = $this->_getHeadersData($header_string);
return count($data) > 0 ? $data : true;
}
//elseif ($method == 'HEAD') {
// //return $this->_get_headers_data(substr($response, 0 , $header_size));
// return $this->_getHeadersData($header_string);
//}
//return True;
}
else {
$message = $this->_getErrorMessage($header_string);
if (is_null($message) && $method == 'GET' && is_resource($file_handle)) {
$message = 'File Not Found';
}
return $this->set_error_msg($message);
}
}/*}}}*/
public function get_error_msg(){
if(!is_null($this->error)){
return array('error' => $this->error);
}
return false;
}
public function set_error_msg($msg){
$error_msg = array(
'Unauthorized' => '访问未授权,可能是帐号密码错误,或者空间名填错',
'Sign error' => '签名错误(操作员和密码,或签名格式错误)',
'Need Date Header' => '发起的请求缺少 Date 头信息',
'Date offset error' => '发起请求的服务器时间错误,请检查服务器时间是否与世界时间一致',
'Not Access' => '权限错误(如非图片文件上传到图片空间)',
'File size too max' => '单个文件超出大小(100Mb 以内)',
'Not a Picture File' => '图片类空间错误码,非图片文件或图片文件格式错误。针对图片空间只允许上传 jpg/png/gif/bmp/tif 格式。',
'Picture Size too max' => ' 图片类空间错误码,图片尺寸太大。针对图片空间,图片总像素在 200000000 以内。',
'Bucket full' => '空间已用满',
'Forbidden' => '请确认该文件是否为图片',
'Bucket blocked' => ' 空间被禁用,请联系管理员',
'User blocked' => ' 操作员被禁用',
'Image Rotate Invalid Parameters' => ' 图片旋转参数错误',
'Image Crop Invalid Parameters' => ' 图片裁剪参数错误',
'Not Found' => '获取文件或目录不存在;上传文件或目录时上级目录不存在',
'Not Acceptable(path)' => '目录错误(创建目录时已存在同名文件;或上传文件时存在同名目录)',
'System Error' => '系统错误',
'File Not Found' => '文件不存在'
);
if(isset($error_msg[$msg])){
$this->error = '又拍云: '.$error_msg[$msg];
}else{
$this->error = '又拍云: '.$msg;
}
return false;
}
/**
* 处理HTTP HEADERS中返回的自定义数据
*
* @param string $text header字符串
*
* @return array
*/
private function _getHeadersData($text) {/*{{{*/
$headers = explode("\r\n", $text);
$items = array();
foreach($headers as $header) {
$header = trim($header);
if(strpos($header, 'x-upyun') !== False){
list($k, $v) = explode(':', $header);
$items[trim($k)] = in_array(substr($k,8,5), array('width','heigh','frame')) ? intval($v) : trim($v);
}
}
return $items;
}/*}}}*/
/**
* 获取返回的错误信息
*
* @param string $header_string
*
* @return mixed
*/
private function _getErrorMessage($header_string) {
list($status, $stash) = explode("\r\n", $header_string, 2);
list($v, $code, $message) = explode(" ", $status, 3);
return $message;
}
/**
* 删除目录
* @deprecated
* @param $path 路径
*
* @return void
*/
public function rmDir($path) {/*{{{*/
$this->_do_request('DELETE', $path);
}/*}}}*/
/**
* 删除文件
*
* @deprecated
* @param string $path 要删除的文件路径
*
* @return boolean
*/
public function deleteFile($path) {/*{{{*/
return $this->_do_request('DELETE', $path);
}/*}}}*/
/**
* 获取目录文件列表
* @deprecated
*
* @param string $path 要获取列表的目录
*
* @return array
*/
public function readDir($path) {/*{{{*/
return $this->getList($path);
}/*}}}*/
/**
* 获取空间使用情况
*
* @deprecated 推荐直接使用 getFolderUsage('/')来获取
* @return mixed
*/
public function getBucketUsage() {/*{{{*/
return $this->getFolderUsage('/');
}/*}}}*/
/**
* 获取文件信息
*
* #deprecated
* @param $file 文件路径(包含文件名)
* return array('type'=> file | folder, 'size'=> file size, 'date'=> unix time) 或 null
*/
//public function getFileInfo($file){/*{{{*/
// $result = $this->head($file);
// if(is_null($r))return null;
// return array('type'=> $this->tmp_infos['x-upyun-file-type'], 'size'=> @intval($this->tmp_infos['x-upyun-file-size']), 'date'=> @intval($this->tmp_infos['x-upyun-file-date']));
//}/*}}}*/
/**
* 切换 API 接口的域名
*
* @deprecated
* @param $domain {默然 v0.api.upyun.com 自动识别, v1.api.upyun.com 电信, v2.api.upyun.com 联通, v3.api.upyun.com 移动}
* return null;
*/
public function setApiDomain($domain){/*{{{*/
$this->endpoint = $domain;
}/*}}}*/
/**
* 设置待上传文件的 Content-MD5 值(如又拍云服务端收到的文件MD5值与用户设置的不一致,将回报 406 Not Acceptable 错误)
*
* @deprecated
* @param $str (文件 MD5 校验码)
* return null;
*/
public function setContentMD5($str){/*{{{*/
$this->_content_md5 = $str;
}/*}}}*/
/**
* 设置待上传文件的 访问密钥(注意:仅支持图片空!,设置密钥后,无法根据原文件URL直接访问,需带 URL 后面加上 (缩略图间隔标志符+密钥) 进行访问)
* 如缩略图间隔标志符为 ! ,密钥为 bac,上传文件路径为 /folder/test.jpg ,那么该图片的对外访问地址为: http://空间域名/folder/test.jpg!bac
*
* @deprecated
* @param $str (文件 MD5 校验码)
* return null;
*/
public function setFileSecret($str){/*{{{*/
$this->_file_secret = $str;
}/*}}}*/
/**
* @deprecated
* 获取上传文件后的信息(仅图片空间有返回数据)
* @param $key 信息字段名(x-upyun-width、x-upyun-height、x-upyun-frames、x-upyun-file-type)
* return value or NULL
*/
public function getWritedFileInfo($key){/*{{{*/
if(!isset($this->_file_infos))return NULL;
return $this->_file_infos[$key];
}/*}}}*/
}
?>