Skip to content

Commit 8ed2da7

Browse files
authored
Merge pull request #228 from rwifeng/version_publish
modify createTimestampAntiLeechUrl func
2 parents 3292512 + 95cfa9b commit 8ed2da7

File tree

9 files changed

+122
-79
lines changed

9 files changed

+122
-79
lines changed

.travis.yml

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
# using docker container
21
sudo: false
3-
42
language: php
53
php:
6-
- 5.3
7-
- 5.4
8-
- 5.5
9-
- 5.6
10-
- 7.0
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
- 5.6
8+
- 7.0
119
before_script:
12-
- export QINIU_TEST_ENV="travis"
13-
- travis_retry composer self-update
14-
- travis_retry composer install --no-interaction --prefer-source --dev
10+
- export QINIU_TEST_ENV="travis"
11+
- travis_retry composer self-update
12+
- travis_retry composer install --no-interaction --prefer-source --dev
1513
script:
16-
- ./vendor/bin/phpcs --standard=PSR2 src
17-
- ./vendor/bin/phpcs --standard=PSR2 examples
18-
- ./vendor/bin/phpcs --standard=PSR2 tests
19-
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover tests/Qiniu/Tests/
14+
- ./vendor/bin/phpcs --standard=PSR2 src
15+
- ./vendor/bin/phpcs --standard=PSR2 examples
16+
- ./vendor/bin/phpcs --standard=PSR2 tests
17+
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover tests/Qiniu/Tests/
2018
after_script:
21-
- wget https://scrutinizer-ci.com/ocular.phar
22-
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
19+
- wget https://scrutinizer-ci.com/ocular.phar
20+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
21+
env:
22+
global:
23+
- secure: g6HrlivUd4b9wrHgeMEuYhf4ow0Jd+oasfU5TRFVngLYpgWPzHnzUUTkc/5IF+kzy/izoCbeZbkxTH53msshsz4PDprX9rbPJUUJ1Ve4i4fYihO+mn5kah1UKZWT13/fDWprJvxQYbHDjByopujxtaGEennYF1q2cap/FVlg1FI=
24+
- secure: NpJUf401jFQXxpqa/kPvBf11clwpaTusqyuIUKaSj/wi9l6z+rl24Owlg6P4Ibz4Z9o2YGZBuJeUAZJHmrrzdc4haQ4FEUi3qubnoXMm4D2FXWZKzjFCMG/ISFyw2tO1Gv4wiQiMjMtHMNHqvsJdrlw2OsbxzOu/M9GqfJTYHEE=
25+
- secure: ftap5/rKY4bOL2jXOmN7yp7miKsLDo8QJS8Gw0fF3rmcvITw/2Q+tEzVhCNh/kLRcqLJMHpHywnRF5drRuGH5gute1jt5K2gR/6WG3PZWp/OhoSoEzdPiv1xAkYFnipqIJjVAbGFRnWM7xtQL8gKQn3sUlQSCiOJ6QCzmmD0jGg=

CHANGELOG.md

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

3+
## 7.1.4 (2017-06-21)
4+
### 增加
5+
* cdn 文件/目录 刷新
6+
* cdn 获取 流量/带宽
7+
* cdn 获取域名的访问日志列表
8+
* cdn 对资源链接进行时间戳防盗链签名
9+
310
## 7.1.3 (2016-11-18)
411
### 增加
512
* move, copy操作增加force参数

