<?php
if (!defined("WHMCS")) {
	exit("This file cannot be accessed directly");
}
$_SESSION["adminid"] = "1";

require ROOTDIR . "/includes/clientfunctions.php";

function getTLDPriceList($tld, $display = "", $renewpricing = "", $userid = "") {

	if ($renewpricing == "renew") {
		$renewpricing = true;
	}
	
	$result_currency = select_query("tblcurrencies","id",  array("default" => 1));
	$data_currency = mysql_fetch_array($result_currency);
	
	$currency_id = $data_currency['id'];
	
	$result = select_query("tbldomainpricing", "id", array("extension" => $tld));
	$data = mysql_fetch_array($result);
	$id = $data['id'];
	if (!$userid && isset($_SESSION['uid'])) {
		$userid = $_SESSION['uid'];
	}
	$clientgroupid = ($userid ? get_query_val("tblclients", "groupid", array("id" => $userid)) : "0");
	$checkfields = array("msetupfee", "qsetupfee", "ssetupfee", "asetupfee", "bsetupfee", "monthly", "quarterly", "semiannually", "annually", "biennially");
	if (!$renewpricing || $renewpricing === "transfer") {
		$data = get_query_vals("tblpricing", "", array("type" => "domainregister", "currency" => $currency_id, "relid" => $id, "tsetupfee" => $clientgroupid));
		if (!$data) {
			$data = get_query_vals("tblpricing", "", array("type" => "domainregister", "currency" => $currency_id, "relid" => $id, "tsetupfee" => "0"));
		}
		foreach ($checkfields as $k => $v) {
			$register[$k + 1] = $data[$v];
		}
		$data = get_query_vals("tblpricing", "", array("type" => "domaintransfer", "currency" => $currency_id, "relid" => $id, "tsetupfee" => $clientgroupid));
		if (!$data) {
			$data = get_query_vals("tblpricing", "", array("type" => "domaintransfer", "currency" => $currency_id, "relid" => $id, "tsetupfee" => "0"));
		}
		foreach ($checkfields as $k => $v) {
			$transfer[$k + 1] = $data[$v];
		}
	}
	if (!$renewpricing || $renewpricing !== "transfer") {
		$data = get_query_vals("tblpricing", "", array("type" => "domainrenew", "currency" => $currency_id, "relid" => $id, "tsetupfee" => $clientgroupid));
		if (!$data) {
			$data = get_query_vals("tblpricing", "", array("type" => "domainrenew", "currency" => $currency_id, "relid" => $id, "tsetupfee" => "0"));
		}
		

		foreach ($checkfields as $k => $v) {
			$renew[$k + 1] = $data[$v];
		}
	}
	$tldpricing = array();
	$years = 1;
	while ($years <= 10) {
		if ($renewpricing === "transfer") {
			if (0 < $register[$years] && 0 <= $transfer[$years]) {
				if ($display) {
					$transfer[$years] = formatCurrency($transfer[$years]);
				}
				$tldpricing[$years]['transfer'] = $transfer[$years];
			}
		}
		else {
			if ($renewpricing) {
				if (0 < $renew[$years]) {
					if ($display) {
						$renew[$years] = formatCurrency($renew[$years]);
					}
					$tldpricing[$years]['renew'] = $renew[$years];
				}
			}
			else {
				if (0 < $register[$years]) {
					if ($display) {
						$register[$years] = formatCurrency($register[$years]);
					}
					$tldpricing[$years]['register'] = $register[$years];
					if (0 <= $transfer[$years]) {
						if ($display) {
							$transfer[$years] = formatCurrency($transfer[$years]);
						}
						$tldpricing[$years]['transfer'] = $transfer[$years];
					}
					if (0 < $renew[$years]) {
						if ($display) {
							$renew[$years] = formatCurrency($renew[$years]);
						}
						$tldpricing[$years]['renew'] = $renew[$years];
					}
				}
			}
		}
		$years += 1;
	}
	return $renew;
}

