PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/api/simpler_db.php

https://bitbucket.org/jdp/gimmee
PHP | 91 lines | 71 code | 9 blank | 11 comment | 0 complexity | cb058e376c863c6368f3984e13a2763b MD5 | raw file
  1. <?php
  2. class SimplerDB {
  3. private $access_key;
  4. private $secret_key;
  5. function __construct($access_key, $secret_key, $domain = null) {
  6. $this->access_key = $access_key;
  7. $this->secret_key = $secret_key;
  8. $this->params = array(
  9. 'AWSAccessKeyId' => $this->access_key,
  10. 'Version' => '2009-04-15',
  11. 'ZoneName.0' => 'us-east-1a',
  12. 'ZoneName.1' => 'us-east-1b',
  13. 'ZoneName.2' => 'us-east-1c',
  14. 'SignatureVersion' => 2,
  15. 'SignatureMethod' => 'HmacSHA256'
  16. );
  17. }
  18. function __call($name, $args) {
  19. return $this->request(ucfirst($name), count($args) > 0 ? $args[0] : array());
  20. }
  21. function uuid($prefix = '') {
  22. $chars = uniqid(md5(mt_rand()), true);
  23. $uuid = substr($chars, 0, 8) . '-';
  24. $uuid .= substr($chars, 8, 4) . '-';
  25. $uuid .= substr($chars, 12, 4) . '-';
  26. $uuid .= substr($chars, 16, 4) . '-';
  27. $uuid .= substr($chars, 20, 12);
  28. return $prefix . $uuid;
  29. }
  30. function batchPutAttributes($items, $params = array()) {
  31. $params = array_merge($this->itemsToParams($items), $params);
  32. return $this->request('BatchPutAttributes', $params);
  33. }
  34. /*
  35. function deleteAttributes($items, $params = array()) {
  36. $params = array_merge($this->itemsToParams($items), $params);
  37. return $this->request('DeleteAttributes', $params);
  38. }
  39. */
  40. private function itemsToParams($items) {
  41. $params = array();
  42. $item_no = 0;
  43. foreach ($items as $item => $attributes) {
  44. $attr_no = 0;
  45. $params["Item.{$item_no}.ItemName"] = $item;
  46. foreach ($attributes as $name => $value) {
  47. $params["Item.{$item_no}.Attribute.{$attr_no}.Name"] = $name;
  48. $params["Item.{$item_no}.Attribute.{$attr_no}.Value"] = $value;
  49. $attr_no++;
  50. }
  51. $item_no++;
  52. }
  53. return $params;
  54. }
  55. private function request($action, $qs_params = array()) {
  56. // Make sure extra query string params are factored in
  57. foreach ($qs_params as $key => $value) {
  58. $this->params[$key] = $value;
  59. }
  60. // Add a timestamp
  61. $this->params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  62. $this->params['Action'] = $action;
  63. // Generate a version 2 signature
  64. uksort($this->params, 'strnatcmp');
  65. $qstr = '';
  66. foreach ($this->params as $key => $value) {
  67. $qstr .= '&'.$key.'='.rawurlencode($value);
  68. }
  69. $qstr = substr($qstr, 1);
  70. $signature_v2 = "GET\nsdb.amazonaws.com\n/\n{$qstr}";
  71. $this->params['Signature'] = base64_encode(hash_hmac('sha256', $signature_v2, $this->secret_key, true));
  72. // Send cURL call
  73. $ch = curl_init();
  74. $request_url = 'https://sdb.amazonaws.com/?'.http_build_query($this->params);
  75. //echo $request_url . "<br />";
  76. curl_setopt($ch, CURLOPT_URL, $request_url);
  77. curl_setopt($ch, CURLOPT_HEADER, 0);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. $response = curl_exec($ch);
  80. return $response;
  81. }
  82. }