<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/client_functions.php";
include $server_inc."/whm_functions.php";
include $server_inc."/login_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))
	{
	$list=file($below_public."/whm_csv_".$whm_id.".log");	
	foreach ($list as $line_num => $line)
		{
		list($xid, $domain, $ip, $username, $reseller, $server, $theme, $quota, $package, $ogcreate)=split("[|]", trim($line));

		if ($id[$line_num]!=$xid)
			{
			// 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";
			}
		}
		
	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);
	}

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 align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Add Accounts to Existing Reseller From WHM CSV File</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='".$http_admin."/csv.php?sid=".$sid."&whm_id=".trim($whm_id)."'>Import Utility Home</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>
			<table width='725' cellpadding='2' cellspacing='1' border='0'>
				<tr>
					<td colspan='9' align='left' valign='top'><img src='".$http_images."/error_arrow.gif'> All values below are read directly from Web Host Manger.</td>
				</tr>
				<tr>
					<td colspan='9' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='2'></td>
				</tr>
				<tr>
					<td align='center' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>#</b>&nbsp;&nbsp;</td>
					<td align='center' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>Remove</b>&nbsp;&nbsp;</td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>Domain</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>IP Address</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>Username</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>Owner</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>Package</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;&nbsp;<b>Created</b></td>
				</tr>
	");
$list=file($below_public."/whm_csv_".$whm_id.".log");	
foreach ($list as $line_num => $line)
	{
	if (($line_num%2)==0) { $bgcolor="#F2F4F6"; } else { $bgcolor="#ECEEF0"; }
	list($id, $domain, $ip, $username, $reseller, $server, $theme, $quota, $package, $ogcreate)=split("[|]", $line);
	$y=substr($ogcreate, 0, 4);
	$m=substr($ogcreate, 4, 2);
	$d=substr($ogcreate, 6, 2);
	$cc=$line_num+1;
		echo("
				<tr>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;&nbsp;".((strlen($cc)==1)?"0".$cc."":"".$cc."").".</td>
					<td align='center' valign='middle' bgcolor='".$bgcolor."'><input type='checkbox' name='id[".$line_num."]' value='".$id."'></td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;&nbsp;".$domain."</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;&nbsp;".$ip."</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;&nbsp;".$username."</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;&nbsp;".$reseller."</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;&nbsp;".$package."</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;&nbsp;".$m."/".$d."/".$y."</td>
				</tr>
			");
	}
echo("
				<tr>
					<td colspan='9' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='12'></td>
				</tr>
				<tr>
					<td colspan='9' align='left' valign='top'><input ".$button_style." type='submit' name='submit' value='Edit and Save Local CSV File'></td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	</form>
	");

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