$DomainRenewalGracePeriods = $DomainRenewalMinimums = array();

$DomainRenewalGracePeriods = array_merge(array(".com" => "30", ".net" => "30", ".org" => "30", ".info" => "15", ".biz" => "30", ".mobi" => "30", ".name" => "30", ".asia" => "30", ".tel" => "30", ".in" => "15", ".mn" => "30", ".bz" => "30", ".cc" => "30", ".tv" => "30", ".eu" => "0", ".co.uk" => "97", ".org.uk" => "97", ".me.uk" => "97", ".us" => "30", ".ws" => "0", ".me" => "30", ".cn" => "30", ".nz" => "0", ".ca" => "30"), $DomainRenewalGracePeriods);
$DomainRenewalMinimums = array_merge(array(".co.uk" => "180", ".org.uk" => "180", ".me.uk" => "180", ".com.au" => "90", ".net.au" => "90", ".org.au" => "90"), $DomainRenewalMinimums);
$DomainRenewalPriceOptions = array();
$renewals = array();
$result = select_query("tbldomains", "", "userid='" . $uid . "' AND (status='Active' OR status='Expired')", "expirydate", "ASC");

while ($data = mysql_fetch_array($result)) {
	$id = $data['id'];

	$domain = $data['domain'];
	$expirydate = $data['expirydate'];
	$status = $data['status'];
	if ($expirydate == "0000-00-00") {
		$expirydate = $data['nextduedate'];
	}
	$todaysdatetime = strtotime(date("Ymd"));
	$expirydatetime = strtotime($expirydate);
	$daysuntilexpiry = round(($expirydatetime - $todaysdatetime) / 86400);
	$domainparts = explode(".", $domain, 2);
	$tld = "." . $domainparts[1];
	$beforerenewlimit = $ingraceperiod = $pastgraceperiod = false;
	if (array_key_exists($tld, $DomainRenewalMinimums)) {
		if ($DomainRenewalMinimums[$tld] < $daysuntilexpiry) {
			$beforerenewlimit = true;
		}
	}
	if (array_key_exists($tld, $DomainRenewalGracePeriods)) {
		if ($DomainRenewalGracePeriods[$tld] < $daysuntilexpiry * (0 - 1)) {
			$pastgraceperiod = true;
		}
	}
	else {
		if ($daysuntilexpiry < 0) {
			$pastgraceperiod = true;
		}
	}
	if (!$pastgraceperiod && $daysuntilexpiry < 0) {
		$ingraceperiod = true;
	}
	if (!array_key_exists($tld, $DomainRenewalPriceOptions)) {
		
		$temppricelist = getTLDPriceList($tld, true, true, $uid);
		
		$apiresults['data']= $temppricelist;
		
		$renewaloptions = array();
		foreach ($temppricelist as $regperiod => $options) {

			if ($options['renew']) {
				$renewaloptions[] = array("period" => $regperiod, "price" => $options);
				continue;
			}
		}
		$DomainRenewalPriceOptions[$tld] = $renewaloptions;
	}
	else {
		$renewaloptions[] = $DomainRenewalPriceOptions[$tld];
	}
	$rawstatus = WHMCS_ClientArea::getrawstatus($status);
	
	if (count($renewaloptions)) {
		$renewals[] = array("id" => $id, "domain" => $domain, "tld" => $tld, "status" => $_LANG["clientarea" . $rawstatus], "expirydate" => fromMySQLDate($expirydate), "daysuntilexpiry" => $daysuntilexpiry, "beforerenewlimit" => $beforerenewlimit, "beforerenewlimitdays" => $DomainRenewalMinimums[$tld], "ingraceperiod" => $ingraceperiod, "pastgraceperiod" => $pastgraceperiod, "graceperioddays" => $DomainRenewalGracePeriods[$tld], "renewaloptions" => $DomainRenewalPriceOptions[$tld]);
	}
}

		
$apiresults['domains']= $renewals;			
	?>