| 11 |
* We use the keys with a delimiter of '/' to create our heirarchy. |
* We use the keys with a delimiter of '/' to create our heirarchy. |
| 12 |
* |
* |
| 13 |
* @todo examine adding caching to save some requests to S3. |
* @todo examine adding caching to save some requests to S3. |
| 14 |
|
* @todo wrap amazon s3 functions into a single class with multiple instance and |
| 15 |
|
* caching support. |
| 16 |
* |
* |
| 17 |
*/ |
*/ |
| 18 |
|
|
| 31 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 32 |
'#title' => t('Amazon S3 storage driver settings'), |
'#title' => t('Amazon S3 storage driver settings'), |
| 33 |
'#tree' => TRUE, |
'#tree' => TRUE, |
| 34 |
|
'#validate' => array('fileapi_driver_s3_settings_form_validate' => array()), |
| 35 |
|
'#submit' => array('fileapi_driver_s3_settings_form_submit' => array()), |
| 36 |
); |
); |
| 37 |
|
|
| 38 |
$form['driver'] = array( |
$form['driver'] = array( |
| 59 |
'#default_value' => $settings['awsSecret'], |
'#default_value' => $settings['awsSecret'], |
| 60 |
); |
); |
| 61 |
|
|
| 62 |
$form['bucket'] = array( |
|
| 63 |
'#type' => 'textfield', |
if (isset($settings['awsID']) && isset($settings['awsSecret'])) { |
| 64 |
'#title' => t('S3 Bucket'), |
|
| 65 |
'#default_value' => $settings['bucket'], |
$buckets = s3_buckets($settings); |
| 66 |
); |
debug_msg($buckets, 'buckets'); |
| 67 |
|
// use !== FALSE to pass on empty array checks. |
| 68 |
|
if ($buckets !== FALSE) { |
| 69 |
|
$buckets['Create a New Bucket'] = t('Create a New Bucket'); |
| 70 |
|
$form['bucket'] = array( |
| 71 |
|
'#type' => 'select', |
| 72 |
|
'#title' => 'Use Existing S3 Bucket', |
| 73 |
|
'#options' => $buckets, |
| 74 |
|
'#default_value' => (isset($settings['bucket'])) ? $settings['bucket'] : 'Create a New Bucket', |
| 75 |
|
); |
| 76 |
|
|
| 77 |
|
$form['new_bucket'] = array( |
| 78 |
|
'#type' => 'textfield', |
| 79 |
|
'#title' => t('New S3 Bucket'), |
| 80 |
|
'#default_value' => '', |
| 81 |
|
); |
| 82 |
|
} |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
|
| 86 |
return $form; |
return $form; |
| 87 |
} |
} |
| 88 |
|
|
| 89 |
function fileapi_driver_s3_settings_form_validate() { |
function fileapi_driver_s3_settings_form_validate($element) { |
| 90 |
//@todo test for trailing slash in 's3 url'; |
//@todo test for trailing slash in 's3 url'; |
| 91 |
|
if (preg_match('/\/$/', $element['s3 url']['#value'])) { |
| 92 |
|
form_set_error('s3 url', t('The URL must not include a trailing slash.')); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
//@todo validate AWS credentials. |
| 98 |
|
$settings = array( |
| 99 |
|
's3 url' => $element['s3 url']['#value'], |
| 100 |
|
'awsID' => $element['awsID']['#value'], |
| 101 |
|
'awsSecret' => $element['awsSecret']['#value'], |
| 102 |
|
); |
| 103 |
|
|
| 104 |
|
debug_msg($settings); |
| 105 |
|
$result = s3_buckets($settings); |
| 106 |
|
debug_msg(htmlspecialchars($result)); |
| 107 |
|
if ($result === FALSE) { |
| 108 |
|
form_set_error('awsID', t('Your AWS credentials seem to be invalid.')); |
| 109 |
|
form_set_error('awsSecret',''); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
} |
} |
| 113 |
|
|
| 114 |
function fileapi_driver_s3_settings_form_submit() { |
//function fileapi_driver_s3_settings_form_submit($form_id, &$form_values) { |
| 115 |
//@todo create bucket if it doesn't exist. |
function fileapi_driver_s3_settings_form_submit($element) { |
| 116 |
|
$settings = array( |
| 117 |
|
's3 url' => $element['s3 url']['#value'], |
| 118 |
|
'awsID' => $element['awsID']['#value'], |
| 119 |
|
'awsSecret' => $element['awsSecret']['#value'], |
| 120 |
|
); |
| 121 |
|
// @todo create a new bucket if one doesn't exist... |
| 122 |
|
if (isset($element['awsID']['#value']) && isset($element['aws_Secret']['#value'])) { |
| 123 |
|
if ($element['bucket']['#value'] == 'Create A New Bucket') { |
| 124 |
|
s3_bucket_put($settings, $element['new_bucket']['#value']); |
| 125 |
|
$element['bucket']['#value'] = $element['new_bucket']['#value']; |
| 126 |
|
unset($element['new_bucket']['#value']); |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
|
else { |
| 130 |
|
return FALSE; |
| 131 |
|
} |
| 132 |
} |
} |
| 133 |
|
|
| 134 |
|
|
| 226 |
$headers['Content-Type'] = 'text/plain'; |
$headers['Content-Type'] = 'text/plain'; |
| 227 |
$body = file_get_contents($tmp); |
$body = file_get_contents($tmp); |
| 228 |
$result = s3_request('PUT', $path, $settings, array(), $body); |
$result = s3_request('PUT', $path, $settings, array(), $body); |
|
$xml = SimpleXMLElement($result['data']); |
|
| 229 |
unlink($tmp); |
unlink($tmp); |
| 230 |
|
if ($result) { |
| 231 |
|
return $TRUE; |
| 232 |
|
} |
| 233 |
|
else { |
| 234 |
|
return FALSE; |
| 235 |
|
} |
| 236 |
} |
} |
| 237 |
|
|
| 238 |
|
|
| 244 |
* action results and array of directory contents. |
* action results and array of directory contents. |
| 245 |
*/ |
*/ |
| 246 |
function fileapi_driver_s3_readdir($settings, $path) { |
function fileapi_driver_s3_readdir($settings, $path) { |
| 247 |
// $result = s3_request('GET', $settings['bucket'] .'?delimiter=/&key=$path', $settings); |
// $result = s3_request('GET', $settings['bucket'], $settings, array(), array(), NULL, array('prefix='. $path, 'delimiter=/'); |
| 248 |
} |
} |
| 249 |
|
|
| 250 |
/** |
/** |
| 266 |
return $result; |
return $result; |
| 267 |
} |
} |
| 268 |
|
|
| 269 |
function s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body=NULL) { |
|
| 270 |
|
|
| 271 |
|
function s3_buckets($settings) { |
| 272 |
|
$result = s3_request('GET','', $settings); |
| 273 |
|
if ($result) { |
| 274 |
|
$buckets = array(); |
| 275 |
|
$xml = simplexml_load_string($result); |
| 276 |
|
foreach($xml->Buckets->Bucket->Name as $Name) { |
| 277 |
|
$name = (string)$Name; |
| 278 |
|
$buckets[$name] = $name; |
| 279 |
|
} |
| 280 |
|
return $buckets; |
| 281 |
|
} |
| 282 |
|
return FALSE; |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
/** Operations on Buckets **/ |
| 286 |
|
function s3_bucket_put($settings, $bucket = '') { |
| 287 |
|
$result = s3_request('PUT', $bucket, $settings); |
| 288 |
|
return $result; |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
function s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body=NULL, $query = array()) { |
| 295 |
$url = $settings['s3 url']; |
$url = $settings['s3 url']; |
| 296 |
|
|
| 297 |
// Set date to proper format for S3. |
// Set date to proper format for S3. |
| 311 |
if (count($amzheaders)) { |
if (count($amzheaders)) { |
| 312 |
$auth_string .= implode("\n",$amzheaders) ."\n"; |
$auth_string .= implode("\n",$amzheaders) ."\n"; |
| 313 |
} |
} |
| 314 |
|
|
| 315 |
$auth_string .= '/'. $resource; |
$auth_string .= '/'. $resource; |
| 316 |
$signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string)); |
$signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string)); |
| 317 |
print "auth_string:\n$auth_string\n"; |
//print "auth_string:\n$auth_string\n"; |
| 318 |
print "signature: $signature\n"; |
//print "signature: $signature\n"; |
| 319 |
|
|
| 320 |
// Set date to proper format for S3. |
// Set date to proper format for S3. |
| 321 |
$headers['Date'] = gmdate(DATE_RFC822); |
$headers['Date'] = gmdate(DATE_RFC822); |
| 322 |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
| 323 |
|
|
| 324 |
|
if (count($query)) { |
| 325 |
|
$qs = '?'. implode('&', $query); |
| 326 |
|
} |
| 327 |
|
|
| 328 |
$result = drupal_http_request($url .'/'. $resource, array_merge($headers, $amzheaders), $method, $body); |
$result = drupal_http_request($url .'/'. $resource . $qs, array_merge($headers, $amzheaders), $method, $body); |
| 329 |
|
if ($result->code == '403') { |
| 330 |
|
//watchdog('fileapi', t('Amazon S3 Authentication Failed')); |
| 331 |
|
// should we log our string to sign, amazon's string to sign, and our signature here? |
| 332 |
|
return FALSE; |
| 333 |
|
} |
| 334 |
|
// clean up the start and finish lines from the XML data that S3 adds for some reason. |
| 335 |
|
$result = split("\n", trim($result->data)); |
| 336 |
|
unset($result[0]); |
| 337 |
|
unset($result[count($result)]); |
| 338 |
|
$result = implode("\n", $result); |
| 339 |
return $result; |
return $result; |
|
// drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>'); |
|
|
// @todo error handling. |
|
| 340 |
} |
} |
| 341 |
|
|
| 342 |
|
|