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

// get the server IP
$rsip=mysql_fetch_row(mysql_query("select server_ip, server_name from server_config where whm_id='".addslashes(trim($whm_id))."'"));
$server_ip=trim($rsip[0]);
$server_name=trim($rsip[1]);

if (isset($submit))
	{
	if (strlen(trim($first_name))==0) {$err0=true;} else {$n0=true;}
	if (strlen(trim($last_name))==0) {$err1=true;} else {$n1=true;}
	if (strlen(trim($street_address_1))==0) {$err2=true;} else {$n2=true;}
	if (strlen(trim($city))==0) {$err3=true;} else {$n3=true;}
	if (strlen(trim($state))==0) {$err4=true;} else {$n4=true;}
	if (strlen(trim($zip_code))==0) {$err5=true;} else {$n5=true;}
	if (strlen(trim($country))==0) {$err6=true;} else {$n6=true;}
	if (strlen(trim($phone))==0) {$err7=true;} else {$n7=true;}

	// decide to validate or not
	if (isset($uid)&&strlen(trim($password))==0&&strlen(trim($password2))==0)
		{
		// set bit to pass validation
		$n10=true;
		}
	else
		{
		if (strcmp($password, $password2)!=0||strlen(trim($password))==0) {$err10=true;} else {$n10=true;}
		}

	if (!isset($uid))
		{
		if (strlen(trim($email))==0) {$err8=true;} else {$n8=true;}
		if (strlen(trim($username))==0) {$err9=true;} else {$n9=true;}
		if (check_user($username)==1) {$err14=true;} else {$n14=true;}
		if (check_email($email)==1) {$err15=true;} else {$n15=true;}
		}
	else
		{
		$n8=true;
		$n9=true;
		$n14=true;
		$n15=true;
		}

	if (isset($n0)&&isset($n1)&&isset($n2)&&isset($n3)&&isset($n4)&&isset($n5)&&isset($n6)&&isset($n7)&&isset($n8)&&isset($n9)&&isset($n10)&&isset($n14)&&isset($n15))
		{
		// insert user
		$insert="insert into user set ";
		$insert.="first_name='".addslashes(trim($first_name))."', ";
		$insert.="last_name='".addslashes(trim($last_name))."', ";
		if (strlen(trim($organization_name))!=0) { $insert.="organization_name='".addslashes(trim($organization_name))."', "; }
		$insert.="street_address_1='".addslashes(trim($street_address_1))."', ";
		if (strlen(trim($street_address_2))!=0) { $insert.="street_address_2='".addslashes(trim($street_address_2))."', "; }
		$insert.="city='".addslashes(trim($city))."', ";
		$insert.="state='".addslashes(trim($state))."', ";
		$insert.="zip_code='".addslashes(trim($zip_code))."', ";
		$insert.="country='".addslashes(trim($country))."', ";
		$insert.="phone='".addslashes(trim($phone))."', ";
		$insert.="fax='".addslashes(trim($fax))."', ";
		$insert.="email='".addslashes(trim($email))."', ";
		$insert.="username='".addslashes(trim($username))."', ";
		$insert.="password='".md5(strtolower(trim($password)))."', ";
		$insert.="advert='OTHER', ";
		$insert.="advert_other='Manually Created', ";
		$insert.="tos='1',";
		// NOTE: ADMINS CAN'T BE USERS!
		$insert.="sid='".create_sid()."',";
		$insert.="ogcreate='".time()."'";

		// execute query
		mysql_query($insert);

		$uid=mysql_insert_id();

		$done=true;
		}
	}

include $server_admin_inc."/header.php";

if (isset($done))
	{
	echo("
		<table width='725' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
				<td width='99%' align='left' valign='top'>
				<table width='100%' cellpadding='2' cellspacing='0' border='0'>
					<tr>
						<td><img src='".$http_images."/menu_arrow.gif'><b>Add a New User to the Database</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/csv.php?sid=".$sid."&whm_id=".$whm_id."'>Go Back</a>]</td>
					</tr>
					<tr>
						<td align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='2'></td>
					</tr>
					<tr>
						<td align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/drop_arrow.gif'> You are currently working with the server: <b>".$server_name."</b></td>
					</tr>
					<tr>
						<td align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='12'></td>
					</tr>
					<tr>
						<td><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/error_arrow.gif'>The user was added successfully.</td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		");
	include $server_admin_inc."/footer.php";
	mysql_close($dblink);
	die();
	}

echo("
	<table width='725' cellpadding='0' cellspacing='0' border='0'>
		<tr>
			<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
			<td width='99%' align='left' valign='top'>
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Add a New User to the Database</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/csv.php?sid=".$sid."&whm_id=".$whm_id."'>Go Back</a>]</td>
				</tr>
				<tr>
					<td align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='2'></td>
				</tr>
				<tr>
					<td align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/drop_arrow.gif'> You are currently working with the server: <b>".$server_name."</b></td>
				</tr>
				<tr>
					<td align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='12'></td>
				</tr>
			</table>
	");
include $server_admin_tools."/client_management_errors_inc.php";
echo("
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td>
	");
include $server_admin_tools."/csv_user.php";
echo("
					</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	");

include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>