1: <?php
2:
3: require_once('ApiSettings.php');
4: require_once('Forward.php');
5:
6: 7: 8: 9: 10: 11: 12:
13: class Transip_ForwardService
14: {
15:
16:
17: const SERVICE = 'ForwardService';
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: 'Forward' => 'Transip_Forward',
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 CANCELLATIONTIME_END = 'end';
184: const CANCELLATIONTIME_IMMEDIATELY = 'immediately';
185: const TRACK_ENDPOINT_NAME = 'Forward';
186:
187: 188: 189: 190: 191:
192: public static function getForwardDomainNames()
193: {
194: return self::_getSoapClient(array_merge(array(), array('__method' => 'getForwardDomainNames')))->getForwardDomainNames();
195: }
196:
197: 198: 199: 200: 201: 202:
203: public static function getInfo($forwardDomainName)
204: {
205: return self::_getSoapClient(array_merge(array($forwardDomainName), array('__method' => 'getInfo')))->getInfo($forwardDomainName);
206: }
207:
208: 209: 210: 211: 212:
213: public static function order($forward)
214: {
215: return self::_getSoapClient(array_merge(array($forward), array('__method' => 'order')))->order($forward);
216: }
217:
218: 219: 220: 221: 222: 223:
224: public static function cancel($forwardDomainName, $endTime)
225: {
226: return self::_getSoapClient(array_merge(array($forwardDomainName, $endTime), array('__method' => 'cancel')))->cancel($forwardDomainName, $endTime);
227: }
228:
229: 230: 231: 232: 233:
234: public static function modify($forward)
235: {
236: return self::_getSoapClient(array_merge(array($forward), array('__method' => 'modify')))->modify($forward);
237: }
238: }
239:
240: ?>
241: