[483] | 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 | |
---|
| 10 | ob_start(); |
---|
| 11 | |
---|
| 12 | require_once 'Crypt/HMAC.php'; |
---|
| 13 | require_once 'HTTP/Request.php'; |
---|
| 14 | |
---|
| 15 | $method = $_SERVER["REQUEST_METHOD"]; |
---|
| 16 | if ($method == "PUT") { |
---|
| 17 | $contentType = $_SERVER['CONTENT_TYPE']; |
---|
| 18 | } |
---|
| 19 | else { |
---|
| 20 | $contentType =''; |
---|
| 21 | } |
---|
| 22 | $resource = str_replace($TARGET_WS, '', $_REQUEST['url']); |
---|
| 23 | $queryIndex = strpos($resource,'?'); // remove the query string |
---|
| 24 | if ($queryIndex) { |
---|
| 25 | $resource = substr($resource,0,$queryIndex); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | if (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); |
---|
| 46 | if ($content != "") { |
---|
| 47 | $req->setBody($content); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | $req->sendRequest(); |
---|
| 51 | |
---|
| 52 | $contentType = $req->getResponseHeader("content-type"); |
---|
| 53 | header("content-type: $contentType"); |
---|
| 54 | header('HTTP/1.1 ' . $req->getResponseCode() . ' Ok'); |
---|
| 55 | |
---|
| 56 | ob_end_flush(); |
---|
| 57 | |
---|
| 58 | $content = $req->getResponseBody(); |
---|
| 59 | if ($content) { |
---|
| 60 | print($content); |
---|
| 61 | } |
---|
| 62 | else { |
---|
| 63 | print("\"success\""); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | function 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 | ?> |
---|