<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2010-2014 Softnik Technologies
// Initial Version: RG
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////

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

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

# Ajax script. 
# Check authentication and quit with http error code if auth fails.

session_start();
check_authentication_error_quit();
doSessionSwipeIn();

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

# only admins can set aliases

if(!is_admin_user())
{
	header("HTTP/1.0 403 Forbidden");
	echo "Permission Denied.";
	exit;
}

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

function get_registrar_for_domain($sid)
{
	global $db_config_domain_table;
	$registrar = "";
	if($sid > 0)
	{
		$sql = "select registrar from " . $db_config_domain_table . " where sid = " . $sid;
		$idresults = mysql_query($sql) or die ("Error reading from domain database");  
		$row = mysql_fetch_array($idresults, MYSQL_ASSOC);
		if(count($row) == 1 && isset($row['registrar']))
			$registrar = $row['registrar'];
	}
	return $registrar;
}

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

connect_to_mysql_and_select_db();

if(isset($_REQUEST['id']) && isset($_REQUEST['alias']))
{
	$sid = validate_request_data($_REQUEST["id"]);
	$alias = validate_request_data($_REQUEST["alias"]);
	$registrar = get_registrar_for_domain($sid);
	
	$status = "FAIL";
	global $db_config_reg_alias_table;
	if($registrar != "")
	{
		$sregistrar = mysql_real_escape_string($registrar);
		$sql = "select COUNT(*) AS count from " . $db_config_reg_alias_table . " where name = '$sregistrar'";
		$results = mysql_query($sql) or die ("Error reading from alias database. " . mysql_error());  
		$existing = mysql_fetch_array($results);  
		if ($existing['count'] == 0) 
		{
			$sqlAdd  = "insert into $db_config_reg_alias_table (name, alias) values ('$sregistrar','$alias');";
			$result = mysql_query($sqlAdd) or die("Couldn't execute your query.".mysql_error() . "<br>" . $sqlAdd);
		}
		else
		{
			$sqlAdd = "update $db_config_reg_alias_table set alias='$alias' where name='$sregistrar'";
			$result = mysql_query( $sqlAdd ) or die("Couldn't execute query.".mysql_error());
		}
		$status = "OK";
	}
	$data_array = array();
	$data_array['status'] = $status;
	$data_array['registrar'] = $registrar;
	echo json_encode($data_array);
}
else if(isset($_REQUEST['id']))
{
	$sid = validate_request_data($_REQUEST["id"]);
		
	global $db_config_reg_alias_table ;
	$registrar = get_registrar_for_domain($sid);
	$alias = "";
	
	if($registrar != "")
	{
		$sregistrar = mysql_real_escape_string($registrar);
		$sql = "select alias from " . $db_config_reg_alias_table . " where name = '$sregistrar'";
		$idresults = mysql_query($sql) or die ("Error reading from alias database. " . mysql_error());  
		$row = mysql_fetch_array($idresults, MYSQL_ASSOC);
		if(count($row) == 1 && isset($row['alias']))
			$alias = $row['alias'];
	}
	
	$data_array = array();
	$data_array['name'] = $registrar;
	$data_array['alias'] = $alias;
	$data_array['id'] = $sid;
	echo json_encode($data_array);
}

?>
