1 | <?php |
||
15 | class Signature |
||
16 | { |
||
17 | const ALGORITHM = "AWS4-HMAC-SHA256"; |
||
18 | const SERVICE = "s3"; |
||
19 | const REQUEST_TYPE = "aws4_request"; |
||
20 | |||
21 | private $options; |
||
22 | |||
23 | /** |
||
24 | * @var string the AWS Key |
||
25 | */ |
||
26 | private $key; |
||
27 | |||
28 | /** |
||
29 | * @var string the AWS Secret |
||
30 | */ |
||
31 | private $secret; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $bucket; |
||
37 | |||
38 | /** |
||
39 | * @var Region |
||
40 | */ |
||
41 | private $region; |
||
42 | |||
43 | /** |
||
44 | * @var int the current unix timestamp |
||
45 | */ |
||
46 | private $time = null; |
||
47 | |||
48 | private $credentials = null; |
||
49 | private $base64Policy = null; |
||
50 | private $signature = null; |
||
51 | |||
52 | /** |
||
53 | * Signature constructor. |
||
54 | * |
||
55 | * @param string $key the AWS API Key to use. |
||
56 | * @param string $secret the AWS API Secret to use. |
||
57 | * @param string $bucket the bucket to upload the file into. |
||
58 | * @param string $region the s3 region this bucket is within. More info: http://amzn.to/1FtPG6r |
||
59 | * @param array $options any additional options, like acl and success status. |
||
60 | */ |
||
61 | public function __construct(string $key, string $secret, string $bucket, string $region = "us-east-1", array $options = []) |
||
71 | |||
72 | /** |
||
73 | * Set the AWS Credentials |
||
74 | * |
||
75 | * @param string $key the AWS API Key to use. |
||
76 | * @param string $secret the AWS API Secret to use. |
||
77 | */ |
||
78 | protected function setAwsCredentials(string $key, string $secret): void |
||
98 | |||
99 | /** |
||
100 | * Build the form url for sending files, this will include the bucket and the region name. |
||
101 | * |
||
102 | * @return string the s3 bucket's url. |
||
103 | */ |
||
104 | public function getFormUrl(): string |
||
112 | |||
113 | private function buildCustomUrl(): string |
||
125 | |||
126 | private function buildAmazonUrl(): string |
||
143 | |||
144 | /** |
||
145 | * Get all options. |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getOptions(): array |
||
153 | |||
154 | /** |
||
155 | * Edit/Update a new list of options. |
||
156 | * |
||
157 | * @param array $options a list of options to update. |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | public function setOptions(array $options): void |
||
165 | |||
166 | /** |
||
167 | * Get an AWS Signature V4 generated. |
||
168 | * |
||
169 | * @return string the aws v4 signature. |
||
170 | */ |
||
171 | public function getSignature(): string |
||
180 | |||
181 | /** |
||
182 | * Generate the necessary hidden inputs to go within the form. These inputs should match what's being send in |
||
183 | * the policy. |
||
184 | * |
||
185 | * @param bool $addKey whether to add the 'key' input (filename), defaults to yes. |
||
186 | * |
||
187 | * @return array of the form inputs. |
||
188 | */ |
||
189 | public function getFormInputs($addKey = true): array |
||
214 | |||
215 | /** |
||
216 | * Based on getFormInputs(), this will build up the html to go within the form. |
||
217 | * |
||
218 | * @param bool $addKey whether to add the 'key' input (filename), defaults to yes. |
||
219 | * |
||
220 | * @return string html of hidden form inputs. |
||
221 | */ |
||
222 | public function getFormInputsAsHtml($addKey = true): string |
||
230 | |||
231 | |||
232 | // Where the magic begins ;) |
||
233 | |||
234 | /** |
||
235 | * Step 1: Generate the Scope |
||
236 | */ |
||
237 | protected function generateScope(): void |
||
248 | |||
249 | /** |
||
250 | * Step 2: Generate a Base64 Policy |
||
251 | */ |
||
252 | protected function generatePolicy(): void |
||
271 | |||
272 | private function getPolicyContentTypeArray(): array |
||
281 | |||
282 | private function addAdditionalInputs($policy): array |
||
289 | |||
290 | /** |
||
291 | * Step 3: Generate and sign the Signature (v4) |
||
292 | */ |
||
293 | protected function generateSignature(): void |
||
311 | |||
312 | |||
313 | // Helper functions |
||
314 | |||
315 | private function keyHash($data, $key, $raw = true): string |
||
319 | |||
320 | private function mbToBytes($megaByte): int |
||
327 | |||
328 | |||
329 | // Dates |
||
330 | private function getShortDateFormat(): string |
||
334 | |||
335 | private function getFullDateFormat(): string |
||
339 | |||
340 | private function getExpirationDate(): string |
||
353 | } |
||
354 |