summaryrefslogtreecommitdiffstats
path: root/src/etc/inc/r53.class
blob: 89faa26ba2a075545f4f4ee6a0d147eced47e931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
<?php
/**
*
* Copyright (c) 2011, Dan Myers.
* Parts copyright (c) 2008, Donovan Schonknecht.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
*   this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
*   notice, this list of conditions and the following disclaimer in the
*   documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* This is a modified BSD license (the third clause has been removed).
* The BSD license may be found here:
* http://www.opensource.org/licenses/bsd-license.php
*
* Amazon Route 53 is a trademark of Amazon.com, Inc. or its affiliates.
*
* Route53 is based on Donovan Schonknecht's Amazon S3 PHP class, found here:
* http://undesigned.org.za/2007/10/22/amazon-s3-php-class
*
*/

/**
* Amazon Route53 PHP class
*
* @link http://sourceforge.net/projects/php-r53/
* version 0.9.0
*
*/
class Route53
{
	const API_VERSION = '2010-10-01';

	protected $__accessKey; // AWS Access key
	protected $__secretKey; // AWS Secret key
	protected $__host;

	public function getAccessKey() { return $this->__accessKey; }
	public function getSecretKey() { return $this->__secretKey; }
	public function getHost() { return $this->__host; }

	protected $__verifyHost = 1;
	protected $__verifyPeer = 1;

	// verifyHost and verifyPeer determine whether curl verifies ssl certificates.
	// It may be necessary to disable these checks on certain systems.
	// These only have an effect if SSL is enabled.
	public function verifyHost() { return $this->__verifyHost; }
	public function enableVerifyHost($enable = true) { $this->__verifyHost = $enable; }

	public function verifyPeer() { return $this->__verifyPeer; }
	public function enableVerifyPeer($enable = true) { $this->__verifyPeer = $enable; }

	/**
	* Constructor
	*
	* @param string $accessKey Access key
	* @param string $secretKey Secret key
	* @return void
	*/
	public function __construct($accessKey = null, $secretKey = null, $host = 'route53.amazonaws.com') {
		if ($accessKey !== null && $secretKey !== null) {
			$this->setAuth($accessKey, $secretKey);
		}
		$this->__host = $host;
	}

	/**
	* Set AWS access key and secret key
	*
	* @param string $accessKey Access key
	* @param string $secretKey Secret key
	* @return void
	*/
	public function setAuth($accessKey, $secretKey) {
		$this->__accessKey = $accessKey;
		$this->__secretKey = $secretKey;
	}

	/**
	* Lists the hosted zones on the account
	*
	* @param string marker A pagination marker returned by a previous truncated call
	* @param int maxItems The maximum number of items per page.  The service uses min($maxItems, 100).
	* @return A list of hosted zones
	*/
	public function listHostedZones($marker = null, $maxItems = 100) {
		$rest = new Route53Request($this, 'hostedzone', 'GET');

		if($marker !== null) {
			$rest->setParameter('marker', $marker);
		}
		if($maxItems !== 100) {
			$rest->setParameter('maxitems', $maxItems);
		}

		$rest = $rest->getResponse();
		if($rest->error === false && $rest->code !== 200) {
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
		}
		if($rest->error !== false) {
			$this->__triggerError('listHostedZones', $rest->error);
			return false;
		}

		$response = array();
		if (!isset($rest->body))
		{
			return $response;
		}

		$zones = array();
		foreach($rest->body->HostedZones->HostedZone as $z)
		{
			$zones[] = $this->parseHostedZone($z);
		}
		$response['HostedZone'] = $zones;

		if(isset($rest->body->MaxItems)) {
			$response['MaxItems'] = (string)$rest->body->MaxItems;
		}

		if(isset($rest->body->IsTruncated)) {
			$response['IsTruncated'] = (string)$rest->body->IsTruncated;
			if($response['IsTruncated'] == 'true') {
				$response['NextMarker'] = (string)$rest->body->NextMarker;
			}
		}

		return $response;
	}

