<?php

require_once dirname(__FILE__).'/../../../newedge/classes/NE_Network.php';

/**
 * A class to hand domain lookups and registrations to PlanetDomain.
 * @package Clientexec
 * @author Dixon Davis
 * @version November 05, 2008
 */
class PlanetDomain
{
    var $host; // Planet Domain use the same URL for both Test and Live API calls.
    var $user;
    var $passwd;
    var $resellerid;
    var $logger;
    var $curlResult;

    /**
     * Constructs a new PlanetDomain object.
     *
     * @param string $user The reseller username
     * @param string $passwd The resellers password
     * @param string $resellerid The reseller Id
     * @return PlanetDomain A PlanetDomain object
     */
    function PlanetDomain($user, $passwd, $resellerid, $logger)
    {
        $this->host = "https://www.planetdomain.com/servlet/TLDServlet";
        $this->user = $user;
        $this->passwd = $passwd;
        $this->resellerid = $resellerid;
        $this->logger = $logger;
    }

    /**
     * Checks the availibility of a domain.
     *
     * @param string $domain The domain name to lookup
     * @return an xml or a KEY_VALUE if not specified(default) - use print_r() to view the structure
     */
    function domain_lookup($domain,$operation)
    {
        /* Create the POST data array */

        $this->logger->log(4, "Domain Lookup for the PlanetDomain"); 

        $lookupfields = array(
                            'operation'=>urlencode($operation),
                            'admin.username'=>urlencode($this->user),
                            'admin.password'=>urlencode($this->passwd),
                            'reseller.id'=>urlencode($this->resellerid),
                            'domain.name'=>urlencode($domain)
                        );

        //url-ify the data for the POST
        $lookup_string = "";
        foreach($lookupfields as $key=>$value) {
            $lookup_string .= $key.'='.$value.'&';
        }

        $lookup_string = rtrim($lookup_string,'&');
        $lookupResult = $this->curlPost($lookup_string);
        $this->logger->log(4, "Data Passed:".print_r($lookup_string,true)); 
        return $lookupResult;

    }

    /**
     * Register a User to the PlanetDomain.
     *
     * @param string $operation The operation to be performed
     * @param array $param The values to be POSTed
     * @return an xml or a KEY_VALUE if not specified(default) - use print_r() to view the structure
     */
     function addUser($operation,$param)
     {
        $this->logger->log(4, "User registration for the PlanetDomain");
        $useraddfields = array(
                            'operation'=>urlencode($operation),
                            'admin.username'=>urlencode($this->user),
                            'admin.password'=>urlencode($this->passwd),
                            'reseller.id'=>urlencode($this->resellerid)
                        );
        //url-ify the data for the POST
        $useradd_string = "";
        foreach($useraddfields as $k=>$val) {
            $useradd_string .= $k.'='.$val.'&';
        }

        // Add the user details

        foreach($param as $pkey=>$pvalue) {
            $useradd_string .= $pkey.'='.$pvalue.'&';
        }

        $useradd_string = rtrim($useradd_string,'&');
        $addUserResult = $this->curlPost($useradd_string);
        return $addUserResult;
     }

     /**
     * Register the domain with the PlanetDomain.
     *
     * @param string $operation The operation to be performed
     * @param array $param The values to be POSTed
     * @return an xml or a KEY_VALUE if not specified(default) - use print_r() to view the structure
     */
     function registerPlanetDomain($operation,$registerData)
     {
        $this->logger->log(4, "Domain registration for the PlanetDomain");
        $regdomainfields = array(
                            'operation'=>urlencode($operation),
                            'admin.username'=>urlencode($this->user),
                            'admin.password'=>urlencode($this->passwd),
                            'reseller.id'=>urlencode($this->resellerid)
                        );
        //url-ify the data for the POST
        $regdomain_string = "";
        foreach($regdomainfields as $regkeys=>$regval) {
            $regdomain_string .= $regkeys.'='.$regval.'&';
        }

        // Add the registration details

        foreach($registerData as $regdatakey=>$regdatavalue) {
            $regdomain_string .= $regdatakey.'='.$regdatavalue.'&';
        }

        $regdomain_string = rtrim($regdomain_string,'&');
        $regDomainResult = $this->curlPost($regdomain_string);

        return $regDomainResult;
     }

