/[drupal]/contributions/modules/fileapi/drivers/s3.driver
ViewVC logotype

Contents of /contributions/modules/fileapi/drivers/s3.driver

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download)
Wed Sep 27 07:40:54 2006 UTC (3 years, 2 months ago) by dopry
Branch: MAIN
Initial split fileapi and filesystem for head.
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     }
77    
78     /**
79     * remove a directory using standard php functions
80     * @param $path
81     */
82    
83     function fileapi_driver_s3_rmdir($settings, $path) {
84     }
85    
86    
87     /**
88     * remove a file using standard php functions
89     * @param $path
90     */
91     function fileapi_driver_s3_remove($settings, $path) {
92     clearstatcache();
93     }
94    
95     /**
96     * copy a file using standard php functions
97     * @param $src
98     * @param $dst
99     */
100     function fileapi_driver_s3_copy($settings, $src, $dst) {
101     }
102    
103     /**
104     * move a file using standard php functions
105     * @param $src
106     * @param $dst
107     */
108     function fileapi_driver_s3_move($settings, $src, $dst) {
109     }
110    
111     /**
112     * check if a file exists using standard php functions
113     * @param $path
114     */
115     function fileapi_driver_s3_exists($settings, $path) {
116     }
117    
118     /**
119     * touch a path and create a file there.
120     * @param $path
121     * path to be touched.
122     */
123     function fileapi_driver_s3_touch($settings, $path) {
124     $tmp = file_directory_tmp() .'/'. basename($path);
125     touch($tmp);
126     $headers = array();
127     $headers['Content-Type'] = 'text/plain';
128     $body = file_get_contents($tmp);
129     _s3_request('PUT', $path, $settings, array(), $body);
130     }
131    
132    
133     function _s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body= '') {
134    
135     $url = $settings['s3 url'] . $settings['bucket'];
136    
137     // Set date to proper format for S3.
138     $headers['Date'] = gmdate(DATE_RFC822);
139     $headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature;
140    
141     // Build S3 authorization hash.
142     $auth_string = $method ."\n\n". $headers['Content-type'] ."\n";
143     $auth_string .= $headers['Date'] ."\n". implode("\n",$amzheaders) ."\n". $resource;
144     $signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string));
145    
146     // Set date to proper format for S3.
147     $headers['Date'] = gmdate(DATE_RFC822);
148     $headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature;
149    
150     $result = drupal_http_request($url .'/'. $resource, array_merge($headers, $amzheaders), $method, $body);
151     drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>');
152    
153     }
154    
155    
156     /**
157     * read the contents of a directory.
158     * @param $path
159     * directory to be read.
160     * @return array
161     * action results and array of directory contents.
162     */
163     function fileapi_driver_s3_readdir($settings, $path) {
164     }
165    
166     /**
167     * read the contents of a file.
168     * @param $path
169     * path of file to be read.
170     *
171     * NOTE: we are not including offset and maxlen since it is only supported in php > 5.1.0
172     * and our support target is 4.3.0.
173     */
174     function fileapi_driver_s3_fileread($settings, $path) {
175     }
176    
177     function fileapi_driver_s3_filewrite($settings, $path, $data) {
178     }
179    
180    
181    
182     /**
183     * Generate an sha1 signed hash of a string.
184     * @param K
185     * key to be used for the signature.
186     * @param string
187     * string to be signed.
188     */
189     function s3_hmac_sha1($K, $string) {
190     // hash function
191     $h = 'sha1';
192     // byte length of blocks operated on by hash function.
193     $B = '64';
194     // byte length of hash function output.
195     // seems unused in the HMAC algorithm
196     $L = '20';
197     return s3_hmac($h, $B, $K, $string);
198     }
199    
200     /**
201     * HMAC implementation for drupal as per
202     * http://www.ietf.org/rfc/rfc2104.txt
203     *
204     * @param $h
205     * hashing function can be any
206     * @param $B
207     * block length iterated over by hashing algorithm
208     * @param $K
209     * key to be used for signing.
210     * @param $string
211     * string to be signed.
212     */
213     function s3_hmac($h, $B, $K, $string) {
214     // Hash the key if it exceeds the blocklength.
215     if (strlen($K) > $B) {
216     // Pack that dirty string.
217     $K = pack('H*', $h($K));
218     }
219     // pad it with 0 bytes to block length
220     $K = str_pad($K, $B, chr(0x00));
221    
222     // Setup inner and outer pads as per the RFC.
223     $ipad = str_repeat(chr(0x36), $B)^$K;
224     $opad = str_repeat(chr(0x5C), $B)^$K;
225    
226     // Do inner concat XOR hashing. Pack that dirty string.
227     $string = pack('H*', $h($ipad.$string));
228    
229     // return outer concat XOR hash
230     return $h($opad.$string);
231     }

  ViewVC Help
Powered by ViewVC 1.1.2