PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/iwp-client/lib/amazon/aws/aws-sdk-php/src/Aws/S3/Model/PostObject.php

https://gitlab.com/treighton/wpgit
PHP | 275 lines | 116 code | 29 blank | 130 comment | 7 complexity | e44cbf161e7dcc583b5dcab8300a3587 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Aws\S3\Model;
  17. use Aws\Common\Enum\DateFormat;
  18. use Aws\S3\S3Client;
  19. use Guzzle\Common\Collection;
  20. use Guzzle\Http\Url;
  21. /**
  22. * Encapsulates the logic for getting the data for an S3 object POST upload form
  23. */
  24. class PostObject extends Collection
  25. {
  26. /**
  27. * @var S3Client The S3 client being used to sign the policy
  28. */
  29. protected $client;
  30. /**
  31. * @var string The bucket name where the object will be posted
  32. */
  33. protected $bucket;
  34. /**
  35. * @var array The <form> tag attributes as an array
  36. */
  37. protected $formAttributes;
  38. /**
  39. * @var array The form's <input> elements as an array
  40. */
  41. protected $formInputs;
  42. /**
  43. * @var string The raw json policy
  44. */
  45. protected $jsonPolicy;
  46. /**
  47. * Constructs the PostObject
  48. *
  49. * The options array accepts the following keys:
  50. *
  51. * - acl: The access control setting to apply to the uploaded file. Accepts any of the
  52. * CannedAcl constants
  53. * - Cache-Control: The Cache-Control HTTP header value to apply to the uploaded file
  54. * - Content-Disposition: The Content-Disposition HTTP header value to apply to the uploaded file
  55. * - Content-Encoding: The Content-Encoding HTTP header value to apply to the uploaded file
  56. * - Content-Type: The Content-Type HTTP header value to apply to the uploaded file. The default
  57. * value is `application/octet-stream`
  58. * - Expires: The Expires HTTP header value to apply to the uploaded file
  59. * - key: The location where the file should be uploaded to. The default value is
  60. * `^${filename}` which will use the name of the uploaded file
  61. * - policy: A raw policy in JSON format. By default, the PostObject creates one for you
  62. * - policy_callback: A callback used to modify the policy before encoding and signing it. The
  63. * method signature for the callback should accept an array of the policy data as
  64. * the 1st argument, (optionally) the PostObject as the 2nd argument, and return
  65. * the policy data with the desired modifications.
  66. * - success_action_redirect: The URI for Amazon S3 to redirect to upon successful upload
  67. * - success_action_status: The status code for Amazon S3 to return upon successful upload
  68. * - ttd: The expiration time for the generated upload form data
  69. * - x-amz-meta-*: Any custom meta tag that should be set to the object
  70. * - x-amz-server-side-encryption: The server-side encryption mechanism to use
  71. * - x-amz-storage-class: The storage setting to apply to the object
  72. * - x-amz-server-side​-encryption​-customer-algorithm: The SSE-C algorithm
  73. * - x-amz-server-side​-encryption​-customer-key: The SSE-C customer secret key
  74. * - x-amz-server-side​-encryption​-customer-key-MD5: The MD5 hash of the SSE-C customer secret key
  75. *
  76. * For the Cache-Control, Content-Disposition, Content-Encoding,
  77. * Content-Type, Expires, and key options, to use a "starts-with" comparison
  78. * instead of an equals comparison, prefix the value with a ^ (carat)
  79. * character
  80. *
  81. * @param S3Client $client
  82. * @param $bucket
  83. * @param array $options
  84. */
  85. public function __construct(S3Client $client, $bucket, array $options = array())
  86. {
  87. $this->setClient($client);
  88. $this->setBucket($bucket);
  89. parent::__construct($options);
  90. }
  91. /**
  92. * Analyzes the provided data and turns it into useful data that can be
  93. * consumed and used to build an upload form
  94. *
  95. * @return PostObject
  96. */
  97. public function prepareData()
  98. {
  99. // Validate required options
  100. $options = Collection::fromConfig($this->data, array(
  101. 'ttd' => '+1 hour',
  102. 'key' => '^${filename}',
  103. ));
  104. // Format ttd option
  105. $ttd = $options['ttd'];
  106. $ttd = is_numeric($ttd) ? (int) $ttd : strtotime($ttd);
  107. unset($options['ttd']);
  108. // If a policy or policy callback were provided, extract those from the options
  109. $rawJsonPolicy = $options['policy'];
  110. $policyCallback = $options['policy_callback'];
  111. unset($options['policy'], $options['policy_callback']);
  112. // Setup policy document
  113. $policy = array(
  114. 'expiration' => gmdate(DateFormat::ISO8601_S3, $ttd),
  115. 'conditions' => array(array('bucket' => $this->bucket))
  116. );
  117. // Configure the endpoint/action
  118. $url = Url::factory($this->client->getBaseUrl());
  119. if ($url->getScheme() === 'https' && strpos($this->bucket, '.') !== false) {
  120. // Use path-style URLs
  121. $url->setPath($this->bucket);
  122. } else {
  123. // Use virtual-style URLs
  124. $url->setHost($this->bucket . '.' . $url->getHost());
  125. }
  126. // Setup basic form
  127. $this->formAttributes = array(
  128. 'action' => (string) $url,
  129. 'method' => 'POST',
  130. 'enctype' => 'multipart/form-data'
  131. );
  132. $this->formInputs = array(
  133. 'AWSAccessKeyId' => $this->client->getCredentials()->getAccessKeyId()
  134. );
  135. // Add success action status
  136. $status = (int) $options->get('success_action_status');
  137. if ($status && in_array($status, array(200, 201, 204))) {
  138. $this->formInputs['success_action_status'] = (string) $status;
  139. $policy['conditions'][] = array(
  140. 'success_action_status' => (string) $status
  141. );
  142. unset($options['success_action_status']);
  143. }
  144. // Add other options
  145. foreach ($options as $key => $value) {
  146. $value = (string) $value;
  147. if ($value[0] === '^') {
  148. $value = substr($value, 1);
  149. $this->formInputs[$key] = $value;
  150. $value = preg_replace('/\$\{(\w*)\}/', '', $value);
  151. $policy['conditions'][] = array('starts-with', '$' . $key, $value);
  152. } else {
  153. $this->formInputs[$key] = $value;
  154. $policy['conditions'][] = array($key => $value);
  155. }
  156. }
  157. // Handle the policy
  158. $policy = is_callable($policyCallback) ? $policyCallback($policy, $this) : $policy;
  159. $this->jsonPolicy = $rawJsonPolicy ?: json_encode($policy);
  160. $this->applyPolicy();
  161. return $this;
  162. }
  163. /**
  164. * Sets the S3 client
  165. *
  166. * @param S3Client $client
  167. *
  168. * @return PostObject
  169. */
  170. public function setClient(S3Client $client)
  171. {
  172. $this->client = $client;
  173. return $this;
  174. }
  175. /**
  176. * Gets the S3 client
  177. *
  178. * @return S3Client
  179. */
  180. public function getClient()
  181. {
  182. return $this->client;
  183. }
  184. /**
  185. * Sets the bucket and makes sure it is a valid bucket name
  186. *
  187. * @param string $bucket
  188. *
  189. * @return PostObject
  190. */
  191. public function setBucket($bucket)
  192. {
  193. $this->bucket = $bucket;
  194. return $this;
  195. }
  196. /**
  197. * Gets the bucket name
  198. *
  199. * @return string
  200. */
  201. public function getBucket()
  202. {
  203. return $this->bucket;
  204. }
  205. /**
  206. * Gets the form attributes as an array
  207. *
  208. * @return array
  209. */
  210. public function getFormAttributes()
  211. {
  212. return $this->formAttributes;
  213. }
  214. /**
  215. * Gets the form inputs as an array
  216. *
  217. * @return array
  218. */
  219. public function getFormInputs()
  220. {
  221. return $this->formInputs;
  222. }
  223. /**
  224. * Gets the raw JSON policy
  225. *
  226. * @return string
  227. */
  228. public function getJsonPolicy()
  229. {
  230. return $this->jsonPolicy;
  231. }
  232. /**
  233. * Handles the encoding, singing, and injecting of the policy
  234. */
  235. protected function applyPolicy()
  236. {
  237. $jsonPolicy64 = base64_encode($this->jsonPolicy);
  238. $this->formInputs['policy'] = $jsonPolicy64;
  239. $this->formInputs['signature'] = base64_encode(hash_hmac(
  240. 'sha1',
  241. $jsonPolicy64,
  242. $this->client->getCredentials()->getSecretKey(),
  243. true
  244. ));
  245. }
  246. }