<?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 ($role_id==1||$role_id==2)
		{
		// prep for a change of the current active server
		$row2=mysql_query("select whm_id, ogcreate from server_config");
		while ($rs2=mysql_fetch_row($row2))
			{
			mysql_query("update server_config set active_server='0', manual_install_active='0', ogcreate='".addslashes(trim($rs2[1]))."' where whm_id='".addslashes(trim($rs2[0]))."'");
			}
		}
	/*
	0 = Installed Server
	1 = Manual Default
	2 = Active Server
	3 = DELETE Server
	*/

	// execute the update
	if ($role_id==0)
		{
		$active_count=mysql_fetch_row(mysql_query("select count(*) from server_config where active_server='1'"));
		$manual_count=mysql_fetch_row(mysql_query("select count(*) from server_config where manual_install_active='1'"));
		$total_count=mysql_fetch_row(mysql_query("select count(*) from server_config"));
		if ($active_count[0]<=0&&$manual_count[0]<=0)
			{
			$err_role=true;
			}
		else
			{
			if ($total_count[0]<=1)
				{
				$err_role=true;
				}
			else
				{
				$err_role_not=true;
				// mysql_query("update server_config set active_server='0', manual_install_active='0', ogcreate='".addslashes(trim($ogcreate))."' where whm_id='".addslashes(trim($whm_id))."'");
				}
			}
		}
	else if ($role_id==1)
		{
		mysql_query("update server_config set active_server='0', manual_install_active='1', ogcreate='".addslashes(trim($ogcreate))."' where whm_id='".addslashes(trim($whm_id))."'");
		}
	else if ($role_id==2)
		{
		mysql_query("update server_config set active_server='1', manual_install_active='0', ogcreate='".addslashes(trim($ogcreate))."' where whm_id='".addslashes(trim($whm_id))."'");
		}
	else if ($role_id==3)
		{
		$account_count=mysql_fetch_row(mysql_query("select count(*) from hosting_order where whm_id='".addslashes(trim($whm_id))."'"));
		if ($account_count[0]>=1)
			{
			$err_del=true;
			}
		else
			{
			// delete the server record
			delete_server($whm_id, $below_public);

			// delete the sql record
			mysql_query("delete from server_config where whm_id='".addslashes(trim($whm_id))."'");
			}
		}
	}

if (isset($delete))
	{
	mysql_query("delete from plan_specs where pid='".addslashes(trim($delete))."'");
	}

