Completed
Push — master ( 24baaf...9f2de6 )
by sabaku
04:08
created

Signature::getHeaderSign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 22
rs 9.2
1
<?php
2
namespace Upyun;
3
4
5
/**
6
 * Class Signature
7
 * @package Upyun
8
 */
9
class Signature {
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
        $gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
35
36
        $signParams = array(
37
            $method,
38
            $path,
39
            $gmtDate
40
        );
41
42
        if ($contentMd5) {
43
            $signParams[] = $contentMd5;
44
        }
45
46
        $sign = self::calcSignature($bucketConfig, $signParams);
47
48
        $headers = array(
49
            'Authorization' => "UPYUN {$bucketConfig->operatorName}:$sign",
50
            'Date' => $gmtDate,
51
            'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion()
52
        );
53
        return $headers;
54
    }
55
56
    /**
57
     * 获取请求缓存刷新接口需要的签名头
58
     *
59
     * @param Config $bucketConfig
60
     * @param $urlString
61
     *
62
     * @return array
63
     */
64
    public static function getPurgeSignHeader(Config $bucketConfig, $urlString) {
65
        $gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
66
        $sign = md5("$urlString&{$bucketConfig->bucketName}&$gmtDate&{$bucketConfig->operatorPassword}");
67
        return array(
68
            'Authorization' => "UpYun {$bucketConfig->bucketName}:{$bucketConfig->operatorName}:$sign",
69
            'Date' => $gmtDate,
70
            'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion() . ' (purge api)'
71
        );
72
    }
73
74
    /**
75
     * 获取表单 API 需要的签名,依据 body 签名规则计算
76
     * @param Config $bucketConfig
77
     * @param $data
78
     *
79
     * @return array
80
     */
81
    public static function getFormSignature(Config $bucketConfig, $data) {
82
        $data['bucket'] = $bucketConfig->bucketName;
83
        $policy = Util::base64Json($data);
84
        $signParams = array(
85
            'method' => 'POST',
86
            'uri' => '/' . $bucketConfig->bucketName,
87
        );
88
        if (isset($data['date'])) {
89
            $signParams['date'] = $data['date'];
90
        }
91
92
        $signParams['policy'] = $policy;
93
        if (isset($data['content-md5'])) {
94
            $signParams['md5'] = $data['content-md5'];
95
        };
96
97
        $signature = self::calcSignature($bucketConfig, $signParams);
98
        return array(
99
            'policy' => $policy,
100
            'signature' => $signature
101
        );
102
    }
103
104
    private static function calcSignature(Config $bucketConfig, $signParams) {
105
        return base64_encode(hash_hmac('sha1', implode('&', $signParams), $bucketConfig->operatorPassword, true));
106
    }
107
108
    public static function getSignature(Config $bucketConfig, $data, $type, $tokenSecret = '') {
109
        if (is_array($data)) {
110
            ksort($data);
111
            $string = '';
112
            foreach ($data as $k => $v) {
113
                if (is_array($v)) {
114
                    $v = implode('', $v);
115
                }
116
                $string .= "$k$v";
117
            }
118
            switch ($type) {
119
                case self::SIGN_MULTIPART:
120
                    $string .= $tokenSecret ? $tokenSecret : $bucketConfig->getFormApiKey();
121
                    break;
122
                case self::SIGN_VIDEO:
123
                    $string = $bucketConfig->operatorName . $string . $bucketConfig->operatorPassword;
124
                    break;
125
                case self::SIGN_VIDEO_NO_OPERATOR:
126
                    break;
127
            }
128
            $sign = md5($string);
129
            return $sign;
130
        }
131
        return false;
132
    }
133
}
134