Skip to content

Commit 232bfb6

Browse files
committed
Merge remote-tracking branch 'qiniu/master' into add_demos
2 parents f6f7f2a + a7c996c commit 232bfb6

25 files changed

+582
-251
lines changed

.travis.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ php:
88
- 5.5
99
- 5.6
1010
before_script:
11-
- export QINIU_ACCESS_KEY="QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm"
12-
- export QINIU_SECRET_KEY="Bxckh6FA-Fbs9Yt3i3cbKVK22UPBmAOHJcL95pGz"
13-
- export QINIU_BUCKET_NAME="phpsdk"
14-
- export QINIU_KEY_NAME="php-logo.png"
1511
- export QINIU_TEST_ENV="travis"
1612
- travis_retry composer self-update
1713
- travis_retry composer install --no-interaction --prefer-source --dev

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
#Changelog
22

3+
## 7.0.4 (2015-07-23)
4+
### 修正
5+
* 一些地方的严格比较检查
6+
* resumeupload 备用地址失效
7+
8+
## 7.0.3 (2015-07-10)
9+
### 修改
10+
* 多zone 支持
11+
12+
## 7.0.2 (2015-04-18)
13+
### 修改
14+
* fetch 接口返回内容调整
15+
* pfop 接口调整
16+
17+
###修正
18+
* exception 类调用
19+
20+
## 7.0.1 (2015-03-27)
21+
### 增加
22+
* 增加代码注释
23+
324
## 7.0.0 (2015-02-03)
425

