<?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";

if (isset($submit))
	{
	if ($action==2)
		{
		mysql_query("update session_history set locked='0' where sid='".addslashes(trim($xsid))."'");
		}
	else if ($action==1)
		{
		mysql_query("delete from session_history where sid='".addslashes(trim($xsid))."'");
		}
	else if ($action==0)
		{
		$query="select ";
		$query.="session_history.pid, ";					// 0
		$query.="session_history.uid, ";					// 1
		$query.="session_history.whm_id, ";					// 2
		$query.="session_history.payment_term, ";			// 3
		$query.="session_history.payment_method, ";			// 4
		$query.="session_history.promotion_code, ";			// 5
		$query.="session_history.referrer_id, ";			// 6
		$query.="session_history.domain_name, ";			// 7
		$query.="session_history.domain_registration, ";	// 8
		$query.="session_history.domain_expire, ";			// 9
		$query.="session_history.addon_choices, ";			// 10
		$query.="session_history.ogcreate, ";				// 11
		$query.="session_history.first_name, ";				// 12
		$query.="session_history.last_name, ";				// 13
		$query.="session_history.organization_name, ";		// 14
		$query.="session_history.street_address_1, ";		// 15
		$query.="session_history.street_address_2, ";		// 16
		$query.="session_history.city, ";					// 17
		$query.="session_history.state, ";					// 18
		$query.="session_history.zip_code, ";				// 19
		$query.="session_history.country, ";				// 20
		$query.="session_history.phone, ";					// 21
		$query.="session_history.fax, ";					// 22
		$query.="session_history.email, ";					// 23
		$query.="session_history.username, ";				// 24
		$query.="session_history.password, ";				// 25
		$query.="session_history.advert, ";					// 26
		$query.="session_history.advert_other, ";			// 27
		$query.="session_history.tos, ";					// 28
		$query.="session_history.whm_username, ";			// 29
		$query.="session_history.whm_password, ";			// 30
		$query.="session_history.total_due_today, ";		// 31
		$query.="session_history.total_due_reoccur, ";		// 32
		$query.="session_history.client_ip, ";				// 33
		$query.="session_history.client_notes, ";			// 34
		$query.="session_history.pns1, ";					// 35
		$query.="session_history.pns2, ";					// 36
		$query.="session_history.server_hostname, ";		// 37
		$query.="session_history.root_pw, ";				// 38
		$query.="plan_specs.dedicated, ";					// 39
		$query.="session_history.tld ";						// 40
		$query.="from ";
		$query.="session_history, plan_specs ";
		$query.="where ";
		$query.="session_history.sid='".addslashes(trim($xsid))."' ";
		$query.="and ";
		$query.="session_history.pid=plan_specs.pid ";
		$query.="limit 0, 1";

		$rs=mysql_fetch_row(mysql_query($query));

		$pid=addslashes(trim($rs[0]));
		$uid=addslashes(trim($rs[1]));
		$whm_id=addslashes(trim($rs[2]));
		$payment_term=addslashes(trim($rs[3]));
		$payment_method=addslashes(trim($rs[4]));
		$promotion_code=addslashes(trim($rs[5]));
		$referrer_id=addslashes(trim($rs[6]));
		$domain_name=addslashes(trim($rs[7]));
		$domain_registration=addslashes(trim($rs[8]));
		$domain_expire=addslashes(trim($rs[9]));
		$addon_choices=addslashes(trim($rs[10]));
		$ogcreate=addslashes(trim($rs[11]));
		$first_name=addslashes(trim($rs[12]));
		$last_name=addslashes(trim($rs[13]));
		$organization_name=addslashes(trim($rs[14]));
		$street_address_1=addslashes(trim($rs[15]));
		$street_address_2=addslashes(trim($rs[16]));
		$city=addslashes(trim($rs[17]));
		$state=addslashes(trim($rs[18]));
		$zip_code=addslashes(trim($rs[19]));
		$country=addslashes(trim($rs[20]));
		$phone=addslashes(trim($rs[21]));
		$fax=addslashes(trim($rs[22]));
		$email=addslashes(trim($rs[23]));
		$username=addslashes(trim($rs[24]));
		$password=addslashes(trim($rs[25]));
		$advert=addslashes(trim($rs[26]));
		$advert_other=addslashes(trim($rs[27]));
		$tos=addslashes(trim($rs[28]));
		$whm_username=addslashes(trim($rs[29]));
		$whm_password=addslashes(trim($rs[30]));
		$total_due_today=addslashes(trim($rs[31]));
		$total_due_reoccur=addslashes(trim($rs[32]));
		$client_ip=addslashes(trim($rs[33]));
		$client_notes=addslashes(trim($rs[34]));
		$pns1=addslashes(trim($rs[35]));
		$pns2=addslashes(trim($rs[36]));
		$server_hostname=addslashes(trim($rs[37]));
		$root_pw=addslashes(trim($rs[38]));
		$dedicated=addslashes(trim($rs[39]));
		$tld_id=stripslashes(trim($rs[40]));
		
		# -----------------------------------------------------------------------

		if ($domain_registration==1)
			{
			$query_tld="select ";
			$query_tld.="tld ";
			$query_tld.="from ";
			$query_tld.="tld_chart ";
			$query_tld.="where ";
			$query_tld.="tld_id='".addslashes(trim($tld_id))."'";

			$rs_tld=mysql_fetch_row(mysql_query($query_tld));

			$domain_name.=".".$rs_tld[0];
			}

		$current_server=current_server($pid, 0);

		// get the server details
		$query1="select ";
		$query1.="server_ip, ";		// 0
		$query1.="whm_id, ";		// 1
		$query1.="reseller, ";		// 2
		$query1.="primary_ns, ";	// 3
		$query1.="secondary_ns, ";	// 4 

		# added 7/2/2003
		# -----------------------------------------
		$query1.="primary_ns_ip, ";	// 5
		$query1.="secondary_ns_ip ";// 6
		# -----------------------------------------
		# added 7/2/2003

		$query1.="from server_config ";

		if ($current_server==99)
			{
			$query_nsip="select ";
			$query_nsip.="whm_id ";
			$query_nsip.="from ";
			$query_nsip.="server_config ";
			$query_nsip.="order by whm_id desc ";
			$query_nsip.="limit 0, 1";

			$rs_nsip=mysql_fetch_row(mysql_query($query_nsip));

			$query1.="where whm_id='".addslashes(trim($rs_nsip[0]))."'";

			$temp=@read_log($rs_nsip[0], $below_public);
			}
		else 
			{ 
			$query1.="where whm_id='".addslashes(trim($current_server))."'";
			
			$temp=@read_log($current_server, $below_public); 
			}

		$rs1=mysql_fetch_row(mysql_query($query1));

		$server_ip=stripslashes(trim($rs1[0]));
		$whm_id=stripslashes(trim($rs1[1]));
		$reseller=stripslashes(trim($rs1[2]));
		$primary_ns=stripslashes(trim($rs1[3]));
		$secondary_ns=stripslashes(trim($rs1[4]));

		list($xwhm_username, $xwhm_password)=split("[|]", $temp);

		// get the IP for the account or use the shared
		if ($reseller==0) 
			{ 
			$unique_ip_ordered=unique_ip_ordered($addon_choices);
			if ($unique_ip_ordered==1) { $default_ip=get_free_ips($server_ip, $xwhm_username, $xwhm_password); }
			else { $default_ip=$server_ip; }
			}
		else { $default_ip=$server_ip; }

		# -----------------------------------------------------------------------

		if ($uid==0) { $insert="insert into user set "; }
		else { $insert="update 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='".addslashes(trim($advert))."', ";
		$insert.="advert_other='".addslashes(trim($advert_other))."', ";
		$insert.="temp_pw='".addslashes(trim($password))."', "; // delete this at first login
		if ($uid==0) { $insert.="sid='".create_sid()."', "; $insert.="ogcreate='".time()."', "; }
		$insert.="tos='1'";

		if ($uid==0) { mysql_query($insert); $uid=mysql_insert_id(); }
		else { $update.=" where uid='".addslashes(trim($uid))."'"; }

		$insert_order="insert into hosting_order set ";
		$insert_order.="uid='".addslashes(trim($uid))."', ";
		$insert_order.="pid='".addslashes(trim($pid))."', ";
		$insert_order.="domain_name='".addslashes(trim($domain_name))."', ";
		$insert_order.="domain_registration='".addslashes(trim($domain_registration))."', ";
		$insert_order.="domain_expire='".addslashes(trim($domain_expire))."', ";
		$insert_order.="payment_method='".addslashes(trim($payment_method))."', ";
		$insert_order.="payment_term='".addslashes(trim($payment_term))."', ";
		$insert_order.="addon_choices='".addslashes(trim($addon_choices))."', ";
		$insert_order.="ns1='".addslashes(trim($primary_ns))."', "; 
		$insert_order.="ns2='".addslashes(trim($secondary_ns))."', ";
		$insert_order.="ip='".addslashes(trim($default_ip))."', ";
		$insert_order.="whm_id='0', ";
		$insert_order.="total_due_today='".addslashes(trim($total_due_today))."', ";
		$insert_order.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', ";
		$insert_order.="promotion_code='".addslashes(trim($promotion_code))."', ";
		$insert_order.="referrer_id='".addslashes(trim($referrer_id))."', ";
		$insert_order.="whm_username='".addslashes(trim($whm_username))."', ";
		$insert_order.="whm_password='".addslashes(trim($whm_password))."', ";
		$insert_order.="client_notes='".addslashes(trim($client_notes))."', ";
		$insert_order.="pns1='".addslashes(trim($pns1))."', ";
		$insert_order.="pns2='".addslashes(trim($pns2))."', ";
		$insert_order.="server_hostname='".addslashes(trim($server_hostname))."', ";
		$insert_order.="root_pw='".addslashes(trim($root_pw))."', ";
		$insert_order.="status='0', ";
		$insert_order.="next_due_date='".time()."', ";
		if ($dedicated==1) { $insert_order.="resolved='1', "; }
		$insert_order.="ogcreate='".time()."'";

		mysql_query($insert_order);
		$oid=mysql_insert_id();
				
		create_new_invoice($oid, 0);

		mysql_query("delete from session_history where sid='".addslashes(trim($xsid))."'");

		$header="Location: ";
		$header.=$http_admin."/client_view.php?";
		$header.="oid=".$oid;
		$header.="&sid=".$sid;
		if ($dedicated==1) { $header.="&dedicated=1"; }
		header($header);
		exit;
		}
	}

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='4' cellspacing='1' border='0'>
					<tr>
						<td align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Manage Incomplete Orders</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/manage_session_history.php?sid=".trim($sid)."'>go back</a>]</td>
					</tr>
					<tr>
						<td align='left' valign='top'>The order was processed successfully.</td>
					</tr>
					<tr>
						<td align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='10'></td>
					</tr>
					<tr>
						<td align='left' valign='top'>Please use the <a href='".$http_admin."/client_view_edit.php?sid=".$sid."&status=99'>Client View Tools</a> to verify this order.</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='4' cellspacing='1' border='0'>
				<tr>
					<td colspan='4' align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Manage Incomplete Orders</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/index2.php?sid=".trim($sid)."'>go back</a>]</td>
				</tr>
				<tr>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>#</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Client Name</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Record Created</b></td>
					<td align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Actions</b></td>
					<td align='center' valign='middle' bgcolor='#E5E7E9'><img src='".$http_images."/down_arrow.gif'></td>
				</tr>
		");
	$y=1;
	$x=1;
	$query="select ";
	$query.="session_history.sid, ";						// 0
	$query.="session_history.first_name, ";					// 1
	$query.="session_history.last_name, ";					// 2
	$query.="session_history.email, ";						// 3
	# $query.="UNIX_TIMESTAMP(session_history.ogcreate), ";	// 4
	$query.="session_history.ogcreate, ";	// 4
	$query.="session_history.domain_name, ";				// 5
	$query.="plan_specs.package_name, ";					// 6
	$query.="payment_process.name, ";						// 7
	$query.="session_history.tld, ";						// 8
	$query.="session_history.domain_registration ";			// 9
	$query.="from ";
	$query.="session_history, ";
	$query.="plan_specs, ";
	$query.="payment_process ";
	$query.="where ";
	$query.="session_history.locked='1' ";
	$query.="and ";
	$query.="session_history.pid=plan_specs.pid ";
	$query.="and ";
	$query.="session_history.payment_method=payment_process.pid ";
	$query.="order by session_history.ogcreate desc";

	$row=mysql_query($query);

	while ($rs=mysql_fetch_row($row))
		{
		if ($y==1) {$bgcolor="#F2F4F6"; $y=0;} else {$bgcolor="#ECEEF0"; $y=1;}
		$xsid=stripslashes(trim($rs[0]));
		$first_name=stripslashes(trim($rs[1]));
		$last_name=stripslashes(trim($rs[2]));
		$email=stripslashes(trim($rs[3]));
		$created=date("m/d/Y h:i:s A", $rs[4]);
		$domain_name=stripslashes(trim($rs[5]));
		$package_name=stripslashes(trim($rs[6]));
		$gateway=stripslashes(trim($rs[7]));
		$tld_id=stripslashes(trim($rs[8]));
		$domain_registration=stripslashes(trim($rs[9]));

		if ($domain_registration==1)
			{
			$query_tld="select ";
			$query_tld.="tld ";
			$query_tld.="from ";
			$query_tld.="tld_chart ";
			$query_tld.="where ";
			$query_tld.="tld_id='".addslashes(trim($tld_id))."'";

			$rs_tld=mysql_fetch_row(mysql_query($query_tld));

			$domain_name.=".".$rs_tld[0];
			}

		echo("
			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".trim($sid)."'>
			<input type='hidden' name='xsid' value='".$xsid."'>
				<tr>
					<td align='center' valign='middle' bgcolor='".$bgcolor."'>".$x.").</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;<a href='mailto: ".$email."' class='cleanlink3' onmouseover=\"return overlib('Domain: ".$domain_name."<br>Package: ".$package_name."<br>Gateway: ".$gateway."', CAPTION, 'Order Details');\" onmouseout='nd();'>".$first_name." ".$last_name."</a></td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;".$created."</td>
					<td align='left' valign='middle' bgcolor='".$bgcolor."'>&nbsp;<select name='action' ".$input_style.">
						<option value='0'>Complete This Order</option>
						<option value='1'>Delete This Order</option>
						<option value='2'>Unlock This Order</option>
					</select></td>
					<td align='center' valign='middle' bgcolor='".$bgcolor."'><input ".$button_style." type='submit' name='submit' value='Process Now'></td>
				</tr>
			</form>
			");
		$x++;
		}
if ($x==1)
	{
	echo("
				<tr>
					<td colspan='4' align='left' valign='top'><img src='".$http_images."/error_arrow.gif'> No incomplete orders found in the database.</td>
				</tr>
		");
	}
echo("
			</table>
			</td>
		</tr>
	</table>
	");
include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>