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

// convert super globals
if(phpversion() <= "4.0.6") { $_POST=$HTTP_POST_VARS; }
while (list ($key, $value) = each ($HTTP_POST_VARS)) 
	{
	if (trim($key)=="cartId") { $sid=trim($value); }
	else if (trim($key)=="transStatus") { $transStatus=trim($value); }
	else if (trim($key)=="callbackPW") { $callbackPW=trim($value); }
	}

$cksid=mysql_fetch_row(mysql_query("select count(*) from session_history where sid='".addslashes(trim($sid))."'"));
if ($cksid[0]<=0) { header("Location: ".$http_web."/step_one.php"); exit; }
else if (!isset($sid)) { header("Location: ".$http_web."/step_one.php"); exit; }

$query0="select ";
$query0.="first_name, ";		// 0
$query0.="last_name, ";			// 1
$query0.="organization_name, ";	// 2
$query0.="street_address_1, ";	// 3
$query0.="street_address_2, ";	// 4
$query0.="city, ";				// 5
$query0.="state, ";				// 6
$query0.="zip_code, ";			// 7
$query0.="country, ";			// 8
$query0.="phone, ";				// 9
$query0.="fax, ";				// 10
$query0.="email, ";				// 11
$query0.="username, ";			// 12
$query0.="password, ";			// 13
$query0.="advert, ";			// 14
$query0.="advert_other, ";		// 15
$query0.="tos, ";				// 16
$query0.="pid, ";				// 17
$query0.="payment_term, ";		// 18
$query0.="domain_name, ";		// 19
$query0.="payment_method, ";	// 20
$query0.="domain_registration, ";	// 21
$query0.="domain_expire, ";		// 22
$query0.="whm_username, ";		// 23
$query0.="whm_password, ";		// 24
$query0.="promotion_code, ";	// 25
$query0.="referrer_id, ";		// 26
$query0.="client_ip, ";			// 27
$query0.="total_due_today, ";	// 28
$query0.="total_due_reoccur, ";	// 29
$query0.="uid, ";				// 30
$query0.="addon_choices, ";		// 31
$query0.="client_notes, ";		// 32
$query0.="pns1, ";				// 33
$query0.="pns2, ";				// 34
$query0.="server_hostname, ";	// 35
$query0.="root_pw, ";			// 36
$query0.="tld, ";				// 37
$query0.="sorp ";				// 38
$query0.="from session_history ";
$query0.="where sid='".addslashes(trim($sid))."'";

$rs0=mysql_fetch_row(mysql_query($query0));

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

# added 11/24/2003
# -----------------------------------------
$tld_id=stripslashes(trim($rs0[37]));
$sorp=stripslashes(trim($rs0[38]));
# -----------------------------------------
# added 11/24/2003

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];
	}

// find the active server
$current_server=current_server($sid, 1);
$rsCallback=mysql_fetch_row(mysql_query("select worldpay_callback from payment_process where pid='2'"));

// 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; }