	/**
	* Retrieves information on a specified hosted zone
	*
	* @param string zoneId The id of the hosted zone, as returned by CreateHostedZoneResponse or ListHostedZoneResponse
	*                      In other words, if ListHostedZoneResponse shows the zone's Id as '/hostedzone/Z1PA6795UKMFR9',
	*                      then that full value should be passed here, including the '/hostedzone/' prefix.
	* @return A data structure containing information about the specified zone
	*/
	public function getHostedZone($zoneId) {
		// we'll strip off the leading forward slash, so we can use it as the action directly.
		$zoneId = trim($zoneId, '/');

		$rest = new Route53Request($this, $zoneId, 'GET');

		$rest = $rest->getResponse();
		if($rest->error === false && $rest->code !== 200) {
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
		}
		if($rest->error !== false) {
			$this->__triggerError('getHostedZone', $rest->error);
			return false;
		}

		$response = array();
		if (!isset($rest->body))
		{
			return $response;
		}

		$response['HostedZone'] = $this->parseHostedZone($rest->body->HostedZone);
		$response['NameServers'] = $this->parseDelegationSet($rest->body->DelegationSet);

		return $response;
	}

	/**
	* Creates a new hosted zone
	*
	* @param string name The name of the hosted zone (e.g. "example.com.")
	* @param string reference A user-specified unique reference for this request
	* @param string comment An optional user-specified comment to attach to the zone
	* @return A data structure containing information about the newly created zone
	*/
	public function createHostedZone($name, $reference, $comment = '') {
		// hosted zone names must end with a period, but people will forget this a lot...
		if(strrpos($name, '.') != (strlen($name) - 1)) {
			$name .= '.';
		}

		$data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		$data .= '<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/'.Route53::API_VERSION."/\">\n";
		$data .= '<Name>'.$name."</Name>\n";
		$data .= '<CallerReference>'.$reference."</CallerReference>\n";
		if(strlen($comment) > 0) {
			$data .= "<HostedZoneConfig>\n";
			$data .= '<Comment>'.$comment."</Comment>\n";
			$data .= "</HostedZoneConfig>\n";
		}
		$data .= "</CreateHostedZoneRequest>\n";

		$rest = new Route53Request($this, 'hostedzone', 'POST', $data);

		$rest = $rest->getResponse();

		if($rest->error === false && !in_array($rest->code, array(200, 201, 202, 204)) ) {
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
		}
		if($rest->error !== false) {
			$this->__triggerError('createHostedZone', $rest->error);
			return false;
		}

		$response = array();
		if (!isset($rest->body))
		{
			return $response;
		}

		$response['HostedZone'] = $this->parseHostedZone($rest->body->HostedZone);
		$response['ChangeInfo'] = $this->parseChangeInfo($rest->body->ChangeInfo);
		$response['NameServers'] = $this->parseDelegationSet($rest->body->DelegationSet);

		return $response;
	}

	/**
	* Retrieves information on a specified hosted zone
	*
	* @param string zoneId The id of the hosted zone, as returned by CreateHostedZoneResponse or ListHostedZoneResponse
	*                      In other words, if ListHostedZoneResponse shows the zone's Id as '/hostedzone/Z1PA6795UKMFR9',
	*                      then that full value should be passed here, including the '/hostedzone/' prefix.
	* @return The change request data corresponding to this delete
	*/
	public function deleteHostedZone($zoneId) {
		// we'll strip off the leading forward slash, so we can use it as the action directly.
		$zoneId = trim($zoneId, '/');

		$rest = new Route53Request($this, $zoneId, 'DELETE');

		$rest = $rest->getResponse();
		if($rest->error === false && $rest->code !== 200) {
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
		}
		if($rest->error !== false) {
			$this->__triggerError('deleteHostedZone', $rest->error);
			return false;
		}

		if (!isset($rest->body))
		{
			return array();
		}

		return $this->parseChangeInfo($rest->body->ChangeInfo);
	}

