1: <?php
2:
3: require_once('ApiSettings.php');
4: require_once('Product.php');
5: require_once('PrivateNetwork.php');
6: require_once('Vps.php');
7: require_once('Snapshot.php');
8: require_once('VpsBackup.php');
9: require_once('OperatingSystem.php');
10: require_once('AvailabilityZone.php');
11:
12: 13: 14: 15: 16: 17: 18:
19: class Transip_VpsService
20: {
21:
22:
23: const SERVICE = 'VpsService';
24:
25: const API_VERSION = '5.23';
26:
27: protected static $_soapClient = null;
28:
29: 30: 31: 32: 33: 34:
35: public static function _getSoapClient($parameters = array())
36: {
37: $endpoint = Transip_ApiSettings::$endpoint;
38:
39: if(self::$_soapClient === null)
40: {
41: $extensions = get_loaded_extensions();
42: $errors = array();
43: if(!class_exists('SoapClient') || !in_array('soap', $extensions))
44: {
45: $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)';
46: }
47: if(!in_array('openssl', $extensions))
48: {
49: $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)';
50: }
51: if(!empty($errors)) die('<p>' . implode("</p>\n<p>", $errors) . '</p>');
52:
53: $classMap = array(
54: 'Product' => 'Transip_Product',
55: 'PrivateNetwork' => 'Transip_PrivateNetwork',
56: 'Vps' => 'Transip_Vps',
57: 'Snapshot' => 'Transip_Snapshot',
58: 'VpsBackup' => 'Transip_VpsBackup',
59: 'OperatingSystem' => 'Transip_OperatingSystem',
60: 'AvailabilityZone' => 'Transip_AvailabilityZone',
61: );
62:
63: $options = array(
64: 'classmap' => $classMap,
65: 'encoding' => 'utf-8',
66: 'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
67: 'trace' => false,
68: );
69:
70: $wsdlUri = "https://{$endpoint}/wsdl/?service=" . self::SERVICE;
71: try
72: {
73: self::$_soapClient = new SoapClient($wsdlUri, $options);
74: }
75: catch(SoapFault $sf)
76: {
77: throw new Exception("Unable to connect to endpoint '{$endpoint}'");
78: }
79: self::$_soapClient->__setCookie('login', Transip_ApiSettings::$login);
80: self::$_soapClient->__setCookie('mode', Transip_ApiSettings::$mode);
81: }
82:
83: $timestamp = time();
84: $nonce = uniqid('', true);
85:
86: self::$_soapClient->__setCookie('timestamp', $timestamp);
87: self::$_soapClient->__setCookie('nonce', $nonce);
88: self::$_soapClient->__setCookie('clientVersion', self::API_VERSION);
89: self::$_soapClient->__setCookie('signature', self::_urlencode(self::_sign(array_merge($parameters, array(
90: '__service' => self::SERVICE,
91: '__hostname' => $endpoint,
92: '__timestamp' => $timestamp,
93: '__nonce' => $nonce
94: )))));
95:
96: return self::$_soapClient;
97: }
98:
99: 100: 101: 102: 103: 104:
105: protected static function _sign($parameters)
106: {
107:
108: if(!preg_match('/-----BEGIN (RSA )?PRIVATE KEY-----(.*)-----END (RSA )?PRIVATE KEY-----/si', Transip_ApiSettings::$privateKey, $matches))
109: 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>');
110:
111: $key = $matches[2];
112: $key = preg_replace('/\s*/s', '', $key);
113: $key = chunk_split($key, 64, "\n");
114:
115: $key = "-----BEGIN PRIVATE KEY-----\n" . $key . "-----END PRIVATE KEY-----";
116:
117: $digest = self::_sha512Asn1(self::_encodeParameters($parameters));
118: if(!@openssl_private_encrypt($digest, $signature, $key))
119: 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>');
120:
121: return base64_encode($signature);
122: }
123:
124: 125: 126: 127: 128: 129:
130: protected static function _sha512Asn1($data)
131: {
132: $digest = hash('sha512', $data, true);
133:
134:
135: $asn1 = chr(0x30).chr(0x51);
136: $asn1 .= chr(0x30).chr(0x0d);
137: $asn1 .= chr(0x06).chr(0x09);
138: $asn1 .= chr(0x60).chr(0x86).chr(0x48).chr(0x01).chr(0x65);
139: $asn1 .= chr(0x03).chr(0x04);
140: $asn1 .= chr(0x02).chr(0x03);
141: $asn1 .= chr(0x05).chr(0x00);
142: $asn1 .= chr(0x04).chr(0x40);
143: $asn1 .= $digest;
144:
145: return $asn1;
146: }
147:
148: 149: 150: 151: 152: 153: 154:
155: protected static function _encodeParameters($parameters, $keyPrefix = null)
156: {
157: if(!is_array($parameters) && !is_object($parameters))
158: return self::_urlencode($parameters);
159:
160: $encodedData = array();
161:
162: foreach($parameters as $key => $value)
163: {
164: $encodedKey = is_null($keyPrefix)
165: ? self::_urlencode($key)
166: : $keyPrefix . '[' . self::_urlencode($key) . ']';
167:
168: if(is_array($value) || is_object($value))
169: {
170: $encodedData[] = self::_encodeParameters($value, $encodedKey);
171: }
172: else
173: {
174: $encodedData[] = $encodedKey . '=' . self::_urlencode($value);
175: }
176: }
177:
178: return implode('&', $encodedData);
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: protected static function _urlencode($string)
189: {
190: $string = trim($string);
191: $string = rawurlencode($string);
192: return str_replace('%7E', '~', $string);
193: }
194:
195: const CANCELLATIONTIME_END = 'end';
196: const CANCELLATIONTIME_IMMEDIATELY = 'immediately';
197: const TRACK_ENDPOINT_NAME = 'VPS';
198:
199: 200: 201: 202: 203:
204: public static function getAvailableProducts()
205: {
206: return self::_getSoapClient(array_merge(array(), array('__method' => 'getAvailableProducts')))->getAvailableProducts();
207: }
208:
209: 210: 211: 212: 213:
214: public static function getAvailableAddons()
215: {
216: return self::_getSoapClient(array_merge(array(), array('__method' => 'getAvailableAddons')))->getAvailableAddons();
217: }
218:
219: 220: 221: 222: 223: 224:
225: public static function getActiveAddonsForVps($vpsName)
226: {
227: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getActiveAddonsForVps')))->getActiveAddonsForVps($vpsName);
228: }
229:
230: 231: 232: 233: 234: 235:
236: public static function getAvailableUpgrades($vpsName)
237: {
238: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getAvailableUpgrades')))->getAvailableUpgrades($vpsName);
239: }
240:
241: 242: 243: 244: 245: 246:
247: public static function getAvailableAddonsForVps($vpsName)
248: {
249: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getAvailableAddonsForVps')))->getAvailableAddonsForVps($vpsName);
250: }
251:
252: 253: 254: 255: 256: 257:
258: public static function getCancellableAddonsForVps($vpsName)
259: {
260: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getCancellableAddonsForVps')))->getCancellableAddonsForVps($vpsName);
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270: 271:
272: public static function orderVps($productName, $addons, $operatingSystemName, $hostname)
273: {
274: return self::_getSoapClient(array_merge(array($productName, $addons, $operatingSystemName, $hostname), array('__method' => 'orderVps')))->orderVps($productName, $addons, $operatingSystemName, $hostname);
275: }
276:
277: 278: 279: 280: 281: 282: 283: 284: 285: 286:
287: public static function orderVpsInAvailabilityZone($productName, $addons, $operatingSystemName, $hostname, $availabilityZone)
288: {
289: return self::_getSoapClient(array_merge(array($productName, $addons, $operatingSystemName, $hostname, $availabilityZone), array('__method' => 'orderVpsInAvailabilityZone')))->orderVpsInAvailabilityZone($productName, $addons, $operatingSystemName, $hostname, $availabilityZone);
290: }
291:
292: 293: 294: 295: 296: 297:
298: public static function cloneVps($vpsName)
299: {
300: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'cloneVps')))->cloneVps($vpsName);
301: }
302:
303: 304: 305: 306: 307: 308: 309:
310: public static function cloneVpsToAvailabilityZone($vpsName, $availabilityZone)
311: {
312: return self::_getSoapClient(array_merge(array($vpsName, $availabilityZone), array('__method' => 'cloneVpsToAvailabilityZone')))->cloneVpsToAvailabilityZone($vpsName, $availabilityZone);
313: }
314:
315: 316: 317: 318: 319: 320: 321:
322: public static function orderAddon($vpsName, $addons)
323: {
324: return self::_getSoapClient(array_merge(array($vpsName, $addons), array('__method' => 'orderAddon')))->orderAddon($vpsName, $addons);
325: }
326:
327: 328: 329: 330: 331:
332: public static function orderPrivateNetwork()
333: {
334: return self::_getSoapClient(array_merge(array(), array('__method' => 'orderPrivateNetwork')))->orderPrivateNetwork();
335: }
336:
337: 338: 339: 340: 341: 342: 343:
344: public static function upgradeVps($vpsName, $upgradeToProductName)
345: {
346: return self::_getSoapClient(array_merge(array($vpsName, $upgradeToProductName), array('__method' => 'upgradeVps')))->upgradeVps($vpsName, $upgradeToProductName);
347: }
348:
349: 350: 351: 352: 353: 354: 355:
356: public static function cancelVps($vpsName, $endTime)
357: {
358: return self::_getSoapClient(array_merge(array($vpsName, $endTime), array('__method' => 'cancelVps')))->cancelVps($vpsName, $endTime);
359: }
360:
361: 362: 363: 364: 365: 366: 367:
368: public static function cancelAddon($vpsName, $addonName)
369: {
370: return self::_getSoapClient(array_merge(array($vpsName, $addonName), array('__method' => 'cancelAddon')))->cancelAddon($vpsName, $addonName);
371: }
372:
373: 374: 375: 376: 377: 378: 379:
380: public static function cancelPrivateNetwork($privateNetworkName, $endTime)
381: {
382: return self::_getSoapClient(array_merge(array($privateNetworkName, $endTime), array('__method' => 'cancelPrivateNetwork')))->cancelPrivateNetwork($privateNetworkName, $endTime);
383: }
384:
385: 386: 387: 388: 389: 390:
391: public static function getPrivateNetworksByVps($vpsName)
392: {
393: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getPrivateNetworksByVps')))->getPrivateNetworksByVps($vpsName);
394: }
395:
396: 397: 398: 399: 400:
401: public static function getAllPrivateNetworks()
402: {
403: return self::_getSoapClient(array_merge(array(), array('__method' => 'getAllPrivateNetworks')))->getAllPrivateNetworks();
404: }
405:
406: 407: 408: 409: 410: 411:
412: public static function addVpsToPrivateNetwork($vpsName, $privateNetworkName)
413: {
414: return self::_getSoapClient(array_merge(array($vpsName, $privateNetworkName), array('__method' => 'addVpsToPrivateNetwork')))->addVpsToPrivateNetwork($vpsName, $privateNetworkName);
415: }
416:
417: 418: 419: 420: 421: 422:
423: public static function removeVpsFromPrivateNetwork($vpsName, $privateNetworkName)
424: {
425: return self::_getSoapClient(array_merge(array($vpsName, $privateNetworkName), array('__method' => 'removeVpsFromPrivateNetwork')))->removeVpsFromPrivateNetwork($vpsName, $privateNetworkName);
426: }
427:
428: 429: 430: 431: 432: 433: 434:
435: public static function getTrafficInformationForVps($vpsName)
436: {
437: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getTrafficInformationForVps')))->getTrafficInformationForVps($vpsName);
438: }
439:
440: 441: 442: 443: 444:
445: public static function getPooledTrafficInformation()
446: {
447: return self::_getSoapClient(array_merge(array(), array('__method' => 'getPooledTrafficInformation')))->getPooledTrafficInformation();
448: }
449:
450: 451: 452: 453: 454: 455:
456: public static function start($vpsName)
457: {
458: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'start')))->start($vpsName);
459: }
460:
461: 462: 463: 464: 465: 466:
467: public static function stop($vpsName)
468: {
469: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'stop')))->stop($vpsName);
470: }
471:
472: 473: 474: 475: 476: 477:
478: public static function reset($vpsName)
479: {
480: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'reset')))->reset($vpsName);
481: }
482:
483: 484: 485: 486: 487: 488: 489:
490: public static function createSnapshot($vpsName, $description)
491: {
492: return self::_getSoapClient(array_merge(array($vpsName, $description), array('__method' => 'createSnapshot')))->createSnapshot($vpsName, $description);
493: }
494:
495: 496: 497: 498: 499: 500: 501:
502: public static function revertSnapshot($vpsName, $snapshotName)
503: {
504: return self::_getSoapClient(array_merge(array($vpsName, $snapshotName), array('__method' => 'revertSnapshot')))->revertSnapshot($vpsName, $snapshotName);
505: }
506:
507: 508: 509: 510: 511: 512: 513: 514:
515: public static function revertSnapshotToOtherVps($sourceVpsName, $snapshotName, $destinationVpsName)
516: {
517: return self::_getSoapClient(array_merge(array($sourceVpsName, $snapshotName, $destinationVpsName), array('__method' => 'revertSnapshotToOtherVps')))->revertSnapshotToOtherVps($sourceVpsName, $snapshotName, $destinationVpsName);
518: }
519:
520: 521: 522: 523: 524: 525: 526:
527: public static function removeSnapshot($vpsName, $snapshotName)
528: {
529: return self::_getSoapClient(array_merge(array($vpsName, $snapshotName), array('__method' => 'removeSnapshot')))->removeSnapshot($vpsName, $snapshotName);
530: }
531:
532: 533: 534: 535: 536: 537: 538:
539: public static function revertVpsBackup($vpsName, $vpsBackupId)
540: {
541: return self::_getSoapClient(array_merge(array($vpsName, $vpsBackupId), array('__method' => 'revertVpsBackup')))->revertVpsBackup($vpsName, $vpsBackupId);
542: }
543:
544: 545: 546: 547: 548: 549:
550: public static function getVps($vpsName)
551: {
552: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getVps')))->getVps($vpsName);
553: }
554:
555: 556: 557: 558: 559:
560: public static function getVpses()
561: {
562: return self::_getSoapClient(array_merge(array(), array('__method' => 'getVpses')))->getVpses();
563: }
564:
565: 566: 567: 568: 569: 570:
571: public static function getSnapshotsByVps($vpsName)
572: {
573: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getSnapshotsByVps')))->getSnapshotsByVps($vpsName);
574: }
575:
576: 577: 578: 579: 580: 581:
582: public static function getVpsBackupsByVps($vpsName)
583: {
584: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getVpsBackupsByVps')))->getVpsBackupsByVps($vpsName);
585: }
586:
587: 588: 589: 590: 591:
592: public static function getOperatingSystems()
593: {
594: return self::_getSoapClient(array_merge(array(), array('__method' => 'getOperatingSystems')))->getOperatingSystems();
595: }
596:
597: 598: 599: 600: 601: 602: 603:
604: public static function installOperatingSystem($vpsName, $operatingSystemName, $hostname)
605: {
606: return self::_getSoapClient(array_merge(array($vpsName, $operatingSystemName, $hostname), array('__method' => 'installOperatingSystem')))->installOperatingSystem($vpsName, $operatingSystemName, $hostname);
607: }
608:
609: 610: 611: 612: 613: 614: 615: 616:
617: public static function installOperatingSystemUnattended($vpsName, $operatingSystemName, $base64InstallText)
618: {
619: return self::_getSoapClient(array_merge(array($vpsName, $operatingSystemName, $base64InstallText), array('__method' => 'installOperatingSystemUnattended')))->installOperatingSystemUnattended($vpsName, $operatingSystemName, $base64InstallText);
620: }
621:
622: 623: 624: 625: 626: 627:
628: public static function getIpsForVps($vpsName)
629: {
630: return self::_getSoapClient(array_merge(array($vpsName), array('__method' => 'getIpsForVps')))->getIpsForVps($vpsName);
631: }
632:
633: 634: 635: 636: 637:
638: public static function getAllIps()
639: {
640: return self::_getSoapClient(array_merge(array(), array('__method' => 'getAllIps')))->getAllIps();
641: }
642:
643: 644: 645: 646: 647: 648: 649:
650: public static function addIpv6ToVps($vpsName, $ipv6Address)
651: {
652: return self::_getSoapClient(array_merge(array($vpsName, $ipv6Address), array('__method' => 'addIpv6ToVps')))->addIpv6ToVps($vpsName, $ipv6Address);
653: }
654:
655: 656: 657: 658: 659: 660: 661:
662: public static function updatePtrRecord($ipAddress, $ptrRecord)
663: {
664: return self::_getSoapClient(array_merge(array($ipAddress, $ptrRecord), array('__method' => 'updatePtrRecord')))->updatePtrRecord($ipAddress, $ptrRecord);
665: }
666:
667: 668: 669: 670: 671: 672: 673:
674: public static function setCustomerLock($vpsName, $enabled)
675: {
676: return self::_getSoapClient(array_merge(array($vpsName, $enabled), array('__method' => 'setCustomerLock')))->setCustomerLock($vpsName, $enabled);
677: }
678:
679: 680: 681: 682: 683: 684: 685:
686: public static function handoverVps($vpsName, $targetAccountname)
687: {
688: return self::_getSoapClient(array_merge(array($vpsName, $targetAccountname), array('__method' => 'handoverVps')))->handoverVps($vpsName, $targetAccountname);
689: }
690:
691: 692: 693: 694: 695:
696: public static function getAvailableAvailabilityZones()
697: {
698: return self::_getSoapClient(array_merge(array(), array('__method' => 'getAvailableAvailabilityZones')))->getAvailableAvailabilityZones();
699: }
700:
701: 702: 703: 704: 705: 706: 707: 708:
709: public static function convertVpsBackupToSnapshot($vpsName, $description, $vpsBackupId)
710: {
711: return self::_getSoapClient(array_merge(array($vpsName, $description, $vpsBackupId), array('__method' => 'convertVpsBackupToSnapshot')))->convertVpsBackupToSnapshot($vpsName, $description, $vpsBackupId);
712: }
713: }
714:
715: ?>
716: