1: <?php
2:
3: require_once('ApiSettings.php');
4: require_once('DataCenterVisitor.php');
5:
6: 7: 8: 9: 10: 11: 12:
13: class Transip_ColocationService
14: {
15:
16:
17: const SERVICE = 'ColocationService';
18:
19: const API_VERSION = '5.23';
20:
21: protected static $_soapClient = null;
22:
23: 24: 25: 26: 27: 28:
29: public static function _getSoapClient($parameters = array())
30: {
31: $endpoint = Transip_ApiSettings::$endpoint;
32:
33: if(self::$_soapClient === null)
34: {
35: $extensions = get_loaded_extensions();
36: $errors = array();
37: if(!class_exists('SoapClient') || !in_array('soap', $extensions))
38: {
39: $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)';
40: }
41: if(!in_array('openssl', $extensions))
42: {
43: $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)';
44: }
45: if(!empty($errors)) die('<p>' . implode("</p>\n<p>", $errors) . '</p>');
46:
47: $classMap = array(
48: 'DataCenterVisitor' => 'Transip_DataCenterVisitor',
49: );
50:
51: $options = array(
52: 'classmap' => $classMap,
53: 'encoding' => 'utf-8',
54: 'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
55: 'trace' => false,
56: );
57:
58: $wsdlUri = "https://{$endpoint}/wsdl/?service=" . self::SERVICE;
59: try
60: {
61: self::$_soapClient = new SoapClient($wsdlUri, $options);
62: }
63: catch(SoapFault $sf)
64: {
65: throw new Exception("Unable to connect to endpoint '{$endpoint}'");
66: }
67: self::$_soapClient->__setCookie('login', Transip_ApiSettings::$login);
68: self::$_soapClient->__setCookie('mode', Transip_ApiSettings::$mode);
69: }
70:
71: $timestamp = time();
72: $nonce = uniqid('', true);
73:
74: self::$_soapClient->__setCookie('timestamp', $timestamp);
75: self::$_soapClient->__setCookie('nonce', $nonce);
76: self::$_soapClient->__setCookie('clientVersion', self::API_VERSION);
77: self::$_soapClient->__setCookie('signature', self::_urlencode(self::_sign(array_merge($parameters, array(
78: '__service' => self::SERVICE,
79: '__hostname' => $endpoint,
80: '__timestamp' => $timestamp,
81: '__nonce' => $nonce
82: )))));
83:
84: return self::$_soapClient;
85: }
86:
87: 88: 89: 90: 91: 92:
93: protected static function _sign($parameters)
94: {
95:
96: if(!preg_match('/-----BEGIN (RSA )?PRIVATE KEY-----(.*)-----END (RSA )?PRIVATE KEY-----/si', Transip_ApiSettings::$privateKey, $matches))
97: 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>');
98:
99: $key = $matches[2];
100: $key = preg_replace('/\s*/s', '', $key);
101: $key = chunk_split($key, 64, "\n");
102:
103: $key = "-----BEGIN PRIVATE KEY-----\n" . $key . "-----END PRIVATE KEY-----";
104:
105: $digest = self::_sha512Asn1(self::_encodeParameters($parameters));
106: if(!@openssl_private_encrypt($digest, $signature, $key))
107: 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>');
108:
109: return base64_encode($signature);
110: }
111:
112: 113: 114: 115: 116: 117:
118: protected static function _sha512Asn1($data)
119: {
120: $digest = hash('sha512', $data, true);
121:
122:
123: $asn1 = chr(0x30).chr(0x51);
124: $asn1 .= chr(0x30).chr(0x0d);
125: $asn1 .= chr(0x06).chr(0x09);
126: $asn1 .= chr(0x60).chr(0x86).chr(0x48).chr(0x01).chr(0x65);
127: $asn1 .= chr(0x03).chr(0x04);
128: $asn1 .= chr(0x02).chr(0x03);
129: $asn1 .= chr(0x05).chr(0x00);
130: $asn1 .= chr(0x04).chr(0x40);
131: $asn1 .= $digest;
132:
133: return $asn1;
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: protected static function _encodeParameters($parameters, $keyPrefix = null)
144: {
145: if(!is_array($parameters) && !is_object($parameters))
146: return self::_urlencode($parameters);
147:
148: $encodedData = array();
149:
150: foreach($parameters as $key => $value)
151: {
152: $encodedKey = is_null($keyPrefix)
153: ? self::_urlencode($key)
154: : $keyPrefix . '[' . self::_urlencode($key) . ']';
155:
156: if(is_array($value) || is_object($value))
157: {
158: $encodedData[] = self::_encodeParameters($value, $encodedKey);
159: }
160: else
161: {
162: $encodedData[] = $encodedKey . '=' . self::_urlencode($value);
163: }
164: }
165:
166: return implode('&', $encodedData);
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: protected static function _urlencode($string)
177: {
178: $string = trim($string);
179: $string = rawurlencode($string);
180: return str_replace('%7E', '~', $string);
181: }
182:
183: const TRACK_ENDPOINT_NAME = 'Colocation';
184:
185: 186: 187: 188: 189: 190: 191: 192: 193: 194:
195: public static function requestAccess($when, $duration, $visitors, $phoneNumber)
196: {
197: return self::_getSoapClient(array_merge(array($when, $duration, $visitors, $phoneNumber), array('__method' => 'requestAccess')))->requestAccess($when, $duration, $visitors, $phoneNumber);
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208: 209:
210: public static function requestRemoteHands($coloName, $contactName, $phoneNumber, $expectedDuration, $instructions)
211: {
212: return self::_getSoapClient(array_merge(array($coloName, $contactName, $phoneNumber, $expectedDuration, $instructions), array('__method' => 'requestRemoteHands')))->requestRemoteHands($coloName, $contactName, $phoneNumber, $expectedDuration, $instructions);
213: }
214:
215: 216: 217: 218: 219:
220: public static function getColoNames()
221: {
222: return self::_getSoapClient(array_merge(array(), array('__method' => 'getColoNames')))->getColoNames();
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232:
233: public static function getIpAddresses($coloName)
234: {
235: return self::_getSoapClient(array_merge(array($coloName), array('__method' => 'getIpAddresses')))->getIpAddresses($coloName);
236: }
237:
238: 239: 240: 241: 242: 243: 244: 245:
246: public static function getIpRanges($coloName)
247: {
248: return self::_getSoapClient(array_merge(array($coloName), array('__method' => 'getIpRanges')))->getIpRanges($coloName);
249: }
250:
251: 252: 253: 254: 255: 256: 257: 258: 259:
260: public static function createIpAddress($ipAddress, $reverseDns)
261: {
262: return self::_getSoapClient(array_merge(array($ipAddress, $reverseDns), array('__method' => 'createIpAddress')))->createIpAddress($ipAddress, $reverseDns);
263: }
264:
265: 266: 267: 268: 269: 270: 271: 272:
273: public static function deleteIpAddress($ipAddress)
274: {
275: return self::_getSoapClient(array_merge(array($ipAddress), array('__method' => 'deleteIpAddress')))->deleteIpAddress($ipAddress);
276: }
277:
278: 279: 280: 281: 282: 283: 284: 285:
286: public static function getReverseDns($ipAddress)
287: {
288: return self::_getSoapClient(array_merge(array($ipAddress), array('__method' => 'getReverseDns')))->getReverseDns($ipAddress);
289: }
290:
291: 292: 293: 294: 295: 296: 297: 298:
299: public static function setReverseDns($ipAddress, $reverseDns)
300: {
301: return self::_getSoapClient(array_merge(array($ipAddress, $reverseDns), array('__method' => 'setReverseDns')))->setReverseDns($ipAddress, $reverseDns);
302: }
303: }
304:
305: ?>
306: