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: require_once('Forward.php');
  5: 
  6: /**
  7:  * This is the API endpoint for the ForwardService
  8:  *
  9:  * @package Transip
 10:  * @class ForwardService
 11:  * @author TransIP (support@transip.nl)
 12:  */
 13: class Transip_ForwardService
 14: {
 15:     // These fields are SOAP related
 16:     /** The SOAP service that corresponds with this class. */
 17:     const SERVICE = 'ForwardService';
 18:     /** The API version. */
 19:     const API_VERSION = '5.23';
 20:     /** @var SoapClient  The SoapClient used to perform the SOAP calls. */
 21:     protected static $_soapClient = null;
 22: 
 23:     /**
 24:      * Gets the singleton SoapClient which is used to connect to the TransIP Api.
 25:      *
 26:      * @param  mixed       $parameters  Parameters.
 27:      * @return SoapClient               The SoapClient object to which we can connect to the TransIP API
 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', // lets support unicode
 54:                 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, // see http://bugs.php.net/bug.php?id=43338
 55:                 'trace'    => false, // can be used for debugging
 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:      * Calculates the hash to sign our request with based on the given parameters.
 89:      *
 90:      * @param  mixed   $parameters  The parameters to sign.
 91:      * @return string               Base64 encoded signing hash.
 92:      */
 93:     protected static function _sign($parameters)
 94:     {
 95:         // Fixup our private key, copy-pasting the key might lead to whitespace faults
 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:      * Creates a digest of the given data, with an asn1 header.
114:      *
115:      * @param  string  $data  The data to create a digest of.
116:      * @return string         The digest of the data, with asn1 header.
117:      */
118:     protected static function _sha512Asn1($data)
119:     {
120:         $digest = hash('sha512', $data, true);
121: 
122:         // this ASN1 header is sha512 specific
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:      * Encodes the given paramaters into a url encoded string based upon RFC 3986.
138:      *
139:      * @param  mixed   $parameters  The parameters to encode.
140:      * @param  string  $keyPrefix   Key prefix.
141:      * @return string               The given parameters encoded according to RFC 3986.
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:      * Our own function to encode a string according to RFC 3986 since.
171:      * PHP < 5.3.0 encodes the ~ character which is not allowed.
172:      *
173:      * @param string $string The string to encode.
174:      * @return string The encoded string according to RFC 3986.
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:      * Gets a list of all domains which have the Forward option enabled.
189:      *
190:      * @return string[] A list of all forwards enabled domains for the user
191:      */
192:     public static function getForwardDomainNames()
193:     {
194:         return self::_getSoapClient(array_merge(array(), array('__method' => 'getForwardDomainNames')))->getForwardDomainNames();
195:     }
196: 
197:     /**
198:      * Gets information about a forwarded domain
199:      *
200:      * @param string $forwardDomainName The domain to get the info for
201:      * @return Transip_Forward Forward object with all info if found, an exception otherwise
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:      * Order webhosting for a domain name
210:      *
211:      * @param Transip_Forward $forward info about the forward to order. Mandatory fields are $domainName. Other fields are optional.
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:      * Cancel webhosting for a domain
220:      *
221:      * @param string $forwardDomainName The domain name of the forward to cancel the forwarding service for
222:      * @param string $endTime the time to cancel the domain (ForwardService::CANCELLATIONTIME_END (end of contract) or ForwardService::CANCELLATIONTIME_IMMEDIATELY (as soon as possible))
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:      * Modify the options of a Forward. All fields set in the Forward object will be changed.
231:      *
232:      * @param Transip_Forward $forward The forward to modify
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: 
API documentation generated by ApiGen 2.8.0