proxy.example-php 1.8 KB

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