| 1 |
dopry |
1.1 |
<?php |
| 2 |
|
|
|
| 3 |
|
|
/** |
| 4 |
|
|
* @file |
| 5 |
|
|
* Amazon S3 storage driver for filesystem. |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
|
| 9 |
|
|
/** |
| 10 |
|
|
* driver settings form. called by filesystem.module when creating a respository using this driver. |
| 11 |
|
|
* @param $settings |
| 12 |
|
|
* settings array from database. |
| 13 |
|
|
*/ |
| 14 |
|
|
|
| 15 |
|
|
function fileapi_driver_s3_settings_form($settings = array()) { |
| 16 |
|
|
//debug_msg($settings); |
| 17 |
|
|
$form = array( |
| 18 |
|
|
'#type' => 'fieldset', |
| 19 |
|
|
'#title' => t('Amazon S3 storage driver settings'), |
| 20 |
|
|
'#tree' => TRUE, |
| 21 |
|
|
); |
| 22 |
|
|
|
| 23 |
|
|
$form['driver'] = array( |
| 24 |
|
|
'#type' => 'hidden', |
| 25 |
|
|
'#value' => 's3', |
| 26 |
|
|
); |
| 27 |
|
|
|
| 28 |
|
|
$form['s3 url'] = array( |
| 29 |
|
|
'#type' => 'textfield', |
| 30 |
|
|
'#title' => t('Amazon S3 url'), |
| 31 |
|
|
'#default_value' => strlen($settings['s3 url']) ? $settings['s3 url'] : 'http://s3.amazonaws.com', |
| 32 |
|
|
); |
| 33 |
|
|
|
| 34 |
|
|
$form['awsID'] = array( |
| 35 |
|
|
'#type' => 'textfield', |
| 36 |
|
|
'#title' => t('AWS Access Key ID'), |
| 37 |
|
|
'#default_value' => $settings['awsID'], |
| 38 |
|
|
); |
| 39 |
|
|
|
| 40 |
|
|
$form['awsSecret'] = array( |
| 41 |
|
|
'#type' => 'textfield', |
| 42 |
|
|
'#title' => t('AWS Secret'), |
| 43 |
|
|
'#default_value' => $settings['awsSecret'], |
| 44 |
|
|
); |
| 45 |
|
|
|
| 46 |
|
|
return $form; |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
|
function fileapi_driver_s3_settings_form_validate() { |
| 50 |
|
|
} |
| 51 |
|
|
|
| 52 |
|
|
|
| 53 |
|
|
/** |
| 54 |
|
|
* test if a path is a file. |
| 55 |
|
|
* @param $path |
| 56 |
|
|
* path to be tested |
| 57 |
|
|
*/ |
| 58 |
|
|
function fileapi_driver_s3_is_file($settings, $path) { |
| 59 |
|
|
|
| 60 |
|
|
} |
| 61 |
|
|
|
| 62 |
|
|
/** |
| 63 |
|
|
* test if a path is a directory. |
| 64 |
|
|
* @param $path |
| 65 |
|
|
* path to be tested |
| 66 |
|
|
*/ |
| 67 |
|
|
function fileapi_driver_s3_is_dir($settings, $path) { |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
|
|
| 71 |
|
|
/** |
| 72 |
|
|
* create a directory using standard php functions |
| 73 |
|
|
* @param $path |
| 74 |
|
|
*/ |
| 75 |
|
|
function fileapi_driver_s3_mkdir($settings, $path) { |
| 76 |
dopry |
1.3 |
// there are technically no directories, so we can safely do |
| 77 |
|
|
// nothing here. |
| 78 |
|
|
return TRUE; |
| 79 |
dopry |
1.1 |
} |
| 80 |
|
|
|
| 81 |
dopry |
1.3 |
|
| 82 |
dopry |
1.1 |
/** |
| 83 |
|
|
* remove a directory using standard php functions |
| 84 |
|
|
* @param $path |
| 85 |
|
|
*/ |
| 86 |
|
|
|
| 87 |
|
|
function fileapi_driver_s3_rmdir($settings, $path) { |
| 88 |
dopry |
1.3 |
// there are technically no directories. |
| 89 |
|
|
// so we can test is there is a file in the 'directory' and return false here. |
| 90 |
dopry |
1.1 |
} |
| 91 |
|
|
|
| 92 |
|
|
|
| 93 |
|
|
/** |
| 94 |
|
|
* remove a file using standard php functions |
| 95 |
|
|
* @param $path |
| 96 |
|
|
*/ |
| 97 |
|
|
function fileapi_driver_s3_remove($settings, $path) { |
| 98 |
|
|
clearstatcache(); |
| 99 |
dopry |
1.3 |
|
| 100 |
dopry |
1.1 |
} |
| 101 |
|
|
|
| 102 |
|
|
/** |
| 103 |
|
|
* copy a file using standard php functions |
| 104 |
|
|
* @param $src |
| 105 |
|
|
* @param $dst |
| 106 |
|
|
*/ |
| 107 |
|
|
function fileapi_driver_s3_copy($settings, $src, $dst) { |
| 108 |
dopry |
1.3 |
|
| 109 |
dopry |
1.1 |
} |
| 110 |
|
|
|
| 111 |
|
|
/** |
| 112 |
|
|
* move a file using standard php functions |
| 113 |
|
|
* @param $src |
| 114 |
|
|
* @param $dst |
| 115 |
|
|
*/ |
| 116 |
|
|
function fileapi_driver_s3_move($settings, $src, $dst) { |
| 117 |
dopry |
1.3 |
|
| 118 |
dopry |
1.1 |
} |
| 119 |
|
|
|
| 120 |
|
|
/** |
| 121 |
|
|
* check if a file exists using standard php functions |
| 122 |
|
|
* @param $path |
| 123 |
|
|
*/ |
| 124 |
|
|
function fileapi_driver_s3_exists($settings, $path) { |
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
/** |
| 128 |
|
|
* touch a path and create a file there. |
| 129 |
|
|
* @param $path |
| 130 |
|
|
* path to be touched. |
| 131 |
|
|
*/ |
| 132 |
|
|
function fileapi_driver_s3_touch($settings, $path) { |
| 133 |
|
|
$tmp = file_directory_tmp() .'/'. basename($path); |
| 134 |
|
|
touch($tmp); |
| 135 |
|
|
$headers = array(); |
| 136 |
|
|
$headers['Content-Type'] = 'text/plain'; |
| 137 |
|
|
$body = file_get_contents($tmp); |
| 138 |
dopry |
1.3 |
s3_request('PUT', $path, $settings, array(), $body); |
| 139 |
|
|
unlink($tmp); |
| 140 |
dopry |
1.1 |
} |
| 141 |
|
|
|
| 142 |
|
|
|
| 143 |
|
|
/** |
| 144 |
|
|
* read the contents of a directory. |
| 145 |
|
|
* @param $path |
| 146 |
|
|
* directory to be read. |
| 147 |
|
|
* @return array |
| 148 |
|
|
* action results and array of directory contents. |
| 149 |
|
|
*/ |
| 150 |
|
|
function fileapi_driver_s3_readdir($settings, $path) { |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
|
/** |
| 154 |
|
|
* read the contents of a file. |
| 155 |
|
|
* @param $path |
| 156 |
|
|
* path of file to be read. |
| 157 |
|
|
* |
| 158 |
|
|
* NOTE: we are not including offset and maxlen since it is only supported in php > 5.1.0 |
| 159 |
|
|
* and our support target is 4.3.0. |
| 160 |
|
|
*/ |
| 161 |
|
|
function fileapi_driver_s3_fileread($settings, $path) { |
| 162 |
dopry |
1.3 |
return s3_request('GET', $path, $settings); |
| 163 |
dopry |
1.1 |
} |
| 164 |
|
|
|
| 165 |
|
|
function fileapi_driver_s3_filewrite($settings, $path, $data) { |
| 166 |
dopry |
1.3 |
$headers = array('Content-Type' => 'text/plain'); |
| 167 |
|
|
s3_request('PUT', $path, $settings, array(), $data); |
| 168 |
dopry |
1.1 |
} |
| 169 |
|
|
|
| 170 |
|
|
|
| 171 |
dopry |
1.3 |
function s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body= '') { |
| 172 |
|
|
$url = $settings['s3 url'] . $settings['bucket']; |
| 173 |
|
|
|
| 174 |
|
|
// Set date to proper format for S3. |
| 175 |
|
|
$headers['Date'] = gmdate(DATE_RFC822); |
| 176 |
|
|
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
| 177 |
|
|
|
| 178 |
|
|
// Build S3 authorization hash. |
| 179 |
|
|
$auth_string = $method ."\n\n". $headers['Content-type'] ."\n"; |
| 180 |
|
|
$auth_string .= $headers['Date'] ."\n". implode("\n",$amzheaders) ."\n". $resource; |
| 181 |
|
|
$signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string)); |
| 182 |
|
|
|
| 183 |
|
|
// Set date to proper format for S3. |
| 184 |
|
|
$headers['Date'] = gmdate(DATE_RFC822); |
| 185 |
|
|
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
| 186 |
|
|
|
| 187 |
|
|
$result = drupal_http_request($url .'/'. $resource, array_merge($headers, $amzheaders), $method, $body); |
| 188 |
|
|
drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>'); |
| 189 |
|
|
} |
| 190 |
dopry |
1.1 |
|
| 191 |
|
|
/** |
| 192 |
|
|
* Generate an sha1 signed hash of a string. |
| 193 |
|
|
* @param K |
| 194 |
|
|
* key to be used for the signature. |
| 195 |
|
|
* @param string |
| 196 |
|
|
* string to be signed. |
| 197 |
|
|
*/ |
| 198 |
|
|
function s3_hmac_sha1($K, $string) { |
| 199 |
|
|
// hash function |
| 200 |
|
|
$h = 'sha1'; |
| 201 |
|
|
// byte length of blocks operated on by hash function. |
| 202 |
|
|
$B = '64'; |
| 203 |
|
|
// byte length of hash function output. |
| 204 |
|
|
// seems unused in the HMAC algorithm |
| 205 |
|
|
$L = '20'; |
| 206 |
|
|
return s3_hmac($h, $B, $K, $string); |
| 207 |
|
|
} |
| 208 |
|
|
|
| 209 |
|
|
/** |
| 210 |
|
|
* HMAC implementation for drupal as per |
| 211 |
|
|
* http://www.ietf.org/rfc/rfc2104.txt |
| 212 |
|
|
* |
| 213 |
|
|
* @param $h |
| 214 |
|
|
* hashing function can be any |
| 215 |
|
|
* @param $B |
| 216 |
|
|
* block length iterated over by hashing algorithm |
| 217 |
|
|
* @param $K |
| 218 |
|
|
* key to be used for signing. |
| 219 |
|
|
* @param $string |
| 220 |
|
|
* string to be signed. |
| 221 |
|
|
*/ |
| 222 |
|
|
function s3_hmac($h, $B, $K, $string) { |
| 223 |
|
|
// Hash the key if it exceeds the blocklength. |
| 224 |
|
|
if (strlen($K) > $B) { |
| 225 |
|
|
// Pack that dirty string. |
| 226 |
|
|
$K = pack('H*', $h($K)); |
| 227 |
|
|
} |
| 228 |
|
|
// pad it with 0 bytes to block length |
| 229 |
|
|
$K = str_pad($K, $B, chr(0x00)); |
| 230 |
|
|
|
| 231 |
|
|
// Setup inner and outer pads as per the RFC. |
| 232 |
|
|
$ipad = str_repeat(chr(0x36), $B)^$K; |
| 233 |
|
|
$opad = str_repeat(chr(0x5C), $B)^$K; |
| 234 |
|
|
|
| 235 |
|
|
// Do inner concat XOR hashing. Pack that dirty string. |
| 236 |
|
|
$string = pack('H*', $h($ipad.$string)); |
| 237 |
|
|
|
| 238 |
|
|
// return outer concat XOR hash |
| 239 |
|
|
return $h($opad.$string); |
| 240 |
|
|
} |
| 241 |
dopry |
1.2 |
|
| 242 |
|
|
|
| 243 |
|
|
|