Completed
Push — master ( bfc0cc...f0e226 )
by sabaku
04:05
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
/**
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
     * 获取 RESET API 请求需要的签名头
25
     *
26
     * @param Config $bucketConfig
27
     * @param $method
28
     * @param $path
29
     * @param $contentLength
30
     *
31
     * @return array
32
     */
33
    public static function getRestApiSignHeader($bucketConfig, $method, $remotePath, $contentLength) {
34
        $gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
35
        $path = '/' . $bucketConfig->bucketName . '/' . ltrim($remotePath, '/');
36
37
        $sign = md5("$method&$path&$gmtDate&$contentLength&{$bucketConfig->operatorPassword}");
38
39
        $headers = array(
40
            'Authorization' => "UpYun {$bucketConfig->operatorName}:$sign",
41
            'Date' => $gmtDate,
42
            'User-agent' => 'Php-Sdk/' . $bucketConfig->getVersion() . ' (rest api)'
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) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between opening bracket and type hint "Config"; 1 found
Loading history...
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
    public static function getFormSignature(Config $bucketConfig, $data) {
66
        $data['bucket'] = $bucketConfig->bucketName;
67
        $policy = Util::base64Json($data);
68
        $signature = md5($policy . '&' . $bucketConfig->getFormApiKey());
69
        return array(
70
            'policy' => $policy,
71
            'signature' => $signature
72
        );
73
    }
74
75
    public static function getSignature( Config $bucketConfig, $data, $type, $tokenSecret = '') {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between opening bracket and type hint "Config"; 1 found
Loading history...
76
        if(is_array($data)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
77
            ksort($data);
78
            $string = '';
79
            foreach($data as $k => $v) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
80
                if(is_array($v)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
81
                    $v = implode('', $v);
82
                }
83
                $string .= "$k$v";
84
            }
85
            switch($type) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
86
                case self::SIGN_MULTIPART:
87
                    $string .= $tokenSecret ? $tokenSecret : $bucketConfig->getFormApiKey();
88
                    break;
89
                case self::SIGN_VIDEO:
90
                    $string = $bucketConfig->operatorName . $string . $bucketConfig->operatorPassword;
91
                    break;
92
                case self::SIGN_VIDEO_NO_OPERATOR:
93
                    break;
94
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
95
            }
96
            $sign = md5($string);
97
            return $sign;
98
        }
99
        return false;
100
    }
101
}