1 | <?php |
||
12 | class Signature |
||
13 | { |
||
14 | |||
15 | CONST ALGORITHM = "AWS4-HMAC-SHA256"; |
||
16 | CONST SERVICE = "s3"; |
||
17 | CONST REQUEST_TYPE = "aws4_request"; |
||
18 | |||
19 | /** |
||
20 | * Default options, these can be overwritten within the constructor. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $options = [ |
||
25 | |||
26 | // If the upload is a success, the http code we get back. |
||
27 | 'success_status' => '201', |
||
28 | |||
29 | // If the file should be private/public-read/public-write. |
||
30 | // This is file specific, not bucket. More info: http://amzn.to/1SSOgwO |
||
31 | 'acl' => 'private', |
||
32 | |||
33 | // The file's name, can be set with JS by changing the input[name="key"] |
||
34 | // ${filename} will just mean the filename of the file being uploaded. |
||
35 | 'default_filename' => '${filename}', |
||
36 | |||
37 | // The maximum file size of an upload in MB. |
||
38 | 'max_file_size' => '500' |
||
39 | |||
40 | ]; |
||
41 | |||
42 | private $key; |
||
43 | private $secret; |
||
44 | |||
45 | private $bucket; |
||
46 | private $region; |
||
47 | |||
48 | private $time = null; |
||
49 | |||
50 | private $credentials = null; |
||
51 | private $base64Policy = null; |
||
52 | private $signature = null; |
||
53 | |||
54 | /** |
||
55 | * Signature constructor. |
||
56 | * |
||
57 | * @param string $key the AWS API Key to use. |
||
58 | * @param string $secret the AWS API Secret to use. |
||
59 | * @param string $bucket the bucket to upload the file into. |
||
60 | * @param string $region the s3 region this bucket is within. More info: http://amzn.to/1FtPG6r |
||
61 | * @param array $options any additional options, like acl and success status. |
||
62 | */ |
||
63 | public function __construct($key, $secret, $bucket, $region = "us-east-1", $options = []) |
||
73 | |||
74 | /** |
||
75 | * Set the AWS Credentials |
||
76 | * |
||
77 | * @param string $key the AWS API Key to use. |
||
78 | * @param string $secret the AWS API Secret to use. |
||
79 | */ |
||
80 | public function setAwsCredentials($key, $secret) |
||
96 | |||
97 | /** |
||
98 | * Build the form url for sending files, this will include the bucket and the region name. |
||
99 | * |
||
100 | * @return string the s3 bucket's url. |
||
101 | */ |
||
102 | public function getFormUrl() |
||
115 | |||
116 | /** |
||
117 | * Get all options. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public function getOptions() |
||
125 | |||
126 | /** |
||
127 | * Set/overwrite any default options. |
||
128 | * |
||
129 | * @param $options |
||
130 | */ |
||
131 | public function setOptions($options) |
||
136 | |||
137 | /** |
||
138 | * Get an AWS Signature V4 generated. |
||
139 | * |
||
140 | * @return string the signature. |
||
141 | */ |
||
142 | public function getSignature() |
||
151 | |||
152 | /** |
||
153 | * Generate the necessary hidden inputs to go within the form. |
||
154 | * |
||
155 | * @param bool $addKey whether to add the 'key' input (filename), defaults to yes. |
||
156 | * |
||
157 | * @return array of the form inputs. |
||
158 | */ |
||
159 | public function getFormInputs($addKey = true) |
||
185 | |||
186 | /** |
||
187 | * Based on getFormInputs(), this will build up the html to go within the form. |
||
188 | * |
||
189 | * @return string html of hidden form inputs. |
||
190 | */ |
||
191 | public function getFormInputsAsHtml() |
||
199 | |||
200 | |||
201 | // Where the magic begins ;) |
||
202 | |||
203 | /** |
||
204 | * Step 1: Generate the Scope |
||
205 | */ |
||
206 | protected function generateScope() |
||
217 | |||
218 | /** |
||
219 | * Step 2: Generate a Base64 Policy |
||
220 | */ |
||
221 | protected function generatePolicy() |
||
240 | |||
241 | /** |
||
242 | * Step 3: Generate and sign the Signature (v4) |
||
243 | */ |
||
244 | protected function generateSignature() |
||
262 | |||
263 | |||
264 | // Helper functions |
||
265 | |||
266 | private function keyHash($date, $key, $raw = true) |
||
270 | |||
271 | private function populateTime() |
||
277 | |||
278 | private function mbToBytes($mb) |
||
285 | |||
286 | |||
287 | // Dates |
||
288 | |||
289 | private function getShortDateFormat() |
||
293 | |||
294 | private function getFullDateFormat() |
||
298 | |||
299 | private function getExpirationDate() |
||
304 | |||
305 | |||
306 | } |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.