<?php
// +----------------------------------------------------------------------+
// | ModernBill [TM] .:. Domain Billing System                            |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001-2005 ModernGigabyte, LLC                          |
// +----------------------------------------------------------------------+
// | This source file is subject to the ModernBill End User License       |
// | Agreement (EULA), that is bundled with this package in the file      |
// | LICENSE, and is available at through the world-wide-web at           |
// | http://www.modernbill.com/extranet/LICENSE.txt                       |
// | If you did not receive a copy of the ModernBill license and are      |
// | unable to obtain it through the world-wide-web, please send a note   |
// | to license@modernbill.com so we can email you a copy immediately.    |
// +----------------------------------------------------------------------+
// | Authors: ModernGigabyte, LLC <info@moderngigabyte.com>               |
// | Support: http://www.modernsupport.com/modernbill/                    |
// +----------------------------------------------------------------------+
// | ModernGigabyte and ModernBill are trademarks of ModernGigabyte, LLC. |
// +----------------------------------------------------------------------+


if(!$CONF || $_REQUEST["CONF"]) {
	if (!defined("MB_LIBPATH")){
		define("MB_LIBPATH", realpath(dirname(__FILE__)."/../../../.."));
		require_once(MB_LIBPATH."/lib-pkg/parsefunction.php");
	}
	require_once mb_path("/lib-tk/include/common.php");
}
require_once mb_path("/lib-action/action.php");
require_once mb_path("/lib-mbapi/mbapi.php");

/**
 * This class is a child class of MB_Action. Using $_REQUEST["domainID"] it displays all
 * the details for the found domain, and only displays the details of a single domain.
 *
 * @author  Michael Fountain
 */
class ShowDomainDetails extends MB_Action {
	/**
	 * The constructor for a ShowDomainDetails object. This merely sets up our string represenation, calls
	 * the parent constructor, and sets up $this->params.
	 */
	function ShowDomainDetails() {
		parent::MB_Action();
		$this->stringRep = "ShowDomainDetails";
		$this->params = array("domainID");
	}