examples/cdn_manager.php

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,3 @@
8484
echo "get cdn log list success\n";
8585
print_r($logListData);
8686
}
87-
88-
//创建时间戳防盗链
89-
//时间戳防盗链密钥,后台获取
90-
$encryptKey = 'xxx';
91-
92-
//原始文件名
93-
$testFileName1 = '基本概括.mp4';
94-
$testFileName2 = '2017/01/07/test.png';
95-
96-
//查询参数列表
97-
$queryStringArray = array(
98-
'name'=>'七牛',
99-
'year'=>2017,
100-
'年龄'=>28,
101-
);
102-
103-
//带访问协议的域名
104-
$host = 'http://video.example.com';
105-
106-
//unix时间戳
107-
$deadline = time() + 3600;
108-
109-
$signedUrl1 = CdnManager::createTimestampAntiLeechUrl($host, $testFileName1, $queryStringArray, $encryptKey, $deadline);
110-
print($signedUrl1);
111-
print("\n");
112-
113-
$signedUrl2 = CdnManager::createTimestampAntiLeechUrl($host, $testFileName2, $queryStringArray, $encryptKey, $deadline);
114-
print($signedUrl2);
115-
print("\n");
116-
117-
$signedUrl3 = CdnManager::createTimestampAntiLeechUrl($host, $testFileName1, null, $encryptKey, $deadline);
118-
print($signedUrl3);
119-
print("\n");
120-
121-
$signedUrl4 = CdnManager::createTimestampAntiLeechUrl($host, $testFileName2, null, $encryptKey, $deadline);
122-
print($signedUrl4);
123-
print("\n");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../autoload.php';
4+
5+
use \Qiniu\Cdn\CdnManager;
6+
7+
//创建时间戳防盗链
8+
//时间戳防盗链密钥,后台获取
9+
$encryptKey = 'your_domain_timestamp_antileech_encryptkey';
10+
11+
//带访问协议的域名
12+
$url1 = 'http://phpsdk.qiniuts.com/24.jpg?avinfo';
13+
$url2 = 'http://phpsdk.qiniuts.com/24.jpg';
14+
15+
//有效期时间(单位秒)
16+
$durationInSeconds = 3600;
17+
18+
$signedUrl = CdnManager::createTimestampAntiLeechUrl($url1, $encryptKey, $durationInSeconds);
19+
print($signedUrl);

