Project

General

Profile

Download (4.63 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
* r53.class for managing Amazon AWS Route53 Resources
4
* 
5
* This is a simplified version of r53.class for trivial "dynDNS" operations
6
* that integrates well with pfSense. Based on the original r53.class by
7
* Dan Myers at  http://sourceforge.net/projects/php-r53/ .
8
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9
* Copyright (c) 2011, Dan Myers.
10
* Parts copyright (c) 2008, Donovan Schonknecht.
11
* All rights reserved.
12
*
13
* Redistribution and use in source and binary forms, with or without
14
* modification, are permitted provided that the following conditions are met:
15
*
16
* - Redistributions of source code must retain the above copyright notice,
17
*   this list of conditions and the following disclaimer.
18
* - Redistributions in binary form must reproduce the above copyright
19
*   notice, this list of conditions and the following disclaimer in the
20
*   documentation and/or other materials provided with the distribution.
21
*
22
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
* POSSIBILITY OF SUCH DAMAGE.
33
*
34
* This is a modified BSD license (the third clause has been removed).
35
* The BSD license may be found here:
36
* http://www.opensource.org/licenses/bsd-license.php
37
*
38
* Amazon Route 53 is a trademark of Amazon.com, Inc. or its affiliates.
39
*
40
* Route53 is based on Donovan Schonknecht's Amazon S3 PHP class, found here:
41
* http://undesigned.org.za/2007/10/22/amazon-s3-php-class
42
*
43
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
*
45
*/
46

    
47
class Route53
48
{
49
	protected $__accessKey; // AWS Access key
50
	protected $__secretKey; // AWS Secret key
51
	
52
	/**
53
	* Constructor
54
	*
55
	* @param string $accessKey Access key
56
	* @param string $secretKey Secret key
57
	* @return void
58
	*/
59
	public function __construct($accessKey = null, $secretKey = null) {
60
		if ($accessKey !== null && $secretKey !== null) {
61
			$this->setAuth($accessKey, $secretKey);
62
		}
63
	}
64
	
65
	/**
66
	* Set AWS access key and secret key
67
	*
68
	* @param string $accessKey Access key
69
	* @param string $secretKey Secret key
70
	* @return void
71
	*/
72
	public function setAuth($accessKey, $secretKey) {
73
		$this->__accessKey = $accessKey;
74
		$this->__secretKey = $secretKey;
75
	}
76
	
77
	/**
78
	* Return XML document for POST
79
	*
80
	* @param string $fqdn FQDN to set/update
81
	* @param string $ip IP to set for the FQDN
82
	* @param string $ttl TTL for the record
83
	* @return string XML document
84
	*/
85
	public function getRequestBody($fqdn, $ip, $ttl){
86
		$xmlreq .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
87
		$xmlreq .= "<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\">";
88
		$xmlreq .= "<ChangeBatch><Changes><Change>";
89
		$xmlreq .= "<Action>UPSERT</Action>";
90
		$xmlreq .= "<ResourceRecordSet>";
91
		$xmlreq .= sprintf("<Name>%s</Name>", $fqdn);
92
		$xmlreq .= "<Type>A</Type>";
93
		$xmlreq .= sprintf("<TTL>%d</TTL>", $ttl);
94
		$xmlreq .= sprintf("<ResourceRecords><ResourceRecord><Value>%s</Value></ResourceRecord></ResourceRecords>",	$ip);
95
		$xmlreq .= "</ResourceRecordSet>";
96
		$xmlreq .= "</Change></Changes></ChangeBatch>";
97
		$xmlreq .= "</ChangeResourceRecordSetsRequest>";
98
			
99
		return $xmlreq;
100
	}
101
	
102
	/**
103
	* Return API URL
104
	*
105
	* @param string $zoneid Amazone Zone ID
106
	* @return string URL
107
	*/
108
	public function getApiUrl($zoneid){
109
		return sprintf("https://route53.amazonaws.com/2013-04-01/hostedzone/%s/rrset", $zoneid);
110
	}
111

    
112
	/**
113
	* Return HTTP post headers 
114
	* 
115
	* @param int $bodylen length of the POST bost body
116
	* @return Array headers
117
	*/
118
	public function getHttpPostHeaders($bodylen){
119
		$reqdate = gmdate('D, d M Y H:i:s e');
120
		$httphead[] = array();
121
		$httphead[] = sprintf("Date: %s", $reqdate);
122
		$httphead[] = "Content-Type: text/plain";
123
		$httphead[] = sprintf("Content-Length: %d", $bodylen);
124
		/* to avoid having user to know their AWS Region, for now use V3 */
125
		$httphead[] = sprintf(
126
			"X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HMACSHA256,SignedHeaders=date,Signature=%s",
127
			$this->__accessKey,
128
			base64_encode(hash_hmac("sha256", $reqdate, $this->__secretKey, true))
129
		);
130
		return $httphead;
131
	}
132
}
(34-34/51)