	/**
	* Retrieves a list of resource record sets for a given zone
	*
	* @param string zoneId The id of the hosted zone, as returned by CreateHostedZoneResponse or ListHostedZoneResponse
	*                      In other words, if ListHostedZoneResponse shows the zone's Id as '/hostedzone/Z1PA6795UKMFR9',
	*                      then that full value should be passed here, including the '/hostedzone/' prefix.
	* @param string type The type of resource record set to begin listing from. If this is specified, $name must also be specified.
	*                    Must be one of: A, AAAA, CNAME, MX, NS, PTR, SOA, SPF, SRV, TXT
	* @param string name The name at which to begin listing resource records (in the lexographic order of records).
	* @param int maxItems The maximum number of results to return.  The service uses min($maxItems, 100).
	* @return The list of matching resource record sets
	*/
	public function listResourceRecordSets($zoneId, $type = '', $name = '', $maxItems = 100) {
		// we'll strip off the leading forward slash, so we can use it as the action directly.
		$zoneId = trim($zoneId, '/');

		$rest = new Route53Request($this, $zoneId.'/rrset', 'GET');

		if(strlen($type) > 0) {
			$rest->setParameter('type', $type);
		}
		if(strlen($name) > 0) {
			$rest->setParameter('name', $name);
		}
		if($maxItems != 100) {
			$rest->setParameter('maxitems', $maxItems);
		}

		$rest = $rest->getResponse();
		if($rest->error === false && $rest->code !== 200) {
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
		}
		if($rest->error !== false) {
			$this->__triggerError('listResourceRecordSets', $rest->error);
			return false;
		}

		$response = array();
		if (!isset($rest->body))
		{
			return $response;
		}

		$recordSets = array();
		foreach($rest->body->ResourceRecordSets->ResourceRecordSet as $set) {
			$recordSets[] = $this->parseResourceRecordSet($set);
		}

		$response['ResourceRecordSets'] = $recordSets;

		if(isset($rest->body->MaxItems)) {
			$response['MaxItems'] = (string)$rest->body->MaxItems;
		}

		if(isset($rest->body->IsTruncated)) {
			$response['IsTruncated'] = (string)$rest->body->IsTruncated;
			if($response['IsTruncated'] == 'true') {
				$response['NextRecordName'] = (string)$rest->body->NextRecordName;
				$response['NextRecordType'] = (string)$rest->body->NextRecordType;
			}
		}

		return $response;
	}

	/**
	* Makes the specified resource record set changes (create or delete).
	*
	* @param string zoneId The id of the hosted zone, as returned by CreateHostedZoneResponse or ListHostedZoneResponse
	*                      In other words, if ListHostedZoneResponse shows the zone's Id as '/hostedzone/Z1PA6795UKMFR9',
	*                      then that full value should be passed here, including the '/hostedzone/' prefix.
	* @param array changes An array of change objects, as they are returned by the prepareChange utility method.
	*                      You may also pass a single change object.
	* @param string comment An optional comment to attach to the change request
	* @return The status of the change request
	*/
	public function changeResourceRecordSets($zoneId, $changes, $comment = '') {
		// we'll strip off the leading forward slash, so we can use it as the action directly.
		$zoneId = trim($zoneId, '/');

		$data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		$data .= '<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/'.Route53::API_VERSION."/\">\n";
		$data .= "<ChangeBatch>\n";

		if(strlen($comment) > 0) {
			$data .= '<Comment>'.$comment."</Comment>\n";
		}

                if(!is_array($changes)) {
			$changes = array($changes);
		}

		$data .= "<Changes>\n";
		foreach($changes as $change) {
			$data .= $change;
		}
		$data .= "</Changes>\n";

		$data .= "</ChangeBatch>\n";
		$data .= "</ChangeResourceRecordSetsRequest>\n";

		$rest = new Route53Request($this, $zoneId.'/rrset', 'POST', $data);

		$rest = $rest->getResponse();
		if($rest->error === false && !in_array($rest->code, array(200, 201, 202, 204))) {
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
		}
		if($rest->error !== false) {
			$this->__triggerError('changeResourceRecordSets', $rest->error);
			return false;
		}

		if (!isset($rest->body))
		{
			return array();
		}

		return $this->parseChangeInfo($rest->body->ChangeInfo);
	}

