1: <?php
2:
3: require_once('ApiSettings.php');
4: require_once('Haip.php');
5: require_once('Vps.php');
6:
7: 8: 9: 10: 11: 12: 13:
14: class Transip_HaipService
15: {
16:
17:
18: const SERVICE = 'HaipService';
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: 'Haip' => 'Transip_Haip',
50: 'Vps' => 'Transip_Vps',
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 = 'HA-IP';
186: const CANCELLATIONTIME_END = 'end';
187: const CANCELLATIONTIME_IMMEDIATELY = 'immediately';
188:
189: 190: 191: 192: 193: 194: 195:
196: public static function getHaip($haipName)
197: {
198: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'getHaip')))->getHaip($haipName);
199: }
200:
201: 202: 203: 204: 205:
206: public static function getHaips()
207: {
208: return self::_getSoapClient(array_merge(array(), array('__method' => 'getHaips')))->getHaips();
209: }
210:
211: 212: 213: 214: 215: 216: 217:
218: public static function changeHaipVps($haipName, $vpsName)
219: {
220: return self::_getSoapClient(array_merge(array($haipName, $vpsName), array('__method' => 'changeHaipVps')))->changeHaipVps($haipName, $vpsName);
221: }
222:
223: 224: 225: 226: 227: 228: 229:
230: public static function setHaipVpses($haipName, $vpsNames)
231: {
232: return self::_getSoapClient(array_merge(array($haipName, $vpsNames), array('__method' => 'setHaipVpses')))->setHaipVpses($haipName, $vpsNames);
233: }
234:
235: 236: 237: 238: 239: 240: 241:
242: public static function setIpSetup($haipName, $ipSetup)
243: {
244: return self::_getSoapClient(array_merge(array($haipName, $ipSetup), array('__method' => 'setIpSetup')))->setIpSetup($haipName, $ipSetup);
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257:
258: public static function setBalancingMode($haipName, $balancingMode, $cookieName)
259: {
260: return self::_getSoapClient(array_merge(array($haipName, $balancingMode, $cookieName), array('__method' => 'setBalancingMode')))->setBalancingMode($haipName, $balancingMode, $cookieName);
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270: 271: 272:
273: public static function setHttpHealthCheck($haipName, $path, $port)
274: {
275: return self::_getSoapClient(array_merge(array($haipName, $path, $port), array('__method' => 'setHttpHealthCheck')))->setHttpHealthCheck($haipName, $path, $port);
276: }
277:
278: 279: 280: 281: 282: 283: 284: 285:
286: public static function setTcpHealthCheck($haipName)
287: {
288: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'setTcpHealthCheck')))->setTcpHealthCheck($haipName);
289: }
290:
291: 292: 293: 294: 295: 296: 297: 298: 299:
300: public static function getStatusReport($haipName)
301: {
302: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'getStatusReport')))->getStatusReport($haipName);
303: }
304:
305: 306: 307: 308: 309: 310: 311:
312: public static function getCertificatesByHaip($haipName)
313: {
314: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'getCertificatesByHaip')))->getCertificatesByHaip($haipName);
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: public static function getAvailableCertificatesByHaip($haipName)
325: {
326: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'getAvailableCertificatesByHaip')))->getAvailableCertificatesByHaip($haipName);
327: }
328:
329: 330: 331: 332: 333: 334: 335:
336: public static function addCertificateToHaip($haipName, $certificateId)
337: {
338: return self::_getSoapClient(array_merge(array($haipName, $certificateId), array('__method' => 'addCertificateToHaip')))->addCertificateToHaip($haipName, $certificateId);
339: }
340:
341: 342: 343: 344: 345: 346: 347:
348: public static function deleteCertificateFromHaip($haipName, $certificateId)
349: {
350: return self::_getSoapClient(array_merge(array($haipName, $certificateId), array('__method' => 'deleteCertificateFromHaip')))->deleteCertificateFromHaip($haipName, $certificateId);
351: }
352:
353: 354: 355: 356: 357: 358: 359:
360: public static function startHaipLetsEncryptCertificateIssue($haipName, $commonName)
361: {
362: return self::_getSoapClient(array_merge(array($haipName, $commonName), array('__method' => 'startHaipLetsEncryptCertificateIssue')))->startHaipLetsEncryptCertificateIssue($haipName, $commonName);
363: }
364:
365: 366: 367: 368: 369: 370: 371:
372: public static function getPtrForHaip($haipName)
373: {
374: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'getPtrForHaip')))->getPtrForHaip($haipName);
375: }
376:
377: 378: 379: 380: 381: 382: 383:
384: public static function setPtrForHaip($haipName, $ptr)
385: {
386: return self::_getSoapClient(array_merge(array($haipName, $ptr), array('__method' => 'setPtrForHaip')))->setPtrForHaip($haipName, $ptr);
387: }
388:
389: 390: 391: 392: 393: 394: 395:
396: public static function setHaipDescription($haipName, $description)
397: {
398: return self::_getSoapClient(array_merge(array($haipName, $description), array('__method' => 'setHaipDescription')))->setHaipDescription($haipName, $description);
399: }
400:
401: 402: 403: 404: 405: 406: 407: 408:
409: public static function getHaipPortConfigurations($haipName)
410: {
411: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'getHaipPortConfigurations')))->getHaipPortConfigurations($haipName);
412: }
413:
414: 415: 416: 417: 418: 419: 420:
421: public static function getPortConfigurations($haipName)
422: {
423: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'getPortConfigurations')))->getPortConfigurations($haipName);
424: }
425:
426: 427: 428: 429: 430: 431:
432: public static function setDefaultPortConfiguration($haipName)
433: {
434: return self::_getSoapClient(array_merge(array($haipName), array('__method' => 'setDefaultPortConfiguration')))->setDefaultPortConfiguration($haipName);
435: }
436:
437: 438: 439: 440: 441: 442: 443: 444: 445: 446:
447: public static function addHaipPortConfiguration($haipName, $name, $portNumber, $mode)
448: {
449: return self::_getSoapClient(array_merge(array($haipName, $name, $portNumber, $mode), array('__method' => 'addHaipPortConfiguration')))->addHaipPortConfiguration($haipName, $name, $portNumber, $mode);
450: }
451:
452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462:
463: public static function addPortConfiguration($haipName, $name, $sourcePort, $targetPort, $mode, $endpointSslMode)
464: {
465: return self::_getSoapClient(array_merge(array($haipName, $name, $sourcePort, $targetPort, $mode, $endpointSslMode), array('__method' => 'addPortConfiguration')))->addPortConfiguration($haipName, $name, $sourcePort, $targetPort, $mode, $endpointSslMode);
466: }
467:
468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478:
479: public static function updateHaipPortConfiguration($haipName, $configurationId, $name, $portNumber, $mode)
480: {
481: return self::_getSoapClient(array_merge(array($haipName, $configurationId, $name, $portNumber, $mode), array('__method' => 'updateHaipPortConfiguration')))->updateHaipPortConfiguration($haipName, $configurationId, $name, $portNumber, $mode);
482: }
483:
484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495:
496: public static function updatePortConfiguration($haipName, $configurationId, $name, $sourcePort, $targetPort, $mode, $endpointSslMode)
497: {
498: return self::_getSoapClient(array_merge(array($haipName, $configurationId, $name, $sourcePort, $targetPort, $mode, $endpointSslMode), array('__method' => 'updatePortConfiguration')))->updatePortConfiguration($haipName, $configurationId, $name, $sourcePort, $targetPort, $mode, $endpointSslMode);
499: }
500:
501: 502: 503: 504: 505: 506: 507: 508:
509: public static function deleteHaipPortConfiguration($haipName, $configurationId)
510: {
511: return self::_getSoapClient(array_merge(array($haipName, $configurationId), array('__method' => 'deleteHaipPortConfiguration')))->deleteHaipPortConfiguration($haipName, $configurationId);
512: }
513:
514: 515: 516: 517: 518: 519: 520:
521: public static function deletePortConfiguration($haipName, $configurationId)
522: {
523: return self::_getSoapClient(array_merge(array($haipName, $configurationId), array('__method' => 'deletePortConfiguration')))->deletePortConfiguration($haipName, $configurationId);
524: }
525:
526: 527: 528: 529: 530: 531: 532:
533: public static function cancelHaip($haipName, $endTime)
534: {
535: return self::_getSoapClient(array_merge(array($haipName, $endTime), array('__method' => 'cancelHaip')))->cancelHaip($haipName, $endTime);
536: }
537: }
538:
539: ?>
540: