1: <?php
2:
3: require_once('ApiSettings.php');
4:
5: 6: 7: 8: 9: 10: 11:
12: class Transip_PropositionService
13: {
14:
15:
16: const SERVICE = 'PropositionService';
17:
18: const API_VERSION = '5.23';
19:
20: protected static $_soapClient = null;
21:
22: 23: 24: 25: 26: 27:
28: public static function _getSoapClient($parameters = array())
29: {
30: $endpoint = Transip_ApiSettings::$endpoint;
31:
32: if(self::$_soapClient === null)
33: {
34: $extensions = get_loaded_extensions();
35: $errors = array();
36: if(!class_exists('SoapClient') || !in_array('soap', $extensions))
37: {
38: $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)';
39: }
40: if(!in_array('openssl', $extensions))
41: {
42: $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)';
43: }
44: if(!empty($errors)) die('<p>' . implode("</p>\n<p>", $errors) . '</p>');
45:
46: $classMap = array(
47: );
48:
49: $options = array(
50: 'classmap' => $classMap,
51: 'encoding' => 'utf-8',
52: 'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
53: 'trace' => false,
54: );
55:
56: $wsdlUri = "https://{$endpoint}/wsdl/?service=" . self::SERVICE;
57: try
58: {
59: self::$_soapClient = new SoapClient($wsdlUri, $options);
60: }
61: catch(SoapFault $sf)
62: {
63: throw new Exception("Unable to connect to endpoint '{$endpoint}'");
64: }
65: self::$_soapClient->__setCookie('login', Transip_ApiSettings::$login);
66: self::$_soapClient->__setCookie('mode', Transip_ApiSettings::$mode);
67: }
68:
69: $timestamp = time();
70: $nonce = uniqid('', true);
71:
72: self::$_soapClient->__setCookie('timestamp', $timestamp);
73: self::$_soapClient->__setCookie('nonce', $nonce);
74: self::$_soapClient->__setCookie('clientVersion', self::API_VERSION);
75: self::$_soapClient->__setCookie('signature', self::_urlencode(self::_sign(array_merge($parameters, array(
76: '__service' => self::SERVICE,
77: '__hostname' => $endpoint,
78: '__timestamp' => $timestamp,
79: '__nonce' => $nonce
80: )))));
81:
82: return self::$_soapClient;
83: }
84:
85: 86: 87: 88: 89: 90:
91: protected static function _sign($parameters)
92: {
93:
94: if(!preg_match('/-----BEGIN (RSA )?PRIVATE KEY-----(.*)-----END (RSA )?PRIVATE KEY-----/si', Transip_ApiSettings::$privateKey, $matches))
95: 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>');
96:
97: $key = $matches[2];
98: $key = preg_replace('/\s*/s', '', $key);
99: $key = chunk_split($key, 64, "\n");
100:
101: $key = "-----BEGIN PRIVATE KEY-----\n" . $key . "-----END PRIVATE KEY-----";
102:
103: $digest = self::_sha512Asn1(self::_encodeParameters($parameters));
104: if(!@openssl_private_encrypt($digest, $signature, $key))
105: 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>');
106:
107: return base64_encode($signature);
108: }
109:
110: 111: 112: 113: 114: 115:
116: protected static function _sha512Asn1($data)
117: {
118: $digest = hash('sha512', $data, true);
119:
120:
121: $asn1 = chr(0x30).chr(0x51);
122: $asn1 .= chr(0x30).chr(0x0d);
123: $asn1 .= chr(0x06).chr(0x09);
124: $asn1 .= chr(0x60).chr(0x86).chr(0x48).chr(0x01).chr(0x65);
125: $asn1 .= chr(0x03).chr(0x04);
126: $asn1 .= chr(0x02).chr(0x03);
127: $asn1 .= chr(0x05).chr(0x00);
128: $asn1 .= chr(0x04).chr(0x40);
129: $asn1 .= $digest;
130:
131: return $asn1;
132: }
133:
134: 135: 136: 137: 138: 139: 140:
141: protected static function _encodeParameters($parameters, $keyPrefix = null)
142: {
143: if(!is_array($parameters) && !is_object($parameters))
144: return self::_urlencode($parameters);
145:
146: $encodedData = array();
147:
148: foreach($parameters as $key => $value)
149: {
150: $encodedKey = is_null($keyPrefix)
151: ? self::_urlencode($key)
152: : $keyPrefix . '[' . self::_urlencode($key) . ']';
153:
154: if(is_array($value) || is_object($value))
155: {
156: $encodedData[] = self::_encodeParameters($value, $encodedKey);
157: }
158: else
159: {
160: $encodedData[] = $encodedKey . '=' . self::_urlencode($value);
161: }
162: }
163:
164: return implode('&', $encodedData);
165: }
166:
167: 168: 169: 170: 171: 172: 173:
174: protected static function _urlencode($string)
175: {
176: $string = trim($string);
177: $string = rawurlencode($string);
178: return str_replace('%7E', '~', $string);
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: public static function getPropositionStatus($propositionNumber)
189: {
190: return self::_getSoapClient(array_merge(array($propositionNumber), array('__method' => 'getPropositionStatus')))->getPropositionStatus($propositionNumber);
191: }
192: }
193:
194: ?>
195: