| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Credentials\CredentialsInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Uri; |
| 7 |
/** |
| 8 |
* @deprecated |
| 9 |
*/ |
| 10 |
class PostObject |
| 11 |
{ |
| 12 |
private $client; |
| 13 |
private $bucket; |
| 14 |
private $formAttributes; |
| 15 |
private $formInputs; |
| 16 |
private $jsonPolicy; |
| 17 |
/** |
| 18 |
* Constructs the PostObject. |
| 19 |
* |
| 20 |
* @param S3ClientInterface $client Client used with the POST object |
| 21 |
* @param string $bucket Bucket to use |
| 22 |
* @param array $formInputs Associative array of form input |
| 23 |
* fields. |
| 24 |
* @param string|array $jsonPolicy JSON encoded POST policy document. |
| 25 |
* The policy will be base64 encoded |
| 26 |
* and applied to the form on your |
| 27 |
* behalf. |
| 28 |
*/ |
| 29 |
public function __construct(S3ClientInterface $client, $bucket, array $formInputs, $jsonPolicy) |
| 30 |
{ |
| 31 |
$this->client = $client; |
| 32 |
$this->bucket = $bucket; |
| 33 |
if (\is_array($jsonPolicy)) { |
| 34 |
$jsonPolicy = \json_encode($jsonPolicy); |
| 35 |
} |
| 36 |
$this->jsonPolicy = $jsonPolicy; |
| 37 |
$this->formAttributes = ['action' => $this->generateUri(), 'method' => 'POST', 'enctype' => 'multipart/form-data']; |
| 38 |
$this->formInputs = $formInputs + ['key' => '${filename}']; |
| 39 |
$credentials = $client->getCredentials()->wait(); |
| 40 |
$this->formInputs += $this->getPolicyAndSignature($credentials); |
| 41 |
} |
| 42 |
/** |
| 43 |
* Gets the S3 client. |
| 44 |
* |
| 45 |
* @return S3ClientInterface |
| 46 |
*/ |
| 47 |
public function getClient() |
| 48 |
{ |
| 49 |
return $this->client; |
| 50 |
} |
| 51 |
/** |
| 52 |
* Gets the bucket name. |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function getBucket() |
| 57 |
{ |
| 58 |
return $this->bucket; |
| 59 |
} |
| 60 |
/** |
| 61 |
* Gets the form attributes as an array. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function getFormAttributes() |
| 66 |
{ |
| 67 |
return $this->formAttributes; |
| 68 |
} |
| 69 |
/** |
| 70 |
* Set a form attribute. |
| 71 |
* |
| 72 |
* @param string $attribute Form attribute to set. |
| 73 |
* @param string $value Value to set. |
| 74 |
*/ |
| 75 |
public function setFormAttribute($attribute, $value) |
| 76 |
{ |
| 77 |
$this->formAttributes[$attribute] = $value; |
| 78 |
} |
| 79 |
/** |
| 80 |
* Gets the form inputs as an array. |
| 81 |
* |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
public function getFormInputs() |
| 85 |
{ |
| 86 |
return $this->formInputs; |
| 87 |
} |
| 88 |
/** |
| 89 |
* Set a form input. |
| 90 |
* |
| 91 |
* @param string $field Field name to set |
| 92 |
* @param string $value Value to set. |
| 93 |
*/ |
| 94 |
public function setFormInput($field, $value) |
| 95 |
{ |
| 96 |
$this->formInputs[$field] = $value; |
| 97 |
} |
| 98 |
/** |
| 99 |
* Gets the raw JSON policy. |
| 100 |
* |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
public function getJsonPolicy() |
| 104 |
{ |
| 105 |
return $this->jsonPolicy; |
| 106 |
} |
| 107 |
private function generateUri() |
| 108 |
{ |
| 109 |
$uri = new Uri($this->client->getEndpoint()); |
| 110 |
if ($this->client->getConfig('use_path_style_endpoint') === \true || $uri->getScheme() === 'https' && \strpos($this->bucket, '.') !== \false) { |
| 111 |
// Use path-style URLs |
| 112 |
$uri = $uri->withPath("/{$this->bucket}"); |
| 113 |
} else { |
| 114 |
// Use virtual-style URLs |
| 115 |
$uri = $uri->withHost($this->bucket . '.' . $uri->getHost()); |
| 116 |
} |
| 117 |
return (string) $uri; |
| 118 |
} |
| 119 |
protected function getPolicyAndSignature(CredentialsInterface $creds) |
| 120 |
{ |
| 121 |
$jsonPolicy64 = \base64_encode($this->jsonPolicy); |
| 122 |
return ['AWSAccessKeyId' => $creds->getAccessKeyId(), 'policy' => $jsonPolicy64, 'signature' => \base64_encode(\hash_hmac('sha1', $jsonPolicy64, $creds->getSecretKey(), \true))]; |
| 123 |
} |
| 124 |
} |
| 125 |
|