Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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