1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Support; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Sign |
10
|
|
|
* |
11
|
|
|
* @package AlibabaCloud\Client\Support |
12
|
|
|
*/ |
13
|
|
|
class Sign |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private static $headerSeparator = "\n"; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Construct standard Header for Alibaba Cloud. |
22
|
|
|
* |
23
|
|
|
* @param array $headers |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
18 |
|
private static function acsHeaderString(array $headers) |
28
|
|
|
{ |
29
|
18 |
|
$array = []; |
30
|
18 |
|
foreach ($headers as $headerKey => $headerValue) { |
31
|
18 |
|
$key = strtolower($headerKey); |
32
|
18 |
|
if (strncmp($key, 'x-acs-', 6) === 0) { |
33
|
18 |
|
$array[$key] = $headerValue; |
34
|
18 |
|
} |
35
|
18 |
|
} |
36
|
18 |
|
ksort($array); |
37
|
18 |
|
$string = ''; |
38
|
18 |
|
foreach ($array as $sortMapKey => $sortMapValue) { |
39
|
18 |
|
$string .= $sortMapKey . ':' . $sortMapValue[0] . self::$headerSeparator; |
40
|
18 |
|
} |
41
|
|
|
|
42
|
18 |
|
return $string; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param UriInterface $uri |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
18 |
|
private static function resourceString(UriInterface $uri) |
51
|
|
|
{ |
52
|
18 |
|
return $uri->getPath() . '?' . $uri->getQuery(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $method |
57
|
|
|
* @param array $headers |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
18 |
|
private static function headerString($method, array $headers) |
62
|
|
|
{ |
63
|
18 |
|
$string = $method . self::$headerSeparator; |
64
|
18 |
|
if (isset($headers['Accept'][0])) { |
65
|
18 |
|
$string .= $headers['Accept'][0]; |
66
|
18 |
|
} |
67
|
18 |
|
$string .= self::$headerSeparator; |
68
|
|
|
|
69
|
18 |
|
if (isset($headers['Content-MD5'][0])) { |
70
|
3 |
|
$string .= $headers['Content-MD5'][0]; |
71
|
3 |
|
} |
72
|
18 |
|
$string .= self::$headerSeparator; |
73
|
|
|
|
74
|
18 |
|
if (isset($headers['Content-Type'][0])) { |
75
|
18 |
|
$string .= $headers['Content-Type'][0]; |
76
|
18 |
|
} |
77
|
18 |
|
$string .= self::$headerSeparator; |
78
|
|
|
|
79
|
18 |
|
if (isset($headers['Date'][0])) { |
80
|
18 |
|
$string .= $headers['Date'][0]; |
81
|
18 |
|
} |
82
|
18 |
|
$string .= self::$headerSeparator; |
83
|
|
|
|
84
|
18 |
|
$string .= self::acsHeaderString($headers); |
85
|
|
|
|
86
|
18 |
|
return $string; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $string |
91
|
|
|
* |
92
|
|
|
* @return null|string|string[] |
93
|
|
|
*/ |
94
|
66 |
|
private static function percentEncode($string) |
95
|
|
|
{ |
96
|
66 |
|
$result = urlencode($string); |
97
|
66 |
|
$result = str_replace(['+', '*'], ['%20', '%2A'], $result); |
98
|
66 |
|
$result = preg_replace('/%7E/', '~', $result); |
99
|
|
|
|
100
|
66 |
|
return $result; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $method |
105
|
|
|
* @param array $parameters |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
63 |
|
public static function rpcString($method, array $parameters) |
110
|
|
|
{ |
111
|
63 |
|
ksort($parameters); |
112
|
63 |
|
$canonicalized = ''; |
113
|
63 |
|
foreach ($parameters as $key => $value) { |
114
|
63 |
|
$canonicalized .= '&' . self::percentEncode($key) . '=' . self::percentEncode($value); |
115
|
63 |
|
} |
116
|
|
|
|
117
|
63 |
|
return $method . '&%2F&' . self::percentEncode(substr($canonicalized, 1)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Request $request |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
18 |
|
public static function roaString(Request $request) |
126
|
|
|
{ |
127
|
18 |
|
return self::headerString($request->getMethod(), $request->getHeaders()) . |
128
|
18 |
|
self::resourceString($request->getUri()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
82 |
|
public static function uuid() |
135
|
|
|
{ |
136
|
82 |
|
return sprintf( |
137
|
82 |
|
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
138
|
82 |
|
mt_rand(0, 0xffff), |
139
|
82 |
|
mt_rand(0, 0xffff), |
140
|
82 |
|
mt_rand(0, 0xffff), |
141
|
82 |
|
mt_rand(0, 0x0fff) | 0x4000, |
142
|
82 |
|
mt_rand(0, 0x3fff) | 0x8000, |
143
|
82 |
|
mt_rand(0, 0xffff), |
144
|
82 |
|
mt_rand(0, 0xffff), |
145
|
82 |
|
mt_rand(0, 0xffff) |
146
|
82 |
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|