	/**
	* Retrieves information on a specified change request
	*
	* @param string changeId The id of the change, as returned by CreateHostedZoneResponse or ChangeResourceRecordSets
	*                      In other words, if CreateHostedZoneResponse showed the change's Id as '/change/C2682N5HXP0BZ4',
	*                      then that full value should be passed here, including the '/change/' prefix.
	* @return The status of the change request
	*/
	public function getChange($changeId) {
		// we'll strip off the leading forward slash, so we can use it as the action directly.
		$zoneId = trim($changeId, '/');

		$rest = new Route53Request($this, $changeId, 'GET');

		$rest = $rest->getResponse();
		if($rest->error === false && $rest->code !== 200) {
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
		}
		if($rest->error !== false) {
			$this->__triggerError('getChange', $rest->error);
			return false;
		}

		if (!isset($rest->body))
		{
			return array();
		}

		return $this->parseChangeInfo($rest->body->ChangeInfo);
	}



	/**
	* Utility function to parse a HostedZone tag structure
	*/
	private function parseHostedZone($tag) {
		$zone = array();
		$zone['Id'] = (string)$tag->Id;
		$zone['Name'] = (string)$tag->Name;
		$zone['CallerReference'] = (string)$tag->CallerReference;

		// these might always be set, but check just in case, since
		// their values are option on CreateHostedZone requests
		if(isset($tag->Config) && isset($tag->Config->Comment)) {
			$zone['Config'] = array('Comment' => (string)$tag->Config->Comment);
		}

		return $zone;
	}

	/**
	* Utility function to parse a ChangeInfo tag structure
	*/
	private function parseChangeInfo($tag) {
		$info = array();
		$info['Id'] = (string)$tag->Id;
		$info['Status'] = (string)$tag->Status;
		$info['SubmittedAt'] = (string)$tag->SubmittedAt;
		return $info;
	}

	/**
	* Utility function to parse a DelegationSet tag structure
	*/
	private function parseDelegationSet($tag) {
		$servers = array();
		foreach($tag->NameServers->NameServer as $ns) {
			$servers[] = (string)$ns;
		}
		return $servers;
	}

	/**
	* Utility function to parse a ResourceRecordSet tag structure
	*/
	private function parseResourceRecordSet($tag) {
		$rrs = array();
		$rrs['Name'] = (string)$tag->Name;
		$rrs['Type'] = (string)$tag->Type;
		$rrs['TTL'] = (string)$tag->TTL;
		$rrs['ResourceRecords'] = array();
		foreach($tag->ResourceRecords->ResourceRecord as $rr) {
			$rrs['ResourceRecords'][] = (string)$rr->Value;
		}
		return $rrs;
	}

