Completed
Push — master ( 695367...87fcf7 )
by sabaku
04:01
created

Signature::getSignature()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 16
nop 4
dl 0
loc 26
rs 5.3846
c 0
b 0
f 0
1
<?php
2
namespace Upyun;
3
4
/**
5
 * Class Signature
6
 * @package Upyun
7
 */
8
class Signature
9
{
10
    /**
11
     * 获取分块上传接口的签名
12
     */
13
    const SIGN_MULTIPART     = 1;
14
    /**
15
     * 生成视频处理接口的签名
16
     */
17
    const SIGN_VIDEO         = 2;
18
    /**
19
     * 生成视频处理接口的签名(不需要操作员时使用)
20
     */
21
    const SIGN_VIDEO_NO_OPERATOR   = 3;
22
23
    /**
24
     * 获取 Header 签名需要的请求头
25
     *
26
     * @param Config $bucketConfig
27
     * @param $method 请求方法
28
     * @param $path  请求路径
29
     * @param $contentMd5 文件内容 md5
30
     *
31
     * @return array
32
     */
33
    public static function getHeaderSign($bucketConfig, $method, $path, $contentMd5 = null)
34
    {
35
        $gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
36
37
        $policy = null;
38
        $sign = self::getBodySignature($bucketConfig, $method, $path, $gmtDate, $policy, $contentMd5);
39
40
        $headers = array(
41
            'Authorization' => $sign,
42
            'Date' => $gmtDate,
43
            'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion()
44
        );
45
        return $headers;
46
    }
47
48
    /**
49
     * 获取请求缓存刷新接口需要的签名头
50
     *
51
     * @param Config $bucketConfig
52
     * @param $urlString
53
     *
54
     * @return array
55
     */
56
    public static function getPurgeSignHeader(Config $bucketConfig, $urlString)
57
    {
58
        $gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
59
        $sign = md5("$urlString&{$bucketConfig->bucketName}&$gmtDate&{$bucketConfig->operatorPassword}");
60
        return array(
61
            'Authorization' => "UpYun {$bucketConfig->bucketName}:{$bucketConfig->operatorName}:$sign",
62
            'Date' => $gmtDate,
63
            'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion() . ' (purge api)'
64
        );
65
    }
66
67
    /**
68
     * 获取表单 API 需要的签名,依据 body 签名规则计算
69
     * @param Config $bucketConfig
70
     * @param $method 请求方法
71
     * @param $uri 请求路径
72
     * @param $date 请求时间
73
     * @param $policy
74
     * @param $contentMd5 请求 body 的 md5
75
     *
76
     * @return array
77
     */
78
    public static function getBodySignature(Config $bucketConfig, $method, $uri, $date = null, $policy = null, $contentMd5 = null)
79
    {
80
        $data = array(
81
            $method,
82
            $uri
83
        );
84
        if ($date) {
85
            $data[] = $date;
86
        }
87
88
        if ($policy) {
89
            $data[] = $policy;
90
        }
91
92
        if ($contentMd5) {
93
            $data[] = $contentMd5;
94
        }
95
        $signature = base64_encode(hash_hmac('sha1', implode('&', $data), $bucketConfig->operatorPassword, true));
96
        return 'UPYUN ' . $bucketConfig->operatorName . ':' . $signature;
97
    }
98
}
99