source: Dev/branches/rest-dojo-ui/client/dojox/data/s3/proxy.example-php @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 1.8 KB
Line 
1<?php
2// enter your Amazon S3 secret key and access key here:
3$accessKey = "access key";
4$secretAccessKey = "secret access key";
5
6
7
8$TARGET_WS = "http://s3.amazonaws.com";
9
10ob_start();
11
12require_once 'Crypt/HMAC.php';
13require_once 'HTTP/Request.php';
14
15$method = $_SERVER["REQUEST_METHOD"];
16if ($method == "PUT") {
17        $contentType = $_SERVER['CONTENT_TYPE'];
18}
19else {
20        $contentType ='';
21}
22$resource = str_replace($TARGET_WS, '', $_REQUEST['url']);
23$queryIndex = strpos($resource,'?'); // remove the query string
24if ($queryIndex) {
25        $resource = substr($resource,0,$queryIndex);
26}
27
28if (substr($resource,strlen($resource)-1,strlen($resource)) == '/') {
29        // remove the last slash
30        $resource = substr($resource,0,strlen($resource)-1);
31}
32$content = file_get_contents('php://input');
33
34$httpDate = gmdate("D, d M Y H:i:s T");
35$acl = "private";
36$stringToSign = "$method\n\n$contentType\n$httpDate\nx-amz-acl:$acl\n$resource";
37$hashObj =& new Crypt_HMAC($secretAccessKey, "sha1");
38$signature = hexTob64($hashObj->hash($stringToSign));
39
40$req =& new HTTP_Request($TARGET_WS . $resource);
41$req->setMethod($method);
42$req->addHeader("content-type", $contentType);
43$req->addHeader("Date", $httpDate);
44$req->addHeader("x-amz-acl", $acl);
45$req->addHeader("Authorization", "AWS " . $accessKey . ":" . $signature);
46if ($content != "") {
47        $req->setBody($content);
48}
49
50$req->sendRequest();
51
52$contentType = $req->getResponseHeader("content-type");
53header("content-type: $contentType");
54header('HTTP/1.1 ' . $req->getResponseCode() . ' Ok');
55
56ob_end_flush();
57
58$content = $req->getResponseBody();
59if ($content) {
60        print($content);
61}
62else {
63        print("\"success\"");
64}
65
66function hexTob64($str) {
67    $raw = '';
68    for ($i=0; $i < strlen($str); $i+=2) {
69        $raw .= chr(hexdec(substr($str, $i, 2)));
70    }
71    return base64_encode($raw);
72}
73
74?>
Note: See TracBrowser for help on using the repository browser.