1: <?php
2:
3: require_once('ApiSettings.php');
4: require_once('WebhostingPackage.php');
5: require_once('WebHost.php');
6: require_once('Cronjob.php');
7: require_once('MailBox.php');
8: require_once('Db.php');
9: require_once('MailForward.php');
10: require_once('SubDomain.php');
11:
12: 13: 14: 15: 16: 17: 18:
19: class Transip_WebhostingService
20: {
21:
22:
23: const SERVICE = 'WebhostingService';
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: 'WebhostingPackage' => 'Transip_WebhostingPackage',
55: 'WebHost' => 'Transip_WebHost',
56: 'Cronjob' => 'Transip_Cronjob',
57: 'MailBox' => 'Transip_MailBox',
58: 'Db' => 'Transip_Db',
59: 'MailForward' => 'Transip_MailForward',
60: 'SubDomain' => 'Transip_SubDomain',
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 = 'Webhosting';
198:
199: 200: 201: 202: 203:
204: public static function getWebhostingDomainNames()
205: {
206: return self::_getSoapClient(array_merge(array(), array('__method' => 'getWebhostingDomainNames')))->getWebhostingDomainNames();
207: }
208:
209: 210: 211: 212: 213:
214: public static function getAvailablePackages()
215: {
216: return self::_getSoapClient(array_merge(array(), array('__method' => 'getAvailablePackages')))->getAvailablePackages();
217: }
218:
219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229:
230: public static function getInfo($domainName)
231: {
232: return self::_getSoapClient(array_merge(array($domainName), array('__method' => 'getInfo')))->getInfo($domainName);
233: }
234:
235: 236: 237: 238: 239: 240: 241:
242: public static function order($domainName, $webhostingPackage)
243: {
244: return self::_getSoapClient(array_merge(array($domainName, $webhostingPackage), array('__method' => 'order')))->order($domainName, $webhostingPackage);
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254:
255: public static function getAvailableUpgrades($domainName)
256: {
257: return self::_getSoapClient(array_merge(array($domainName), array('__method' => 'getAvailableUpgrades')))->getAvailableUpgrades($domainName);
258: }
259:
260: 261: 262: 263: 264: 265: 266:
267: public static function upgrade($domainName, $newWebhostingPackage)
268: {
269: return self::_getSoapClient(array_merge(array($domainName, $newWebhostingPackage), array('__method' => 'upgrade')))->upgrade($domainName, $newWebhostingPackage);
270: }
271:
272: 273: 274: 275: 276: 277: 278:
279: public static function cancel($domainName, $endTime)
280: {
281: return self::_getSoapClient(array_merge(array($domainName, $endTime), array('__method' => 'cancel')))->cancel($domainName, $endTime);
282: }
283:
284: 285: 286: 287: 288: 289: 290:
291: public static function setFtpPassword($domainName, $newPassword)
292: {
293: return self::_getSoapClient(array_merge(array($domainName, $newPassword), array('__method' => 'setFtpPassword')))->setFtpPassword($domainName, $newPassword);
294: }
295:
296: 297: 298: 299: 300: 301: 302:
303: public static function createCronjob($domainName, $cronjob)
304: {
305: return self::_getSoapClient(array_merge(array($domainName, $cronjob), array('__method' => 'createCronjob')))->createCronjob($domainName, $cronjob);
306: }
307:
308: 309: 310: 311: 312: 313: 314: 315:
316: public static function deleteCronjob($domainName, $cronjob)
317: {
318: return self::_getSoapClient(array_merge(array($domainName, $cronjob), array('__method' => 'deleteCronjob')))->deleteCronjob($domainName, $cronjob);
319: }
320:
321: 322: 323: 324: 325: 326: 327:
328: public static function createMailBox($domainName, $mailBox)
329: {
330: return self::_getSoapClient(array_merge(array($domainName, $mailBox), array('__method' => 'createMailBox')))->createMailBox($domainName, $mailBox);
331: }
332:
333: 334: 335: 336: 337: 338: 339:
340: public static function modifyMailBox($domainName, $mailBox)
341: {
342: return self::_getSoapClient(array_merge(array($domainName, $mailBox), array('__method' => 'modifyMailBox')))->modifyMailBox($domainName, $mailBox);
343: }
344:
345: 346: 347: 348: 349: 350: 351: 352:
353: public static function setMailBoxPassword($domainName, $mailBox, $newPassword)
354: {
355: return self::_getSoapClient(array_merge(array($domainName, $mailBox, $newPassword), array('__method' => 'setMailBoxPassword')))->setMailBoxPassword($domainName, $mailBox, $newPassword);
356: }
357:
358: 359: 360: 361: 362: 363: 364:
365: public static function deleteMailBox($domainName, $mailBox)
366: {
367: return self::_getSoapClient(array_merge(array($domainName, $mailBox), array('__method' => 'deleteMailBox')))->deleteMailBox($domainName, $mailBox);
368: }
369:
370: 371: 372: 373: 374: 375:
376: public static function createMailForward($domainName, $mailForward)
377: {
378: return self::_getSoapClient(array_merge(array($domainName, $mailForward), array('__method' => 'createMailForward')))->createMailForward($domainName, $mailForward);
379: }
380:
381: 382: 383: 384: 385: 386: 387:
388: public static function modifyMailForward($domainName, $mailForward)
389: {
390: return self::_getSoapClient(array_merge(array($domainName, $mailForward), array('__method' => 'modifyMailForward')))->modifyMailForward($domainName, $mailForward);
391: }
392:
393: 394: 395: 396: 397: 398:
399: public static function deleteMailForward($domainName, $mailForward)
400: {
401: return self::_getSoapClient(array_merge(array($domainName, $mailForward), array('__method' => 'deleteMailForward')))->deleteMailForward($domainName, $mailForward);
402: }
403:
404: 405: 406: 407: 408: 409:
410: public static function createDatabase($domainName, $db)
411: {
412: return self::_getSoapClient(array_merge(array($domainName, $db), array('__method' => 'createDatabase')))->createDatabase($domainName, $db);
413: }
414:
415: 416: 417: 418: 419: 420:
421: public static function modifyDatabase($domainName, $db)
422: {
423: return self::_getSoapClient(array_merge(array($domainName, $db), array('__method' => 'modifyDatabase')))->modifyDatabase($domainName, $db);
424: }
425:
426: 427: 428: 429: 430: 431: 432:
433: public static function setDatabasePassword($domainName, $db, $newPassword)
434: {
435: return self::_getSoapClient(array_merge(array($domainName, $db, $newPassword), array('__method' => 'setDatabasePassword')))->setDatabasePassword($domainName, $db, $newPassword);
436: }
437:
438: 439: 440: 441: 442: 443: 444:
445: public static function deleteDatabase($domainName, $db)
446: {
447: return self::_getSoapClient(array_merge(array($domainName, $db), array('__method' => 'deleteDatabase')))->deleteDatabase($domainName, $db);
448: }
449:
450: 451: 452: 453: 454: 455:
456: public static function createSubdomain($domainName, $subDomain)
457: {
458: return self::_getSoapClient(array_merge(array($domainName, $subDomain), array('__method' => 'createSubdomain')))->createSubdomain($domainName, $subDomain);
459: }
460:
461: 462: 463: 464: 465: 466: 467:
468: public static function deleteSubdomain($domainName, $subDomain)
469: {
470: return self::_getSoapClient(array_merge(array($domainName, $subDomain), array('__method' => 'deleteSubdomain')))->deleteSubdomain($domainName, $subDomain);
471: }
472: }
473:
474: ?>
475: