· 7 years ago · Jun 03, 2018, 07:00 PM
1<?php
2require_once 'cloudfusion/cloudfusion.class.php';
3
4class EasyAmazonSDB
5{
6 /* The CloudFusion SimpleDB object */
7 private $sdb;
8
9 /* Result of the last operation */
10 public $last_result = null;
11
12 /* Do we have the extended API? */
13 private $xapi = false;
14
15 function __construct($keyid = null, $secretkey = null, $region = 'default')
16 {
17 $this->sdb = new AmazonSDB($keyid, $secretkey);
18 $this->set_region($region);
19 $this->xapi = property_exists($this->sdb, 'extended_api');
20 }
21
22 public function set_region($region)
23 {
24 static $regions = array(
25 'us-east-1' => 'sdb.amazonaws.com',
26 'us-west-1' => 'sdb.us-west-1.amazonaws.com',
27 'eu-west-1' => 'sdb.eu-west-1.amazonaws.com',
28 'ap-southeast-1' => 'sdb.ap-southeast-1.amazonaws.com'
29 );
30 static $aliases = array(
31 'eu' => 'eu-west-1',
32 'apac' => 'ap-southeast-1',
33 'default' => 'us-east-1'
34 );
35
36 $region = strtolower($region);
37
38 // If we have a '.' assume hostname
39 if (strpos($region, '.') === FALSE) {
40 if (isset($aliases[$region])) {
41 $region = $aliases[$region];
42 }
43 if (isset($regions[$region])) {
44 $region = $regions[$region];
45 }
46 // No hostname now? fail
47 if (strpos($region, '.') === FALSE) {
48 return FALSE;
49 }
50 }
51 $this->sdb->set_hostname($region);
52 return TRUE;
53 }
54
55 public function create_domain($domain_name)
56 {
57 $this->last_result = $this->sdb->create_domain($domain_name);
58 if (!$this->last_result->isOK()) {
59 return FALSE;
60 }
61 return TRUE;
62 }
63
64 public function list_domains($max_domains = null, $next_token = null)
65 {
66 $opt = array();
67 if (!is_null($max_domains)) {
68 $opt['MaxNumberOfDomains'] = $max_domains;
69 }
70 if (!is_null($next_token)) {
71 $opt['NextToken'] = $next_token;
72 }
73 $r = $this->last_result = $this->sdb->list_domains($opt);
74 if (!$this->last_result->isOK()) {
75 return FALSE;
76 }
77 $ret = array('DomainName' => array());
78 if ($r->body->ListDomainsResult->NextToken) {
79 $ret['NextToken'] = $r->body->ListDomainsResult->NextToken;
80 }
81 foreach ($r->body->ListDomainsResult->DomainName as $d) {
82 $ret['DomainName'][] = (string) $d;
83 }
84 return $ret;
85 }
86
87 public function delete_domain($domain_name)
88 {
89 $this->last_result = $this->sdb->delete_domain($domain_name);
90 if (!$this->last_result->isOK()) {
91 return FALSE;
92 }
93 return TRUE;
94 }
95
96 public function domain_metadata($domain_name)
97 {
98 $r = $this->last_result = $this->sdb->domain_metadata($domain_name);
99 if (!$this->last_result->isOK()) {
100 return FALSE;
101 }
102 $ret = array();
103 foreach ($r->body->DomainMetadataResult->children() as $a) {
104 $n = $a->getName();
105 $v = (string) $a;
106 $ret[$n] = $v;
107 }
108 return $ret;
109 }
110
111 public function put_attributes($domain_name, $item_name, $keypairs, $replace = null, $expect= null)
112 {
113 if (!is_null($expect)) {
114 if (!$this->xapi) {
115 $this->last_result = 'Extended API not available';
116 return FALSE;
117 }
118 $this->last_result = $this->sdb->put_attributes($domain_name, $item_name, $keypairs, $replace, $expect);
119 } else {
120 $this->last_result = $this->sdb->put_attributes($domain_name, $item_name, $keypairs, $replace);
121 }
122 if (!$this->last_result->isOK()) {
123 return FALSE;
124 }
125 return TRUE;
126 }
127
128 public function batch_put_attributes($domain_name, $item_keypairs, $replace = null)
129 {
130 $this->last_result = $this->sdb->batch_put_attributes($domain_name, $item_keypairs, $replace);
131 if (!$this->last_result->isOK()) {
132 return FALSE;
133 }
134 return TRUE;
135 }
136
137 public function get_attributes($domain_name, $item_name, $keys = null, $consistent = false)
138 {
139 if ($consistent) {
140 if (!$this->xapi) {
141 $this->last_result = 'Extended API not available';
142 return FALSE;
143 }
144 $r = $this->last_result = $this->sdb->get_attributes($domain_name, $item_name, $keys, $consistent);
145 } else {
146 $r = $this->last_result = $this->sdb->get_attributes($domain_name, $item_name, $keys);
147 }
148 if (!$this->last_result->isOK()) {
149 return FALSE;
150 }
151 $attrs = array();
152 foreach ($r->body->GetAttributesResult->Attribute as $a) {
153 $n = (string)$a->Name;
154 $v = (string) $a->Value;
155 if (isset($attrs[$n])) {
156 if (!is_array($attrs[$n])) {
157 $attrs[$n] = array($attrs[$n]);
158 }
159 $attrs[$n][] = $v;
160 } else {
161 $attrs[$n] = $v;
162 }
163 }
164 return $attrs;
165 }
166
167 public function delete_attributes($domain_name, $item_name, $keys = null, $expect = null)
168 {
169 if (!is_null($expect)) {
170 if (!$this->xapi) {
171 $this->last_result = 'Extended API not available';
172 return FALSE;
173 }
174 $this->last_result = $this->sdb->delete_attributes($domain_name, $item_name, $keys, $expect);
175 } else {
176 $this->last_result = $this->sdb->delete_attributes($domain_name, $item_name, $keys);
177 }
178 if (!$this->last_result->isOK()) {
179 return FALSE;
180 }
181 return TRUE;
182 }
183
184
185 public function select($expression = null, $next_token = null, $consistent_read = false)
186 {
187 $opts = array();
188 if (!is_null($next_token)) {
189 $opts['NextToken'] = $next_token;
190 }
191 if ($consistent_read) {
192 $opts['ConsistentRead'] = 'true';
193 }
194
195 $r = $this->last_result = $this->sdb->select($expression, $opts);
196 if (!$this->last_result->isOK()) {
197 return FALSE;
198 }
199 $ret = array();
200 foreach ($r->body->SelectResult->Item as $item) {
201 $iname = (string) $item->Name;
202 $ret[$iname] = array();
203 foreach ($item->Attribute as $attr) {
204 $n = (string) $attr->Name;
205 $v = (string) $attr->Value;
206 if (isset($ret[$iname][$n])) {
207 if (!is_array($ret[$iname][$n])) {
208 $ret[$iname][$n] = array($ret[$iname][$n]);
209 }
210 $ret[$iname][$n][] = $v;
211 } else {
212 $ret[$iname][$n] = $v;
213 }
214 }
215 }
216 return $ret;
217 }
218
219}
220
221?>