	/**
	* Utility function to prepare a Change object for ChangeResourceRecordSets requests.
	* All fields are required.
	*
	* @param string action The action to perform.  One of: CREATE, DELETE
	* @param string name The name to perform the action on.
	*                    If it does not end with '.', then AWS treats the name as relative to the zone root.
	* @param string type The type of record being modified.
	*                    Must be one of: A, AAAA, CNAME, MX, NS, PTR, SOA, SPF, SRV, TXT
	* @param int ttl The time-to-live value for this record, in seconds.
	* @param array records An array of resource records to attach to this change.
	*                      Each member of this array can either be a string, or an array of strings.
	*                      Passing an array of strings will attach multiple values to a single resource record.
	*                      If a single string is passed as $records instead of an array,
	*                      it will be treated as a single-member array.
	* @return object An opaque object containing the change request.
	*                Do not write code that depends on the contents of this object, as it may change at any time.
	*/
	public function prepareChange($action, $name, $type, $ttl, $records) {
		$change = "<Change>\n";
		$change .= '<Action>'.$action."</Action>\n";
		$change .= "<ResourceRecordSet>\n";
		$change .= '<Name>'.$name."</Name>\n";
		$change .= '<Type>'.$type."</Type>\n";
		$change .= '<TTL>'.$ttl."</TTL>\n";
		$change .= "<ResourceRecords>\n";

		if(!is_array($records)) {
			$records = array($records);
		}

		foreach($records as $record) {
			$change .= "<ResourceRecord>\n";
			if(is_array($record)) {
				foreach($record as $value) {
					$change .= '<Value>'.$value."</Value>\n";
				}
			}
			else {
				$change .= '<Value>'.$record."</Value>\n";
			}
			$change .= "</ResourceRecord>\n";
		}

		$change .= "</ResourceRecords>\n";
		$change .= "</ResourceRecordSet>\n";
		$change .= "</Change>\n";

		return $change;
	}

	/**
	* Trigger an error message
	*
	* @internal Used by member functions to output errors
	* @param array $error Array containing error information
	* @return string
	*/
	public function __triggerError($functionname, $error)
	{
		if($error == false) {
			trigger_error(sprintf("Route53::%s(): Encountered an error, but no description given", $functionname), E_USER_WARNING);
		}
		else if(isset($error['curl']) && $error['curl'])
		{
			trigger_error(sprintf("Route53::%s(): %s %s", $functionname, $error['code'], $error['message']), E_USER_WARNING);
		}
		else if(isset($error['Error']))
		{
			$e = $error['Error'];
			$message = sprintf("Route53::%s(): %s - %s: %s\nRequest Id: %s\n", $functionname, $e['Type'], $e['Code'], $e['Message'], $error['RequestId']);
			trigger_error($message, E_USER_WARNING);
		}
	}

	/**
	* Callback handler for 503 retries.
	*
	* @internal Used by SimpleDBRequest to call the user-specified callback, if set
	* @param $attempt The number of failed attempts so far
	* @return The retry delay in microseconds, or 0 to stop retrying.
	*/
	public function __executeServiceTemporarilyUnavailableRetryDelay($attempt)
	{
		if(is_callable($this->__serviceUnavailableRetryDelayCallback)) {
			$callback = $this->__serviceUnavailableRetryDelayCallback;
			return $callback($attempt);
		}
		return 0;
	}
}

final class Route53Request
{
	private $r53, $action, $verb, $data, $parameters = array();
	public $response;

	/**
	* Constructor
	*
	* @param string $r53 The Route53 object making this request
	* @param string $action SimpleDB action
	* @param string $verb HTTP verb
	* @param string $data For POST requests, the data being posted (optional)
	* @return mixed
	*/
	function __construct($r53, $action, $verb, $data = '') {
		$this->r53 = $r53;
		$this->action = $action;
		$this->verb = $verb;
		$this->data = $data;
		$this->response = new STDClass;
		$this->response->error = false;
	}

	/**
	* Set request parameter
	*
	* @param string  $key Key
	* @param string  $value Value
	* @param boolean $replace Whether to replace the key if it already exists (default true)
	* @return void
	*/
	public function setParameter($key, $value, $replace = true) {
		if(!$replace && isset($this->parameters[$key]))
		{
			$temp = (array)($this->parameters[$key]);
			$temp[] = $value;
			$this->parameters[$key] = $temp;
		}
		else
		{
			$this->parameters[$key] = $value;
		}
	}