// End Form Handler
$ct=mysql_fetch_row(mysql_query("select count(*) from server_config"));
if ($ct[0]<=0)
	{
	echo("
	<table width='100%' cellpadding='2' cellspacing='0' border='0'>
		<tr>
			<td>You have no servers configured. Please <a href='".$http_admin."/server_config.php?sid=".trim($sid)."'>click here</a> to add a new server.</td>
		</tr>
	</table>
		");
	// End Main Process
	mysql_close($dblink);
	include $server_inc."/footer.php";
	die();
	}
$ct=mysql_fetch_row(mysql_query("select count(*) from server_config where active_server='1'"));
$ctx=mysql_fetch_row(mysql_query("select count(*) from server_config"));

$nav="";
if (isset($err_del))
	{
	echo("
		<table width='725' border='0' cellspacing='1' cellpadding='4'>
			<tr>
				<td align='left' valign='top'><img src='".$http_images."/error_arrow.gif'>This server has active accounts installed. Delete the accounts, then delete the server.</td>
			</tr>
		</table>
		");
	}
if (isset($err_role))
	{
	echo("
		<table width='725' border='0' cellspacing='1' cellpadding='4'>
			<tr>
				<td align='left' valign='top'><img src='".$http_images."/error_arrow.gif'>You must define at least 1 server as the <b>active</b> or <b>manual default</b> server.</td>
			</tr>
		</table>
		");
	}
if (isset($err_role_not))
	{
	echo("
		<table width='725' border='0' cellspacing='1' cellpadding='4'>
			<tr>
				<td align='left' valign='top'><img src='".$http_images."/error_arrow.gif'>You must promote an installed server before this server can be demoted to <b>installed</b>.</td>
			</tr>
		</table>
		");
	}
/*
if ($ct[0]<=0)
	{
	echo("
		<table width='725' border='0' cellspacing='1' cellpadding='4'>
			<tr>
				<td width='50%' align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'>Automatic Account Creation Mode is <b>Disabled</b>.</td>
				<td width='50%' align='right' valign='top'><a href='".$http_admin."/server_config.php?sid=".trim($sid)."'><b>Add a new server &gt&gt</b></a></td>
			</tr>
		</table>
		");
	}
else
	{
	echo("
		<table width='725' border='0' cellspacing='1' cellpadding='4'>
			<tr>
				<td width='50%' align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'>Automatic Account Creation Mode is <b>Enabled</b>.</td>
				<td width='50%' align='right' valign='top'><a href='".$http_admin."/server_config.php?sid=".trim($sid)."'><b>Add a new server &gt&gt</b></a></td>
			</tr>
		</table>
		");
	}
*/
echo("
<!--- Start Main --->
<table width='725' border='0' cellspacing='1' cellpadding='6'>
		<tr>
			<td align='center' valign='top' bgcolor='#E5E7E9'><b>This page may take some time to load depending on how many servers are currently installed</b>
			</td>
		</tr>
</table><BR>
<table width='725' border='0' cellspacing='1' cellpadding='6'>
		<tr>
			<td align='center' valign='top' bgcolor='#E5E7E9'><b>#</b></td>
			<td align='left' valign='top' bgcolor='#E5E7E9'><b>Server Name</b></td>
			<td align='left' valign='top' bgcolor='#E5E7E9'><b>Server Accounts</b></td>
			<td align='center' valign='top' bgcolor='#E5E7E9'><b>Server Role</b></td>
		</tr>
	");

$x=1;
$y=1;
# $row=mysql_query("select server_name, max_accounts, active_server, manual_install_active, UNIX_TIMESTAMP(ogcreate), whm_id, ogcreate, server_ip from server_config order by ogcreate asc");
$row=mysql_query("select server_name, max_accounts, active_server, manual_install_active, ogcreate, whm_id, ogcreate, server_ip, server_cost, server_noclocal from server_config order by ogcreate asc");
while ($rs=mysql_fetch_row($row))
	{

	// get total accounts

	// read in un pw
	$temp=@read_log($rs[5], $below_public);
	list($whm_username, $whm_password)=split("[|]", $temp);
	$total_accounts=@list_accounts($rs[7], $whm_username, $whm_password, 1);
	$total_cost_per_month=$currency.sprintf("%01.2f", $rs[8])." ".$currency_type;
	$rowrow=mysql_fetch_row(mysql_query("select count(*) from hosting_order where whm_id='".addslashes(trim($rs[5]))."' and status='1'"));
	$mycount1=$rowrow[0];

	// end get total accounts

	if ($y==1) {$bgcolor="#F2F4F6"; $y=0;} else {$bgcolor="#ECEEF0"; $y=1;}
	echo("
	<form action='".$PHP_SELF."' method='POST'>
	<input type='hidden' name='whm_id' value='".$rs[5]."'>
	<input type='hidden' name='ogcreate' value='".$rs[6]."'>
	<input type='hidden' name='sid' value='".trim($sid)."'>
		<tr>
			<td align='center' valign='top' bgcolor='".$bgcolor."'>".$x.").</td>
			<td align='left' valign='top' bgcolor='".$bgcolor."'>
			<table width='100%' cellpadding='0' cellspacing='0' border='0'>
				<tr>
					<td width='10%' align='left' valign='top'><a href='".$http_admin."/server_config.php?whm_id=".trim($rs[5])."&sid=".trim($sid)."'><img border='0' src='".$http_images."/icon_view.gif'></a></td>
					<td width='90%' align='left' valign='middle'><B><a href='".$http_admin."/server_config.php?whm_id=".trim($rs[5])."&sid=".trim($sid)."'>".stripslashes($rs[0])."</a></b><BR>Location: <b>".$rs[9]."</b><BR><img src='".$http_images."/drop_arrow.gif'>Cost per Month: <b>".$total_cost_per_month."</b></td>
				</tr>
			</table>
			</td>
			<td align='left' valign='top' bgcolor='".$bgcolor."'>
			<table width='100%' cellpadding='0' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='top'>".$total_accounts." Installed</td>
					<td align='left' valign='top'>".$rs[1]." Max.</td>
					<td colspan='2' align='left' valign='top'>".@sprintf("%01.0f",(($total_accounts/$rs[1])*100))."% Full</td>
				</tr>
			</table>
			</td>
			<td align='center' valign='top' bgcolor='".$bgcolor."'>
		");
	if (trim($rs[2])=="0")
		{
		// not an active server
		if (trim($rs[3])=="0")
			{
			// not the ns default
			$role=0;
			}
		else
			{
			// the ns default
			$role=1;
			}
		}
	else
		{
		// active server
		$role=2;
		}

	echo("
		<input type='hidden' name='role' value='".$role."'>
		<select name='role_id' ".$input_style.">
			<option value='0'".(($role==0)?" selected":"").">Installed Server</option>
			<option value='2'".(($role==2)?" selected":"").">Active Server</option>
			<option value='1'".(($role==1)?" selected":"").">Manual Default</option>
			<option value='3'".(($role==3)?" selected":"").">DELETE Server</option>
		</select> <input ".$button_style." type='submit' name='submit' value='Update'><BR><BR>Total Active in AutoPilot: <b>".$mycount1."</b>
			</td>
		</tr>
		</form>
		");
	$x++;
	}
echo("
	<tr>
		<td colspan='4'><img src='".$http_images."/space.gif' width='1' height='8'></td>
	</tr>
	<tr>
		<td colspan='4'><img src='".$http_images."/space.gif' width='1' height='4'>Your total active accounts in AutoPilot is represented by total hosting orders active ( not pending, suspended or cancelled ) in your database that are assigned to that particular server.  If this number is lower than the installed count, that states there are more installed on the server than you have assigned to that server in AutoPilot.<BR><BR><b>Please note: manual default will not display any nameservers at step_two.php - only active servers will provide that information to the client.</b></td>
	</tr>
</table>
<!--- End Main --->
	");
// End Main Process
mysql_close($dblink);
include $server_admin_inc."/footer.php";
?>