forked from born2c0de/ThalliumBackup-Cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3functions.php
200 lines (176 loc) · 5.98 KB
/
s3functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
function uploadObject($s3, $bucket, $key, $data, $acl = S3_ACL_PRIVATE, $contentType = "text/plain")
{
$try = 1;
$sleep = 1;
$res = "";
do
{
$res = $s3->create_object($bucket, $key,
array(
'body' => $data,
'acl' => 'private',
'contentType' => $contentType
));
if($res->isOK())
{
return true;
}
sleep($sleep);
$sleep *= 2;
}while(++$try < 6);
return $res;
}
function getSignedURL($resource, $timeout)
{
//This comes from key pair you generated for cloudfront
$keyPairId = "APKAIA3QRQOKVKEQDHZA";
$expires = time() + $timeout; //Time out in seconds
$json = '{"Statement":[{"Resource":"'.$resource.'","Condition":{"DateLessThan":{"AWS:EpochTime":'.$expires.'}}}]}';
//Read Cloudfront Private Key Pair
$fp=fopen("private_key.pem","r");
$priv_key=fread($fp,8192);
fclose($fp);
//Create the private key
//$key = openssl_get_privatekey($priv_key);
$key = openssl_get_privatekey("file://private_key.pem");
if(!$key)
{
echo "<p>Failed to load private key!</p>";
return;
}
//Sign the policy with the private key
if(!openssl_sign($json, $signed_policy, $key, OPENSSL_ALGO_SHA1))
{
echo '<p>Failed to sign policy: '.openssl_error_string().'</p>';
return;
}
//Create url safe signed policy
$base64_signed_policy = base64_encode($signed_policy);
$signature = str_replace(array('+','=','/'), array('-','_','~'), $base64_signed_policy);
//Construct the URL
$url = $resource.'?Expires='.$expires.'&Signature='.$signature.'&Key-Pair-Id='.$keyPairId;
return $url;
}
/**
* Function: get_private_object_url()
* Generates a time-limited and/or query signed request for a private file with additional optional restrictions.
*
* Parameters:
* $distribution_hostname - _string_ (Required) The hostname of the distribution.
* $file_pattern - _string_ (Required) The filename of the object. Query parameters can be included. You can use multi-character match wild cards (*) or a single-character match wild card (?) anywhere in the string.
* $expires - _integer_|_string_ (Required) The expiration time can be expressed either as a number of seconds since Unix Epoch, or any string that `strtotime()` can understand.
* $opt - _array_ (Optional) Associative array of parameters which can have the following keys:
*
* Keys for the $opt parameter:
* BecomeAvailable - _integer_|_string_ (Optional) The time when the private URL becomes active. Can be expressed either as a number of seconds since Unix Epoch, or any string that `strtotime()` can understand. This is the same as `DateLessThan` that the documentation discusses.
* IPAddress - _string_ (Optional) A single IP address to restrict the access to.
* Secure - _boolean_ (Optional) Whether or not to use HTTPS as the protocol scheme.
*
* Returns:
* _string_ The file URL with authentication parameters.
*
* See Also:
* [Serving Private Content](http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html)
*/
function get_private_object_url($distribution_hostname, $file_pattern, $expires, $opt = null)
{
if (!$opt) $opt = array();
$resource = '';
$expiration_key = 'Expires';
$expires = strtotime($expires);
$conjunction = (strpos($file_pattern, '?') === false ? '?' : '&');
// Determine the protocol scheme
switch (substr($distribution_hostname, 0, 1) === 's')
{
// Streaming
case 's':
$scheme = 'rtmp';
$resource = str_replace(array('%3F', '%3D', '%26', '%2F'), array('?', '=', '&', '/'), rawurlencode($file_pattern));
break;
// Default
case 'd':
default:
$scheme = 'http';
$scheme .= (isset($opt['Secure']) && $opt['Secure'] === true ? 's' : '');
$resource = $scheme . '://' . $distribution_hostname . '/' . str_replace(array('%3F', '%3D', '%26', '%2F'), array('?', '=', '&', '/'), rawurlencode($file_pattern));
break;
}
// Generate default policy
$raw_policy = array(
'Statement' => array(
array(
'Resource' => $resource,
'Condition' => array(
'DateLessThan' => array(
'AWS:EpochTime' => $expires
)
)
)
)
);
// Become Available
if (isset($opt['BecomeAvailable']))
{
// Switch to 'Policy' instead
$expiration_key = 'Policy';
// Update the policy
$raw_policy['Statement'][0]['Condition']['DateGreaterThan'] = array(
'AWS:EpochTime' => strtotime($opt['BecomeAvailable'])
);
}
// IP Address
if (isset($opt['IPAddress']))
{
// Switch to 'Policy' instead
$expiration_key = 'Policy';
// Update the policy
$raw_policy['Statement'][0]['Condition']['IpAddress'] = array(
'AWS:SourceIp' => $opt['IPAddress']
);
}
// Munge the policy
$json_policy = str_replace('\/', '/', json_encode($raw_policy));
$json_policy = decode_uhex($json_policy);
$encoded_policy = strtr(base64_encode($json_policy), '+=/', '-_~');
// Generate the signature
//Read Cloudfront Private Key Pair
$fp=fopen("private_key.pem","r");
$priv_key=fread($fp,8192);
fclose($fp);
openssl_sign($json_policy, $signature, $priv_key);
$signature = strtr(base64_encode($signature), '+=/', '-_~');
//added
//$expiration_key = "Policy";
return $scheme . '://' . $distribution_hostname . '/'
. str_replace(array('%3F', '%3D', '%26', '%2F'), array('?', '=', '&', '/'), rawurlencode($file_pattern))
. $conjunction
. ($expiration_key === 'Expires' ? ($expiration_key . '=' . $expires) : ($expiration_key . '=' . $encoded_policy))
. '&Key-Pair-Id=' . "APKAIA3QRQOKVKEQDHZA"
. '&Signature=' . $signature;
}
/**
* Function: decode_uhex()
* Decodes \uXXXX entities into their real unicode character equivalents.
*
* Parameters:
* $s - _string_ (Required) The string to decode.
*
* Returns:
* _string_ The decoded string.
*/
function decode_uhex($s)
{
preg_match_all('/\\\u([0-9a-f]{4})/i', $s, $matches);
$matches = $matches[count($matches) - 1];
$map = array();
foreach ($matches as $match)
{
if (!isset($map[$match]))
{
$map['\u' . $match] = html_entity_decode('&#' . hexdec($match) . ';', ENT_NOQUOTES, 'UTF-8');
}
}
return str_replace(array_keys($map), $map, $s);
}
?>