Merge pull request #1 from rupertj/master

Untested. Use with caution, or use previous commit until this merge is verified working.
This commit is contained in:
Will Bradley 2011-07-04 02:32:05 -07:00
commit 99b03ae4eb

View File

@ -1,237 +1,180 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); <?php
/** /**
* Dynect API SOAP Library * Dynect API SOAP Library
* *
* Interfaces with the Dynect DNS service to query and modfiy DNS records. * Interfaces with the Dynect DNS service to query and modfiy DNS records.
* *
* @category Libraries * @category Libraries
* @author Will Bradley, based on Dynect API examples. * @author Will Bradley, based on Dynect API examples.
* @link http://www.zyphon.com * @link http://www.zyphon.com
* @link https://github.com/zyphlar/Dynect-PHP-API
*/ */
class Dynect_API { class Dynect_API {
//private protected $base_url = 'https://api2.dynect.net/wsdl/current/Dynect.wsdl';
var $CI; protected $client; // The SOAP client
var $base_url = 'https://api2.dynect.net/wsdl/2.0.0/Dynect.wsdl'; // The Base Dynect API2 URL protected $token = null; // Dynect login token
var $client; // The SOAP client protected $messages = array();
var $token; // Dynect login token protected $error_function = null;
var $user_name = 'USER';
var $customer_name = 'CUSTOMER';
var $password = 'PASSWORD';
protected $allowed_records = array('A', 'AAAA', 'CNAME', 'DNSKEY', 'DS', 'KEY', 'LOC', 'MX', 'NS', 'PTR', 'RP', 'SOA', 'SRV', 'TXT');
/** public function __construct($url = null) {
* Constructor
*
* @access public
*/
function Dynect_API() {
$this->CI =& get_instance();
$this->client = new SoapClient($this->base_url, array('cache_wsdl' => 0)); //Connect to the WSDL if($url) {
$this->base_url = $url;
log_message('debug', 'Dynect_API Class Initialized');
} }
// -------------------------------------------------------------------- $this->client = new SoapClient($this->base_url, array('cache_wsdl' => 1)); //Connect to the WSDL
function login() {
/* ##########################
Logging In
------------
To log in to the dynect API you must call SessionLogin with customer name, username and password
Some Returned Values
status - success or failure
data->token - to be used with all other commands
** Complete Documentations can be found at
https://manage.dynect.net/help/docs/api2/soap/
########################## */
if(!isset($this->user_name) || !isset($this->customer_name) || !isset($this->password)) {
show_error('You must set your username, customer name, and password in application/config/dynect_api.php in order to login.');
} }
else {
public function setErrorFunction($error_function) {
$this->error_function = $error_function;
}
protected function error($message) {
$this->messages[] = $message;
// If we've been given a custom error function, use it.
// Eg for Drupal, you can pass in drupal_set_message to setErrorFunction()
if($this->error_function) {
$error_function = $this->error_function;
$error_function($message);
}
}
public function getErrors() {
return $this->messages;
}
public function login($customer_name, $user_name, $password) {
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'user_name'=> $this->user_name, 'user_name'=> $user_name,
'customer_name' => $this->customer_name, 'customer_name' => $customer_name,
'password' => $this->password 'password' => $password,
) ),
); );
$result = $this->client->__soapCall('SessionLogin',$parameters); $result = $this->soapCall('SessionLogin', $parameters);
if(is_soap_fault($result)){ if($result && ($result->status == 'success')){
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
die();
}
if($result->status == 'success'){
$this->token = $result->data->token; $this->token = $result->data->token;
return true; return true;
} else { }
log_message('error', 'Dynect_API could not log in. Result status: '.$result->status); else {
die(); // This case seems to be handled adequately by catching the soapfault above.
// Dynect's servers return HTTP 500 on failed login -> soapfault.
}
} }
} // end if isset user_name
} // end login
function get_all_records($zone, $fqdn) { function get_all_records($zone, $fqdn) {
/* ##########################
Getting All Records on a zone
------------
To get a list of all records send a GetANYRecords command with the token, zone, and fqdn as paramters
Some Returned Values
status - success or failure
data - object containing a list record type containers each with the rdata, fqdn, record_type, ttl and zone
** Complete Documentations can be found at
https://manage.dynect.net/help/docs/api2/soap/
########################## */
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token, 'token'=> $this->token,
'zone' => $zone, 'zone' => $zone,
'fqdn' => $fqdn 'fqdn' => $fqdn,
) )
); );
echo '<b>Retrieving all Records</b><br/>'; $result = $this->soapCall('GetANYRecords', $parameters);
echo '--------------------------<br/>';
$result = $this->client->__soapCall('GetANYRecords',$parameters);
if(is_soap_fault($result)){ if($result && ($result->status == 'success')){
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
die();
}
if($result->status == 'success'){
return $result->data; return $result->data;
} else {
die('Unable to Get records');
} }
else {
} // end get_record return false;
}
}
/** /**
* Create a Zone * Create a Zone
* *
* @access public * @access public
* @param Name of the zone * @param Name of the zone
* @param Administrative contact for this zone * @param Administrative contact for this zone
* @param Default TTL (in seconds) for records in the zone * @param Default TTL (in seconds) for records in the zone
* @return void * @return void
*/ */
function create_zone($zone, $rname, $ttl = 3600) { public function create_zone($zone, $rname, $ttl = 3600) {
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token, 'token'=> $this->token,
'zone' => $zone, 'zone' => $zone,
'rname' => $rname, 'rname' => $rname,
'ttl' => $ttl 'ttl' => $ttl,
) ),
); );
try{ $result = $this->soapCall('CreateZone', $parameters);
$result = $this->client->__soapCall('CreateZone',$parameters);
}
catch (SoapFault $ex) {
trigger_error("SOAP Fault: ( ".var_export($ex->detail,true)." )", E_USER_ERROR);
die();
}
if($result->status == 'success'){ if($result && ($result->status == 'success')) {
return $result->data; return $result->data;
} else { }
die('Unable to create zone'); else {
return false;
}
} }
} // end create_zone public function get_node_list($zone) {
/**
* Delete a Zone
*
* @access public
* @param Name of the zone
* @return boolean (true = success)
*/
function delete_zone($zone) {
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token, 'token'=> $this->token,
'zone' => $zone 'zone' => $zone,
) ),
); );
try{ $result = $this->soapCall('GetNodeList', $parameters);
$result = $this->client->__soapCall('DeleteOneZone',$parameters);
if($result && ($result->status == 'success')) {
return $result->data;
}
else {
return false;
} }
catch (SoapFault $ex) {
trigger_error("SOAP Fault: ( ".var_export($ex->detail,true)." )", E_USER_ERROR);
die();
} }
if($result->status == 'success'){ public function delete_zone($zone) {
return true;
} else { $parameters = array(
die('Unable to delete zone'); 'parameters' => array(
'token'=> $this->token,
'zone' => $zone,
),
);
$result = $this->soapCall('DeleteOneZone', $parameters);
return ($result && ($result->status == 'success'));
} }
} // end delete_zone /**
/**
* Publish a Zone * Publish a Zone
* *
* @access public * @access public
* @param Name of the zone * @param Name of the zone
* @return boolean (true = success) * @return boolean (true = success)
*/ */
function publish_zone($zone) { public function publish_zone($zone) {
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token, 'token'=> $this->token,
'zone' => $zone 'zone' => $zone,
) )
); );
try{ $result = $this->soapCall('PublishZone', $parameters);
$result = $this->client->__soapCall('PublishZone',$parameters); return ($result && ($result->status == 'success'));
}
catch (SoapFault $ex) {
trigger_error("SOAP Fault: ( ".var_export($ex->detail,true)." )", E_USER_ERROR);
die();
} }
if($result->status == 'success'){
return true;
} else {
die('Unable to publish zone');
}
} // end publish_zone /**
/**
* Delete Records - Deletes all records at fqdn of type * Delete Records - Deletes all records at fqdn of type
* *
* @access public * @access public
@ -239,37 +182,28 @@ class Dynect_API {
* @param Name of zone to delete records from * @param Name of zone to delete records from
* @param Name of node to delete records from * @param Name of node to delete records from
* @return bool (true=success) * @return bool (true=success)
*/ */
function delete_records($type, $zone, $fqdn) { public function delete_records($type, $zone, $fqdn) {
if(in_array($type, array('A', 'AAAA', 'CNAME', 'DNSKEY', 'DS', 'KEY', 'LOC', 'MX', 'NS', 'PTR', 'RP', 'SOA', 'SRV', 'TXT'))) {
if(!in_array($type, $this->allowed_records)) {
$this->error('Supplied record type ' . $type . ' is not in the allowed list.');
return false;
}
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token, 'token'=> $this->token,
'fqdn' => $fqdn, 'fqdn' => $fqdn,
'zone' => $zone 'zone' => $zone,
) ),
); );
try{ $result = $this->soapCall('Delete'.$type.'Records', $parameters);
$result = $this->client->__soapCall('Delete'.$type.'Records',$parameters); return ($result && ($result->status == 'success'));
}
catch (SoapFault $ex) {
trigger_error("SOAP Fault: ( ".var_export($ex->detail,true)." )", E_USER_ERROR);
die();
} }
if($result->status == 'success'){
return true;
} else {
die('Unable to create '.$type.' record.');
}
} /**
} // end delete_records
/**
* Create Record * Create Record
* *
* @access public * @access public
@ -277,6 +211,7 @@ class Dynect_API {
* @param Name of zone to add the record to * @param Name of zone to add the record to
* @param Name of node to add the record to * @param Name of node to add the record to
* @param RData defining the record to add * @param RData defining the record to add
*
* @return array data * @return array data
string fqdn Fully qualified domain name of a node in the zone string fqdn Fully qualified domain name of a node in the zone
hash rdata RData defining the record hash rdata RData defining the record
@ -285,37 +220,34 @@ class Dynect_API {
string ttl TTL for the record. string ttl TTL for the record.
string zone Name of the zone string zone Name of the zone
*/ */
function create_record($type, $zone, $fqdn, $rdata) {
if(in_array($type, array('A', 'AAAA', 'CNAME', 'DNSKEY', 'DS', 'KEY', 'LOC', 'MX', 'NS', 'PTR', 'RP', 'SOA', 'SRV', 'TXT'))) { public function create_record($type, $zone, $fqdn, $rdata) {
if(!in_array($type, $this->allowed_records)) {
$this->error('Supplied record type ' . $type . ' is not in the allowed list.');
return false;
}
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token, 'token'=> $this->token,
'fqdn' => $fqdn, 'fqdn' => $fqdn,
'zone' => $zone, 'zone' => $zone,
'rdata' => $rdata 'rdata' => $rdata,
) ),
); );
try{ $result = $this->soapCall('Create'.$type.'Record', $parameters);
$result = $this->client->__soapCall('Create'.$type.'Record',$parameters);
}
catch (SoapFault $ex) {
trigger_error("SOAP Fault: ( ".var_export($ex->detail,true)." )", E_USER_ERROR);
die();
}
if($result->status == 'success'){ if($result && ($result->status == 'success')){
return $result->data; return $result->data;
} else { }
die('Unable to create '.$type.' record.'); else {
return false;
}
} }
} /**
} // end create_record
/**
* Get Records * Get Records
* *
* @access public * @access public
@ -323,41 +255,38 @@ class Dynect_API {
* @param Name of zone to get the record of * @param Name of zone to get the record of
* @param Name of node to get the record of * @param Name of node to get the record of
* @return array data * @return array data
string fqdn Fully qualified domain name of a node in the zone string fqdn Fully qualified domain name of a node in the zone
hash rdata RData defining the record hash rdata RData defining the record
(response data) (response data)
string record_type The RRType of the record string record_type The RRType of the record
string ttl TTL for the record. string ttl TTL for the record.
string zone Name of the zone string zone Name of the zone
*/ */
function get_records($type, $zone, $fqdn) {
if(in_array($type, array('A', 'AAAA', 'CNAME', 'DNSKEY', 'DS', 'KEY', 'LOC', 'MX', 'NS', 'PTR', 'RP', 'SOA', 'SRV', 'TXT'))) { public function get_records($type, $zone, $fqdn) {
if(!in_array($type, $this->allowed_records)) {
$this->error('Supplied record type ' . $type . ' is not in the allowed list.');
return false;
}
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token, 'token'=> $this->token,
'fqdn' => $fqdn, 'fqdn' => $fqdn,
'zone' => $zone 'zone' => $zone,
) )
); );
try{ $result = $this->soapCall('Get'.$type.'Records', $parameters);
$result = $this->client->__soapCall('Get'.$type.'Records',$parameters);
}
catch (SoapFault $ex) {
trigger_error("SOAP Fault: ( ".var_export($ex->faultstring,true)." )", E_USER_ERROR);
die();
}
if($result->status == 'success'){ if($result && ($result->status == 'success')) {
return $result->data; return $this->rtn($result->data);
} else { }
die('Unable to get '.$type.' records.'); else {
return false;
} }
} }
} // end get_records
/** /**
* Get all Zones * Get all Zones
@ -365,64 +294,73 @@ class Dynect_API {
* @access public * @access public
* @return zone data * @return zone data
*/ */
function get_zones() {
public function get_zones() {
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token 'token'=> $this->token,
) ),
); );
$result = $this->client->__soapCall('GetZones',$parameters); $result = $this->soapCall('GetZones', $parameters);
if(is_soap_fault($result)){ if($result && ($result->status == 'success')) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR); // Normalise records to an array of objects.
die(); return $this->rtn($result->data);
}
else {
return false;
}
} }
if($result->status == 'success'){ public function logout() {
return $result->data;
} else {
die('Unable to get zones');
}
} // end get_zones
function logout() {
/* ##########################
Logging Out
------------
To log in to the dynect API you must call SessionLogout with the token received at login
Some Returned Values
status - success or failure
** Complete Documentations can be found at
https://manage.dynect.net/help/docs/api2/soap/
########################## */
$parameters = array( $parameters = array(
'parameters' => array( 'parameters' => array(
'token'=> $this->token 'token'=> $this->token,
) ),
); );
$result = $this->client->__soapCall('SessionLogout',$parameters); $result = $this->soapCall('SessionLogout', $parameters);
return ($result && ($result->status == 'success'));
if(is_soap_fault($result)){
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
die();
} }
$message = $result->msgs; /**
* A wrapper around $this->client->__soapCall that handles exceptions and logs
* them as messages.
* @param string $method
* @param array $parameters
*/
if($result->status != 'success'){ protected function soapCall($method, $parameters) {
log_message('error','Dynect_API unable to log out.'); try{
$result = $this->client->__soapCall($method, $parameters);
}
catch (SoapFault $ex) {
// Log all the messages:
foreach($ex->detail->ErrorResponse->enc_value->msgs as $message) {
$this->error($message->info);
}
return false;
}
return $result;
} }
} // end logout
/**
* DynECT's API has a habit of returning a single object for a single result,
* or an array of objects for multiple results. This is kind of annoying as
* we can't safely interate over the result. This method normalises the result
* to an array.
*/
protected function rtn($return) {
if(is_array($return)) {
return $return;
}
else {
return array($return);
}
}
}
} // end class