// it's a valid auto install
if ($current_server!=99&&$transStatus=="Y"&&$callbackPW==$rsCallback[0])
	{
	// extra space ordered?
	$extra_space=extra_webspace_ordered($addon_choices);

	$query2="select ";
	$query2.="cgi_access, ";		// 0
	$query2.="web_space, ";			// 1
	$query2.="frontpage, ";			// 2
	$query2.="cpanel_theme, ";		// 3
	$query2.="ftp, ";				// 4
	$query2.="sql, ";				// 5 
	$query2.="email, ";				// 6
	$query2.="email_list, ";		// 7
	$query2.="sub_domain, ";		// 8
	$query2.="bandwidth, ";			// 9
	$query2.="shell_access, ";		// 10
	$query2.="parked_domain, ";		// 11
	$query2.="addon_domain, ";		// 12
	$query2.="gid, ";				// 13
	$query2.="whm_package_name, ";	// 14
	$query2.="rid, ";	// 15
	$query2.="dedicated ";	// 16
	$query2.="from plan_specs ";		
	$query2.="where pid='".addslashes(trim($pid))."'";

	$rs2=mysql_fetch_row(mysql_query($query2));

	$cgi=stripslashes($rs2[0]);
	if (trim($extra_space)=="0") { $quota=stripslashes($rs2[1]); }
	else { $quota=stripslashes($rs2[1]+$extra_space); }
	$frontpage=stripslashes($rs2[2]);
	$cpmod=stripslashes($rs2[3]);
	$maxftp=stripslashes($rs2[4]);
	$maxsql=stripslashes($rs2[5]);
	$maxpop=stripslashes($rs2[6]);
	$maxlst=stripslashes($rs2[7]);
	$maxsub=stripslashes($rs2[8]);
	$bwlimit=stripslashes($rs2[9]);
	$hasshell=stripslashes($rs2[10]);
	$maxpark=stripslashes($rspd[11]);
	$maxaddon=stripslashes($rs2[12]);
	$gid=stripslashes($rs2[13]);
	$whm_package_name=stripslashes($rs2[14]);
	$rid=stripslashes($rs2[15]);
	$dedicated=stripslashes($rs2[16]);
	
	if ($dedicated==0)
		{
		if ($reseller==0)
			{
			$account_creation_status=create_account($server_ip, $xwhm_username, $xwhm_password, $domain_name, $whm_username, $whm_password, $unique_ip_ordered, $cgi, $quota, $frontpage, $cpmod, $maxftp, $maxsql, $maxpop, $maxlst, $maxsub, $bwlimit, $hasshell, $maxpark, $maxaddon, $whm_package_name, $default_ip);
			}
		else
			{
			$account_creation_status=create_account($server_ip, $xwhm_username, $xwhm_password, $domain_name, $whm_username, $whm_password, 0, $cgi, $quota, $frontpage, $cpmod, $maxftp, $maxsql, $maxpop, $maxlst, $maxsub, $bwlimit, $hasshell, $maxpark, $maxaddon, $whm_package_name, $default_ip);
			}
		}

	// account installed successful
	if ($account_creation_status==99)
		{
		if ($uid==0)
			{
			$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))."', ";

			$enc_pw=md5(strtolower(trim($password)));
			$insert.="password='".$enc_pw."', ";

			$insert.="advert='".addslashes(trim($advert))."', ";
			$insert.="advert_other='".addslashes(trim($advert_other))."', ";
			$insert.="temp_pw='".addslashes(trim($password))."', "; // delete this at first login
			$insert.="tos='1',";
			$insert.="sid='".create_sid()."', ";
			$insert.="ogcreate='".time()."'";

			mysql_query($insert);
			$uid=mysql_insert_id();
			
			if ($phpbb_active=="yes") { migrate_user_to_phpbb($username, $enc_pw, $email); }
			}
		else
			{			
			$update="update user set ";
			$update.="first_name='".addslashes(trim($first_name))."', ";
			$update.="last_name='".addslashes(trim($last_name))."', ";
			if (strlen(trim($organization_name))!=0) { $update.="organization_name='".addslashes(trim($organization_name))."', "; }
			$update.="street_address_1='".addslashes(trim($street_address_1))."', ";
			if (strlen(trim($street_address_2))!=0) { $update.="street_address_2='".addslashes(trim($street_address_2))."', "; }
			$update.="city='".addslashes(trim($city))."', ";
			$update.="state='".addslashes(trim($state))."', ";
			$update.="zip_code='".addslashes(trim($zip_code))."', ";
			$update.="country='".addslashes(trim($country))."', ";
			$update.="phone='".addslashes(trim($phone))."', ";
			$update.="fax='".addslashes(trim($fax))."', ";
			$update.="email='".addslashes(trim($email))."', ";
			$update.="advert='".addslashes(trim($advert))."', ";
			$update.="advert_other='".addslashes(trim($advert_other))."', ";
			$update.="tos='1' ";
			$update.="where uid='".addslashes(trim($uid))."'";

			mysql_query($update);
			}

		$insert_order="insert into hosting_order set ";
		$insert_order.="sorp='".addslashes(trim($sorp))."', ";
		$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.="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.="whm_id='".addslashes(trim($current_server))."', ";
		$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='1', ";
		if ($dedicated==1) { $insert_order.="resolved='1', "; }
		if ($domain_registration==1) { $insert_order.="tld_id='".addslashes(trim($tld_id))."', "; }
		
		if (getenv('HTTP_X_FORWARDED_FOR')) { $client_ip=getenv('HTTP_X_FORWARD_FOR'); } 
		else { $client_ip=getenv('REMOTE_ADDR'); }

		$insert_order.="client_ip='".addslashes(trim($client_ip))."', ";
		$insert_order.="client_host='".@gethostbyaddr($client_ip)."', ";
		$insert_order.="next_due_date='".time()."', ";
		$insert_order.="ogcreate='".time()."'";

		mysql_query($insert_order);

		$oid=mysql_insert_id();
		
		create_new_invoice($oid, 1);
		if ($domain_registration==1&&$use_enom==0) { domain_reg_notice($oid); }

		# added 10/16/2003
		$nq2="update ";
		$nq2.="hosting_order ";
		$nq2.="set ";
		$nq2.="next_due_date='".create_next_due(time(), convert_payment_term_to_number($payment_term))."' ";
		$nq2.="where ";
		$nq2.="oid='".addslashes(trim($oid))."'";
		
		mysql_query($nq2);
		# added 10/16/2003

		# START EDITED FOR RESELLER 7/2/2003
		# -----------------------------------------------------------------------------------------
		if ($rid!=0&&trim($rid)!=""&&$dedicated==0)
			{
			# 1a. set resellers primary domain to the order id
			$rq0="select ";
			$rq0.="reseller_primary_domain, ";
			$rq0.="ogcreate ";
			$rq0.="from ";
			$rq0.="user ";
			$rq0.="where ";
			$rq0.="uid='".addslashes(trim($uid))."'";
			$mrd=mysql_fetch_row(mysql_query($rq0));

			if ($mrd[0]==0)
				{
				$rq1="update ";
				$rq1.="user ";
				$rq1.="set ";
				$rq1.="reseller_primary_domain='".addslashes(trim($oid))."', ";
				$rq1.="reseller='1' ";
				$rq1.="where ";
				$rq1.="uid='".addslashes(trim($uid))."'";
				mysql_query($rq1);
				}

			# 1b. set the reseller flag in the hosting order to 1
			$rq2="update ";
			$rq2.="hosting_order ";
			$rq2.="set ";
			$rq2.="reseller='1' ";
			$rq2.="where ";
			$rq2.="oid='".addslashes(trim($oid))."'";

			mysql_query($rq2);
			
			create_reseller($server_ip, $xwhm_username, $xwhm_password, $whm_username, $rid);

			# add a entries if private ns are ordered - going to introduce later 7/4/2003
			
			send_reseller_welcome_email(34, $oid);
			}
		else { send_welcome_email_ip($email_admin, $oid, $unique_ip_ordered); }

		// send emails 
		send_dns_config($email_admin, $oid);
		new_account_installed($email_admin, $oid, $currency, $currency_type, $addon_choices);

		# -----------------------------------------------------------------------------------------
		# END EDITED FOR RESELLER 7/2/2003

		// check server status
		auto_switch_server($below_public, $email_admin);
		
		// affiliate functions
		if (strlen(trim($referrer_id))!=0)
			{
			// add the affiliate details
			add_new_referred_client($pid, $referrer_id, $uid, $oid);

			// send out affiliate email
			affiliate_notification_to_client($uid, $oid, $email_admin);
			}

		// delete the session for security
		mysql_query("delete from session_history where sid='".addslashes(trim($sid))."'");

		// redirect to the next step
		$data=base64_encode($gid."|".$oid);

		$read=fopen($below_public."/".$data."_invoice.log", "w");
		fwrite($read, $data);
		fclose($read);

		if ($use_enom==1&&$domain_registration==1) { file($http_web."/register_enom.php?c=".$data); }
		exit;
		}

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

			mysql_query($insert);

			$uid=mysql_insert_id();
			}
		else
			{			
			$update="update user set ";
			$update.="first_name='".addslashes(trim($first_name))."', ";
			$update.="last_name='".addslashes(trim($last_name))."', ";
			if (strlen(trim($organization_name))!=0) { $update.="organization_name='".addslashes(trim($organization_name))."', "; }
			$update.="street_address_1='".addslashes(trim($street_address_1))."', ";
			if (strlen(trim($street_address_2))!=0) { $update.="street_address_2='".addslashes(trim($street_address_2))."', "; }
			$update.="city='".addslashes(trim($city))."', ";
			$update.="state='".addslashes(trim($state))."', ";
			$update.="zip_code='".addslashes(trim($zip_code))."', ";
			$update.="country='".addslashes(trim($country))."', ";
			$update.="phone='".addslashes(trim($phone))."', ";
			$update.="fax='".addslashes(trim($fax))."', ";
			$update.="email='".addslashes(trim($email))."', ";
			$update.="advert='".addslashes(trim($advert))."', ";
			$update.="advert_other='".addslashes(trim($advert_other))."', ";
			$update.="tos='1' ";
			$update.="where uid='".addslashes(trim($uid))."'";

			mysql_query($update);
			}

		$insert_order="insert into hosting_order set ";
		$insert_order.="sorp='".addslashes(trim($sorp))."', ";
		$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.="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.="whm_id='".addslashes(trim($current_server))."', ";
		$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', ";
		if ($dedicated==1) { $insert_order.="resolved='1', "; }
		if ($domain_registration==1) { $insert_order.="tld_id='".addslashes(trim($tld_id))."', "; }
		
		if (getenv('HTTP_X_FORWARDED_FOR')) { $client_ip=getenv('HTTP_X_FORWARD_FOR'); } 
		else { $client_ip=getenv('REMOTE_ADDR'); }

		$insert_order.="client_ip='".addslashes(trim($client_ip))."', ";
		$insert_order.="client_host='".@gethostbyaddr($client_ip)."', ";
		$insert_order.="next_due_date='".time()."', ";
		$insert_order.="ogcreate='".time()."'";

		mysql_query($insert_order);

		$oid=mysql_insert_id();

		create_new_invoice($oid, 0);
		if ($domain_registration==1&&$use_enom==0) { domain_reg_notice($oid); }

		# added 10/16/2003
		$nq2="update ";
		$nq2.="hosting_order ";
		$nq2.="set ";
		$nq2.="next_due_date='".create_next_due(time(), convert_payment_term_to_number($payment_term))."' ";
		$nq2.="where ";
		$nq2.="oid='".addslashes(trim($oid))."'";
		
		mysql_query($nq2);
		# added 10/16/2003

		if ($dedicated==0)
			{
			$xmessage=("Greetings,\nThe recent order for ".$domain_name." failed. Web Host Manager returned: ".parse_whm_error($account_creation_status)."\n\nThanks,\nYour AutoPilot\n\nThis message was generated on: ".date("m/d/Y h:i:s a")."");
			mail($email_admin, "WHM Automation Error", $xmessage, "From: ".$email_admin."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
			}

		// affiliate functions
		if (strlen(trim($referrer_id))!=0)
			{
			// add the affiliate details
			add_new_referred_client($pid, $referrer_id, $uid, $oid);
			}
		
		// send out new order rec. email to clients
		new_order_received($email_admin, $sid, $currency, $currency_type, $addon_choices);
		
		// send out email to notify admin
		new_account_installed($email_admin, $oid, $currency, $currency_type, $addon_choices);

		// delete the session for security
		mysql_query("delete from session_history where sid='".addslashes(trim($sid))."'");
		
		
		// redirect to the next step
		$data=base64_encode($gid."|".$oid);

		$read=fopen($below_public."/".$data."_invoice.log", "w");
		fwrite($read, $data);
		fclose($read);

		if ($use_enom==1&&$domain_registration==1) { file($http_web."/register_enom.php?c=".$data); }
		exit;
		}
	}



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

		mysql_query($insert);
		$uid=mysql_insert_id();
		}
	else
		{			
		$update="update user set ";
		$update.="first_name='".addslashes(trim($first_name))."', ";
		$update.="last_name='".addslashes(trim($last_name))."', ";
		if (strlen(trim($organization_name))!=0) { $update.="organization_name='".addslashes(trim($organization_name))."', "; }
		$update.="street_address_1='".addslashes(trim($street_address_1))."', ";
		if (strlen(trim($street_address_2))!=0) { $update.="street_address_2='".addslashes(trim($street_address_2))."', "; }
		$update.="city='".addslashes(trim($city))."', ";
		$update.="state='".addslashes(trim($state))."', ";
		$update.="zip_code='".addslashes(trim($zip_code))."', ";
		$update.="country='".addslashes(trim($country))."', ";
		$update.="phone='".addslashes(trim($phone))."', ";
		$update.="fax='".addslashes(trim($fax))."', ";
		$update.="email='".addslashes(trim($email))."', ";
		$update.="advert='".addslashes(trim($advert))."', ";
		$update.="advert_other='".addslashes(trim($advert_other))."', ";
		$update.="tos='1' ";
		$update.="where uid='".addslashes(trim($uid))."'";

		mysql_query($update);
		}

	$insert_order="insert into hosting_order set ";
	$insert_order.="sorp='".addslashes(trim($sorp))."', ";
	$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', ";
	if ($dedicated==1) { $insert_order.="resolved='1', "; }
	if ($domain_registration==1) { $insert_order.="tld_id='".addslashes(trim($tld_id))."', "; }
		
	if (getenv('HTTP_X_FORWARDED_FOR')) { $client_ip=getenv('HTTP_X_FORWARD_FOR'); } 
	else { $client_ip=getenv('REMOTE_ADDR'); }

	$insert_order.="client_ip='".addslashes(trim($client_ip))."', ";
	$insert_order.="client_host='".@gethostbyaddr($client_ip)."', ";
	$insert_order.="next_due_date='".time()."', ";
	$insert_order.="ogcreate='".time()."'";

	mysql_query($insert_order);
	$oid=mysql_insert_id();
		
	create_new_invoice($oid, 0);
	if ($domain_registration==1&&$use_enom==0) { domain_reg_notice($oid); }

	# added 10/16/2003
	$nq2="update ";
	$nq2.="hosting_order ";
	$nq2.="set ";
	$nq2.="next_due_date='".create_next_due(time(), convert_payment_term_to_number($payment_term))."' ";
	$nq2.="where ";
	$nq2.="oid='".addslashes(trim($oid))."'";
	
	mysql_query($nq2);
	# added 10/16/2003

	// affiliate functions
	if (strlen(trim($referrer_id))!=0)
		{
		// add the affiliate details
		add_new_referred_client($pid, $referrer_id, $uid, $oid);
		}
		
	// send out new order rec. email to clients
	new_order_received($email_admin, $sid, $currency, $currency_type, $addon_choices);
		
	// send out email to notify admin
	new_account_installed($email_admin, $oid, $currency, $currency_type, $addon_choices);

	// delete the session for security
	mysql_query("delete from session_history where sid='".addslashes(trim($sid))."'");
	
	
	// redirect to the next step
	$data=base64_encode($gid."|".$oid);

	$read=fopen($below_public."/".$data."_invoice.log", "w");
	fwrite($read, $data);
	fclose($read);

	if ($use_enom==1&&$domain_registration==1) { file($http_web."/register_enom.php?c=".$data); }
	exit;
	}

mysql_close($dblink);
?>