|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Qiniu; |
|
4
|
|
|
|
|
5
|
|
|
use Qiniu\Config; |
|
6
|
|
|
|
|
7
|
|
|
if (!defined('QINIU_FUNCTIONS_VERSION')) { |
|
8
|
|
|
define('QINIU_FUNCTIONS_VERSION', Config::SDK_VER); |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* 计算文件的crc32检验码: |
|
12
|
|
|
* |
|
13
|
|
|
* @param $file string 待计算校验码的文件路径 |
|
14
|
|
|
* |
|
15
|
|
|
* @return string 文件内容的crc32校验码 |
|
16
|
|
|
*/ |
|
17
|
|
|
function crc32_file($file) |
|
18
|
|
|
{ |
|
19
|
6 |
|
$hash = hash_file('crc32b', $file); |
|
20
|
6 |
|
$array = unpack('N', pack('H*', $hash)); |
|
21
|
6 |
|
return sprintf('%u', $array[1]); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* 计算输入流的crc32检验码 |
|
26
|
|
|
* |
|
27
|
|
|
* @param $data 待计算校验码的字符串 |
|
28
|
|
|
* |
|
29
|
|
|
* @return string 输入字符串的crc32校验码 |
|
30
|
|
|
*/ |
|
31
|
|
|
function crc32_data($data) |
|
32
|
|
|
{ |
|
33
|
18 |
|
$hash = hash('crc32b', $data); |
|
34
|
18 |
|
$array = unpack('N', pack('H*', $hash)); |
|
35
|
18 |
|
return sprintf('%u', $array[1]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* 对提供的数据进行urlsafe的base64编码。 |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $data 待编码的数据,一般为字符串 |
|
42
|
|
|
* |
|
43
|
|
|
* @return string 编码后的字符串 |
|
44
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/overview/appendix.html#urlsafe-base64 |
|
45
|
|
|
*/ |
|
46
|
|
|
function base64_urlSafeEncode($data) |
|
47
|
|
|
{ |
|
48
|
105 |
|
$find = array('+', '/'); |
|
49
|
105 |
|
$replace = array('-', '_'); |
|
50
|
105 |
|
return str_replace($find, $replace, base64_encode($data)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* 对提供的urlsafe的base64编码的数据进行解码 |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $str 待解码的数据,一般为字符串 |
|
57
|
|
|
* |
|
58
|
|
|
* @return string 解码后的字符串 |
|
59
|
|
|
*/ |
|
60
|
|
|
function base64_urlSafeDecode($str) |
|
61
|
|
|
{ |
|
62
|
3 |
|
$find = array('-', '_'); |
|
63
|
3 |
|
$replace = array('+', '/'); |
|
64
|
3 |
|
return base64_decode(str_replace($find, $replace, $str)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Wrapper for JSON decode that implements error detection with helpful |
|
69
|
|
|
* error messages. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $json JSON data to parse |
|
72
|
|
|
* @param bool $assoc When true, returned objects will be converted |
|
73
|
|
|
* into associative arrays. |
|
74
|
|
|
* @param int $depth User specified recursion depth. |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
* @throws \InvalidArgumentException if the JSON cannot be parsed. |
|
78
|
|
|
* @link http://www.php.net/manual/en/function.json-decode.php |
|
79
|
|
|
*/ |
|
80
|
|
|
function json_decode($json, $assoc = false, $depth = 512) |
|
81
|
|
|
{ |
|
82
|
|
|
static $jsonErrors = array( |
|
83
|
|
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
|
84
|
|
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
|
85
|
|
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
|
86
|
|
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
|
87
|
|
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' |
|
88
|
81 |
|
); |
|
89
|
|
|
|
|
90
|
81 |
|
if (empty($json)) { |
|
91
|
15 |
|
return null; |
|
92
|
|
|
} |
|
93
|
72 |
|
$data = \json_decode($json, $assoc, $depth); |
|
94
|
|
|
|
|
95
|
72 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
96
|
|
|
$last = json_last_error(); |
|
97
|
|
|
throw new \InvalidArgumentException( |
|
98
|
|
|
'Unable to parse JSON data: ' |
|
99
|
|
|
. (isset($jsonErrors[$last]) |
|
100
|
|
|
? $jsonErrors[$last] |
|
101
|
|
|
: 'Unknown error') |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
72 |
|
return $data; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* 计算七牛API中的数据格式 |
|
110
|
|
|
* |
|
111
|
|
|
* @param $bucket 待操作的空间名 |
|
112
|
|
|
* @param $key 待操作的文件名 |
|
113
|
|
|
* |
|
114
|
|
|
* @return string 符合七牛API规格的数据格式 |
|
115
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/data-formats.html |
|
116
|
|
|
*/ |
|
117
|
|
|
function entry($bucket, $key) |
|
118
|
|
|
{ |
|
119
|
33 |
|
$en = $bucket; |
|
120
|
33 |
|
if (!empty($key)) { |
|
121
|
33 |
|
$en = $bucket . ':' . $key; |
|
122
|
33 |
|
} |
|
123
|
33 |
|
return base64_urlSafeEncode($en); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* array 辅助方法,无值时不set |
|
128
|
|
|
* |
|
129
|
|
|
* @param $array 待操作array |
|
130
|
|
|
* @param $key key |
|
131
|
|
|
* @param $value value 为null时 不设置 |
|
132
|
|
|
* |
|
133
|
|
|
* @return array 原来的array,便于连续操作 |
|
134
|
|
|
*/ |
|
135
|
|
|
function setWithoutEmpty(&$array, $key, $value) |
|
136
|
|
|
{ |
|
137
|
12 |
|
if (!empty($value) || $value === '0') { |
|
138
|
3 |
|
$array[$key] = $value; |
|
139
|
3 |
|
} |
|
140
|
12 |
|
return $array; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|