Overview

Packages

  • PHP
  • Transip

Classes

  • Transip_ApiSettings
  • Transip_AvailabilityZone
  • Transip_ColocationService
  • Transip_DataCenterVisitor
  • Transip_DnsEntry
  • Transip_DnsService
  • Transip_Domain
  • Transip_DomainAction
  • Transip_DomainBranding
  • Transip_DomainCheckResult
  • Transip_DomainService
  • Transip_ExtraContactField
  • Transip_Forward
  • Transip_ForwardService
  • Transip_Haip
  • Transip_HaipService
  • Transip_Nameserver
  • Transip_OperatingSystem
  • Transip_PrivateNetwork
  • Transip_Product
  • Transip_PropositionService
  • Transip_Snapshot
  • Transip_Tld
  • Transip_Vps
  • Transip_VpsBackup
  • Transip_VpsService
  • Transip_WhoisContact
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: require_once('ApiSettings.php');
  4: 
  5: /**
  6:  * This is the API endpoint for the PropositionService
  7:  *
  8:  * @package Transip
  9:  * @class PropositionService
 10:  * @author TransIP (support@transip.nl)
 11:  */
 12: class Transip_PropositionService
 13: {
 14:     // These fields are SOAP related
 15:     /** The SOAP service that corresponds with this class. */
 16:     const SERVICE = 'PropositionService';
 17:     /** The API version. */
 18:     const API_VERSION = '5.23';
 19:     /** @var SoapClient  The SoapClient used to perform the SOAP calls. */
 20:     protected static $_soapClient = null;
 21: 
 22:     /**
 23:      * Gets the singleton SoapClient which is used to connect to the TransIP Api.
 24:      *
 25:      * @param  mixed       $parameters  Parameters.
 26:      * @return SoapClient               The SoapClient object to which we can connect to the TransIP API
 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', // lets support unicode
 52:                 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, // see http://bugs.php.net/bug.php?id=43338
 53:                 'trace'    => false, // can be used for debugging
 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:      * Calculates the hash to sign our request with based on the given parameters.
 87:      *
 88:      * @param  mixed   $parameters  The parameters to sign.
 89:      * @return string               Base64 encoded signing hash.
 90:      */
 91:     protected static function _sign($parameters)
 92:     {
 93:         // Fixup our private key, copy-pasting the key might lead to whitespace faults
 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:      * Creates a digest of the given data, with an asn1 header.
112:      *
113:      * @param  string  $data  The data to create a digest of.
114:      * @return string         The digest of the data, with asn1 header.
115:      */
116:     protected static function _sha512Asn1($data)
117:     {
118:         $digest = hash('sha512', $data, true);
119: 
120:         // this ASN1 header is sha512 specific
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:      * Encodes the given paramaters into a url encoded string based upon RFC 3986.
136:      *
137:      * @param  mixed   $parameters  The parameters to encode.
138:      * @param  string  $keyPrefix   Key prefix.
139:      * @return string               The given parameters encoded according to RFC 3986.
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:      * Our own function to encode a string according to RFC 3986 since.
169:      * PHP < 5.3.0 encodes the ~ character which is not allowed.
170:      *
171:      * @param string $string The string to encode.
172:      * @return string The encoded string according to RFC 3986.
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:      * Returns the status for a proposition number ('pending', 'finished' or 'invalid')
183:      *
184:      * @param string $propositionNumber 
185:      * @return string 
186:      * @throws ApiException
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: 
API documentation generated by ApiGen 2.8.0