1: <?php
2:
3: require_once('ApiSettings.php');
4: require_once('DnsEntry.php');
5: require_once('DnsSecEntry.php');
6:
7: 8: 9: 10: 11: 12: 13:
14: class Transip_DnsService
15: {
16:
17:
18: const SERVICE = 'DnsService';
19:
20: const API_VERSION = '5.23';
21:
22: protected static $_soapClient = null;
23:
24: 25: 26: 27: 28: 29:
30: public static function _getSoapClient($parameters = array())
31: {
32: $endpoint = Transip_ApiSettings::$endpoint;
33:
34: if(self::$_soapClient === null)
35: {
36: $extensions = get_loaded_extensions();
37: $errors = array();
38: if(!class_exists('SoapClient') || !in_array('soap', $extensions))
39: {
40: $errors[] = 'The PHP SOAP extension doesn\'t seem to be installed. You need to install the PHP SOAP extension. (See: http://www.php.net/manual/en/book.soap.php)';
41: }
42: if(!in_array('openssl', $extensions))
43: {
44: $errors[] = 'The PHP OpenSSL extension doesn\'t seem to be installed. You need to install PHP with the OpenSSL extension. (See: http://www.php.net/manual/en/book.openssl.php)';
45: }
46: if(!empty($errors)) die('<p>' . implode("</p>\n<p>", $errors) . '</p>');
47:
48: $classMap = array(
49: 'DnsEntry' => 'Transip_DnsEntry',
50: 'DnsSecEntry' => 'Transip_DnsSecEntry',
51: );
52:
53: $options = array(
54: 'classmap' => $classMap,
55: 'encoding' => 'utf-8',
56: 'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
57: 'trace' => false,
58: );
59:
60: $wsdlUri = "https://{$endpoint}/wsdl/?service=" . self::SERVICE;
61: try
62: {
63: self::$_soapClient = new SoapClient($wsdlUri, $options);
64: }
65: catch(SoapFault $sf)
66: {
67: throw new Exception("Unable to connect to endpoint '{$endpoint}'");
68: }
69: self::$_soapClient->__setCookie('login', Transip_ApiSettings::$login);
70: self::$_soapClient->__setCookie('mode', Transip_ApiSettings::$mode);
71: }
72:
73: $timestamp = time();
74: $nonce = uniqid('', true);
75:
76: self::$_soapClient->__setCookie('timestamp', $timestamp);
77: self::$_soapClient->__setCookie('nonce', $nonce);
78: self::$_soapClient->__setCookie('clientVersion', self::API_VERSION);
79: self::$_soapClient->__setCookie('signature', self::_urlencode(self::_sign(array_merge($parameters, array(
80: '__service' => self::SERVICE,
81: '__hostname' => $endpoint,
82: '__timestamp' => $timestamp,
83: '__nonce' => $nonce
84: )))));
85:
86: return self::$_soapClient;
87: }
88:
89: 90: 91: 92: 93: 94:
95: protected static function _sign($parameters)
96: {
97:
98: if(!preg_match('/-----BEGIN (RSA )?PRIVATE KEY-----(.*)-----END (RSA )?PRIVATE KEY-----/si', Transip_ApiSettings::$privateKey, $matches))
99: die('<p>Could not find your private key, please supply your private key in the ApiSettings file. You can request a new private key in your TransIP Controlpanel.</p>');
100:
101: $key = $matches[2];
102: $key = preg_replace('/\s*/s', '', $key);
103: $key = chunk_split($key, 64, "\n");
104:
105: $key = "-----BEGIN PRIVATE KEY-----\n" . $key . "-----END PRIVATE KEY-----";
106:
107: $digest = self::_sha512Asn1(self::_encodeParameters($parameters));
108: if(!@openssl_private_encrypt($digest, $signature, $key))
109: die('<p>Could not sign your request, please supply your private key in the ApiSettings file. You can request a new private key in your TransIP Controlpanel.</p>');
110:
111: return base64_encode($signature);
112: }
113:
114: 115: 116: 117: 118: 119:
120: protected static function _sha512Asn1($data)
121: {
122: $digest = hash('sha512', $data, true);
123:
124:
125: $asn1 = chr(0x30).chr(0x51);
126: $asn1 .= chr(0x30).chr(0x0d);
127: $asn1 .= chr(0x06).chr(0x09);
128: $asn1 .= chr(0x60).chr(0x86).chr(0x48).chr(0x01).chr(0x65);
129: $asn1 .= chr(0x03).chr(0x04);
130: $asn1 .= chr(0x02).chr(0x03);
131: $asn1 .= chr(0x05).chr(0x00);
132: $asn1 .= chr(0x04).chr(0x40);
133: $asn1 .= $digest;
134:
135: return $asn1;
136: }
137:
138: 139: 140: 141: 142: 143: 144:
145: protected static function _encodeParameters($parameters, $keyPrefix = null)
146: {
147: if(!is_array($parameters) && !is_object($parameters))
148: return self::_urlencode($parameters);
149:
150: $encodedData = array();
151:
152: foreach($parameters as $key => $value)
153: {
154: $encodedKey = is_null($keyPrefix)
155: ? self::_urlencode($key)
156: : $keyPrefix . '[' . self::_urlencode($key) . ']';
157:
158: if(is_array($value) || is_object($value))
159: {
160: $encodedData[] = self::_encodeParameters($value, $encodedKey);
161: }
162: else
163: {
164: $encodedData[] = $encodedKey . '=' . self::_urlencode($value);
165: }
166: }
167:
168: return implode('&', $encodedData);
169: }
170:
171: 172: 173: 174: 175: 176: 177:
178: protected static function _urlencode($string)
179: {
180: $string = trim($string);
181: $string = rawurlencode($string);
182: return str_replace('%7E', '~', $string);
183: }
184:
185: const TRACK_ENDPOINT_NAME = 'Dns';
186:
187: 188: 189: 190: 191: 192: 193:
194: public static function setDnsEntries($domainName, $dnsEntries)
195: {
196: return self::_getSoapClient(array_merge(array($domainName, $dnsEntries), array('__method' => 'setDnsEntries')))->setDnsEntries($domainName, $dnsEntries);
197: }
198:
199: 200: 201: 202: 203: 204: 205:
206: public static function canEditDnsSec($domainName)
207: {
208: return self::_getSoapClient(array_merge(array($domainName), array('__method' => 'canEditDnsSec')))->canEditDnsSec($domainName);
209: }
210:
211: 212: 213: 214: 215: 216: 217:
218: public static function getDnsSecEntries($domainName)
219: {
220: return self::_getSoapClient(array_merge(array($domainName), array('__method' => 'getDnsSecEntries')))->getDnsSecEntries($domainName);
221: }
222:
223: 224: 225: 226: 227: 228: 229: 230:
231: public static function setDnsSecEntries($domainName, $dnssecKeyEntrySet)
232: {
233: return self::_getSoapClient(array_merge(array($domainName, $dnssecKeyEntrySet), array('__method' => 'setDnsSecEntries')))->setDnsSecEntries($domainName, $dnssecKeyEntrySet);
234: }
235:
236: 237: 238: 239: 240: 241:
242: public static function removeAllDnsSecEntries($domainName)
243: {
244: return self::_getSoapClient(array_merge(array($domainName), array('__method' => 'removeAllDnsSecEntries')))->removeAllDnsSecEntries($domainName);
245: }
246: }
247:
248: ?>
249: