1 | <?php |
||
13 | class Signature |
||
14 | { |
||
15 | |||
16 | CONST ALGORITHM = "AWS4-HMAC-SHA256"; |
||
17 | CONST SERVICE = "s3"; |
||
18 | CONST REQUEST_TYPE = "aws4_request"; |
||
19 | |||
20 | /** |
||
21 | * Default options, these can be overwritten within the constructor. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $options = [ |
||
26 | |||
27 | // If the upload is a success, this is the http code we get back from S3. |
||
28 | // By default this will be a 201 Created. |
||
29 | 'success_status' => 201, |
||
30 | |||
31 | // If the file should be private/public-read/public-write. |
||
32 | // This is file specific, not bucket. More info: http://amzn.to/1SSOgwO |
||
33 | 'acl' => 'private', |
||
34 | |||
35 | // The file's name on s3, can be set with JS by changing the input[name="key"]. |
||
36 | // ${filename} will just mean the original filename of the file being uploaded. |
||
37 | 'default_filename' => '${filename}', |
||
38 | |||
39 | // The maximum file size of an upload in MB. Will refuse with a EntityTooLarge |
||
40 | // and 400 Bad Request if you exceed this limit. |
||
41 | 'max_file_size' => 500, |
||
42 | |||
43 | // Request expiration time, specified in relative time format or in seconds. |
||
44 | // minimum of 1 (+1 second), maximum of 604800 (+7 days) |
||
45 | 'expires' => '+6 hours', |
||
46 | |||
47 | // Server will check that the filename starts with this prefix and fail |
||
48 | // with a AccessDenied 403 if not. |
||
49 | 'valid_prefix' => '', |
||
50 | |||
51 | // Strictly only allow a single content type, blank will allow all. Will fail |
||
52 | // with a AccessDenied 403 is this condition is not met. |
||
53 | 'content_type' => '', |
||
54 | |||
55 | // Any additional inputs to add to the form. This is an array of name => value |
||
56 | // pairs e.g. ['Content-Disposition' => 'attachment'] |
||
57 | 'additional_inputs' => [] |
||
58 | |||
59 | ]; |
||
60 | |||
61 | private $key; |
||
62 | private $secret; |
||
63 | |||
64 | private $bucket; |
||
65 | private $region; |
||
66 | |||
67 | private $time = null; |
||
68 | |||
69 | private $credentials = null; |
||
70 | private $base64Policy = null; |
||
71 | private $signature = null; |
||
72 | |||
73 | /** |
||
74 | * Signature constructor. |
||
75 | * |
||
76 | * @param string $key the AWS API Key to use. |
||
77 | * @param string $secret the AWS API Secret to use. |
||
78 | * @param string $bucket the bucket to upload the file into. |
||
79 | * @param string $region the s3 region this bucket is within. More info: http://amzn.to/1FtPG6r |
||
80 | * @param array $options any additional options, like acl and success status. |
||
81 | */ |
||
82 | public function __construct($key, $secret, $bucket, $region = "us-east-1", $options = []) |
||
92 | |||
93 | /** |
||
94 | * Set the AWS Credentials |
||
95 | * |
||
96 | * @param string $key the AWS API Key to use. |
||
97 | * @param string $secret the AWS API Secret to use. |
||
98 | */ |
||
99 | protected function setAwsCredentials($key, $secret) |
||
113 | |||
114 | /** |
||
115 | * Build the form url for sending files, this will include the bucket and the region name. |
||
116 | * |
||
117 | * @return string the s3 bucket's url. |
||
118 | */ |
||
119 | public function getFormUrl() |
||
132 | |||
133 | /** |
||
134 | * Get all options. |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function getOptions() |
||
142 | |||
143 | /** |
||
144 | * Set/overwrite any default options. |
||
145 | * |
||
146 | * @param array $options any options to override. |
||
147 | */ |
||
148 | public function setOptions($options) |
||
156 | |||
157 | /** |
||
158 | * Get an AWS Signature V4 generated. |
||
159 | * |
||
160 | * @return string the aws v4 signature. |
||
161 | */ |
||
162 | public function getSignature() |
||
171 | |||
172 | /** |
||
173 | * Generate the necessary hidden inputs to go within the form. These inputs should match what's being send in |
||
174 | * the policy. |
||
175 | * |
||
176 | * @param bool $addKey whether to add the 'key' input (filename), defaults to yes. |
||
177 | * |
||
178 | * @return array of the form inputs. |
||
179 | */ |
||
180 | public function getFormInputs($addKey = true) |
||
205 | |||
206 | /** |
||
207 | * Based on getFormInputs(), this will build up the html to go within the form. |
||
208 | * |
||
209 | * @param bool $addKey whether to add the 'key' input (filename), defaults to yes. |
||
210 | * |
||
211 | * @return string html of hidden form inputs. |
||
212 | */ |
||
213 | public function getFormInputsAsHtml($addKey = true) |
||
221 | |||
222 | |||
223 | // Where the magic begins ;) |
||
224 | |||
225 | /** |
||
226 | * Step 1: Generate the Scope |
||
227 | */ |
||
228 | protected function generateScope() |
||
239 | |||
240 | /** |
||
241 | * Step 2: Generate a Base64 Policy |
||
242 | */ |
||
243 | protected function generatePolicy() |
||
262 | |||
263 | private function getPolicyContentTypeArray() |
||
272 | |||
273 | private function addAdditionalInputs($policy) |
||
280 | |||
281 | /** |
||
282 | * Step 3: Generate and sign the Signature (v4) |
||
283 | */ |
||
284 | protected function generateSignature() |
||
302 | |||
303 | |||
304 | // Helper functions |
||
305 | |||
306 | private function keyHash($date, $key, $raw = true) |
||
310 | |||
311 | private function populateTime() |
||
317 | |||
318 | private function mbToBytes($megaByte) |
||
325 | |||
326 | |||
327 | // Dates |
||
328 | |||
329 | private function getShortDateFormat() |
||
333 | |||
334 | private function getFullDateFormat() |
||
338 | |||
339 | private function getExpirationDate() |
||
352 | |||
353 | |||
354 | } |
||
355 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.