     /**
     * Get the User Information from the PlanetDomain by domain username.
     *
     * @param string $operation The operation to be performed
     * @param string $username Domain username
     * @return an xml or a KEY_VALUE if not specified(default) - use print_r() to view the structure
     */
     function userInfo($operation,$username)
     {
        $userinfofields = array(
                            'operation'=>urlencode($operation),
                            'admin.username'=>urlencode($this->user),
                            'admin.password'=>urlencode($this->passwd),
                            'reseller.id'=>urlencode($this->resellerid),
                            'user.username'=>urlencode($username)
                        );
        //url-ify the data for the POST
        $userinfo_string = "";
        foreach($userinfofields as $uinfokeys=>$uinfoval) {
            $userinfo_string .= $uinfokeys.'='.$uinfoval.'&';
        }

        $userinfo_string = rtrim($userinfo_string,'&');
        $userinfoResult = $this->curlPost($userinfo_string);

        return $userinfoResult;
     }

     /**
     * Update the User Information.
     *
     * @param string $operation The operation to be performed
     * @param array $userdetails Domain user details
     * @return an xml or a KEY_VALUE if not specified(default) - use print_r() to view the structure
     */
     function updateUser($operation,$userDetails)
     {
        $userupdatefields = array(
                            'operation'=>urlencode($operation),
                            'admin.username'=>urlencode($this->user),
                            'admin.password'=>urlencode($this->passwd),
                            'reseller.id'=>urlencode($this->resellerid)
                        );
        //url-ify the data for the POST
        $userupdate_string = "";
        foreach($userupdatefields as $Uupdatekey=>$Uupdateval) {
            $userupdate_string .= $Uupdatekey.'='.$Uupdateval.'&';
        }

        foreach($userDetails as $userkey=>$userval) {
            $userupdate_string .= $userkey.'='.$userval.'&';
        }

        $userupdate_string = rtrim($userupdate_string,'&');
        $userUpdateResult = $this->curlPost($userupdate_string);

        return $userUpdateResult;
     }

     /**
     * Get the General Domain Information.
     *
     * @param string $operation The operation to be performed
     * @param string $domain Domain Name
     * @return an xml or a KEY_VALUE if not specified(default) - use print_r() to view the structure
     */
     function domainInfo($operation,$domain)
     {
        $domaininfofields = array(
                            'operation'=>urlencode($operation),
                            'admin.username'=>urlencode($this->user),
                            'admin.password'=>urlencode($this->passwd),
                            'reseller.id'=>urlencode($this->resellerid),
                            'domain.name'=>urlencode($domain)
                        );

        //url-ify the data for the POST
        $domaininfo_string = "";
        foreach($domaininfofields as $key=>$value) {
            $domaininfo_string .= $key.'='.$value.'&';
        }

        $domaininfo_string = rtrim($domaininfo_string,'&');
        $domaininfoResult = $this->curlPost($domaininfo_string);

        return $domaininfoResult;
     }

     /**
     * Update DNS Information.
     *
     * @param string $operation The operation to be performed
     * @param array $dns DNS data
     * @return an xml or a KEY_VALUE if not specified(default) - use print_r() to view the structure
     */
     function updateDNS($operation,$domain)
     {
        $nsfields = array(
                            'operation'=>urlencode($operation),
                            'admin.username'=>urlencode($this->user),
                            'admin.password'=>urlencode($this->passwd),
                            'reseller.id'=>urlencode($this->resellerid),
                            'domain.name'=>urlencode($domain)
                        );

        //url-ify the data for the POST
        $ns_string = "";
        foreach($nsfields as $key=>$value) {
            $ns_string .= $key.'='.$value.'&';
        }

        $this->logger->log(4, "DNS Array Info :".print_r($dns,true));
//         foreach($dns as $nskey=>$nsvalue) {
//             $ns_string .= $nskey.'='.$nsvalue.'&';
//         }

        $ns_string = rtrim($ns_string,'&');
        $this->logger->log(4, "updateDNS Info :".print_r($ns_string,true));
        $dnsResult = $this->curlPost($ns_string);

        return $dnsResult;
     }

    /**
     * POST to the given URL using CURL. The URL is already been set.
     *
     * @param string $data The formatted POST data
     * @return the output from the POST API usually
     */
    function curlPost($data)
    {
        unset($this->curlResult);

        if (!function_exists('curl_init')) {
                return new NE_Error('Error: the curl extension is not enabled so you have to specify the path to the curl command in the admin settings');
        }

        $this->logger->log(4, "Connecting to URL using CURL: ".print_r($this->host,true));

        $ch = curl_init();    // initialize curl handle
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_URL,$this->host); // set url to post to
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);// allow redirects
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); // return into a variable
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 5s
        curl_setopt($ch, CURLOPT_POST, true); // set POST method
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields
        $this->curlResult = curl_exec($ch); // run the whole process
        curl_close($ch);
        $this->logger->log(4, "Curl Retrived Data :".print_r($this->curlResult,true));
        return $this->curlResult;
    }

}

?>
