<?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_inc."/invoice_functions.php";

// START CONNECTION STRING ----------------------

// 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]);

// read in logins
$temp=@read_log($whm_id, $below_public);
list($xwhm_username, $xwhm_password)=split("[|]", $temp);

// END CONNECTION STRING ------------------------

if (isset($submit))
	{
	$x=1;
	$list=file($below_public."/whm_csv_".$whm_id.".log");	
	foreach ($list as $line_num => $line)
		{
		list($id, $domain, $ip, $username, $reseller, $server, $theme, $quota, $package, $ogcreate)=split("[|]", trim($line));
		if ($id==$lid)
			{ 
			$y=substr($ogcreate, 0, 4);
			$m=substr($ogcreate, 4, 2);
			$d=substr($ogcreate, 6, 2);
			$ogcreate=mktime(0, 0, 0, $m, $d, $y);

			// write to db the record
			$insert="insert into hosting_order set ";
			$insert.="domain_name='".addslashes(trim($domain))."', ";
			$insert.="whm_username='".addslashes(trim($username))."', ";
			$insert.="ip='".addslashes(trim($ip))."', ";
			$insert.="reseller='0', ";
			$insert.="status='1', ";
			$insert.="resolved='1', ";
			$insert.="uid='".addslashes(trim($uid))."', ";
			$insert.="ogcreate='".addslashes(trim($ogcreate))."', ";
			$insert.="next_due_date='".time()."'";
			
			mysql_query($insert);
			
			$oid=mysql_insert_id();

			create_new_invoice($oid, 0);
			}
		else
			{
			// build buffer to update csv
			if ($x==1)
				{
				$write_data=$id."|".$domain."|".$ip."|".$username."|".$reseller."|".$server."|";
				}
			else
				{
				$write_data.=$id."|".$domain."|".$ip."|".$username."|".$reseller."|".$server."|";
				}

			$write_data.=$theme."|".$quota."|".$package."|".$ogcreate."\n";
			
			$x++;
			}
		}
	
	unlink($below_public."/whm_csv_".$whm_id.".log");
		
	// update csv
	$read=fopen($below_public."/whm_csv_".$whm_id.".log", "w");
	$result=fwrite($read, trim($write_data));
	fclose($read);
	
	chmod($below_public."/whm_csv_".$whm_id.".log", 0777);

	header("Location: ".$http_admin."/csv_server.php?sid=".$sid."&oid=".$oid."&whm_id=".trim($whm_id));
	exit;
	}

include $server_admin_inc."/header.php";

echo("
	<form action='".$PHP_SELF."' method='POST'>
	<input type='hidden' name='sid' value='".trim($sid)."'>
	<input type='hidden' name='whm_id' value='".trim($whm_id)."'>
	<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 colspan='2' align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Add Account to User from WHM CSV File</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/csv.php?sid=".$sid."&whm_id=".trim($whm_id)."'>Go Back</a>]</td>
				</tr>
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='2'></td>
				</tr>
				<tr>
					<td colspan='2' 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 colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='12'></td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Choose Domain:</td>
					<td width='75%' align='left' valign='top'>
					<select name='lid' ".$input_style.">
	");

$list=file($below_public."/whm_csv_".$whm_id.".log");	

foreach ($list as $line_num => $line)
	{
	list($id, $domain, $ip, $username, $reseller, $server, $theme, $quota, $package, $ogcreate)=split("[|]", $line);
	
	if ($reseller==$xwhm_username)
		{
		echo "<option value='".$id."'".((isset($lid))?"".(($lid==$id)?" selected":"")."":"").">[".$reseller."] ".$domain."</option>\n";
		}
	}

unset($username);
echo("
					</select><br><img src='".$http_images."/space.gif' width='2' height='16'><img src='".$http_images."/drop_arrow.gif'>ex. [username] domain</td>
				</tr>
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='12'></td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Choose User:</td>
					<td width='75%' align='left' valign='top'>
					<select name='uid' ".$input_style.">
	");
$row=mysql_query("select uid, first_name, last_name, zip_code, status, username from user where reseller='0' order by first_name asc, last_name asc");
while ($rs=mysql_fetch_row($row))
	{
	if ($rs[4]!=69)
		{
		echo "<option value='".$rs[0]."'".((isset($uid))?"".(($uid==$rs[0])?" selected":"")."":"").">[".stripslashes(trim($rs[3]))."] [".stripslashes(trim($rs[5]))."] ".stripslashes(trim($rs[1]))." ".stripslashes(trim($rs[2]))."</option>\n";
		}
	}
echo("
					</select>
					</td>
				</tr>
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='16'></td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='top'></td>
					<td width='75%' align='left' valign='top'><input ".$button_style." type='submit' name='submit' value='Add Account to User'></td>
				</tr>
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='6'></td>
				</tr>
				<tr>
					<td width='25%' align='left' valign='top'></td>
					<td width='75%' align='left' valign='top'>
					<table width='300' cellpadding='0' cellspacing='0'>
						<tr>
							<td align='left' valign='top'><p align='Justify'><font color='#990000'>* Submitting this form will remove the domain record from the local AutoPilot CSV file. It also creates a record for the domain and inserts it into the live AutoPilot database. If something should happen and you are unable to completely create this record, please use the <a href='".$http_admin."/client_view.php?sid=".$sid."&status=99'><b>Client Editor Tool</b></a> to complete the process. Click here to access the <a href='".$http_admin."/client_view.php?sid=".$sid."&status=99'><b>Client Editor Tool</b></a>.</font></p></td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	</form>
	");

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