Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 * Copyright 2010-2012 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
 
17
 
18
class S3BrowserUpload extends AmazonS3
19
{
20
	/**
21
	 * The <code>POST</code> operation adds an object to a specified bucket using HTML forms. POST is an alternate
22
	 * form of <code>PUT</code> that enables browser-based uploads as a way of putting objects in buckets.
23
	 * Parameters that are passed to <code>PUT</code> via HTTP headers are instead passed as form fields to
24
	 * <code>POST</code> in the <code>multipart/form-data</code> encoded message body. You must have
25
	 * <code>WRITE</code> access on a bucket to add an object to it. Amazon S3 never stores partial objects: if
26
	 * you receive a successful response, you can be confident the entire object was stored.
27
	 *
28
	 * @param string $bucket (Required) The name of the bucket to use.
29
	 * @param string|integer $expires (Optional) The point in time when the upload form field should expire. The default value is <code>+1 hour</code>.
30
	 * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
31
	 * 	<li><code>acl</code> - <code>string</code> - Optional - The access control setting to apply to the uploaded file. Accepts any of the following constants: [Allowed values: <code>AmazonS3::ACL_PRIVATE</code>, <code>AmazonS3::ACL_PUBLIC</code>, <code>AmazonS3::ACL_OPEN</code>, <code>AmazonS3::ACL_AUTH_READ</code>, <code>AmazonS3::ACL_OWNER_READ</code>, <code>AmazonS3::ACL_OWNER_FULL_CONTROL</code>].</li>
32
	 * 	<li><code>Cache-Control</code> - <code>string</code> - Optional - The Cache-Control HTTP header value to apply to the uploaded file. To use a <code>starts-with</code> comparison instead of an <code>equals</code> comparison, prefix the value with a <code>^</code> (carat) character.</li>
33
	 * 	<li><code>Content-Disposition</code> - <code>string</code> - Optional - The Content-Disposition HTTP header value to apply to the uploaded file. To use a <code>starts-with</code> comparison instead of an <code>equals</code> comparison, prefix the value with a <code>^</code> (carat) character.</li>
34
	 * 	<li><code>Content-Encoding</code> - <code>string</code> - Optional - The Content-Encoding HTTP header value to apply to the uploaded file. To use a <code>starts-with</code> comparison instead of an <code>equals</code> comparison, prefix the value with a <code>^</code> (carat) character.</li>
35
	 * 	<li><code>Content-Type</code> - <code>string</code> - Optional - The Content-Type HTTP header value to apply to the uploaded file. The default value is <code>application/octet-stream</code>. To use a <code>starts-with</code> comparison instead of an <code>equals</code> comparison, prefix the value with a <code>^</code> (carat) character.</li>
36
	 * 	<li><code>Expires</code> - <code>string</code> - Optional - The Expires HTTP header value to apply to the uploaded file. To use a <code>starts-with</code> comparison instead of an <code>equals</code> comparison, prefix the value with a <code>^</code> (carat) character.</li>
37
	 * 	<li><code>key</code> - <code>string</code> - Optional - The location where the file should be uploaded to. The default value is <code>${filename}</code>.</li>
38
	 * 	<li><code>success_action_redirect</code> - <code>string</code> - Optional - The URI for Amazon S3 to redirect to upon successful upload.</li>
39
	 * 	<li><code>success_action_status</code> - <code>integer</code> - Optional - The status code for Amazon S3 to return upon successful upload.</li>
40
	 * 	<li><code>x-amz-server-side-encryption</code> - <code>string</code> - Optional - The server-side encryption mechanism to use. [Allowed values: <code>AES256</code>].</li>
41
	 * 	<li><code>x-amz-storage-class</code> - <code>string</code> - Optional - The storage setting to apply to the object. [Allowed values: <code>AmazonS3::STORAGE_STANDARD</code>, <code>AmazonS3::STORAGE_REDUCED</code>]. The default value is <code>AmazonS3::STORAGE_STANDARD</code>.</li>
42
	 * 	<li><code>x-amz-meta-*</code> - <code>mixed</code> - Optional - Any custom meta tag that should be set to the object.</li>
43
	 * </ul>
44
	 * @return array An array of fields that can be converted into markup.
45
	 * @link http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectPOST.html POST Object
46
	 */
47
	public function generate_upload_parameters($bucket, $expires = '+1 hour', $opt = null)
48
	{
49
		if (!$opt) $opt = array();
50
 
51
		// Policy document
52
		$policy = array(
53
			'conditions' => array(
54
				array('bucket' => $bucket),
55
			)
56
		);
57
 
58
		// Basic form
59
		$form = array();
60
		$form['form'] = array(
61
			'action' => $bucket . '.s3.amazonaws.com',
62
			'method' => 'POST',
63
			'enctype' => 'multipart/form-data'
64
		);
65
 
66
		// Inputs
67
		$form['inputs'] = array(
68
			'AWSAccessKeyId' => $this->key
69
		);
70
 
71
		// Expires
72
		if ($expires)
73
		{
74
			if (is_numeric($expires))
75
			{
76
				$expires = gmdate('j M Y, g:i a Z', (integer) $expires);
77
			}
78
 
79
			$expires = $this->util->convert_date_to_iso8601($expires);
80
			$policy['expiration'] = (string) $expires;
81
		}
82
 
83
		// Default values
84
		if (!isset($opt['key']))
85
		{
86
			$opt['key'] = '${filename}';
87
		}
88
 
89
		// Success Action Status
90
		if (isset($opt['success_action_status']) && !empty($opt['success_action_status']))
91
		{
92
			$form['inputs']['success_action_status'] = (string) $opt['success_action_status'];
93
			$policy['conditions'][] = array(
94
				'success_action_status' => (string) $opt['success_action_status']
95
			);
96
			unset($opt['success_action_status']);
97
		}
98
 
99
		// Other parameters
100
		foreach ($opt as $param_key => $param_value)
101
		{
102
			if ($param_value[0] === '^')
103
			{
104
				$form['inputs'][$param_key] = substr((string) $param_value, 1);
105
				$param_value = preg_replace('/\$\{(\w*)\}/', '', (string) $param_value);
106
				$policy['conditions'][] = array('starts-with', '$' . $param_key, (substr((string) $param_value, 1) ? substr((string) $param_value, 1) : ''));
107
			}
108
			else
109
			{
110
				$form['inputs'][$param_key] = (string) $param_value;
111
				$policy['conditions'][] = array(
112
					$param_key => (string) $param_value
113
				);
114
			}
115
		}
116
 
117
		// Add policy
118
		$json_policy = json_encode($policy);
119
		$json_policy_b64 = base64_encode($json_policy);
120
		$form['inputs']['policy'] = $json_policy_b64;
121
		$form['metadata']['json_policy'] = $json_policy;
122
 
123
		// Add signature
124
		$form['inputs']['signature'] = base64_encode(hash_hmac('sha1', $json_policy_b64, $this->secret_key, true));
125
 
126
		return $form;
127
	}
128
 
129
 
130
	/*%******************************************************************************************%*/
131
	// HELPERS
132
 
133
	/**
134
	 * Returns the protocol of the web page that this script is currently running on. This method only works
135
	 * correctly when run from a publicly-accessible web page.
136
	 */
137
	public static function protocol()
138
	{
139
		return (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on') ? 'https://' : 'http://';
140
	}
141
 
142
	/**
143
	 * Returns the domain (and port) of the web page that this script is currently running on. This method
144
	 * only works correctly when run from a publicly-accessible web page.
145
	 */
146
	public static function domain()
147
	{
148
		if (isset($_SERVER['SERVER_NAME']) && isset($_SERVER['SERVER_PORT']))
149
		{
150
			return $_SERVER['SERVER_NAME'] . ((integer) $_SERVER['SERVER_PORT'] === 80 ? '' : ':' . $_SERVER['SERVER_PORT']);
151
		}
152
 
153
		return null;
154
	}
155
 
156
	/**
157
	 * Returns the URI of the web page that this script is currently running on. This method only works
158
	 * correctly when run from a publicly-accessible web page.
159
	 */
160
	public static function current_uri()
161
	{
162
		if (isset($_SERVER['REQUEST_URI']))
163
		{
164
			$uri = self::protocol();
165
			$uri .= self::domain();
166
			$uri .= $_SERVER['REQUEST_URI'];
167
			return $uri;
168
		}
169
 
170
		return null;
171
	}
172
}