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 | // min: 1 (+1 second), max: 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) |
||
115 | |||
116 | /** |
||
117 | * Build the form url for sending files, this will include the bucket and the region name. |
||
118 | * |
||
119 | * @return string the s3 bucket's url. |
||
120 | */ |
||
121 | public function getFormUrl() |
||
134 | |||
135 | /** |
||
136 | * Get all options. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | public function getOptions() |
||
144 | |||
145 | /** |
||
146 | * Set/overwrite any default options. |
||
147 | * |
||
148 | * @param array $options any options to override. |
||
149 | */ |
||
150 | public function setOptions($options) |
||
158 | |||
159 | /** |
||
160 | * Get an AWS Signature V4 generated. |
||
161 | * |
||
162 | * @return string the aws v4 signature. |
||
163 | */ |
||
164 | public function getSignature() |
||
173 | |||
174 | /** |
||
175 | * Generate the necessary hidden inputs to go within the form. These inputs should match what's being send in |
||
176 | * the policy. |
||
177 | * |
||
178 | * @param bool $addKey whether to add the 'key' input (filename), defaults to yes. |
||
179 | * |
||
180 | * @return array of the form inputs. |
||
181 | */ |
||
182 | public function getFormInputs($addKey = true) |
||
207 | |||
208 | /** |
||
209 | * Based on getFormInputs(), this will build up the html to go within the form. |
||
210 | * |
||
211 | * @param bool $addKey whether to add the 'key' input (filename), defaults to yes. |
||
212 | * |
||
213 | * @return string html of hidden form inputs. |
||
214 | */ |
||
215 | public function getFormInputsAsHtml($addKey = true) |
||
223 | |||
224 | |||
225 | // Where the magic begins ;) |
||
226 | |||
227 | /** |
||
228 | * Step 1: Generate the Scope |
||
229 | */ |
||
230 | protected function generateScope() |
||
241 | |||
242 | /** |
||
243 | * Step 2: Generate a Base64 Policy |
||
244 | */ |
||
245 | protected function generatePolicy() |
||
274 | |||
275 | /** |
||
276 | * Step 3: Generate and sign the Signature (v4) |
||
277 | */ |
||
278 | protected function generateSignature() |
||
296 | |||
297 | |||
298 | // Helper functions |
||
299 | |||
300 | private function keyHash($date, $key, $raw = true) |
||
304 | |||
305 | private function populateTime() |
||
311 | |||
312 | private function mbToBytes($mb) |
||
319 | |||
320 | |||
321 | // Dates |
||
322 | |||
323 | private function getShortDateFormat() |
||
327 | |||
328 | private function getFullDateFormat() |
||
332 | |||
333 | private function getExpirationDate() |
||
346 | |||
347 | |||
348 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.