526
### 增加

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Qiniu Resource Storage SDK for PHP
2-
[![@qiniu on weibo](http://img.shields.io/badge/weibo-%40qiniutek-blue.svg)](http://weibo.com/qiniutek)
2+
[![doxygen.io](http://doxygen.io/github.com/qiniu/php-sdk/?status.svg)](http://doxygen.io/github.com/qiniu/php-sdk/)
33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)
44
[![Build Status](https://travis-ci.org/qiniu/php-sdk.svg)](https://travis-ci.org/qiniu/php-sdk)
55
[![Latest Stable Version](https://img.shields.io/packagist/v/qiniu/php-sdk.svg)](https://packagist.org/packages/qiniu/php-sdk)
66
[![Total Downloads](https://img.shields.io/packagist/dt/qiniu/php-sdk.svg)](https://packagist.org/packages/qiniu/php-sdk)
77
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/qiniu/php-sdk/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/qiniu/php-sdk/?branch=master)
88
[![Code Coverage](https://scrutinizer-ci.com/g/qiniu/php-sdk/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/qiniu/php-sdk/?branch=master)
99
[![Join Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/qiniu/php-sdk?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
10+
[![@qiniu on weibo](http://img.shields.io/badge/weibo-%40qiniutek-blue.svg)](http://weibo.com/qiniutek)
11+
1012
## 安装
1113

1214
* 通过composer,这是推荐的方式,可以使用composer.json 声明依赖,或者运行下面的命令。SDK 包已经放到这里 [`qiniu/php-sdk`][install-packagist]

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=5.3.3"
16+
"php": ">=5.3.3",
17+
"squizlabs/php_codesniffer": "~2.3"
1718
},
1819
"require-dev": {
19-
"phpunit/phpunit": "~4.0",
20-
"squizlabs/php_codesniffer": "~2.0"
20+
"phpunit/phpunit": "~4.0"
2121
},
2222
"autoload": {
2323
"psr-4": {"Qiniu\\": "src/Qiniu"},

src/Qiniu/Auth.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,40 @@ public function __construct($accessKey, $secretKey)
1414
$this->secretKey = $secretKey;
1515
}
1616

17-
public function token($data)
17+
public function sign($data)
1818
{
1919
$hmac = hash_hmac('sha1', $data, $this->secretKey, true);
2020
return $this->accessKey . ':' . \Qiniu\base64_urlSafeEncode($hmac);
2121
}
2222

23-
public function tokenWithData($data)
23+
public function signWithData($data)
2424
{
2525
$data = \Qiniu\base64_urlSafeEncode($data);
26-
return $this->token($data) . ':' . $data;
26+
return $this->sign($data) . ':' . $data;
2727
}
2828

29-
public function tokenOfRequest($urlString, $body, $contentType = null)
29+
public function signRequest($urlString, $body, $contentType = null)
3030
{
3131
$url = parse_url($urlString);
3232
$data = '';
33-
if (isset($url['path'])) {
33+
if (array_key_exists('path', $url)) {
3434
$data = $url['path'];
3535
}
36-
if (isset($url['query'])) {
36+
if (array_key_exists('query', $url)) {
3737
$data .= '?' . $url['query'];
3838
}
3939
$data .= "\n";
4040

41-
if ($body != null &&
42-
($contentType == 'application/x-www-form-urlencoded') || $contentType == 'application/json') {
41+
if ($body !== null &&
42+
in_array((string) $contentType, array('application/x-www-form-urlencoded', 'application/json'), true)) {
4343
$data .= $body;
4444
}
45-
return $this->token($data);
45+
return $this->sign($data);
4646
}
4747

4848
public function verifyCallback($contentType, $originAuthorization, $url, $body)
4949
{
50-
$authorization = 'QBox ' . $this->tokenOfRequest($url, $body, $contentType);
50+
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
5151
return $originAuthorization === $authorization;
5252
}
5353

@@ -63,7 +63,7 @@ public function privateDownloadUrl($baseUrl, $expires = 3600)
6363
}
6464
$baseUrl .= $deadline;
6565

66-
$token = $this->token($baseUrl);
66+
$token = $this->sign($baseUrl);
6767
return "$baseUrl&token=$token";
6868
}
6969

@@ -76,15 +76,21 @@ public function uploadToken(
7676
) {
7777
$deadline = time() + $expires;
7878
$scope = $bucket;
79-
if ($key != null) {
79+
if ($key !== null) {
8080
$scope .= ':' . $key;
8181
}
82-
$args = array('scope' => $scope, 'deadline' => $deadline);
83-
self::copyPolicy($args, $policy, $strictPolicy);
82+
$args = array();
83+
$args = self::copyPolicy($args, $policy, $strictPolicy);
84+
$args['scope'] = $scope;
85+
$args['deadline'] = $deadline;
8486
$b = json_encode($args);
85-
return $this->tokenWithData($b);
87+
return $this->signWithData($b);
8688
}
8789

90+
/**
91+
*上传策略,参数规格详见
92+
*http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
93+
*/
8894
private static $policyFields = array(
8995
'callbackUrl',
9096
'callbackBody',
@@ -112,24 +118,25 @@ public function uploadToken(
112118
'asyncOps',
113119
);
114120

115-
private static function copyPolicy($policy, $originPolicy, $strictPolicy)
121+
private static function copyPolicy(&$policy, $originPolicy, $strictPolicy)
116122
{
117-
if ($originPolicy == null) {
118-
return;
123+
if ($originPolicy === null) {
124+
return array();
119125
}
120126
foreach ($originPolicy as $key => $value) {
121-
if (in_array($key, self::$deprecatedPolicyFields)) {
127+
if (in_array((string) $key, self::$deprecatedPolicyFields, true)) {
122128
throw new \InvalidArgumentException("{$key} has deprecated");
123129
}
124-
if (!$strictPolicy || in_array($key, self::$policyFields)) {
130+
if (!$strictPolicy || in_array((string) $key, self::$policyFields, true)) {
125131
$policy[$key] = $value;
126132
}
127133
}
134+
return $policy;
128135
}
129136

130137
public function authorization($url, $body = null, $contentType = null)
131138
{
132-
$authorization = 'QBox ' . $this->tokenOfRequest($url, $body, $contentType);
139+
$authorization = 'QBox ' . $this->signRequest($url, $body, $contentType);
133140
return array('Authorization' => $authorization);
134141
}
135142
}

src/Qiniu/Config.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,34 @@
33

44
final class Config
55
{
6-
const SDK_VER = '7.0.0';
6+
const SDK_VER = '7.0.4';
77

8-
const IO_HOST = 'http://iovip.qbox.me';
9-
const RS_HOST = 'http://rs.qbox.me';
10-
const RSF_HOST = 'http://rsf.qbox.me';
11-
const API_HOST = 'http://api.qiniu.com';
8+
const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改
129

13-
const UPAUTO_HOST = 'http://up.qiniu.com';
14-
const UPDX_HOST = 'http://updx.qiniu.com';
15-
const UPLT_HOST = 'http://uplt.qiniu.com';
16-
const UPYD_HOST = 'http://upyd.qiniu.com';
17-
const UPBACKUP_HOST = 'http://upload.qiniu.com';
10+
const IO_HOST = 'http://iovip.qbox.me'; // 七牛源站Host
11+
const RS_HOST = 'http://rs.qbox.me'; // 文件元信息管理操作Host
12+
const RSF_HOST = 'http://rsf.qbox.me'; // 列举操作Host
13+
const API_HOST = 'http://api.qiniu.com'; // 数据处理操作Host
1814

19-
const BLOCK_SIZE = 4194304; # 4*1024*1024
15+
private $upHost; // 上传Host
16+
private $upHostBackup; // 上传备用Host
2017

21-
public static $defaultHost = self::UPAUTO_HOST;
18+
public function __construct(Zone $z = null) // 构造函数,默认为zone0
19+
{
20+
if ($z === null) {
21+
$z = Zone::zone0();
22+
}
23+
$this->upHost = $z->upHost;
24+
$this->upHostBackup = $z->upHostBackup;
25+
}
26+
27+
public function getUpHost()
28+
{
29+
return $this->upHost;
30+
}
31+
32+
public function getUpHostBackup()
33+
{
34+
return $this->upHostBackup;
35+
}
2236
}

src/Qiniu/Etag.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private static function calcSha1($data)
2020
{
2121
$sha1Str = sha1($data, true);
2222
$err = error_get_last();
23-
if ($err != null) {
23+
if ($err !== null) {
2424
return array(null, $err);
2525
}
2626
$byteArray = unpack('C*', $sha1Str);
@@ -32,7 +32,7 @@ public static function sum($filename)
3232
{
3333
$fhandler = fopen($filename, 'r');
3434
$err = error_get_last();
35-
if ($err != null) {
35+
if ($err !== null) {
3636
return array(null, $err);
3737
}
3838

@@ -48,26 +48,26 @@ public static function sum($filename)
4848
if ($blockCnt <= 1) {
4949
array_push($sha1Buf, 0x16);
5050
$fdata = fread($fhandler, Config::BLOCK_SIZE);
51-
if ($err != null) {
51+
if ($err !== null) {
5252
fclose($fhandler);
5353
return array(null, $err);
5454
}
55-
list($sha1Code, $err) = calSha1($fdata);
55+
list($sha1Code, ) = calSha1($fdata);
5656
$sha1Buf = array_merge($sha1Buf, $sha1Code);
5757
} else {
5858
array_push($sha1Buf, 0x96);
5959
$sha1BlockBuf = array();
6060
for ($i=0; $i < $blockCnt; $i++) {
6161
$fdata = fread($fhandler, Config::BLOCK_SIZE);
6262
list($sha1Code, $err) = self::calcSha1($fdata);
63-
if ($err != null) {
63+
if ($err !== null) {
6464
fclose($fhandler);
6565
return array(null, $err);
6666
}
6767
$sha1BlockBuf = array_merge($sha1BlockBuf, $sha1Code);
6868
}
6969
$tmpData = self::packArray('C*', $sha1BlockBuf);
70-
list($sha1Final, $_err) = self::calcSha1($tmpData);
70+
list($sha1Final, ) = self::calcSha1($tmpData);
7171
$sha1Buf = array_merge($sha1Buf, $sha1Final);
7272
}
7373
$etag = \Qiniu\base64_urlSafeEncode(self::packArray('C*', $sha1Buf));

src/Qiniu/Http/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,19 @@ private static function sendRequest($request)
8585
CURLOPT_CUSTOMREQUEST => $request->method,
8686
CURLOPT_URL => $request->url
8787
);
88+
8889
if (!empty($request->headers)) {
8990
$headers = array();
9091
foreach ($request->headers as $key => $val) {
9192
array_push($headers, "$key: $val");
9293
}
9394
$options[CURLOPT_HTTPHEADER] = $headers;
9495
}
96+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
9597

9698
if (!empty($request->body)) {
9799
$options[CURLOPT_POSTFIELDS] = $request->body;
98100
}
99-
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
100101
curl_setopt_array($ch, $options);
101102
$result = curl_exec($ch);
102103
$t2 = microtime(true);

src/Qiniu/Http/Response.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ public function __construct($code, $duration, array $headers = array(), $body =
8484
$this->body = $body;
8585
$this->error = $error;
8686
$this->jsonData = null;
87-
if ($error != null) {
87+
if ($error !== null) {
8888
return;
8989
}
9090

91-
if ($body == null) {
91+
if ($body === null) {
9292
if ($code >= 400) {
9393
$this->error = self::$statusTexts[$code];
9494
}
@@ -98,23 +98,22 @@ public function __construct($code, $duration, array $headers = array(), $body =
9898
try {
9999
$jsonData = self::bodyJson($body);
100100
if ($code >=400) {
101-
if ($jsonData['error'] != null) {
101+
$this->error = $body;
102+
if ($jsonData['error'] !== null) {
102103
$this->error = $jsonData['error'];
103-
} else {
104-
$this->error = $body;
105104
}
106105
}
107106
$this->jsonData = $jsonData;
108107
} catch (\InvalidArgumentException $e) {
108+
$this->error = $body;
109109
if ($code >= 200 && $code < 300) {
110110
$this->error = $e->getMessage();
111-
} else {
112-
$this->error = $body;
113111
}
114112
}
115113
} elseif ($code >=400) {
116114
$this->error = $body;
117115
}
116+
return;
118117
}
119118

120119
public function json()
@@ -126,19 +125,19 @@ private static function bodyJson($body, array $config = array())
126125
{
127126
return \Qiniu\json_decode(
128127
(string) $body,
129-
isset($config['object']) ? !$config['object'] : true,
128+
array_key_exists('object', $config) ? !$config['object'] : true,
130129
512,
131-
isset($config['big_int_strings']) ? JSON_BIGINT_AS_STRING : 0
130+
array_key_exists('big_int_strings', $config) ? JSON_BIGINT_AS_STRING : 0
132131
);
133132
}
134133

135134
public function xVia()
136135
{
137136
$via = $this->headers['X-Via'];
138-
if ($via == null) {
137+
if ($via === null) {
139138
$via = $this->headers['X-Px'];
140139
}
141-
if ($via == null) {
140+
if ($via === null) {
142141
$via = $this->headers['Fw-Via'];
143142
}
144143
return $via;
@@ -156,19 +155,20 @@ public function xReqId()
156155

157156
public function ok()
158157
{
159-
return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error == null;
158+
return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error === null;
160159
}
161160

162161
public function needRetry()
163162
{
164163
$code = $this->statusCode;
165-
if ($code< 0 || ($code / 100 == 5 and $code != 579) || $code == 996) {
164+
if ($code< 0 || ($code / 100 === 5 and $code !== 579) || $code === 996) {
166165
return true;
167166
}
168167
}
169168

170169
private static function isJson($headers)
171170
{
172-
return isset($headers['Content-Type']) && $headers['Content-Type'] == 'application/json';
171+
return array_key_exists('Content-Type', $headers) &&
172+
strpos($headers['Content-Type'], 'application/json') === 0;
173173
}
174174
}

0 commit comments

Comments
 (0)