<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/whm_functions.php";
include $server_inc."/client_functions.php";
include $server_inc."/login_functions.php";

include $server_admin_inc."/header.php";

// Start Form Handler

if (isset($submit))
	{
	if (strlen(trim($server_name))==0) { $err0=true; } else { $n0=true; }
	if (strlen(trim($server_ip))==0) { $err1=true; } else { $n1=true; }
	if (strlen(trim($primary_ns))==0) { $err2=true; } else { $n2=true; }
	if (strlen(trim($primary_ns_ip))==0) { $err3=true; } else { $n3=true; }
	if (strlen(trim($secondary_ns))==0) { $err4=true; } else { $n4=true; }
	if (strlen(trim($secondary_ns_ip))==0) { $err5=true; } else { $n5=true; }
	if (strlen(trim($whm_username))==0) { $err6=true; } else { $n6=true; }
	if (strlen(trim($whm_password))==0) { $err7=true; } else { $n7=true; }
	if (strlen(trim($max_accounts))==0) { $err8=true; } else { $n8=true; }
	if (isset($n0)&&isset($n1)&&isset($n2)&&isset($n3)&&isset($n4)&&isset($n5)&&isset($n6)&&isset($n7)&&isset($n8))
		{
		// check to see if this is an edit or new record
		if (isset($whm_id))
			{
			// build the query
			$update="update server_config set ";
			$update.="server_name='".addslashes(trim($server_name))."', ";
			$update.="server_ip='".addslashes(trim($server_ip))."', ";
			$update.="primary_ns='".addslashes(trim($primary_ns))."', ";
			$update.="primary_ns_ip='".addslashes(trim($primary_ns_ip))."', ";
			$update.="secondary_ns='".addslashes(trim($secondary_ns))."', ";
			$update.="secondary_ns_ip='".addslashes(trim($secondary_ns_ip))."', ";
			$update.="max_accounts='".addslashes(trim($max_accounts))."',";

			// set reseller status to no if not set
			if (!isset($reseller)) { $reseller=0; }
			$update.="reseller='".addslashes(trim($reseller))."'";

			$update.="where whm_id='".addslashes(trim($whm_id))."'";

			// execute the query
			mysql_query($update);
			}
		else
			{
			// prep for a change of the current active server
			// this will insure we have only 1 active server
			if (isset($active_server))
				{
				mysql_query("update server_config set active_server='0', manual_install_active='0'");
				}

			// build the query
			$insert="insert into server_config set ";
			$insert.="server_name='".addslashes(trim($server_name))."', ";
			$insert.="server_ip='".addslashes(trim($server_ip))."', ";
			$insert.="primary_ns='".addslashes(trim($primary_ns))."', ";
			$insert.="primary_ns_ip='".addslashes(trim($primary_ns_ip))."', ";
			$insert.="secondary_ns='".addslashes(trim($secondary_ns))."', ";
			$insert.="secondary_ns_ip='".addslashes(trim($secondary_ns_ip))."', ";
			$insert.="max_accounts='".addslashes(trim($max_accounts))."',";

			// set reseller status to no if not set
			if (!isset($reseller)) { $reseller=0; }
			$insert.="reseller='".addslashes(trim($reseller))."'";

			// execute the query
			mysql_query($insert);
			
			send_newly_added_server_notification($email_admin, $server_name, $server_ip, $primary_ns, $primary_ns_ip, $secondary_ns, $secondary_ns_ip, $max_accounts);

			// get the latest ID
			$whm_id=mysql_insert_id();
			}

		// write whm ID to file
		define_servers($whm_id, $whm_username, $whm_password, $below_public);
		}
	}

if (isset($whm_id))
	{
	// build the query
	$equery="select ";
	$equery.="server_name, ";          // 0
	$equery.="server_ip, ";            // 1
	$equery.="primary_ns, ";	       // 2
	$equery.="primary_ns_ip, ";        // 3
	$equery.="secondary_ns, ";         // 4
	$equery.="secondary_ns_ip, ";      // 5
	$equery.="max_accounts, ";         // 6
	$equery.="active_server, ";        // 7 
	$equery.="manual_install_active, "; // 8 
	$equery.="reseller "; // 9
	$equery.="from server_config where whm_id='".addslashes(trim($whm_id))."'";

	// execute the query
	$rs0=mysql_fetch_row(mysql_query($equery));

	// define variables
	$server_name=$rs0[0];
	$server_ip=$rs0[1];
	$primary_ns=$rs0[2];
	$primary_ns_ip=$rs0[3];
	$secondary_ns=$rs0[4];
	$secondary_ns_ip=$rs0[5];
	$max_accounts=$rs0[6];
	$active_server=$rs0[7];
	$manual_install_active=$rs0[8];
	$reseller=$rs0[9];

	// read in un pw
	$temp=read_log($whm_id, $below_public);
	list($whm_username, $whm_password)=split("[|]", $temp);

	}

