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('DataCenterVisitor.php');
  5: 
  6: /**
  7:  * This is the API endpoint for the ColocationService
  8:  *
  9:  * @package Transip
 10:  * @class ColocationService
 11:  * @author TransIP (support@transip.nl)
 12:  */
 13: class Transip_ColocationService
 14: {
 15:     // These fields are SOAP related
 16:     /** The SOAP service that corresponds with this class. */
 17:     const SERVICE = 'ColocationService';
 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:                 'DataCenterVisitor' => 'Transip_DataCenterVisitor',
 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 TRACK_ENDPOINT_NAME = 'Colocation';
184: 
185:     /**
186:      * Requests access to the data-center
187:      *
188:      * @param string $when the datetime of the wanted datacenter access, in YYYY-MM-DD hh:mm:ss format
189:      * @param int $duration the expected duration of the visit, in minutes
190:      * @param string[] $visitors the names of the visitors for this data-center visit, must be at least 1 and at most 20
191:      * @param string $phoneNumber if an SMS with access codes needs to be sent, set the phonenumber of the receiving phone here;
192:      * @return Transip_DataCenterVisitor[] An array of Visitor objects holding information (such as reservation and access number) about
193:      * @throws ApiException
194:      */
195:     public static function requestAccess($when, $duration, $visitors, $phoneNumber)
196:     {
197:         return self::_getSoapClient(array_merge(array($when, $duration, $visitors, $phoneNumber), array('__method' => 'requestAccess')))->requestAccess($when, $duration, $visitors, $phoneNumber);
198:     }
199: 
200:     /**
201:      * Request remote hands to the data-center
202:      *
203:      * @param string $coloName The name of the colocation
204:      * @param string $contactName The contact name
205:      * @param string $phoneNumber Phone number to contact
206:      * @param int $expectedDuration Expected duration of the job in minutes
207:      * @param string $instructions What to do
208:      * @throws ApiException
209:      */
210:     public static function requestRemoteHands($coloName, $contactName, $phoneNumber, $expectedDuration, $instructions)
211:     {
212:         return self::_getSoapClient(array_merge(array($coloName, $contactName, $phoneNumber, $expectedDuration, $instructions), array('__method' => 'requestRemoteHands')))->requestRemoteHands($coloName, $contactName, $phoneNumber, $expectedDuration, $instructions);
213:     }
214: 
215:     /**
216:      * Get coloNames for customer
217:      *
218:      * @return string[] Array with colo names
219:      */
220:     public static function getColoNames()
221:     {
222:         return self::_getSoapClient(array_merge(array(), array('__method' => 'getColoNames')))->getColoNames();
223:     }
224: 
225:     /**
226:      * Get IpAddresses that are active and assigned to a Colo.
227:      * Both ipv4 and ipv6 addresses are returned: ipv4 adresses in dot notation,
228:      * ipv6 addresses in ipv6 presentation.
229:      *
230:      * @param string $coloName The name of the colo to get the ipaddresses for for
231:      * @return string[] Array with assigned IPv4 and IPv6 addresses for the colo
232:      */
233:     public static function getIpAddresses($coloName)
234:     {
235:         return self::_getSoapClient(array_merge(array($coloName), array('__method' => 'getIpAddresses')))->getIpAddresses($coloName);
236:     }
237: 
238:     /**
239:      * Get ipranges that are assigned to a Colo. Both ipv4 and ipv6 ranges are
240:      * returned, in CIDR notation.
241:      *
242:      * @param string $coloName The name of the colo to get the ranges for
243:      * @see http://en.wikipedia.org/wiki/CIDR_notation
244:      * @return string[] Array of ipranges in CIDR format assigned to this colo.
245:      */
246:     public static function getIpRanges($coloName)
247:     {
248:         return self::_getSoapClient(array_merge(array($coloName), array('__method' => 'getIpRanges')))->getIpRanges($coloName);
249:     }
250: 
251:     /**
252:      * Adds a new IpAddress, either an ipv6 or an ipv4 address.
253:      * The service will validate the address, ensure the user is entitled
254:      * to the address and will add the address to the correct Colo and range.
255:      *
256:      * @param string $ipAddress The IpAddress to create, can be either ipv4 or ipv6.
257:      * @param string $reverseDns The RDNS name for this IpAddress
258:      * @throws ApiException
259:      */
260:     public static function createIpAddress($ipAddress, $reverseDns)
261:     {
262:         return self::_getSoapClient(array_merge(array($ipAddress, $reverseDns), array('__method' => 'createIpAddress')))->createIpAddress($ipAddress, $reverseDns);
263:     }
264: 
265:     /**
266:      * Deletes an IpAddress currently in use this account.
267:      * IpAddress can be either ipv4 or ipv6. The service will validate
268:      * if the user has rights to remove the address and will remove it completely,
269:      * together with any RDNS or monitoring assigned to the address.
270:      *
271:      * @param string $ipAddress the IpAddress to delete, can be either ipv4 or ipv6.
272:      */
273:     public static function deleteIpAddress($ipAddress)
274:     {
275:         return self::_getSoapClient(array_merge(array($ipAddress), array('__method' => 'deleteIpAddress')))->deleteIpAddress($ipAddress);
276:     }
277: 
278:     /**
279:      * Get the Reverse DNS for an IpAddress assigned to the user
280:      * Throws an Exception when the Address does not exist or is not
281:      * owned by the user.
282:      *
283:      * @param string $ipAddress the IpAddress, either ipv4 or ipv6
284:      * @return string rdns
285:      */
286:     public static function getReverseDns($ipAddress)
287:     {
288:         return self::_getSoapClient(array_merge(array($ipAddress), array('__method' => 'getReverseDns')))->getReverseDns($ipAddress);
289:     }
290: 
291:     /**
292:      * Set the RDNS name for an ipAddress.
293:      * Throws an Exception when the Address does not exist or is not
294:      * owned by the user.
295:      *
296:      * @param string $ipAddress The IpAddress to set the reverse dns for, can be either ipv4 or ipv6.
297:      * @param string $reverseDns The new reverse DNS, must be a valid RDNS value.
298:      */
299:     public static function setReverseDns($ipAddress, $reverseDns)
300:     {
301:         return self::_getSoapClient(array_merge(array($ipAddress, $reverseDns), array('__method' => 'setReverseDns')))->setReverseDns($ipAddress, $reverseDns);
302:     }
303: }
304: 
305: ?>
306: 
API documentation generated by ApiGen 2.8.0