<?php include("../../../dbconnect.php");
if ("3">$_SESSION['adminuserlevel']) { header("Location: accessdenied.php"); exit; }
$query = "SELECT * FROM tbladmins WHERE username='".$_SESSION['adminusername']."'";
$result=mysql_query($query);
$data = mysql_fetch_array($result);
if ($data["password"]!=$_SESSION['adminpassword']) { session_unset(); session_destroy(); header("Location: login.php"); }
if ($data["id"]!=$_SESSION['adminid']) { session_unset(); session_destroy(); header("Location: login.php"); }
 ?><html>
<head>
<title>DirectI API</title>
<link href="../../../admin/style.css" rel="stylesheet" type="text/css">
</head>
<body style="margin:4px;">
<b>DirectI Interface</b> <?php

$query = "SELECT * FROM tbldomains WHERE id='$domainid'"; 
$result=mysql_query($query);
$data = mysql_fetch_array($result);
$userid = $data["userid"];
$domainname = $data["domain"];
$registrationperiod = $data["registrationperiod"];

function checkDomain($domain) {
global $domainparts;
if (preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i",$domain)) {
$domainparts = explode(".", $domain,2);
return true;
} else {
return false;
}
}

$x = checkDomain($domainname);
if ($x == true) {

$SERVICE_USERNAME=$CONFIG["DirectIUsername"];// User Name.
$SERVICE_PASSWORD=$CONFIG["DirectIPassword"];// Password
$SERVICE_ROLE="reseller";     // Role, always leave this as reseller
$SERVICE_LANGPREF="en";// Language Preference ISO Code, 'en' for English
$SERVICE_PARENTID=$CONFIG["DirectIParentID"];// Parent id.
$LIB_DIR="lib/";	   // Absolute or relative Path to your Lib folder
$customerid=$CONFIG["DirectICustomerID"];
			
include($LIB_DIR."apiutil.php");
include($LIB_DIR."response.class.php");

if ($action=="register") {
$query = "SELECT * FROM tblclients WHERE id='$userid'"; 
$result=mysql_query($query);
$data = mysql_fetch_array($result);
$name = $data["firstname"]." ".$data["lastname"];
$companyname = $data["companyname"].trim($domainid);
$email = $data["email"];
$address1 = $data["address1"];
$address2 = $data["address2"];
$city = $data["city"];
$state = $data["state"];
$postcode = $data["postcode"];
$country = $data["country"];
$phonenumber = $data["phonenumber"];
$phonenumber = str_replace(" ", "", $phonenumber);
$phonenumber = str_replace("-", "", $phonenumber);
include_once($LIB_DIR."DomContact.class.php");
$service = new DomContact($LIB_DIR . "wsdl/DomContact.wsdl");
$ret = $service->add($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID, $name, $companyname, $email, $address1, $address2, "", $city, $state, "US", $postcode, "00", $phonenumber, "", "", $customerid);
$res = new Response($ret);
if($res->isError())	{
$errorObj = $res->getErrorObj();
if ($errorObj["errorCode"]=="com.logicboxes.error.ValidationException") {
echo("(The Clients Details Failed DirectI Validation)");
}
}
# Contact ID
$contactid = $res->getResult();
# Now Register Domain
include_once($LIB_DIR."DomOrder.class.php");
$service = new DomOrder($LIB_DIR . "wsdl/DomOrder.wsdl");
$ns1=$CONFIG["DefaultNameserver1"];
$ns2=$CONFIG["DefaultNameserver2"];
$domainHash = getHashFromString(array($domainname."->".$registrationperiod));
$ns = getVectorFromString(array("$ns2","$ns1"));
$invoiceOption = "NoInvoice";
$ret = $service->add($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID, $domainHash, $ns, $contactid, $contactid, $contactid, $contactid, $customerid, $invoiceOption);
$res = new Response($ret);
if($res->isError())	{
echo("(Error: Domain may already be registered)");
} else {
echo("(Domain Registered Successfully)");
}
}

if ($action=="transfer") {
include_once($LIB_DIR."DomOrder.class.php");
$service = new DomOrder($LIB_DIR . "wsdl/DomOrder.wsdl");
$ret1 = $service->isTransferRequestValid($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID, $domainname);
$res1 = new Response($ret1);
if($res1->isError())	{
echo("(Error: Invalid Domain for transfer)");
} else {
if ($res1->getResult()) {
$query = "SELECT * FROM tblclients WHERE id='$userid'"; 
$result=mysql_query($query);
$data = mysql_fetch_array($result);
$name = $data["firstname"]." ".$data["lastname"];
$companyname = $data["companyname"].trim($domainid);
$email = $data["email"];
$address1 = $data["address1"];
$address2 = $data["address2"];
$city = $data["city"];
$state = $data["state"];
$postcode = $data["postcode"];
$country = $data["country"];
$phonenumber = $data["phonenumber"];
$phonenumber = str_replace(" ", "", $phonenumber);
include_once($LIB_DIR."DomContact.class.php");
$service = new DomContact($LIB_DIR . "wsdl/DomContact.wsdl");
$ret = $service->add($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID, $name, $companyname, $email, $address1, $address2, "", $city, $state, "US", $postcode, "00", $phonenumber, "", "", $customerid);
$res = new Response($ret);
if($res->isError())	{
echo("(Error - #114)");
}
# Contact ID
$contactid = $res->getResult();
# Transfer Domain
$ret = $service->transferDomain($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID, getHashFromString(array($domainname.'-> ')), $contactid,$contactid,$contactid,$contactid,$customerid,"NoInvoice");
$res = new Response($ret);
if($res->isError())	{
echo("(Error: Transfer request failed)");
} else {
echo("(Domain Transfer Successful)");
}}}
}

include_once($LIB_DIR."DomOrder.class.php");
$service = new DomOrder($LIB_DIR . "wsdl/DomOrder.wsdl");

if ($action=="save") {

if ($usedefault=="on") {
$ns1=$CONFIG["DefaultNameserver1"];
$ns2=$CONFIG["DefaultNameserver2"];
}

$ret = $service->modifyNameServer($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID,$orderid,getHashFromString(array("ns1->$ns2","ns2->$ns1")));
$res = new Response($ret);
if($res->isError())	{
echo("(Error - #141)");
} else {
echo("(Changes Submitted Successfully)");
}
unset($res);

}

$service = new DomOrder($LIB_DIR . "wsdl/DomOrder.wsdl");
$option = "domcno";
$ret = $service->getOrderIdByDomain($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID, $domainname, $option);
$res = new Response($ret);
if($res->isError())	{
echo("(Error: Domain Pending or Not Registered)");
}
?>
<br><form method="post" action="<?php echo $PHP_SELF ?>?domainid=<?php echo $domainid ?>&action=save&orderid=<?php echo $orderid ?>"><table><tr><td colspan=2><input type="button" value="Register" onClick="window.location='<?php echo $PHP_SELF ?>?domainid=<?php echo $domainid ?>&action=register'"> <input type="button" value="Transfer" onClick="window.location='<?php echo $PHP_SELF ?>?domainid=<?php echo $domainid ?>&action=transfer'"></td></tr>
<?php
$orderid = $res->getResult();
$option = getVectorFromString(array("All"));
$ret1 = $service->getDetails($SERVICE_USERNAME, $SERVICE_PASSWORD, $SERVICE_ROLE, $SERVICE_LANGPREF, $SERVICE_PARENTID, $orderid, $option);
$res1 = new Response($ret1);
if($res1->isError()){}else{$data = $res1->getResult();}
echo("<tr><td>Nameserver 1:</td><td><input type=\"text\" name=\"ns1\" size=\"40\" value=\"".$data["ns1"]."\"> <input type=\"checkbox\" name=\"usedefault\"> Use Default</td></tr><tr><td>Nameserver 2:</td><td><input type=\"text\" name=\"ns2\" size=\"40\" value=\"".$data["ns2"]."\"> <input type=\"submit\" value=\"Submit Changes\"></td></tr></table><a href=\"#\" onClick=\"window.open('modifycontactinfo.php?domainid=$domainid','','width=320,height=385');return false\">Modify Contact Information</a></form>");

} else {
echo("BAD DOMAIN");
}
			
?>
</body>
</html>