<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2010-2013 Softnik Technologies
// Feb-18-2013
// Author: SK
/////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////
require_once("../../config.php");
require_once("base.php");
/////////////////////////////////////////////////////////////////////////////////////////////////////////

$wserver_table_text_fields 	= array (	'query', 'port', 'searchtoken', 
											'dateformat', 'maxconnections', 'conninterval', 
											'cleantoken_1', 'cleantoken_2', 'xlate', 'proxy', 'bannedtext', 'stop_lookups'
										);
										
$wserver_table_datetime_fields = array (	'last_connect_at'
										);
										
/////////////////////////////////////////////////////////////////////////////////////////////////////////

function update_whois_server_table($darray, $server)
{
	global $db_config_whoisservers_table, $wserver_table_text_fields, $wserver_table_datetime_fields, $wserver_table_date_fields;
	
	$sqlCount = "SELECT COUNT(*) AS count FROM $db_config_whoisservers_table WHERE server='$server'";  
	$results = mysql_query($sqlCount) or die("Couldn't execute query.".mysql_error());
	$existing = mysql_fetch_array($results);  
	if ($existing['count'] == 0) 
	{
		$sqlAdd  = "insert into $db_config_whoisservers_table (server) values ('$server');";
		$result = mysql_query($sqlAdd) or die("Couldn't execute your query.".mysql_error() . "<br>" . $sqlAdd);
	}
	
	$sql = 'update ' . $db_config_whoisservers_table . ' set ';
		
	$updateArray = array();
	
	foreach($darray as $key=>$uvalue) 
	{
		$value = trim(validate_request_data($uvalue));
		$value = trim($value,chr(0xC2).chr(0xA0)); 
		
		if($key == 'id' || $key == 'server')
			continue;
		else if(in_array($key, $wserver_table_text_fields))
		{
			if($key == "stop_lookups" && ($value == "yes" || $value == "true" || $value=="1"))
				$value = 1;
			else if($key == "stop_lookups")
				$value = 0;
				
			$updateArray[$key] = $key . '=' . "'" . $value . "'"; 
		}
		else if(in_array($key, $wserver_table_datetime_fields))
		{
			if(strlen($value) < 12)
				$updateArray[$key] = $key . "=NULL"; 
			else if(is_date($value) === true)
			{
				$dateval = strtotime($value);
				$updateArray[$key] = $key . '=' . "'" . date("Y-m-d H:i:s", $dateval) . "'"; 
			}
		}
	}
	$sql .= implode(',',$updateArray);
	$sql .= " where server = '" . $server . "'";
	
	//echo $sql . "<br>";
	
	if(count($updateArray))
	{
		$result = mysql_query( $sql );
		if(!$result)
		{
			//$msg = "Couldn't execute your query. " . mysql_error() . "\n";
			//logToFile("php.log", $msg);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

if(isset($_REQUEST['tld']) || isset($_REQUEST['server']))
{
	connect_to_mysql_and_select_db();
	
	$server = "";
	if(isset($_REQUEST['server']))
	{
		$server = validate_request_data($_REQUEST["server"]);
		$server = trim($server);
		if(strstr($server, " ") !== false)
			$server = "";
		if(isset($_REQUEST['set']) && $server != "")
			update_whois_server_table($_REQUEST, $server);
	}
	else if(isset($_REQUEST['tld']))
	{
		$tld = validate_request_data($_REQUEST["tld"]);
		$tld = trim($tld);
		if(strstr($tld, " ") !== false)
			$tld = "";
		$server = get_whois_server_from_tld($tld);
	}
	
	if($server !== "")
	{
		$data_array = array();
		$data_array['server'] = $server;
		$data_array['query'] = get_raw_whois_query_from_whoisserver($server);
		$data_array['port'] = get_whois_port_from_whoisserver($server);
		$data_array['searchtoken'] = get_searchtoken_from_whoisserver($server);
		$data_array['xlate'] = get_raw_translate_data_from_whoisserver($server);
		$data_array['dateformat'] = get_dateformat_from_whoisserver($server);
		$data_array['maxconnections'] = get_max_connections_from_whoisserver($server);
		$data_array['conninterval'] = get_connection_interval_from_whoisserver($server);
		$data_array['bannedtext'] = get_bannedtext_from_whoisserver($server);
		$data_array['proxy'] = get_proxy_from_whoisserver($server);
		$data_array['stop_lookups'] = get_lustatus_from_whoisserver($server);
		$data_array['last_connect_at'] = get_last_connect_time_from_external_server($server);
		echo json_encode($data_array);
	}
}
else if(isset($_REQUEST['resetserver']))
{
	connect_to_mysql_and_select_db();
	
	$server = "";
	if(isset($_REQUEST['resetserver']))
	{
		$server = validate_request_data($_REQUEST["resetserver"]);
	}
	if($server !== "")
	{
		$data_array = array();
		$data_array['resetserver'] = $server;
		reset_last_connect_time_for_external_server($server);
		$data_array['last_connect_at'] = get_last_connect_time_from_external_server($server);
		echo json_encode($data_array);
	}
}


?>
