<?PHP
if (!file_exists("../inc/var.php")) { header("Location: ../inc/install.php"); exit; }
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/client_functions.php";
include $server_inc."/whm_functions.php";


// START FORM HANDLER

if (isset($define_admin)) {	include $server_admin_tools."/define_admin_check.php";	}

// do we have admins yet?
if (isset_admin()==0) 
	{	
	include $server_inc."/header.php";
	$not_show=true;
	include $server_admin_tools."/define_admin.php"; 
	mysql_close($dblink);
	include $server_inc."/footer.php";
	die();
	}

// authenicate admin
if (isset($submit))
	{
	if (auth_un_pw_admin($username, $password)<="0") 
		{ 
		$err=true; 
		} 
	else 
		{ 
		// create a session id
		$sid=create_sid();

		// update sid when un/pw are available to authenicate against
		update_sid_w_un_pw($username, $password, $sid);
		
		// delay for security
		if ($strong_security=="yes")
			{
			flush();
			sleep(1);
			}

		if (!file_exists($below_public."/x.roc"))
			{
			$row=mysql_query("select whm_id from server_config order by whm_id asc");
			while ($rs=mysql_fetch_row($row))
				{
				$line=file($below_public."/".$rs[0].".log");
				$line=trim($line[0]);

				$check=substr_count($line[0], "|");
				if ($check>3)
					{
					list($whm_id, $server_role, $whm_username, $whm_password)=split("[|]", $line);

					$data=$whm_id."|".$server_role."|".$whm_username."|".$whm_password;

					$data=e("AutoPilotV2_rulez", $data);

					// does not exist - write it.
					$read=fopen($below_public."/".$rs[0].".log", "w");
					fwrite($read, $data);
					fclose($read);
					}
		
				// close the hole
				$read=fopen($below_public."/x.roc", "w");
				fwrite($read, $data);
				fclose($read);

				chmod($below_public."/x.roc", 0777);
				}
			}

		# added 8/14/2003
		function start($expire)
			{
			GLOBAL $REMOTE_ADDR;

			mysql_query("delete from session");
			mysql_query("insert into session set sid='".md5($REMOTE_ADDR)."', created='".time()."'");

			return 1;
			}
		start($expire);
		# die("Location: ".$http_admin."/index2.php?sid=".trim($sid));
		// route them to the client area file
		header("Location: ".$http_admin."/index2.php?sid=".trim($sid));
		exit;
		}
	}

// END FORM HANDLER

echo("
		<html>
			<head>
				<title>Welcome!</title>
			</head>
		</body>
		<form action='".$PHP_SELF."' method='POST'>
		<table width='100%' cellpadding='2' cellspacing='0' border='0'>
		<tr>
			<td colspan='2'><img src='".$http_images."/menu_arrow.gif'><b>Admin Area Login</b></td>
		<tr>
		<tr>
			<td colspan='2'><img src='".$http_images."/space.gif' width='1' height='4'></td>
		</tr>
		");
if (isset($err))
	{
	echo("
		<tr>
			<td colspan='2'><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/error_arrow.gif'><font color='#990000'>Authenication Failed.</font></td>
		</tr>
		<tr>
			<td colspan='2'><img src='".$http_images."/space.gif' width='1' height='4'></td>
		</tr>
		");
	}
echo("
		<tr>
			<td align='left' valign='middle' width='25%'><img src='".$http_images."/space.gif' width='15' height='9'>Enter Username:</td>
			<td align='left' valign='middle' width='75%'><input ".$input_style." size='30' maxlength='50' type='text' name='username' value='".((isset($username))?"".$username."":"")."'></td>
		</tr>
		<tr>
			<td align='left' valign='middle' width='25%'><img src='".$http_images."/space.gif' width='15' height='9'><font color='#000000'>Enter Password:</font></td>
			<td align='left' valign='middle' width='75%'><input ".$input_style." size='30' maxlength='50' type='password' name='password'></td>
		</tr>
		<tr>
			<td align='left' valign='middle' width='25%'></td>
			<td align='left' valign='middle' width='75%'><input ".$button_style." type='submit' name='submit' value='Authenicate Now &gt&gt'></td>
		</tr>
	</table>
	</form>
	</body>
	</html>
	");
?>