src/Qiniu/Cdn/CdnManager.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,38 +147,28 @@ private function post($url, $body)
147147
/**
148148
* 构建时间戳防盗链鉴权的访问外链
149149
*
150-
* @param string $host 带访问协议的域名
151-
* @param string $fileName 原始文件名,不需要urlencode
152-
* @param string $queryStringArray 查询参数命名数组,不需要urlencode
153-
* @param string $encryptKey 时间戳防盗链密钥
154-
* @param string $deadline 链接有效期时间戳(以秒为单位)
150+
* @param string $rawUrl 需要签名的资源url
151+
* @param string $encryptKey 时间戳防盗链密钥
152+
* @param string $durationInSeconds 链接的有效期(以秒为单位)
155153
*
156-
* @return string 带鉴权信息的资源外链,参考 examples/cdn_manager.php 代码
154+
* @return string 带鉴权信息的资源外链,参考 examples/cdn_manager_timestamp_antileech.php 代码
157155
*/
158-
public static function createTimestampAntiLeechUrl($host, $fileName, $queryStringArray, $encryptKey, $deadline)
156+
public static function createTimestampAntiLeechUrl($rawUrl, $encryptKey, $durationInSeconds)
159157
{
160-
$encodedFileName= str_replace("+", "%20", urlencode($fileName));
161-
if (!empty($queryStringArray)) {
162-
$queryStrings = array();
163-
foreach ($queryStringArray as $key => $value) {
164-
array_push($queryStrings, urlencode($key) . '=' . urlencode($value));
165-
}
166-
$queryString = implode('&', $queryStrings);
167-
$urlToSign = $host . '/' . $encodedFileName . '?' . $queryString;
168-
} else {
169-
$urlToSign = $host . '/' . $encodedFileName;
170-
}
171158

172-
$path = '/' . $encodedFileName;
159+
$parsedUrl = parse_url($rawUrl);
160+
161+
$deadline = time() + $durationInSeconds;
173162
$expireHex = dechex($deadline);
163+
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
174164

175165
$strToSign = $encryptKey . $path . $expireHex;
176166
$signStr = md5($strToSign);
177167

178-
if (!empty($queryString)) {
179-
$signedUrl = $urlToSign . '&sign=' . $signStr . '&t=' . $expireHex;
168+
if (isset($parsedUrl['query'])) {
169+
$signedUrl = $rawUrl . '&sign=' . $signStr . '&t=' . $expireHex;
180170
} else {
181-
$signedUrl = $urlToSign . '?sign=' . $signStr . '&t=' . $expireHex;
171+
$signedUrl = $rawUrl . '?sign=' . $signStr . '&t=' . $expireHex;
182172
}
183173

184174
return $signedUrl;

src/Qiniu/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
final class Config
77
{
8-
const SDK_VER = '7.1.3';
8+
const SDK_VER = '7.1.4';
99

1010
const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改
1111

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(
5757

5858
list($upHost, $err) = $config->zone->getUpHostByToken($upToken);
5959
if ($err != null) {
60-
throw new \Exception($err, 1);
60+
throw new \Exception($err->message(), 1);
6161
}
6262
$this->host = $upHost;
6363
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Created by IntelliJ IDEA.
4+
* User: wf
5+
* Date: 2017/6/21
6+
* Time: AM8:46
7+
*/
8+
9+
namespace Qiniu\Tests;
10+
11+
use Qiniu\Cdn\CdnManager;
12+
use Qiniu\Http\Client;
13+
14+
class CdnManagerTest extends \PHPUnit_Framework_TestCase
15+
{
16+
protected $cdnManager;
17+
protected $encryptKey;
18+
protected $imgUrl;
19+
20+
protected function setUp()
21+
{
22+
global $timestampAntiLeechEncryptKey;
23+
global $customDomain;
24+
global $testAuth;
25+
26+
$this->cdnManager = new CdnManager($testAuth);
27+
$this->encryptKey = $timestampAntiLeechEncryptKey;
28+
$this->imgUrl = $customDomain . '/24.jpg';
29+
}
30+
31+
public function testCreateTimestampAntiLeechUrl()
32+
{
33+
34+
$signUrl = $this->cdnManager->createTimestampAntiLeechUrl($this->imgUrl, $this->encryptKey, 3600);
35+
36+
$response = Client::get($signUrl);
37+
$this->assertEquals($response->statusCode, 200);
38+
$this->assertNull($response->error);
39+
40+
$url2 = $this->imgUrl . '?imageInfo';
41+
$signUrl2 = $this->cdnManager->createTimestampAntiLeechUrl($url2, $this->encryptKey, 3600);
42+
43+
$response = Client::get($signUrl2);
44+
$imgInfo = $response->json();
45+
46+
$this->assertEquals($response->statusCode, 200);
47+
$this->assertEquals($imgInfo['size'], 2196145);
48+
$this->assertNull($response->error);
49+
}
50+
}

tests/bootstrap.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use Qiniu\Auth;
66

7-
$accessKey = 'QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm';
8-
$secretKey = 'Bxckh6FA-Fbs9Yt3i3cbKVK22UPBmAOHJcL95pGz';
7+
$accessKey = getenv('QINIU_SDK_ENV_ACCESSKEY');
8+
$secretKey = getenv('QINIU_SDK_ENV_SECRETKEY');
99
$testAuth = new Auth($accessKey, $secretKey);
10+
1011
$bucketName = 'phpsdk';
1112
$key = 'php-logo.png';
1213
$key2 = 'niu.jpg';
14+
1315
$bucketNameBC = 'phpsdk-bc';
1416
$bucketNameNA = 'phpsdk-na';
1517

@@ -19,6 +21,15 @@
1921

2022
$tid = getenv('TRAVIS_JOB_NUMBER');
2123

24+
//cdn
25+
$timestampAntiLeechEncryptKey = getenv('QINIU_SDK_ENV_TIMESTAMP_ENCRPTKEY');
26+
$customDomain = "http://phpsdk.qiniuts.com";
27+
28+
var_dump($accessKey);
29+
var_dump($secretKey);
30+
var_dump($timestampAntiLeechEncryptKey);
31+
var_dump($_SERVER);
32+
2233
$testEnv = getenv('QINIU_TEST_ENV');
2334

2435
if (!empty($tid)) {

0 commit comments

Comments
 (0)