From cfc020814ef87368b43aa7e9661c0dfd96defb00 Mon Sep 17 00:00:00 2001
From: Nigel Kukard <nkukard@lbsd.net>
Date: Sat, 3 Nov 2012 13:01:22 +0000
Subject: [PATCH] Major error handling rewrite & cleanup

Quite a number of code cleanups, error handling rewrite in many cases,
also implemented a temporary _Sync function to allow automatic info
syncing with the registrar.

Set autorenew to false when domain is updated.

Change-Id: Ic205ebe2aa319866d5e96b6e3f98e439a3471df6
---
 modules/registrars/cozaepp/cozaepp.php | 465 +++++++++++++++++--------
 1 file changed, 318 insertions(+), 147 deletions(-)

diff --git a/modules/registrars/cozaepp/cozaepp.php b/modules/registrars/cozaepp/cozaepp.php
index ce587f8..a2088e6 100644
--- a/modules/registrars/cozaepp/cozaepp.php
+++ b/modules/registrars/cozaepp/cozaepp.php
@@ -58,9 +58,15 @@ function cozaepp_GetNameservers($params) {
 	$tld = $params["tld"];
 	$sld = $params["sld"];
 	$domain = "{$sld}.{$tld}";
-	
+
+
 	# Create new EPP client
 	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'GetNameservers/EPP: '.$client->toString();
+		return $values;
+	}
+
 	$result = $client->request($xml = '
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
 	<epp:command>
@@ -72,22 +78,30 @@ function cozaepp_GetNameservers($params) {
 	</epp:command>
 </epp:epp>
 ');
-	# Check results	
-	if(!is_array($result)) {
-		# Parse XML
-		$doc = new DOMDocument();
-		$doc->preserveWhiteSpace = false;
-		$doc->loadXML($result);
-		$ns = $doc->getElementsByTagName('hostName');
-		# Extract nameservers
-		$i =0;
-		$values = array();
-		foreach ($ns as $nn) {
-			$i++;
-			$values["ns{$i}"] = $nn->nodeValue;
-		}
+	# Parse XML result
+	$doc = new DOMDocument();
+	$doc->preserveWhiteSpace = false;
+	$doc->loadXML($result);
+	# Pull off status
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	# Check the result is ok
+	if($coderes != '1000') {
+		$values["error"] = "GetNameservers/domain-info($domain): Code ($coderes) $msg";
+		return $values;
+	}
+
+	# Grab hostname array
+	$ns = $doc->getElementsByTagName('hostName');
+	# Extract nameservers & build return result
+	$i = 1;	$values = array();
+	foreach ($ns as $nn) {
+		$values["ns{$i}"] = $nn->nodeValue;
+		$i++;
 	}
 
+	$values["status"] = $msg;
+
 	return $values;
 }
 
@@ -99,6 +113,7 @@ function cozaepp_SaveNameservers($params) {
 	$tld = $params["tld"];
 	$sld = $params["sld"];
 
+
 	# Generate XML for nameservers
 	if ($nameserver1 = $params["ns1"]) { 
 		$add_hosts = '
@@ -136,6 +151,11 @@ function cozaepp_SaveNameservers($params) {
 	
 	# Grab list of current nameservers
 	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'SaveNameservers/EPP: '.$client->toString();
+		return $values;
+	}
+
 	$registrarinfo = $client->request('
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" 
 		xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
@@ -148,25 +168,32 @@ function cozaepp_SaveNameservers($params) {
 	</epp:command>
 </epp:epp>
 ');
-	# Parse XML
+	# Parse XML result
 	$doc= new DOMDocument();
 	$doc->loadXML($registrarinfo);
+	# Pull off status
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	# Check if result is ok
 	if($coderes != '1000') {
-		$values["error"] = "Code (".$coderes.") ".$msg;
-	} else { 
-		# Generate list of nameservers to remove
-		$hostlist = $doc->getElementsByTagName('hostName');
-		foreach ($hostlist as $host) {
-			$rem_hosts .= '
+		$values["error"] = "SaveNameservers/domain-info($sld.$tld): Code ($coderes) $msg";
+		return $values;
+	}
+
+	$values["status"] = $msg;
+
+	# Generate list of nameservers to remove
+	$hostlist = $doc->getElementsByTagName('hostName');
+	foreach ($hostlist as $host) {
+		$rem_hosts .= '
 <domain:hostAttr>
 	<domain:hostName>'.$host->nodeValue.'</domain:hostName>
 </domain:hostAttr>
 ';
-		}
+	}
 
-		# Build request
-		$domainrenew = $client->request('
+	# Build request
+	$domainrenew = $client->request('
 <epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
 	<epp:command>
 		<epp:update>
@@ -180,24 +207,28 @@ function cozaepp_SaveNameservers($params) {
 				</domain:rem>
 			</domain:update>
 		</epp:update>
+		<epp:extension>
+			<cozadomain:update xsi:schemaLocation="http://co.za/epp/extensions/cozadomain-1-0 coza-domain-1.0.xsd">
+			<cozadomain:chg><cozadomain:autorenew>false</cozadomain:autorenew></cozadomain:chg></cozadomain:update>
+		</epp:extension>
 	</epp:command>
 </epp:epp>
 ');
 
-		# Parse XML result
-		$doc= new DOMDocument();
-		$doc->loadXML($domainrenew);
-		$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
-		$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
-		if($coderes != '1001') {
-			$values["error"] = "Code (".$coderes.") ".$msg;
-		} else { 
-			$values['status'] = "Domain update Pending. Based on .co.za policy, the estimated time taken is around 5 days.";
-		}
+	# Parse XML result
+	$doc= new DOMDocument();
+	$doc->loadXML($domainrenew);
+	# Pull off status
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	# Check if result is ok
+	if($coderes != '1001') {
+		$values["error"] = "SaveNameservers/domain-update($sld.$tld): Code ($coderes) $msg";
+		return $values;
 	}
 
-	# If error, return the error message in the value below
-	$values["error"] = $error;
+	$values['status'] = "Domain update Pending. Based on .co.za policy, the estimated time taken is around 5 days.";
+
 	return $values;
 }
 
@@ -208,8 +239,10 @@ function cozaepp_GetRegistrarLock($params) {
 	# Grab variables
 	$tld = $params["tld"];
 	$sld = $params["sld"];
+
+
 	# Get lock status
-	$lock = 1;
+	$lock = 0;
 	if ($lock=="1") {
 		$lockstatus="locked";
 	} else {
@@ -222,7 +255,7 @@ function cozaepp_GetRegistrarLock($params) {
 
 # NOT IMPLEMENTED
 function cozaepp_SaveRegistrarLock($params) {
-	$values["error"] = "Current co.za policy does not allow for the addition of client-side statuses on domains.";
+	$values["error"] = "SaveRegistrarLock: Current co.za policy does not allow for the addition of client-side statuses on domains.";
 	return $values;
 }
 
@@ -246,7 +279,7 @@ function cozaepp_RegisterDomain($params) {
 	$RegistrantCountry = $params["country"];
 	$RegistrantEmailAddress = $params["email"];
 	$RegistrantPhone = $params["phonenumber"];
-	# Get admin Details
+	# Get admin details
 	$AdminFirstName = $params["adminfirstname"];
 	$AdminLastName = $params["adminlastname"];
 	$AdminAddress1 = $params["adminaddress1"];
@@ -257,6 +290,9 @@ function cozaepp_RegisterDomain($params) {
 	$AdminCountry = $params["admincountry"];
 	$AdminEmailAddress = $params["adminemail"];
 	$AdminPhone = $params["adminphonenumber"];
+	# Our details
+	$contactid = $RegistrantFirstName.$params['userid'];
+
 
 	# Generate XML for namseverss
 	if ($nameserver1 = $params["ns1"]) { 
@@ -295,13 +331,18 @@ function cozaepp_RegisterDomain($params) {
 
 	# Send registration
 	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'RegisterDomain/EPP: '.$client->toString();
+		return $values;
+	}
+
 	$contact = $client->request('
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0"
 		xmlns:contact="urn:ietf:params:xml:ns:contact-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
 	<epp:command>
 		<epp:create>
 			<contact:create xsi:schemaLocation="urn:ietf:params:xml:ns:contact-1.0 contact-1.0.xsd">
-				<contact:id>'.$RegistrantFirstName.$params['userid'].'</contact:id>
+				<contact:id>'.$contactid.'</contact:id>
 				<contact:postalInfo type="loc">
 					<contact:name>'.$RegistrantFirstName.' '.$RegistrantLastName.'</contact:name>
 					<contact:addr>
@@ -328,20 +369,21 @@ function cozaepp_RegisterDomain($params) {
 	# Parse XML result
 	$doc= new DOMDocument();
 	$doc->loadXML($contact);
+	# Pull off status
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
 	if($coderes == '1000') {
-		$contactid = $RegistrantFirstName.$params['userid'];
 		$values['contact'] = 'Contact Created';
 	} else if($coderes == '2302') { 
-		$contactid = $RegistrantFirstName.$params['userid'];
 		$values['contact'] = 'Contact Already exists';
 	} else { 
-		$values["error"] = "Code (".$coderes.") ".$msg;
+		$values["error"] = "RegisterDomain/contact-create($contactid): Code ($coderes) $msg";
+		return $values;
 	}
 
-	# If our result is success, carry on
-	if ( $coderes == '1000' or $coderes =='2302' ) {
-		$domaincreate = $client->request('
+	$values["status"] = $msg;
+
+	$domaincreate = $client->request('
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0"
 		xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
 	<epp:command>
@@ -349,26 +391,31 @@ function cozaepp_RegisterDomain($params) {
 			<domain:create xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
 				<domain:name>'.$sld.'.'.$tld.'</domain:name>
 				<domain:ns>'.$add_hosts.'</domain:ns>
-			<domain:registrant>'.$contactid.'</domain:registrant>
-			<domain:authInfo>
-				<domain:pw>coza</domain:pw>
-			</domain:authInfo>
-		</domain:create>
+				<domain:registrant>'.$contactid.'</domain:registrant>
+				<domain:authInfo>
+					<domain:pw>coza</domain:pw>
+				</domain:authInfo>
+			</domain:create>
 		</epp:create>
+		<epp:extension>
+			<cozadomain:create>
+				<cozadomain:autorenew>false</cozadomain:autorenew>
+			</cozadomain:create>
+		</epp:extension>
 	</epp:command>
 </epp:epp>
 ');
-		$doc= new DOMDocument();
-		$doc->loadXML($domaincreate);
-		$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
-		$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
-		if($coderes != '1000') {
-			$values["error"] = "Code (".$coderes.") ".$msg;
-		} else {
-			$values["status"] = $msg;
-		}					
+	$doc= new DOMDocument();
+	$doc->loadXML($domaincreate);
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	if($coderes != '1000') {
+		$values["error"] = "RegisterDomain/domain-create($sld.$tld): Code ($coderes) $msg";
+		return $values;
 	}
 
+	$values["status"] = $msg;
+
 	return $values;
 }
 
@@ -397,7 +444,7 @@ function cozaepp_TransferDomain($params) {
 	$RegistrantCountry = $params["country"];
 	$RegistrantEmailAddress = $params["email"];
 	$RegistrantPhone = $params["phonenumber"];
-	# Admin Details
+	# Admin details
 	$AdminFirstName = $params["adminfirstname"];
 	$AdminLastName = $params["adminlastname"];
 	$AdminAddress1 = $params["adminaddress1"];
@@ -408,9 +455,17 @@ function cozaepp_TransferDomain($params) {
 	$AdminCountry = $params["admincountry"];
 	$AdminEmailAddress = $params["adminemail"];
 	$AdminPhone = $params["adminphonenumber"];
+	# Our details
+	$contactid = $RegistrantFirstName.$params['userid'];
+
 
 	# Grab registrar info	
 	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'TransferDomain/EPP: '.$client->toString();
+		return $values;
+	}
+
 	$registrarinfo = $client->request('
 <epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
 	<epp:command>
@@ -427,20 +482,22 @@ function cozaepp_TransferDomain($params) {
 	$doc->loadXML($registrarinfo);
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
 	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
-
+	# We should get a 1001 back
 	if($coderes != '1001') {
-		$values["error"] = "Code (".$coderes.") ".$msg;
-	} else { 
-		$values["status"] = $msg;
-				
-		# Create contact details
-		$contact = $client->request('
+		$values["error"] = "TransferDomain/domain-transfer($sld.$tld): Code ($coderes) $msg";
+		return $values;
+	}
+
+	$values["status"] = $msg;
+
+	# Create contact details
+	$contact = $client->request('
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0"
 		xmlns:contact="urn:ietf:params:xml:ns:contact-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
 	<epp:command>
 		<epp:create>
 			<contact:create xsi:schemaLocation="urn:ietf:params:xml:ns:contact-1.0 contact-1.0.xsd">
-				<contact:id>'.$RegistrantFirstName.$params['userid'].'</contact:id>
+				<contact:id>'.$contactid.'</contact:id>
 				<contact:postalInfo type="loc">
 					<contact:name>'.$RegistrantFirstName.' '.$RegistrantLastName.'</contact:name>
 					<contact:addr>
@@ -463,21 +520,24 @@ function cozaepp_TransferDomain($params) {
 	</epp:command>
 </epp:epp>
 ');
-		# Parse XML result
-		$doc= new DOMDocument();
-		$doc->loadXML($contact);
-		$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
-		if($coderes == '1000') {
-			$contactid = $RegistrantFirstName.$params['userid'];
-			$values['contact'] = 'Contact Created';
-		} else if($coderes == '2302') { 
-			$contactid = $RegistrantFirstName.$params['userid'];
-			$values['contact'] = 'Contact Already exists';
-		} else { 
-			$values["error"] = "Code (".$coderes.") ".$msg;
-		}
+	# Parse XML result
+	$doc= new DOMDocument();
+	$doc->loadXML($contact);
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	if($coderes == '1000') {
+		$contactid = $RegistrantFirstName.$params['userid'];
+		$values['contact'] = 'Contact Created';
+	} else if($coderes == '2302') {
+		$contactid = $RegistrantFirstName.$params['userid'];
+		$values['contact'] = 'Contact Already exists';
+	} else {
+		$values["error"] = "TransferDomain/contact-create($contactid): Code ($coderes) $msg";
+		return $values;
 	}
 
+	$values["status"] = $msg;
+
 	return $values;
 }
 
@@ -490,8 +550,14 @@ function cozaepp_RenewDomain($params) {
 	$sld = $params["sld"];
 	$regperiod = $params["regperiod"];
 
+
 	# Put your code to renew domain here
 	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'RenewDomain/EPP: '.$client->toString();
+		return $values;
+	}
+
 	$registrarinfo = $client->request('
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" 
 		xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
@@ -509,14 +575,23 @@ function cozaepp_RenewDomain($params) {
 	$doc= new DOMDocument();
 	$doc->loadXML($registrarinfo);
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
 	if($coderes != '1000') {
-		$values["error"] = "Code (".$coderes.") ".$msg;
-	} else { 
-		# Sanitize expiry date
-		$expdate = substr($doc->getElementsByTagName('exDate')->item(0)->nodeValue,0,10);
+		$values["error"] = "RenewDomain/domain-info($sld.$tld)): Code ($coderes) $msg";
+		return $values;
+	}
+
+	$values["status"] = $msg;
+
+	# Sanitize expiry date
+	$expdate = substr($doc->getElementsByTagName('exDate')->item(0)->nodeValue,0,10);
+	if (empty($expdate)) {
+		$values["error"] = "RenewDomain/domain-info($sld.$tld): Domain info not available";
+		return $values;
+	}
 
-		# Send request to renew
-		$domainrenew = $client->request('
+	# Send request to renew
+	$domainrenew = $client->request('
 <epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
 	<epp:command>
 		<epp:renew>
@@ -529,18 +604,18 @@ function cozaepp_RenewDomain($params) {
 </epp:epp>
 ');
 
-		# Parse XML result	
-		$doc= new DOMDocument();
-		$doc->loadXML($domainrenew);
-		$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
-		$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
-		if($coderes != '1000') {
-			$values["error"] = "Code (".$coderes.") ".$msg;
-		} else {
-			$values["status"] = $msg;
-		}
+	# Parse XML result
+	$doc= new DOMDocument();
+	$doc->loadXML($domainrenew);
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	if($coderes != '1000') {
+		$values["error"] = "RenewDomain/domain-renew($sld.$tld,$expdate): Code (".$coderes.") ".$msg;
+		return $values;
 	}
 
+	$values["status"] = $msg;
+
 	# If error, return the error message in the value below
 	return $values;
 }
@@ -555,6 +630,11 @@ function cozaepp_GetContactDetails($params) {
 
 	# Grab contact details
 	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'GetContactDetails/EPP: '.$client->toString();
+		return $values;
+	}
+
 	$registrarinfo = $client->request('
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" 
 		xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
@@ -573,15 +653,20 @@ function cozaepp_GetContactDetails($params) {
 	$doc->loadXML($registrarinfo);
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
 	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
-
 	# Check result
 	if($coderes != '1000') {
-		$values["error"] = "Code (".$coderes.") ".$msg;
-	} else { 
+		$values["error"] = "GetContactDetails/domain-info($sld.$tld): Code (".$coderes.") ".$msg;
+		return $values;
+	}
 
-		# Grab contact info
-		$registrant = $doc->getElementsByTagName('registrant')->item(0)->nodeValue;
-		$domaininfo = $client->request('
+	# Grab contact info
+	$registrant = $doc->getElementsByTagName('registrant')->item(0)->nodeValue;
+	if (empty($registrant)) {
+		$values["error"] = "GetContactDetails/domain-info($sld.$tld): Registrant info not available";
+		return $values;
+	}
+
+	$domaininfo = $client->request('
 <epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
 	<epp:command>
 		<epp:info>
@@ -592,30 +677,29 @@ function cozaepp_GetContactDetails($params) {
 	</epp:command>
 </epp:epp>
 ');
-		# Parse XML result
-		$doc= new DOMDocument();
-		$doc->loadXML($domaininfo);
-		$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
-		$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
-
-		# Check result
-		if($coderes != '1000') {
-			$values["error"] = "Code (".$coderes.") ".$msg;
-		} else { 
-			# Setup return values
-			$values["Registrant"]["Contact Name"] = $doc->getElementsByTagName('name')->item(0)->nodeValue;
-			$values["Registrant"]["Organisation"] = $doc->getElementsByTagName('org')->item(0)->nodeValue;
-			$values["Registrant"]["Address line 1"] = $doc->getElementsByTagName('street')->item(0)->nodeValue;
-			$values["Registrant"]["Address line 2"] = $doc->getElementsByTagName('street')->item(1)->nodeValue;
-			$values["Registrant"]["TownCity"] = $doc->getElementsByTagName('city')->item(0)->nodeValue;
-			$values["Registrant"]["State"] = $doc->getElementsByTagName('sp')->item(0)->nodeValue;
-			$values["Registrant"]["Zip code"] = $doc->getElementsByTagName('pc')->item(0)->nodeValue;
-			$values["Registrant"]["Country Code"] = $doc->getElementsByTagName('cc')->item(0)->nodeValue;
-			$values["Registrant"]["Phone"] = $doc->getElementsByTagName('voice')->item(0)->nodeValue;
-			$values["Registrant"]["Email"] = $doc->getElementsByTagName('email')->item(0)->nodeValue;
-		}
+	# Parse XML result
+	$doc= new DOMDocument();
+	$doc->loadXML($domaininfo);
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	# Check result
+	if($coderes != '1000') {
+		$values["error"] = "GetContactDetails/contact-info($registrant): Code (".$coderes.") ".$msg;
+		return $values;
 	}
 
+	# Setup return values
+	$values["Registrant"]["Contact Name"] = $doc->getElementsByTagName('name')->item(0)->nodeValue;
+	$values["Registrant"]["Organisation"] = $doc->getElementsByTagName('org')->item(0)->nodeValue;
+	$values["Registrant"]["Address line 1"] = $doc->getElementsByTagName('street')->item(0)->nodeValue;
+	$values["Registrant"]["Address line 2"] = $doc->getElementsByTagName('street')->item(1)->nodeValue;
+	$values["Registrant"]["TownCity"] = $doc->getElementsByTagName('city')->item(0)->nodeValue;
+	$values["Registrant"]["State"] = $doc->getElementsByTagName('sp')->item(0)->nodeValue;
+	$values["Registrant"]["Zip code"] = $doc->getElementsByTagName('pc')->item(0)->nodeValue;
+	$values["Registrant"]["Country Code"] = $doc->getElementsByTagName('cc')->item(0)->nodeValue;
+	$values["Registrant"]["Phone"] = $doc->getElementsByTagName('voice')->item(0)->nodeValue;
+	$values["Registrant"]["Email"] = $doc->getElementsByTagName('email')->item(0)->nodeValue;
+
 	return $values;
 }
 
@@ -626,7 +710,7 @@ function cozaepp_SaveContactDetails($params) {
 	# Grab variables
 	$tld = $params["tld"];
 	$sld = $params["sld"];
-	# Registrant Details
+	# Registrant details
 	$registrant_name = $params["contactdetails"]["Registrant"]["Contact Name"];
 	$registrant_org = $params["contactdetails"]["Registrant"]["Organisation"];
 	$registrant_address1 =  $params["contactdetails"]["Registrant"]["Address line 1"];
@@ -639,8 +723,13 @@ function cozaepp_SaveContactDetails($params) {
 	#$registrant_fax = '',
 	$registrant_email = $params["contactdetails"]["Registrant"]["Email"];
 
-	#Grab domain info
+	# Grab domain info
 	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'SaveContactDetails/EPP: '.$client->toString();
+		return $values;
+	}
+
 	$registrarinfo = $client->request('
 <epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" 
 		xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
@@ -657,12 +746,17 @@ function cozaepp_SaveContactDetails($params) {
 	$doc= new DOMDocument();
 	$doc->loadXML($registrarinfo);
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
 	if($coderes != '1000') {
-		$values["error"] = "Code (".$coderes.") ".$msg;
-	} else { 
-		# Time to do the update
-		$registrant = $doc->getElementsByTagName('registrant')->item(0)->nodeValue;
-		$contact = $client->request('
+		$values["error"] = "SaveContactDetails/domain-info($sld.$tld): Code (".$coderes.") ".$msg;
+		return $values;
+	}
+
+	$values["status"] = $msg;
+
+	# Time to do the update
+	$registrant = $doc->getElementsByTagName('registrant')->item(0)->nodeValue;
+	$contact = $client->request('
 <epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0" xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
 	<epp:command>
 		<epp:update>
@@ -691,18 +785,18 @@ function cozaepp_SaveContactDetails($params) {
 </epp:epp>
 ');
 
-		# Parse XML result
-		$doc= new DOMDocument();
-		$doc->loadXML($contact);
-		$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
-		$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
-		if($coderes == '1001') { 
-			$values['status'] = $msg;
-		} else { 
-			$values["error"] = "Code (".$coderes.") ".$msg;
-		}
+	# Parse XML result
+	$doc= new DOMDocument();
+	$doc->loadXML($contact);
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	if($coderes != '1001') {
+		$values["error"] = "SaveContactDetails/contact-update($registrant): Code ($coderes) $msg";
+		return $values;
 	}
 
+	$values["status"] = $msg;
+
 	return $values;
 }
 
@@ -795,7 +889,7 @@ function _cozaepp_ackpoll($client,$msgid) {
 	# Ack poll message
 	$output = $client->request('
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-		<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
+<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
 	<command>
 		<poll op="ack" msgID="'.$id.'"/>
 	</command>
@@ -810,10 +904,11 @@ function _cozaepp_ackpoll($client,$msgid) {
 	$doc = new DOMDocument();
 	$doc->loadXML($output);
 	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
 
 	# Check result
 	if($coderes != '1301') {
-		return new PEAR_Error("Failed to ACK message $msgid");
+		return new PEAR_Error("ackpoll/poll-ack($id): Code ($msgid) $msg");
 	}
 }
 
@@ -837,7 +932,11 @@ function _cozaepp_Client() {
 		$use_ssl = true;
 	}
 	# Set certificate if we have one
- 	if ($use_ssl && isset($params['Certificate']) && file_exists($params['Certificate'])) {
+ 	if ($use_ssl && !empty($params['Certificate'])) {
+		if (!file_exists($params['Certificate'])) {
+			return PEAR_Error("Certificate file does not exist");
+		}
+
 		# Create SSL context
 		$context = stream_context_create();
 		stream_context_set_option($context, 'ssl', 'local_cert', $params['Certificate']);
@@ -875,6 +974,78 @@ function _cozaepp_Client() {
 	return $client;
 }
 
+function cozaepp_Sync($params) {
+	$domainid = $params['domainid'];
+	$domain = $params['domain'];
+	$sld = $params['sld'];
+	$tld = $params['tld'];
+	$registrar = $params['registrar'];
+	$regperiod = $params['regperiod'];
+	$status = $params['status'];
+	$dnsmanagement = $params['dnsmanagement'];
+	$emailforwarding = $params['emailforwarding'];
+	$idprotection = $params['idprotection'];
+
+	# Other parameters used in your _getConfigArray() function would also be available for use in this function
+
+	# Grab domain info
+	$client = _cozaepp_Client();
+	if (PEAR::isError($client)) {
+		$values["error"] = 'Sync/EPP: '.$client->toString();
+		return $values;
+	}
+
+	$registrarinfo = $client->request('
+<epp:epp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:epp="urn:ietf:params:xml:ns:epp-1.0"
+		xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
+	<epp:command>
+		<epp:info>
+			<domain:info xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
+				<domain:name hosts="all">'.$domain.'</domain:name>
+			</domain:info>
+		</epp:info>
+	</epp:command>
+</epp:epp>
+');
 
+	$doc= new DOMDocument();
+	$doc->loadXML($registrarinfo);
+	$coderes = $doc->getElementsByTagName('result')->item(0)->getAttribute('code');
+	$msg = $doc->getElementsByTagName('msg')->item(0)->nodeValue;
+	# Check result
+	if($coderes != '1000') {
+		$values['error'] = "Sync/domain-info($domain): Code("._cozaepp_message($coderes).") $msg";
+		return $values;
+	}
+
+	# Check if we can get a status back
+	if($doc->getElementsByTagName('status')->item(0)) {
+		$statusres = $doc->getElementsByTagName('status')->item(0)->getAttribute('s');
+		$createdate = substr($doc->getElementsByTagName('crDate')->item(0)->nodeValue,0,10);
+		$nextduedate = substr($doc->getElementsByTagName('exDate')->item(0)->nodeValue,0,10);
+	} else {
+		$values['error'] = "Sync/domain-info($domain): Domain not found";
+		return $values;
+	}
+
+	$values['status'] = $msg;
+
+	# Check status and update
+	if ($statusres == "ok") {
+		$values['active'] = true;
+
+	} elseif ($statusres == "serverHold") {
+
+	} elseif ($statusres == "expired") {
+		$values['expired'] = true;
+
+	} else {
+		$values['error'] = "Sync/domain-info($domain): Unknown status code '$statusres' (File a bug report here: http://devlabs.linuxassist.net/projects/whmcs-coza-epp/issues/new)";
+	}
+
+	$values['expirydate'] = $nextduedate;
+
+	return $values;
+}
 
 ?>
-- 
GitLab