// End Form Handler

echo("
	<form action='".$PHP_SELF."' method='POST'>
	".((isset($whm_id))?"<input type='hidden' name='whm_id' value='".trim($whm_id)."'>":"")."
	<input type='hidden' name='sid' value='".trim($sid)."'>
	<table width='725' cellpadding='2' cellspacing='0' border='0'>
		<tr>
			<td colspan='2'><img src='".$http_images."/menu_arrow.gif'><b>Manage Servers and Configuration</b></td>
		</tr>
		<tr>
			<td colspan='2'><img src='".$http_images."/space.gif' width='1' height='6'></td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err0))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Server Name:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='server_name' value='".((isset($server_name))?"".trim($server_name)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err1))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Server IP Address:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='server_ip' value='".((isset($server_ip))?"".trim($server_ip)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td colspan='2'><img src='".$http_images."/space.gif' width='1' height='2'></td>
		</tr>
		<tr>
			<td align='left' valign='top'><img src='".$http_images."/".((isset($err0))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Server Role:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'>".(($active_server==1)?"<b>Active Server</b><br><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/drop_arrow.gif'>New accounts will auto install on this server.":"".(($manual_install_active==1)?"<b>Manual Install</b><br><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/drop_arrow.gif'>Default MIB nameservers listed below:":"<b>Installed Server</b>")."")."</td>
		</tr>
		<tr>
			<td colspan='2'><img src='".$http_images."/space.gif' width='1' height='2'></td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err2))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Primary Nameserver:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='primary_ns' value='".((isset($primary_ns))?"".trim($primary_ns)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err3))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Primary Nameserver IP:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='primary_ns_ip' value='".((isset($primary_ns_ip))?"".trim($primary_ns_ip)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err4))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Secondary Nameserver:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='secondary_ns' value='".((isset($secondary_ns))?"".trim($secondary_ns)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err5))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Secondary Nameserver IP:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='secondary_ns_ip' value='".((isset($secondary_ns_ip))?"".trim($secondary_ns_ip)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err8))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Max. Accounts per Server:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='max_accounts' value='".((isset($max_accounts))?"".trim($max_accounts)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err8))?"error_arrow.gif":"space.gif")."' width='15' height='9'>Are you a reseller?</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input type='checkbox' name='reseller' value='1'".((isset($reseller))?"".((trim($reseller)==1)?" checked":"")."":"")."> Click if your WHM access is reseller level.</td>
		</tr>
		<tr>
			<td><img src='".$http_images."/".((isset($err6))?"error_arrow.gif":"space.gif")."' width='15' height='9'>WHM Root Username:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$input_style." type='text' size='39' maxlength='255' name='whm_username' value='".((isset($whm_username))?"".trim($whm_username)."":"")."'> [Required]</td>
		</tr>
		<tr>
			<td align='left' valign='top'><img src='".$http_images."/".((isset($err7))?"error_arrow.gif":"space.gif")."' width='15' height='9'>WHM Remote Access Key:</td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><font color='#990000'>------BEGIN WHM ACCESS KEY------</font><br><img src='".$http_images."/space.gif' width='15' height='9'><textarea ".$input_style." rows='29' cols='38' name='whm_password'>".((isset($whm_password))?"".trim($whm_password)."":"")."</textarea> [Required]<br><img src='".$http_images."/space.gif' width='15' height='9'><font color='#990000'>-------END WHM ACCESS KEY-------</font></td>
		</tr>
		<tr>
			<td><img src='".$http_images."/space.gif' width='210' height='12'></td>
			<td><img src='".$http_images."/space.gif' width='490' height='12'></td>
		</tr>
		<tr>
			<td><img src='".$http_images."/space.gif' width='15' height='9'></td>
			<td><img src='".$http_images."/space.gif' width='15' height='9'><input ".$button_style." type='submit' name='submit' value='".((isset($whm_id))?"Edit Server Config &gt&gt":"Add This Server &gt&gt")."'></td>
		</tr>
	<tr>
		<td colspan='2'><img src='".$http_images."/space.gif' width='1' height='8'></td>
	</tr>
	</table>
	</form>
	");
// End Main Process
mysql_close($dblink);
include $server_admin_inc."/footer.php";
?>