	/**
	 * Execute this action object, and display any output generated to the screen.
	 *
	 * @return  1 if no errors occurred during execution, or 0 if otherwise.
	 */
	function execute() {
		global $CONF;
		parent::execute();

		$domainID = ((int) $_REQUEST["domainID"]);
		if(!$domainID || !$this->confirmClientPermission("GetDomains", "domainID", $domainID, $CONF["user"]["id"])) {
			$this->addError(TRANS_ACTIONERROR, "Invalid domain ID given. ($domainID)");
		} else {

//			$query = $this->getMBAPIQuery("GetDomains");
			$query = $this->getMBAPIQuery("GetDomains", "", array("packageType" => getTypeID("products",TRANS_DOMAIN_PRODUCT)));
			$data = dispatchMBAPI($query, 1);

			//Pull the registrar info
			$capabilities = array();
			$registrarName = NULL;
			$registrarID = $data["mbapi"][0]["results"][0]["domains"][0]["domain"][0]["registrarID"][0];
			if($registrarID) {
				$registrarQuery = $this->getMBAPIQuery("GetConfigs", "", array("configGroupID" => $registrarID));
				$registrar = dispatchMBAPI($registrarQuery, 1);
				if($this->addMBAPIErrors($registrar) === 0 && !$this->getNumErrors()) {
					$registrarName = $registrar["mbapi"][0]["results"][0]["configs"][0]["config"][0]["configGroupName"][0];
				}
				if($registrarName) {
					try {
						require_once("ModuleTool.php");
						$r = ModuleTool::getModuleInstance("registrar", $registrarName);
						$registrarCapabilities = $r->getCapabilities();
						$capabilities["domainGetRenewDate"] = $registrarCapabilities[REGISTRAR_GETDOMAINRENEWDATE];
						//$capabilities[] = $registrarCapabilities[REGISTRAR_GETDOMAINCONTACTDATA];
						$capabilities["domainContact"] = $registrarCapabilities[REGISTRAR_SETDOMAINCONTACTDATA];
						//$capabilities[] = $registrarCapabilities[REGISTRAR_GETDOMAINRENEWTYPEOPTIONS];
						$capabilities["domainRenewType"] = $registrarCapabilities[REGISTRAR_SETDOMAINRENEWTYPEOPTIONS];
						//$capabilities[] = $registrarCapabilities[REGISTRAR_GETDOMAINREGISTRARLOCKOPTIONS];
						$capabilities["domainLock"] = $registrarCapabilities[REGISTRAR_SETDOMAINREGISTRARLOCKOPTIONS];
						//$capabilities[] = $registrarCapabilities[REGISTRAR_GETDOMAINACCESSPASSWORD];
						$capabilities["domainPassword"] = $registrarCapabilities[REGISTRAR_SETDOMAINACCESSPASSWORD];
						//$capabilities[] = $registrarCapabilities[REGISTRAR_GETDOMAINDNS];
						$capabilities["domainDNS"] = $registrarCapabilities[REGISTRAR_SETDOMAINDNS];
						$capabilities["domainLiveRenew"] = $registrarCapabilities[REGISTRAR_LIVERENEW];
						$capabilities["domainUploadDocuments"] = $registrarCapabilities[REGISTRAR_UPLOADDOCUMENTS];
					} catch (ProductException $e) {
						if ($e->getCode() != ProductException::E_MODULE_NOT_FOUND) {
							throw $e;
						}
					}
				}
			}

			if($this->addMBAPIErrors($data) === 0 && !$this->getNumErrors()) {
				if(((int) $data["mbapi"][0]["header"][0]["numResults"][0]) > 0) {

					//saveStateOfCONF();
					$registrarData = $CONF["domain"]["registrar"];

					$registrarID = (int) $data["mbapi"][0]["results"][0]["domains"][0]["domain"][0]["registrarID"][0];
					//Get the Regirstrars
					loadVendorConfigs(getCurrentSession()->getVendorId(), array("domain"));

					//Find the domain management settings for this registrar
					foreach($CONF["domain"]["registrar"] as $value) {
						if($value["configGroupID"] == $registrarID) {
							$registrarRules["moduleAllowChangeContactInfo"]		= (isset($value["moduleAllowChangeContactInfo"]) ? $value["moduleAllowChangeContactInfo"] : true);
							$registrarRules["moduleAllowChangeDNS"]				= (isset($value["moduleAllowChangeDNS"]) ? $value["moduleAllowChangeDNS"] : true);
							$registrarRules["moduleAllowChangeRegistrarLock"]	= (isset($value["moduleAllowChangeRegistrarLock"]) ? $value["moduleAllowChangeRegistrarLock"] : true);
							$registrarRules["moduleAllowChangeAutorenew"]		= (isset($value["moduleAllowChangeAutorenew"]) ? $value["moduleAllowChangeAutorenew"] : true);
							$registrarRules["moduleAllowChangeUpdateDomainPassword"] = (isset($value["moduleAllowChangeUpdateDomainPassword"]) ? $value["moduleAllowChangeUpdateDomainPassword"] : true);
							$registrarRules["moduleAllowUploadDocuments"]		= (isset($value["moduleAllowUploadDocuments"]) ? $value["moduleAllowUploadDocuments"] : true);
						}
					}
					$smartyVars = array();
					$smartyVars["capabilities"] = $capabilities;
					$smartyVars["rules"] = $registrarRules;
					$smartyVars["domain"] = collapseArrayElements($data["mbapi"][0]["results"][0]["domains"][0]["domain"][0]);

					$this->addTemplate("domains/ShowDomainDetails", $smartyVars);
				} else {
					$this->addError(TRANS_ACTIONERROR, "Domain does not exist.");
				}
			}
		}
		$this->addErrorTemplates();
		return $this->hasExecutedSuccessfully();
	}
} // ShowDomainDetails
?>
