Completed
Push — master ( 9f2de6...30d7f3 )
by sabaku
04:09
created

Signature::getFormSignature()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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