	/**
	* Get the response
	*
	* @return object | false
	*/
	public function getResponse() {

		$params = array();
		foreach ($this->parameters as $var => $value)
		{
			if(is_array($value))
			{
				foreach($value as $v)
				{
					$params[] = $var.'='.$this->__customUrlEncode($v);
				}
			}
			else
			{
				$params[] = $var.'='.$this->__customUrlEncode($value);
			}
		}

		sort($params, SORT_STRING);

		$query = implode('&', $params);

		// must be in format 'Sun, 06 Nov 1994 08:49:37 GMT'
		$date = gmdate('D, d M Y H:i:s e');

		$headers = array();
		$headers[] = 'Date: '.$date;
		$headers[] = 'Host: '.$this->r53->getHost();

		$auth = 'AWS3-HTTPS AWSAccessKeyId='.$this->r53->getAccessKey();
		$auth .= ',Algorithm=HmacSHA256,Signature='.$this->__getSignature($date);
		$headers[] = 'X-Amzn-Authorization: '.$auth;

		$url = 'https://'.$this->r53->getHost().'/'.Route53::API_VERSION.'/'.$this->action.'?'.$query;

		// Basic setup
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_USERAGENT, 'Route53/php');

		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, ($this->r53->verifyHost() ? 1 : 0));
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, ($this->r53->verifyPeer() ? 1 : 0));

		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

		// Request types
		switch ($this->verb) {
			case 'GET': break;
			case 'POST':
				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
				if(strlen($this->data) > 0) {
					curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
					$headers[] = 'Content-Type: text/plain';
					$headers[] = 'Content-Length: '.strlen($this->data);
				}
			break;
			case 'DELETE':
				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
			break;
			default: break;
		}
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_HEADER, false);

		// Execute, grab errors
		if (curl_exec($curl)) {
			$this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
		} else {
			$this->response->error = array(
				'curl' => true,
				'code' => curl_errno($curl),
				'message' => curl_error($curl),
				'resource' => $this->resource
			);
		}

		@curl_close($curl);

		// Parse body into XML
		if ($this->response->error === false && isset($this->response->body)) {
			$this->response->body = simplexml_load_string($this->response->body);

			// Grab Route53 errors
			if (!in_array($this->response->code, array(200, 201, 202, 204))
				&& isset($this->response->body->Error)) {
				$error = $this->response->body->Error;
				$output = array();
				$output['curl'] = false;
				$output['Error'] = array();
				$output['Error']['Type'] = (string)$error->Type;
				$output['Error']['Code'] = (string)$error->Code;
				$output['Error']['Message'] = (string)$error->Message;
				$output['RequestId'] = (string)$this->response->body->RequestId;

				$this->response->error = $output;
				unset($this->response->body);
			}
		}

		return $this->response;
	}

	/**
	* CURL write callback
	*
	* @param resource &$curl CURL resource
	* @param string &$data Data
	* @return integer
	*/
	private function __responseWriteCallback(&$curl, &$data) {
		$this->response->body .= $data;
		return strlen($data);
	}

	/**
	* Contributed by afx114
	* URL encode the parameters as per http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?Query_QueryAuth.html
	* PHP's rawurlencode() follows RFC 1738, not RFC 3986 as required by Amazon. The only difference is the tilde (~), so convert it back after rawurlencode
	* See: http://www.morganney.com/blog/API/AWS-Product-Advertising-API-Requires-a-Signed-Request.php
	*
	* @param string $var String to encode
	* @return string
	*/
	private function __customUrlEncode($var) {
		return str_replace('%7E', '~', rawurlencode($var));
	}

	/**
	* Generate the auth string using Hmac-SHA256
	*
	* @internal Used by SimpleDBRequest::getResponse()
	* @param string $string String to sign
	* @return string
	*/
	private function __getSignature($string) {
		return base64_encode(hash_hmac('sha256', $string, $this->r53->getSecretKey(), true));
	}
}
OpenPOWER on IntegriCloud