<?PHP
# Cron execution
function exec_cron($http_admin, $request, $qstring)
	{
	$link=curl_init();
	curl_setopt($link, CURLOPT_URL, $http_admin.$request);
	curl_setopt($link, CURLOPT_HEADER, 0);
	curl_setopt($link, CURLOPT_RETURNTRANSFER, 1);

	if ($qstring!==0) { curl_setopt($link, CURLOPT_POSTFIELDS, $qstring); }

	$data=curl_exec($link);
	$data=split("\,", $data);
	curl_close($link);

	return 0;
	}

# WHM Automation Functions
# ------------------------------------------------------------------------------------------------

# WHM Automation
function exec_whm($server_ip, $request, $qstring, $auth)
	{
	# @exec("/usr/bin/curl -d '".$qstring."' http://".$server_ip.":2086".$request, $xresult, $return);
	
	# $data= $xresult[0];
	#$data = split("\,", $xresult[0]);
	
	# echo $auth; die;
	$link=curl_init();
	curl_setopt($link, CURLOPT_TIMEOUT, 8);
	curl_setopt($link, CURLOPT_URL, "http://".$server_ip.":2086".$request);
	curl_setopt($link, CURLOPT_HEADER, 0);
	curl_setopt($link, CURLOPT_RETURNTRANSFER, 1);
	$curlheaders[0]="Authorization: WHM ".$auth;
	curl_setopt($link, CURLOPT_HTTPHEADER, $curlheaders);

	if ($qstring!==0) { curl_setopt($link, CURLOPT_POSTFIELDS, $qstring); }

	$data=curl_exec($link);
	$data=split("\,", $data);
	curl_close($link);
	
	
	return $data;
	}

# change cpanel pw!
function check_IP_reserve($server_ip, $whm_username, $whm_password) 
	{
	$auth=$whm_username.":".preg_replace("'(\r|\n)'", "", $whm_password);
	
	$request="/scripts/reservedip";
	$qstring="";

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line) { $buffer.=$line; }

	$buffer2=explode("\n", $buffer);
	foreach ($buffer2 as $line2)
		{
		$buffer3=explode(" ", $line2);
		foreach ($buffer3 as $line3)
			{
			if (substr($line3, 0, 4)=="name") 
				{ 
				list($bad, $good)=explode(">", strip_tags($line3));
				$good=trim($good);
				$ip=trim($good);
				
				# check to see if checked is true
				$buffer4=explode(" ", $line2);
				foreach ($buffer4 as $line4)
					{
					if (strcmp(strtolower(trim($line4)), "checked")==0) { $taken=true; }
					}
				
				if (!isset($taken)) { if ($ip!="") { $list.=$ip."|"; } }
				else { unset($taken); }
				}
			}
		}
	
	if (!isset($list)) { return 0; }
	else { return $list; }
	}

# change cpanel pw!
function change_cpanel_password($server_ip, $whm_username, $whm_password, $user, $new_password) 
	{
	$auth=$whm_username.":".preg_replace("'(\r|\n)'", "", $whm_password);
	
	$request="/scripts/passwd";
	
	$qstring="password=".$new_password;
	$qstring.="&domain=".$user;
	$qstring.="&user=".$user;
	$qstring.="&submit-domain=Change%0D%0A++++Password";

	$data=exec_whm($server_ip, $request, $qstring, $auth);

	foreach ($data as $line) 
		{ 
		$base=strip_tags($line);
		$base=explode(" ", $base);
		foreach ($base as $data) 
			{ 
			if (ereg("changed", trim($data))==true) { return 1; }
			}
		}

	return 0;
	}

# list accounts from WHM
# $which == 0 == return | delimited list of account
# $which == 1 == return total # of accounts in whm
function list_accounts($server_ip, $whm_username, $whm_password, $which) 
	{
	$auth=$whm_username.":".preg_replace("'(\r|\n)'", "", $whm_password);
	
	$request="/scripts2/listaccts";
	
	$qstring="viewall=1";
	$qstring.="&nohtml=1";

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	$buffer="";
	#foreach ($data as $line) { echo $line; }  die;
	foreach ($data as $line) 
		{
		$line=str_replace("=", "©", $line);
		$line=str_replace("\n", "{{", $line);
		$buffer.=$line."©";
		}
	unset($line);
	# echo $buffer; die("<0---------");
	$buffer2=explode("{{", $buffer);
	$buffer3="";
	foreach ($buffer2 as $line_num => $line)
		{
		list($user, $domain, $package, $owner)=explode("©", $line);
		if (trim($user)!=""&&trim($package)!=""&&trim($owner)!="")
			{
			$buffer3.=$user."|".$package."|".$owner."\n";
			}
		}
	
	if ($which==0) { return $buffer3; }
	else if ($which==1) 
		{ 
		if (trim($line_num)=="") { return 0; } 
		else { return $line_num; } 
		}
	}


# Return Apache, Bind and the Server Load
function show_server_status($server_ip, $xwhm_username, $xwhm_password)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/servup";

	$data=exec_whm($server_ip, $request, 0, $auth);

	// Read each line into an array
	foreach ($data as $line_num => $line) { $buffer=strip_tags(trim($line)); }

	list($junk, $good)=explode("}", $buffer);
	$explode=explode("\n", $good);
	$nl_count=substr_count($good, "\n");
	for ($i=0; $i<=$nl_count; $i++)
		{
		if (ereg("apache", $explode[$i])==true) 
			{ 
			$alert=0;
			if (ereg("red", $explode[$i])) { $alert=1; }
			$apache=strip_tags(trim(str_replace("up", "", $explode[$i])))."©".$alert;
			}
		 else if (ereg("bind", $explode[$i])==true)
			{
			$alert=0;
			if (ereg("red", $explode[$i])) { $alert=1; }
			$bind=strip_tags(trim(str_replace("up", "", $explode[$i])))."©".$alert;
			}
		 if (ereg("Server Load", $explode[$i])==true)
			{
			$alert=0;
			if (ereg("red", $explode[$i])) { $alert=1; }
			$server_load=strip_tags(trim(str_replace("Server Load", "", $explode[$i])))."©".$alert;
			}
		}

	return $apache."|".$bind."|".$server_load."|";
	}


# return the cpanel and whm versions 
function show_cpanel_version($server_ip, $xwhm_username, $xwhm_password)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/command";

	$data=exec_whm($server_ip, $request, 0, $auth);

	// Read each line into an array
	foreach ($data as $line_num => $line) 
		{
		if (ereg(">WHM", $line)==true) 
			{ 
			$buffer=strip_tags(trim($line));
			list($junk, $good)=explode("}", $buffer);
			$ct=substr_count($good, "\n");
			$explode=explode("\n", $good);
			$data="";
			for ($i=0; $i<=$ct; $i++) { if (trim($explode[$i])!="") { $data.=trim($explode[$i])."|";	} }
			list($wv, $cv)=explode("|", $data);

			return $wv."|".$cv."|";
			}
		}
	}


# return 0 or first free IP found
function get_free_ips($server_ip, $xwhm_username, $xwhm_password)
	{	
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/ipusage";

	$data=exec_whm($server_ip, $request, 0, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	$reserved=check_IP_reserve($server_ip, $xwhm_username, $xwhm_password);
	$reserved_list=explode("|", $reserved);

	$buffer2=explode("\n", $buffer);
	foreach ($buffer2 as $line2)
		{
		list($ip, $junk)=explode("</td>", $line2);
		$ip=strip_tags($ip);
		$ip=trim($ip);

		$valid_ip=

		$junk=strip_tags($junk);
		$junk=trim($junk);

		if ($junk==""&&is_numeric(str_replace(".", "", $ip))==true)
			{
			if (in_array($ip, $reserved_list)) { return $ip; }
			}
		}

	return 0;
	}


# return the default install IP address
function default_install_ip($server_ip, $xwhm_username, $xwhm_password)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/editsets";

	$data=exec_whm($server_ip, $request, 0, $auth);

	// Read each line into an array
	foreach ($data as $line_num => $line) 
		{

		if (ereg("name=\"ADDR\"", $line)==true) 
			{ 
			list($junk, $good)=explode("value=\"", strtolower($line));
			list($ip, $junk0)=explode("\"", $good);
			return $ip;
			}
		}
	}


# return total bandwidth usage for this month
function show_bandwidth_usage($server_ip, $xwhm_username, $xwhm_password)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/showbw";

	$data=exec_whm($server_ip, $request, 0, $auth);

	// Read each line into an array
	foreach ($data as $line_num => $line) 
		{
		if (ereg("Total", $line)==true) 
			{ 
			list($junk, $good, $bad, $ugly)=explode("]", $line);
			$buffer=strip_tags($ugly);
			list($band, $width)=explode(".", $buffer);
			return $band.".".substr($width, 0, 6);
			}
		}
	}
	

# return list of installed cpanel themes
function list_cpanel_themes($server_ip, $whm_username, $whm_password) 
	{
	$auth=$whm_username.":".preg_replace("'(\r|\n)'", "", $whm_password);
	
	$request="/scripts/listthemes";

	$data=exec_whm($server_ip, $request, 0, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line;	}

	$buffer=strip_tags($buffer);
	list($junk, $good)=explode("List", $buffer);
	$ct=substr_count($good, "\n");
	$explode=explode("\n", $good);
	$list="";
	for ($i=0; $i<=$ct; $i++)
		{
		if (trim($explode[$i])!="") { $list.=trim($explode[$i])."|"; }
		}

	// return the content of the file.
	return $list;
	}
	

# return 1 if $whm_username is found in list
function check_whm_username($server_ip, $xwhm_username, $xwhm_password, $whm_username) 
	{
	GLOBAL $domain_name;

	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts2/listaccts";

	$qstring="viewall=1";

	$data=exec_whm($server_ip, $request, $qstring, $auth);

	$buffer="";
	$whm_username=strtolower($whm_username);
	foreach ($data as $line_num => $line) {	$buffer.=$line;	}

	list($junk, $good)=explode("reseller</b>", strtolower($buffer));
	$ct=(substr_count($good, "\n")-1);
	$explode=explode("\n", $good);
	for ($i=0; $i<=$ct; $i++)
		{
		if (trim($explode[$i])!="")
			{
			list($y0, $y1, $y2, $y3, $y4, $y5, $y6, $y7, $y8, $y9, $y10)=explode("</td>", $explode[$i]);
			
			$current=strip_tags(strtolower($y2));
			$current_domain=strip_tags(strtolower($y0));
			if (strcmp($current_domain, $domain_name)==0&&trim($current_domain)!="") { return 1; }
			else if (strcmp($current, $whm_username)==0&&trim($current)!="") { return 2; }
			}
		}
	}

# change ownership of an account in whm
function change_ownership($server_ip, $xwhm_username, $xwhm_password, $username, $new_owner)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/dochangeowner";

	$qstring="user=".$username."&owner=".$new_owner;

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }

	return 1;
	}

# parse and return the csv file
# which == 0 == return string
# which == 1 == write file return 1
# $display_time == 0 == UNIX time
# $display_time == 1 == MySQL time
function write_csv($server_ip, $xwhm_username, $xwhm_password, $below_public, $which, $display_time, $whm_id)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/fetchcsv";

	$qstring="";

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	# fix the csv if needed
	$buffer="";
	foreach ($data as $line_num => $line) { if (ereg(",", $line)==false) { $line.=","; } $buffer.=$line; }
	
	# parse the csv into lines
	$line_array=explode("\n", $buffer);
	$buffer2="";
	for ($i=0; $i<=count($line_array); $i++)
		{
		list($j0, $domain, $ip, $username, $j1, $j2, $j3, $j4, $j5, $j6, 
			$j7, $j8, $owner, $dns, $cp, $j9, $mb, $j10, $j11, $j12, $package, 
			$j13, $j14, $j15, $j16, $j17, $j18, $created)=explode(",", $line_array[$i]);
		
		$display_time=display_time(trim($created), 1);
		if (substr($display_time, -4)!="0000")
			{
			$buffer2.=trim($i)."|";
			$buffer2.=trim($domain)."|";
			$buffer2.=trim($ip)."|";
			$buffer2.=trim($username)."|";
			$buffer2.=trim($owner)."|";
			$buffer2.=trim($dns)."|";
			$buffer2.=trim($cp)."|";
			$buffer2.=trim($mb)."|";
			$buffer2.=trim($package)."|";
			$buffer2.=$display_time."|\n";
			
			$buffer2=str_replace("|||||||-1|\n", "", $buffer2);
			$buffer2=str_replace(":443", "", $buffer2);
			}
		}
	
	if ($which==0) { return trim($buffer2); }
	else if ($which==1)
		{
		$read=fopen($below_public."/whm_csv_".$whm_id.".log", "w");
		$result=fwrite($read, trim($buffer2));
		fclose ($read);

		chmod($below_public."/whm_csv_".$whm_id.".log", 0777);
		
		return 1;
		}
	}

# Create a new account!
function create_account($server_ip, $xwhm_username, $xwhm_password, $domain_name, $whm_username, $whm_password, $ip, $cgi, $quota, $frontpage, $cpmod, $maxftp, $maxsql, $maxpop, $maxlst, $maxsub, $bwlimit, $hasshell, $maxpark, $maxaddon, $whm_package_name, $default_ip)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/wwwacct";

	$filename="domain=".$domain_name;
	$filename.="&username=".$whm_username;
	$filename.="&password=".$whm_password;
	$filename.="&quota=".$quota;
	$filename.="&cpmod=".$cpmod;
	
	if ($ip==1) { $filename.="&ip=".$ip; }
	# if ($ip==1) { $filename.="&ip=y"; }	else { $filename.="&ip=n"; }
	if ($cgi==1) { $filename.="&cgi=1"; } else { $filename.="&cgi=n"; }
	if ($frontpage==1) { $filename.="&frontpage=y"; }	else { $filename.="&frontpage=n"; }
	$filename.="&maxftp=".((trim($maxftp)=="-1")?"unlimited":"".$maxftp."");
	$filename.="&maxsql=".((trim($maxsql)=="-1")?"unlimited":"".$maxsql."");
	$filename.="&maxpop=".((trim($maxpop)=="-1")?"unlimited":"".$maxpop."");
	$filename.="&maxlst=".((trim($maxlst)=="-1")?"unlimited":"".$maxlst."");
	$filename.="&maxsub=".((trim($maxsub)=="-1")?"unlimited":"".$maxsub."");
	$filename.="&bwlimit=".$bwlimit;
	if ($hasshell==1) { $filename.="&hasshell=y"; }	else { $filename.="&hasshell=n"; }
	$filename.="&maxpark=".$maxpark;
	$filename.="&maxaddon=".((trim($maxaddon)=="-1")?"unlimited":"".$maxaddon."");

	if ($ip==1) { $filename.="&customip=".trim($default_ip); } else { $filename.="&customip=--Auto+Assign--"; }
	$filename.="&nohtml=1";
	
	$msel="";
	if ($ip==1) { $msel.="y,"; } else { $msel.="n,"; }
	if ($cgi==1) { $msel.="y,"; } else { $msel.="n,"; }
	$msel.=$quota.",";
	if ($frontpage==1) { $msel.="y,"; } else { $msel.="n,"; }
	$msel.=$cpmod.",";
	$msel.=((trim($maxftp)=="-1")?"unlimited":"".$maxftp."").",";
	$msel.=((trim($maxsql)=="-1")?"unlimited":"".$maxsql."").",";
	$msel.=((trim($maxpop)=="-1")?"unlimited":"".$maxpop."").",";
	$msel.=((trim($maxlst)=="-1")?"unlimited":"".$maxlst."").",";
	$msel.=((trim($maxsub)=="-1")?"unlimited":"".$maxsub."").",";
	$msel.=((trim($maxpark)=="-1")?"unlimited":"".$maxpark."").",";
	$msel.=$bwlimit.",";
	if ($hasshell==1) { $msel.="y,"; } else { $msel.="n,"; }
	$msel.=$whm_package_name;
	
	$filename.="&msel=".$msel;
	$qstring=$filename;
	# echo $qstring."<br><br>"; die;
	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$result="";
	foreach ($data as $line_num => $line) {	$result.=$line."|"; }
	# echo $result; die;
	$result=strip_tags($result);

	# echo nl2br($result); die;

	// check the status of the account creation attempt
	if (ereg("/etc/wwwacct.conf not found",$result)==true) { return 1; }
	else if (ereg("Missing NS Config Line in /etc/wwwacct.conf",$result)==true) { return 2; }
	else if (ereg("Missing NS2 Config Line in /etc/wwwacct.conf",$result)==true) { return 3; }
	else if (ereg("Missing HOMEDIR Config Line in /etc/wwwacct.conf",$result)==true) { return 4; }
	else if (ereg("Missing HOMEMATCH Config Line in /etc/wwwacct.conf",$result)==true) { return 5; }
	else if (ereg("Missing DEFMOD Config Line in /etc/wwwacct.conf",$result)==true) { return 6; }
	else if (ereg("Missing HOST Config Line in /etc/wwwacct.conf",$result)==true) { return 7; }
	else if (ereg("Missing LOGSTYLE Config Line in /etc/wwwacct.conf",$result)==true) { return 8; }
	else if (ereg("Missing BINDVER Config Line in /etc/wwwacct.conf",$result)==true) { return 9; }
	else if (ereg("Missing FTPTYPE Config Line in /etc/wwwacct.conf",$result)==true) { return 10; }
	else if (ereg("Missing SCRIPTALIAS Config Line in /etc/wwwacct.conf",$result)==true) { return 11; }
	else if (ereg("httpd.conf is corrupt.",$result)==true) { return 12; }
	else if (ereg("proftpd.conf",$result)==true) { return 13; }
	else if (ereg("Unable to determine main ip",$result)==true) { return 14; }
	else if (ereg("Ouch, sorry that username is taken",$result)==true) { return 15; }
	else if (ereg("Sorry that username is too long.",$result)==true) { return 16; }
	else if (ereg("Sorry usernames cannot begin with a number.",$result)==true) { return 17; }
	else if (ereg("Sorry usernames cannot begin with a dash.",$result)==true)  { return 18; }
	else if (ereg("Requested IP address",$result)==true) { return 19; }
	else if (ereg("Unable to find an ip address",$result)==true) { return 20; }
	else if (ereg("You cannot setup a domain that is the same as the servers hostname",$result)==true) { return 21; }
	else if (ereg("Sorry, thats an invalid domain",$result)==true) { return 22; }
	else if (ereg("Sorry, thats an invalid username",$result)==true) { return 23; }
	else if (ereg("No domain name given",$result)==true) { return 24; }
	else if (ereg("Sorry, that domain is already setup",$result)==true) { return 25; }
	else if (ereg("Ok Guess NOT",$result)==true) { return 26; }
	else if (ereg("Sorry thats not a valid domain",$result)==true) { return 27; }
	else if (ereg("is not a valid domain name",$result)==true) { return 28; }
	else if (ereg("is not a valid domain name",$result)==true) { return 29; }
	else if (ereg("Sorry you are at your limit for creating",$result)==true) { return 30; }
	else if (ereg("Sorry, you must choose a plan",$result)==true) { return 31; } 
	else if (ereg("Sorry, you cannot create an account with an unlimited bandwidth limit.",$result)==true) { return 33; } 
	# else if (ereg("wwwacct creation finished",$result)==true) { return 99; }
	else { return 99; }
	}

// parse out whm error codes
function parse_whm_error($create_account)
	{
	if (trim($create_account)=="1") { return("/etc/wwwacct.conf not found"); }
	else if (trim($create_account=="2")) { return("Missing NS Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="3")) { return("Missing NS2 Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="4")) { return("Missing HOMEDIR Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="5")) { return("Missing HOMEMATCH Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="6")) { return("Missing DEFMOD Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="7")) { return("Missing HOST Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="8")) { return("Missing LOGSTYLE Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="9")) { return("Missing BINDVER Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="10")) { return("Missing FTPTYPE Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="11")) { return("Missing SCRIPTALIAS Config Line in /etc/wwwacct.conf"); }
	else if (trim($create_account=="12")) { return("httpd.conf is corrupt."); }
	else if (trim($create_account=="13")) { return("proftpd.conf issues"); }
	else if (trim($create_account=="14")) { return("Unable to determine main ip"); }
	else if (trim($create_account=="15")) { return("Ouch, sorry that username is taken"); }
	else if (trim($create_account=="16")) { return("Username is too long."); }
	else if (trim($create_account=="17")) { return("Usernames cannot begin with a number."); }
	else if (trim($create_account=="18")) { return("Usernames cannot begin with a dash."); }
	else if (trim($create_account=="19")) { return("Requested IP address issue"); }
	else if (trim($create_account=="20")) { return("Unable to find an ip address"); }
	else if (trim($create_account=="21")) { return("You cannot setup a domain that is the same as the servers hostname"); }
	else if (trim($create_account=="22")) { return("Invalid domain"); }
	else if (trim($create_account=="23")) { return("Invalid username"); }
	else if (trim($create_account=="24")) { return("No domain name given"); }
	else if (trim($create_account=="25")) { return ("Domain is already installed on this server."); }
	else if (trim($create_account=="26")) { return("Unknown Error, returned: [Ok Guess NOT]"); }
	else if (trim($create_account=="27")) { return("1. Not a valid domain"); }
	else if (trim($create_account=="28")) { return("2. Not a valid domain name"); }
	else if (trim($create_account=="29")) { return("3. Not a valid domain name"); }
	else if (trim($create_account=="1001")) { return("Unique IP Request must be installed manually using WHM."); }
	else if (trim($create_account=="1002")) { return("The account is now suspended."); }
	else if (trim($create_account=="1003")) { return("The account is now active."); }
	else if (trim($create_account=="1004")) { return("The account is cancelled."); }
	else if (trim($create_account=="30")) { return("One of the following applies:<br>- You are at your limit in creating accounts for this plan.<br>- You have not defined the plan attributes correctly in AutoPilot.<br>- The chosen install server is in reseller mode by mistake.<br><br>"); }
	else if (trim($create_account=="31")) { return("No reseller plan defined."); }
	else if (trim($create_account=="32")) { return("Unknown Error."); }
	else if (trim($create_account=="33")) { return("Sorry, you cannot create an account with an unlimited bandwidth limit."); } 
	}


# suspend an account
function suspend_account($server_ip, $xwhm_username, $xwhm_password, $whm_username)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts2/suspendacct";

	$qstring="domain=".$whm_username;
	$qstring.="&user=".$whm_username;
	$qstring.="&suspend-domain=Suspend";
	$qstring.="&reason=Processed_via_AutoPilot";

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	return 0;
	}


# unsuspend an account
function unsuspend_account($server_ip, $xwhm_username, $xwhm_password, $whm_username)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts2/suspendacct";

	$qstring="domain=".$whm_username;
	$qstring.="&user=".$whm_username;
	$qstring.="&unsuspend-domain=UnSuspend";
	$qstring.="&reason=Processed_via_AutoPilot";

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	return 0;
	}
	

# terminate an account
function terminate_account($server_ip, $xwhm_username, $xwhm_password, $whm_username)
	{
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/killacct";

	$qstring="domain=".$whm_username;
	$qstring.="&user=".$whm_username;
	$qstring.="&submit-domain=Terminate";

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	return 0;
	}
	
# change a sites quota
function change_sites_quota($server_ip, $xwhm_username, $xwhm_password, $whm_username, $quota)
	{	
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/editquota";

	$qstring="user=".$whm_username;
	$qstring.="&quota=".$quota;

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	return 0;
	}
	

# change a sites IP addr
function change_sites_ip($server_ip, $xwhm_username, $xwhm_password, $whm_username, $old_ip, $new_ip)
	{	
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts2/dochangeip";

	$qstring="user=".$whm_username;
	$qstring.="&oldip=".$old_ip;
	$qstring.="&customip=".$new_ip;

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	return 0;
	}


# list all available packages for options
# this option: 
# "Limit The Amount of Accounts {user} can create by account type."
function list_available_packages($server_ip, $xwhm_username, $xwhm_password, $whm_username)
	{	
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts2/editres";

	$qstring="res=".$whm_username;

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	$ct=substr_count($buffer, "\n");
	$ex=explode("\n", $buffer);
	for ($i=0; $i<=$ct; $i++)
		{
		if (ereg("Number Allowed", $ex[$i])||isset($bit))
			{
			if (ereg("</table>", strtolower($ex[$i]))) { break; }
			else { $bit.=$ex[$i]."{|}"; }
			}
		}
	unset($ct);
	unset($ex);
	unset($i);
	
	$bit=strtolower($bit);
	$bit=str_replace("<tr>", "", $bit);
	$bit=str_replace("</tr>", "", $bit);
	$bit=str_replace("<td>", "", $bit);

	$ct=substr_count($bit, "{|}");
	$ex=explode("{|}", $bit);
	for ($i=0; $i<=$ct; $i++)
		{
		if ($i>=1&&$i<($ct-1))
			{
			list($a, $b, $c)=explode("</td>", $ex[$i]);
			list($junk, $c)=explode("value=\"", $c);
			$c=str_replace("\">", "", $c);

			# name | current | alloted # | limit type
			$res.=$a."|".$b."|".$c."|acctlim-|\n";
			}
		}

	return strip_tags($res);
	}


# Grant a normal user reseller privileges
function grant_reseller($server_ip, $xwhm_username, $xwhm_password, $whm_username)
	{	
	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts/addres";

	$qstring="res=".$whm_username;
	$qstring.="&submit=ok";

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	$buffer="";
	foreach ($data as $line_num => $line) { $buffer.=$line; }
	
	return 0;
	}


# create a WHM reseller - grant's reseller access and permissions.
function create_reseller($server_ip, $xwhm_username, $xwhm_password, $whm_username, $rid)
	{
	grant_reseller($server_ip, $xwhm_username, $xwhm_password, $whm_username);
	
	$rqw3="select ";
	$rqw3.="limit_type, ";		// 0
	$rqw3.="acl_list, ";		// 1
	$rqw3.="resnumlimitamt, ";	// 2
	$rqw3.="rslimit_disk, ";	// 3
	$rqw3.="rsolimit_disk, ";	// 4
	$rqw3.="rslimit_bw, ";		// 5
	$rqw3.="rsolimit_bw ";		// 6
	$rqw3.="from ";
	$rqw3.="reseller_profile ";
	$rqw3.="where ";
	$rqw3.="rid='".addslashes(trim($rid))."'";

	$rq3=mysql_fetch_row(mysql_query($rqw3));

	$limit_type=$rq3[0];
	$acl_list=base64_decode($rq3[1]);
	$resnumlimitamt=stripslashes(trim($rq3[2]));
	$rslimit_disk=stripslashes(trim($rq3[3]));
	$rsolimit_disk=stripslashes(trim($rq3[4]));
	$rslimit_bw=stripslashes(trim($rq3[5]));
	$rsolimit_bw=stripslashes(trim($rq3[6]));
	$buffer="res=".$whm_username;
	$buffer.="&submit=Save";

	$acl_ct=substr_count($acl_list, "|");
	$ex=explode("|", $acl_list);
	for ($i=0; $i<=$acl_ct; $i++) {	$buffer.="&".str_replace("_", "-", $ex[$i])."=1"; }

	if ($limit_type==0) { $buffer.="&reslimit=1"; }
	else if ($limit_type==1)
		{
		$buffer.="&resnumlimit=1";
		$buffer.="&resnumlimitamt=".$resnumlimitamt;
		}
	else if ($limit_type==2)
		{
		$buffer.="&resreslimit=1";
		$buffer.="&rslimit-disk=".$rslimit_disk;
		$buffer.="&rsolimit-disk=".$rsolimit_disk;
		$buffer.="&rslimit-bw=".$rslimit_bw;
		$buffer.="&rsolimit-bw=".$rsolimit_bw;
		}

	$auth=$xwhm_username.":".preg_replace("'(\r|\n)'", "", $xwhm_password);
	
	$request="/scripts2/editressv";

	$qstring=$buffer;

	$data=exec_whm($server_ip, $request, $qstring, $auth);
	
	return 1;
	}


# sends a welcome email to the newly created reseller
function send_reseller_welcome_email($emid, $oid)
	{
	GLOBAL $email_admin;

	# query out template
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='".addslashes(trim($emid))."' ";
	$query0.="limit 0, 1";

	$rs0=mysql_fetch_row(mysql_query($query0));
	$default_email=stripslashes(trim($rs0[2]));
	# -------------------------------------------------

	$query1="select ";
	$query1.="user.first_name, ";					// 0
	$query1.="hosting_order.domain_name, ";			// 1
	$query1.="hosting_order.ip, ";					// 2
	$query1.="hosting_order.whm_username, ";		// 3
	$query1.="hosting_order.whm_password, ";		// 4
	$query1.="plan_specs.package_name, ";			// 5
	$query1.="plan_specs.web_space, ";				// 6
	$query1.="plan_specs.bandwidth, ";				// 7
	$query1.="reseller_profile.limit_type, ";		// 8
	$query1.="reseller_profile.resnumlimitamt, ";	// 9
	$query1.="server_config.server_name, ";			// 10
	$query1.="user.email, ";						// 11
	$query1.="server_config.primary_ns, ";			// 12
	$query1.="server_config.primary_ns_ip, ";		// 13
	$query1.="server_config.secondary_ns, ";		// 14
	$query1.="server_config.secondary_ns_ip, ";		// 15
	$query1.="hosting_order.pns1, ";				// 16
	$query1.="hosting_order.pns2 ";					// 17
	$query1.="from ";
	$query1.="hosting_order, ";
	$query1.="plan_specs, ";
	$query1.="reseller_profile, ";
	$query1.="server_config, ";
	$query1.="user ";
	$query1.="where ";
	$query1.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query1.="and ";
	$query1.="hosting_order.whm_id=server_config.whm_id ";
	$query1.="and ";
	$query1.="hosting_order.pid=plan_specs.pid ";
	$query1.="and ";
	$query1.="plan_specs.rid=reseller_profile.rid ";
	$query1.="and ";
	$query1.="hosting_order.uid=user.uid ";
	$query1.="order by hosting_order.oid ";
	$query1.="asc ";
	$query1.="limit 0, 1";
	
	$rs1=mysql_fetch_row(mysql_query($query1));
	# -------------------------------------------------

	# gather variables
	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$first_name=stripslashes(trim($rs1[0]));
	$domain=stripslashes(trim($rs1[1]));
	$ip=stripslashes(trim($rs1[2]));
	$username=stripslashes(trim($rs1[3]));
	$password=stripslashes(trim($rs1[4]));
	$package=stripslashes(trim($rs1[5]));
	$space=stripslashes(trim($rs1[6]));
	$bandwidth=stripslashes(trim($rs1[7]));
	$limit_type=stripslashes(trim($rs1[8]));
	if ($limit_type==1) { $accounts=stripslashes(trim($rs1[9])); }
	else { $accounts="Not limited by number of resold accounts."; }
	$server_name=stripslashes(trim($rs1[10]));
	$generate_date=date("m/d/Y h:i:s a");
	$email=stripslashes(trim($rs1[11]));
	$primary_nameserver=stripslashes(trim($rs1[12]));
	$primary_nameserver_ip=stripslashes(trim($rs1[13]));
	$secondary_nameserver=stripslashes(trim($rs1[14]));
	$secondary_nameserver_ip=stripslashes(trim($rs1[15]));
	$primary_nameserver_reseller=stripslashes(trim($rs1[16])).$domain;
	$secondary_nameserver_reseller=stripslashes(trim($rs1[17])).$domain;
	$primary_nameserver_ip_reseller=$primary_nameserver_ip;
	$secondary_nameserver_ip_reseller=$secondary_nameserver_ip;

	# Parsed Variables
	# ----------------
	# {{first_name}}
	# {{package}}
	# {{space}}
	# {{bandwidth}}
	# {{accounts}}
	# {{server_name}}
	# {{domain}}
	# {ip}}
	# {{username}}
	# {{password}}
	# {{generate_date}}
	# {{primary_nameserver}}
	# {{primary_nameserver_ip}}
	# {{secondary_nameserver}}
	# {{secondary_nameserver_ip}}
	# {{primary_nameserver_reseller}}
	# {{primary_nameserver_ip_reseller}}
	# {{secondary_nameserver_reseller}}
	# {{secondary_nameserver_ip_reseller}}

	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{package}}", $package, $subject);
	$subject=str_replace("{{space}}", $space, $subject);
	$subject=str_replace("{{bandwidth}}", $bandwidth, $subject);
	$subject=str_replace("{{accounts}}", $accounts, $subject);
	$subject=str_replace("{{server_name}}", $server_name, $subject);
	$subject=str_replace("{{domain}}", $domain, $subject);
	$subject=str_replace("{{ip}}", $ip, $subject);
	$subject=str_replace("{{username}}", $username, $subject);
	$subject=str_replace("{{password}}", $password, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);
	$subject=str_replace("{{primary_nameserver}}", $primary_nameserver, $subject);
	$subject=str_replace("{{primary_nameserver_ip}}", $primary_nameserver_ip, $subject);
	$subject=str_replace("{{secondary_nameserver}}", $secondary_nameserver, $subject);
	$subject=str_replace("{{secondary_nameserver_ip}}", $secondary_nameserver_ip, $subject);
	$subject=str_replace("{{primary_nameserver_reseller}}", $primary_nameserver_reseller, $subject);
	$subject=str_replace("{{primary_nameserver_ip_reseller}}", $primary_nameserver_ip_reseller, $subject);
	$subject=str_replace("{{secondary_nameserver_reseller}}", $secondary_nameserver_reseller, $subject);
	$subject=str_replace("{{secondary_nameserver_ip_reseller}}", $secondary_nameserver_ip_reseller, $subject);

	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{package}}", $package, $message);
	$message=str_replace("{{space}}", $space, $message);
	$message=str_replace("{{bandwidth}}", $bandwidth, $message);
	$message=str_replace("{{accounts}}", $accounts, $message);
	$message=str_replace("{{server_name}}", $server_name, $message);
	$message=str_replace("{{domain}}", $domain, $message);
	$message=str_replace("{{ip}}", $ip, $message);
	$message=str_replace("{{username}}", $username, $message);
	$message=str_replace("{{password}}", $password, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);
	$message=str_replace("{{primary_nameserver}}", $primary_nameserver, $message);
	$message=str_replace("{{primary_nameserver_ip}}", $primary_nameserver_ip, $message);
	$message=str_replace("{{secondary_nameserver}}", $secondary_nameserver, $message);
	$message=str_replace("{{secondary_nameserver_ip}}", $secondary_nameserver_ip, $message);
	$message=str_replace("{{primary_nameserver_reseller}}", $primary_nameserver_reseller, $message);
	$message=str_replace("{{primary_nameserver_ip_reseller}}", $primary_nameserver_ip_reseller, $message);
	$message=str_replace("{{secondary_nameserver_reseller}}", $secondary_nameserver_reseller, $message);
	$message=str_replace("{{secondary_nameserver_ip_reseller}}", $secondary_nameserver_ip_reseller, $message);

	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	mail($email, $subject, $message, $headers);

	if ($which==1)
		{
		$subject.=" - Admin Copy";
		$message.="\n\nThis message is an exact copy of what was sent to the client.";
		mail($email_admin, $subject, $message, $headers);
		}
	}


# ------------------------------------------------------------------------------------------------
# WHM Automation Functions


# Send a dedicated server email
# $which == 0 send to client only
# $which == 1 send to client & admin
function send_dedicated_activation($emid, $oid, $which)
	{
	GLOBAL $email_admin;
	
	$query0="select ";
	$query0.="subject, ";	// 0
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='".addslashes(trim($emid))."' ";
	$query0.="limit 0, 1";

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

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	# -------------------------------------------------

	$query1="select ";
	$query1.="user.first_name, ";				// 0
	$query1.="hosting_order.server_hostname, ";	// 1
	$query1.="hosting_order.domain_name, ";		// 2
	$query1.="plan_specs.os, ";					// 3
	$query1.="hosting_order.ip, ";				// 4
	$query1.="hosting_order.root_pw, ";			// 5
	$query1.="user.email, ";					// 6
	$query1.="hosting_order.server_specs, ";	// 7
	$query1.="hosting_order.ipblock, ";			// 8
	$query1.="hosting_order.rack ";				// 9
	$query1.="from ";
	$query1.="user, hosting_order, plan_specs ";
	$query1.="where ";
	$query1.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query1.="and ";
	$query1.="hosting_order.uid=user.uid ";
	$query1.="and ";
	$query1.="hosting_order.pid=plan_specs.pid ";
	$query1.="order by hosting_order.oid asc ";
	$query1.="limit 0, 1";
	
	$rs1=mysql_fetch_row(mysql_query($query1));

	$first_name=stripslashes(trim($rs1[0]));
	$server_hostname=stripslashes(trim($rs1[1]));
	$domain_name=stripslashes(trim($rs1[2]));
	$dedicated_server_name=$server_hostname.".".$domain_name;
	$dedicated_operating_system=stripslashes(trim($rs1[3]));
	$dedicated_main_ip=stripslashes(trim($rs1[4]));
	$dedicated_root_password=stripslashes(trim($rs1[5]));
	$email=stripslashes(trim($rs1[6]));
	$dedicated_server_specs=stripslashes(trim($rs1[7]));
	$dedicated_ipblock_range=stripslashes(trim($rs1[8]));
	$dedicated_rack_location=stripslashes(trim($rs1[9]));
	# -------------------------------------------------

	# {{first_name}}
	# {{dedicated_root_password}}
	# {{dedicated_main_ip}}
	# {{dedicated_operating_system}}
	# {{dedicated_server_name}}
	# {{dedicated_server_specs}}
	# {{dedicated_ipblock_range}}
	# {{dedicated_rack_location}}

	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{dedicated_root_password}}", $dedicated_root_password, $subject);
	$subject=str_replace("{{dedicated_main_ip}}", $dedicated_main_ip, $subject);
	$subject=str_replace("{{dedicated_operating_system}}", $dedicated_operating_system, $subject);
	$subject=str_replace("{{dedicated_server_name}}", $dedicated_server_name, $subject);
	$subject=str_replace("{{dedicated_server_specs}}", $dedicated_server_specs, $subject);
	$subject=str_replace("{{dedicated_ipblock_range}}", $dedicated_ipblock_range, $subject);
	$subject=str_replace("{{dedicated_rack_location}}", $dedicated_rack_location, $subject);

	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{dedicated_root_password}}", $dedicated_root_password, $message);
	$message=str_replace("{{dedicated_main_ip}}", $dedicated_main_ip, $message);
	$message=str_replace("{{dedicated_operating_system}}", $dedicated_operating_system, $message);
	$message=str_replace("{{dedicated_server_name}}", $dedicated_server_name, $message);
	$message=str_replace("{{dedicated_server_specs}}", $dedicated_server_specs, $message);
	$message=str_replace("{{dedicated_ipblock_range}}", $dedicated_ipblock_range, $message);
	$message=str_replace("{{dedicated_rack_location}}", $dedicated_rack_location, $message);
	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	mail($email, $subject, $message, $headers);

	if ($which==1)
		{
		$subject.=" - Admin Copy";
		$message.="\n\nThis message is an exact copy of what was sent to the client.";
		mail($email_admin, $subject, $message, $headers);
		}

	return 1;
	}

# clears cache - straight from the manual
function clear_cache()
	{
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");				// Date in the past
	header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");		// always modified
	header("Cache-Control: no-store, no-cache, must-revalidate");	// HTTP/1.1
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");										// HTTP/1.0
	}

// find next due date
function create_next_due($og, $payment_term)
	{
	$hour=date("h", $og);
	$min=date("i", $og);
	$sec=date("s", $og);
	$month=date("m", $og);
	$day=date("d", $og);
	$year=date("Y", $og);
	
	$data=mktime($hour, $min, $sec, ($month+$payment_term), $day, $year);

	return $data;
	}

// return next due date
# which == 0 == return timestamp from todays date
# which == 1 == use hosting order ogcreate to build next due date
function find_next_due($payment_term, $oid, $which)
	{
	$payment_term=strtolower(stripslashes(trim($payment_term)));
	$payment_term=str_replace("-", "_", $payment_term);

	if ($payment_term=="monthly") { $payment_term="1"; }
	else if ($payment_term=="quarterly") { $payment_term="3"; }
	else if ($payment_term=="semi_annual") { $payment_term="6"; }
	else if ($payment_term=="annual") { $payment_term="12"; }
	
	if ($which==0)
		{
		return "1|".mktime(date("h"), date("i"), date("s"), (date("m")+$payment_term), date("d"), date("Y"))."|";
		}
	else if ($which==1)
		{
		$query="select ";
		$query.="next_due_date, ";
		# $query.="UNIX_TIMESTAMP(ogcreate), ";
		$query.="ogcreate, ";
		$query.="domain_name ";
		$query.="from ";
		$query.="hosting_order ";
		$query.="where ";
		$query.="oid='".addslashes(trim($oid))."'";

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

		$next_due_date=stripslashes(trim($rs[0]));
		$og=stripslashes(trim($rs[1]));
		$domain_name=stripslashes(trim($rs[2]));

		if ($next_due_date==""||$next_due_date=="---")
			{
			$i=0;
			$x=0;
			while ($i==0)
				{
				if ($x==24) { return "0|".$domain_name."|"; }
				else if ($x==0)
					{
					$next_due_date=create_next_due($og, $payment_term);
					if ($next_due_date>time()) { return "1|".$next_due_date."|"; }
					$x=1;
					}
				else
					{
					$next_due_date=create_next_due($next_due_date, $payment_term);
					if ($next_due_date>time()) { return "1|".$next_due_date."|"; }
					$x+=1;
					}
				}
			}
		else
			{
			$hour=date("h", $next_due_date);
			$min=date("i", $next_due_date);
			$sec=date("s", $next_due_date);
			$month=date("m", $next_due_date);
			$day=date("d", $next_due_date);
			$year=date("Y", $next_due_date);
			
			return "1|".mktime($hour, $min, $sec, ($month+$payment_term), $day, $year)."|";
			}
		}
	}

// checks addon_specs to see if any of the
// addon_choices is a dedicated IP
function unique_ip_ordered($addon_choices)
	{
	$count=substr_count($addon_choices, "|");
	$explode=explode("|", $addon_choices);
	for ($i=0; $i<=$count; $i++) 
		{ 
		$rs1=mysql_fetch_row(mysql_query("select unique_ip from addon_specs where aid='".addslashes(trim($explode[$i]))."'"));
		$unique_ip=stripslashes(trim($rs1[0])); 

		if ($unique_ip==1) { return 1; }
		else if ($unique_ip==0) { return 0; }
		}
	}

// checks addon_specs to see if any of the
// addon_choices is a dedicated IP
function display_addons_ordered_email($addon_choices)
	{
	$count=substr_count($addon_choices, "|");
	$explode=explode("|", $addon_choices);
	for ($i=0; $i<=$count; $i++) 
		{ 
		$rs1=mysql_fetch_row(mysql_query("select addon_name, extra_webspace, unique_ip from addon_specs where aid='".addslashes(trim($explode[$i]))."'"));

		$unique_ip=stripslashes(trim($rs1[2])); 
		if ($unique_ip==1) 
			{ 
			$bit=true; 
			}
		else if ($rs1[1]!=0)
			{
			$extra_webspace+=stripslashes(trim($rs1[1])); 
			}
		else if (trim($rs1[0])!="")
			{
			@$addons.=stripslashes(trim($rs1[0]))."\n";
			}
		}
	if (isset($bit)) { $addons.="Unique IP\n"; }
	if ($rs1[1]!=0) { $addons.=$extra_webspace."MB of extra webspace\n"; }

	return $addons;
	}

// checks addon_specs to see if any of the
// addon_choices is extra webspace
function extra_webspace_ordered($addon_choices)
	{
	$count=substr_count($addon_choices, "|");
	$explode=explode("|", $addon_choices);
	for ($i=0; $i<=$count; $i++) 
		{ 
		$rs1=mysql_fetch_row(mysql_query("select extra_webspace from addon_specs where aid='".addslashes(trim($explode[$i]))."'"));
		$extra_webspace+=stripslashes(trim($rs1[0])); 
		}

	if ($extra_webspace==0) { return 0; }
	else if ($extra_webspace!=0) { return $extra_webspace; }
	}

// grab the shortened news form mib
function grab_mib_news($below_public, $sid, $http_admin)
	{
	$news=file($below_public."/mib_news_cache.nfo");

	// Read each line into an array
	foreach($news as $line_num => $line) 
		{
		if ($line_num==0)
			{
			$mib_news=$line."<br>";
			}
		else
			{
			$mib_news.=$line."<br>";
			}
		}
	$mib_news=@trim($mib_news);
	@list($keep, $not)=@split("{{stop_here}}", $mib_news);

	$keep.="<br>Quick Links:";
	$keep.="<br>---> <a href='tno://www.whmautopilot.com/bugs' target='_blank'>Report A Bug</a>";
	$keep.="<br>---> <a href='tno://www.whmautopilot.com/support' target='_blank'>Contact AP Support</a>";
	$keep.="<br>---> <a href='tno://www.whmautopilot.com/forum' target='_blank'>Visit AP support forums</a>";
	$keep.="<br>---> <a href='".$http_admin."/mib_all_news.php?sid=".trim($sid)."'>Read All Script News & Updates</a>";

	return $keep;
	}

// check to see if whm access is root
function current_server_root($current_server)
	{
	$rs=mysql_fetch_row(mysql_query("select reseller from server_config where whm_id='".addslashes(trim($current_server))."'"));
	
	return $rs[0];
	}

// grab list of installed cpanel themes
function list_reseller_plans($server_ip, $whm_username, $whm_password) 
	{
	$filename="http://".$whm_username.":".$whm_password."@".$server_ip.":2086/scripts2/wwwacctform";
	
	// Read in file.
	$lines = file($filename);

	// Read each line into an array
	foreach ($lines as $line_num => $line) 
		{
		if ($x==1)
			{
			$x=2;
			}
		if (ereg("<tr background=\"/themes/blue/bg.gif\"  class=cellheader><td>Account Type</td><td>Current</td><td>Max Allowed</td><td>Remaining</td></tr>",$line)==true)
			{
			$x=1;
			}
		if ($x==2)
			{
			$buffer=strpos($line, "</td>");
			$current=substr($line, 0, $buffer);
			if (strlen(trim($current))!=0)
				{
				$output.=str_replace("<tr><td>", "", $current)."|";
				}
			}
		}
	return $output;
	}

// clean out all of the bad parts of a domain name
function create_clean_domain_name($domain_name)
	{
	// Prepare the domain name
	$domain_name=strtolower($domain_name);
	$domain_name=str_replace("http://", "", $domain_name);
	$domain_name=str_replace("www.", "", $domain_name);
	$domain_name=str_replace(" ", "", $domain_name);
	$domain_name=str_replace("/", "", $domain_name);
	$domain_name=str_replace("'", "", $domain_name);
	
	if (substr_count($domain_name, ".")>0)
		{
		$ext=strstr($domain_name, ".");
		$domain=substr($domain_name, 0, (strlen($domain_name)-strlen($ext)));
		
		if (ereg("-", substr($domain_name, 0, 1))==true) { return -1; }
		if (ereg("-", substr($domain_name, -1))==true) { return -2; }
		if (ereg("_", $domain_name)==true) { return -3; }
		
		return $domain."|".$ext."|"; 
		}
	else 
		{ 
		if (ereg("-", substr($domain_name, 0, 1))==true) { return -1; }
		if (ereg("-", substr($domain_name, -1))==true) { return -2; }
		if (ereg("_", $domain_name)==true) { return -3; }
		
		return $domain_name."||"; 
		}
	}
	

// create the whm client password
function create_whm_username($domain) 
	{ 
	$domain=substr(preg_replace("/[^A-Za-z0-9]/", chr(rand(97,122)), $domain), 0, 7);

	if (ereg("[0-9]", substr($domain, 0, 1))==true) { return str_replace(substr($domain, 0, 1), "x", $domain); }
	else { return $domain; }
	}

// encrypts data from plain text
function e($key, $data) 
	{
	$data=trim($data);

	$data=$data."abcd";

	// create a seed
	$iv=substr(md5($key), 0, mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB));

	// encrypt the data  
	$enc_data=mcrypt_encrypt(MCRYPT_CAST_256, $key, $data, "cfb", $iv);

	return trim(chop(base64_encode($enc_data)));
	}

// decrypts data from encrypt_data() function
function d($key, $enc_data) 
	{
	// decode first
	$enc_data=trim(chop(base64_decode($enc_data)));

	// extract the seed 
	$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));

	// decrypt the data
	$dec_data = mcrypt_decrypt (MCRYPT_CAST_256, $key, $enc_data, "cfb", $iv);
	
	if (strcmp(substr($dec_data, -4), "abcd")==0) { $dec_data=substr($dec_data, 0, (strlen($dec_data)-4)); }
	else if (strcmp(substr($dec_data, -3), "abc")==0) { $dec_data=substr($dec_data, 0, (strlen($dec_data)-3)); }

	return trim(chop($dec_data));
	}

// define servers
function define_servers($whm_id, $whm_username, $whm_password, $below_public)
	{
	// find the server role from db
	$rs=mysql_fetch_row(mysql_query("select active_server from server_config where whm_id='".addslashes(trim($whm_id))."'"));
	
	if ($rs[0]==1) { $server_role=1; } else { $server_role=0; }

	$data=$whm_id."|".$server_role."|".$whm_username."|".preg_replace("'(\r|\n)'", "", $whm_password);
	$data=e("AutoPilotV2_rulez", $data);

	// does not exist - write it.
	$read = fopen ($below_public."/".trim($whm_id).".log", "w");
	$result = fwrite ($read, $data);
	fclose ($read);

	chmod($below_public."/".trim($whm_id).".log", 0777);
	}

// read in the un/pw for the whm acct
function read_log($whm_id, $below_public)
	{
	$read = @fopen ($below_public."/".trim($whm_id).".log", "r");
	$result = @fread ($read, filesize($below_public."/".trim($whm_id).".log"));
	@fclose ($read);
	
	$data=d("AutoPilotV2_rulez", $result);

	$read = fopen ($below_public."/".trim($whm_id)."_tmp.log", "w");
	$result = fwrite ($read, $data);
	fclose ($read);

	// read in log file
	$read_old_log=file($below_public."/".trim($whm_id)."_tmp.log");

	// parse log file
	foreach ($read_old_log as $line_num => $line) 
		{
		// split line into variables
		list($xwhm_id, $server_role, $whm_username, $whm_password)=split("[|]", trim($line));
	
		// see if this is the current record
		if ($xwhm_id==$whm_id)
			{
			unlink($below_public."/".trim($whm_id)."_tmp.log");

			return $whm_username."|".chunk_split($whm_password, 32, "\n");
			}
		}
	}

// read in the un/pw for the whm acct
function delete_server($whm_id, $below_public)
	{
	unlink($below_public."/".$whm_id.".log");
	}

// get the current ns
function current_ns()
	{
	$ct=mysql_fetch_row(mysql_query("select count(*) from server_config where active_server='1'"));
	if ($ct[0]<=0)
		{
		$ct0==mysql_fetch_row(mysql_query("select count(*) from server_config where manual_install_active='1'"));
		if ($ct0[0]<=0)
			{
			return 99;
			}
		else
			{
			$rs=mysql_fetch_row(mysql_query("select primary_ns, primary_ns_ip, secondary_ns, secondary_ns_ip from server_config where manual_install_active='1'"));
			return $rs[0]."|".$rs[1]."|".$rs[2]."|".$rs[3];
			}
		}
	else
		{
		$rs=mysql_fetch_row(mysql_query("select primary_ns, primary_ns_ip, secondary_ns, secondary_ns_ip from server_config where active_server='1'"));
		return $rs[0]."|".$rs[1]."|".$rs[2]."|".$rs[3];
		}
	}
/*
// return the server id in which to install on
// $which == 0 == called in admin area
// $which == 1 == called from the public system
function current_server($identifier, $which)
	{
	if ($which==0)
		{
		$r=mysql_fetch_row(mysql_query("select whm_id from plan_specs where pid='".addslashes(trim($identifier))."'"));
		}
	else if ($which==1)
		{
		$r=mysql_fetch_row(mysql_query("select plan_specs.whm_id from session_history, plan_specs where session_history.sid='".addslashes(trim($identifier))."' and session_history.pid=plan_specs.pid"));
		}

	if ($r[0]==-1)
		{
		return 99;
		}
	else if ($r[0]==0)
		{
		return current_server_internal();
		}
	else
		{
		return $r[0];
		}
	}
*/

// return the server id in which to install on
// $which == 0 == called in admin area
// $which == 1 == called from the public system
function current_server($identifier, $which)
	{
	if ($which==0)
		{
		$r=mysql_fetch_row(mysql_query("select whm_id from plan_specs where pid='".addslashes(trim($identifier))."'"));
		}
	else if ($which==1)
		{
		#$r=mysql_fetch_row(mysql_query("select whm_id from session_history where sid='".addslashes(trim($identifier))."'"));
		#if ($r[0]==0)
		#	{
			$r=mysql_fetch_row(mysql_query("select plan_specs.whm_id from session_history, plan_specs where session_history.sid='".addslashes(trim($identifier))."' and session_history.pid=plan_specs.pid"));
		#	}
		}

	if ($r[0]==-1)
		{
		return 99;
		}
	else if ($r[0]==0)
		{
		return current_server_internal();
		#$s=mysql_fetch_row(mysql_query("select "));
		#return 99;
		}
	else
		{
		return $r[0];
		}
	}

// get the active/manual/installed server
function current_server_internal()
	{
	$ct=mysql_fetch_row(mysql_query("select count(*) from server_config where active_server='1'"));
	if ($ct[0]<=0)
		{
		/*
		$ct0=mysql_fetch_row(mysql_query("select count(*) from server_config where manual_install_active='1'"));
		if ($ct0[0]<=0)
			{
			return 99;
			}
		else
			{
			$rs=mysql_fetch_row(mysql_query("select whm_id from server_config where manual_install_active='1'"));
			return $rs[0];
			}
		*/
		return 99;
		}
	else
		{
		$rs=mysql_fetch_row(mysql_query("select whm_id from server_config where active_server='1'"));
		return $rs[0];
		}
	}

// Auto server switch functions
// return 0 for not switched
// return new active/manual whm_id for successful switch
function auto_switch_server($below_public, $email_admin)
	{
	$ct=mysql_fetch_row(mysql_query("select count(*) from server_config where install_order='1'"));
	
	if ($ct[0]<=0) { return 0; }

	// is server switch enabled?
	if ($ct[0]==1)
		{
		// get the current active/manual server whm_id
		$current_server=current_server_internal();

		# added 8/27/2003
		if ($current_server==0) { return 0; }
		# end added 8/27/2003

		// pull in the server IP
		$rs=mysql_fetch_row(mysql_query("select server_ip, max_accounts from server_config where whm_id='".addslashes(trim($current_server))."'"));
		$server_ip=$rs[0];
		$max_accounts=$rs[1];

		// read in logins
		$temp=@read_log($current_server, $below_public);
		list($whm_username, $whm_password)=split("[|]", $temp);

		// grab the total accounts installed on the server
		$total_accounts_on_server=list_accounts($server_ip, $whm_username, $whm_password, 1); 

		// evaluate the situation
		if ($total_accounts_on_server>=$max_accounts)
			{
			// grab current active/manual server details
			$rs0=mysql_fetch_row(mysql_query("select whm_id, ogcreate, active_server, manual_install_active, server_name from server_config where whm_id='".addslashes(trim($current_server))."'"));

			// grab current active/manual server details
			$rs1=mysql_fetch_row(mysql_query("select whm_id, server_name from server_config where install_order='1'"));

			// set all servers even
			$rowx=mysql_query("select whm_id, install_order from server_config");
			while ($rx=mysql_fetch_row($rowx))
				{
				if ($rx[1]!=2)
					{
					mysql_query("update server_config set active_server='0', manual_install_active='0', install_server='0' where whm_id='".addslashes(trim($rx[0]))."'");
					}
				}

			// update the new server with old config
			mysql_query("update server_config set active_server='".addslashes(trim($rs0[2]))."', manual_install_active='".addslashes(trim($rs0[3]))."', ogcreate='".addslashes(trim($rs0[1]))."', install_order='0' where whm_id='".addslashes(trim($rs1[0]))."'");

			// update the old server as full
			mysql_query("update server_config set active_server='0', manual_install_active='0', ogcreate='".$rs0[1]."', install_order='2' where whm_id='".addslashes(trim($current_server))."'");

			if ($rs0[2]==1)
				{
				$server_role="Active Install";
				}
			else if ($rs0[3]==1)
				{
				$server_role="Manual Install";
				}
			
			// update packages with new whm_id, added on 3/24/2003
			mysql_query("update plan_specs set whm_id='".addslashes(trim($rs1[0]))."' where whm_id='".addslashes(trim($rs0[0]))."'");

			send_successful_server_switch($email_admin, $max_accounts, $rs0[4], $rs1[1], $server_role);
			}

		return $rs1[0];
		}
	else
		{
		// not enabled
		
		// get the current active/manual server whm_id
		$current_server=current_server_internal();
		
		# added 8/27/2003
		if ($current_server==0) { return 0; }
		# end added 8/27/2003

		// pull in the server IP
		$rs=mysql_fetch_row(mysql_query("select server_ip, max_accounts, server_name from server_config where whm_id='".addslashes(trim($current_server))."'"));
		$server_ip=$rs[0];
		$max_accounts=$rs[1];
		$server_name=$rs[2];

		// read in logins
		$temp=@read_log($current_server, $below_public);
		list($whm_username, $whm_password)=split("[|]", $temp);

		// grab the total accounts installed on the server
		$total_accounts_on_server=list_accounts($server_ip, $whm_username, $whm_password, 1);

		if ($total_accounts_on_server>=$max_accounts)
			{
			send_max_accounts_reached($email_admin, $max_accounts, $server_name);
			}

		return 0;
		}
	}

// Start Domain Expire Check ------------------------------------------------------------------
	
// send the 30 day expire domain notice
function domain_expire_30_days($oid, $email_admin)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='20'"));
	$default_email=stripslashes(trim($rs[2]));

	$rs1=mysql_fetch_row(mysql_query("select hosting_order.domain_name, hosting_order.domain_expire, user.email from hosting_order, user where hosting_order.oid='".addslashes(trim($oid))."' and hosting_order.uid=user.uid order by hosting_order.oid asc limit 0, 1"));

	// DEFINE VARs
	$domain_name="http://".$rs1[0];
	$expire_date=$rs1[1];
	$generate_date=date("m/d/Y h:i:s a");
	$client_email=$rs1[2];

	// ATTRIBUTES AVAILABLE
	// {{domain_name}}
	// {{expire_date}}
	// {{generate_date}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{expire_date}}", $expire_date, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{expire_date}}", $expire_date, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($client_email, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}
	
// send the 5 day expire domain notice
function domain_expire_5_days($oid, $email_admin)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='21'"));
	$default_email=stripslashes(trim($rs[2]));

	$rs1=mysql_fetch_row(mysql_query("select hosting_order.domain_name, hosting_order.domain_expire, user.email from hosting_order, user where hosting_order.oid='".addslashes(trim($oid))."' and hosting_order.uid=user.uid order by hosting_order.oid asc limit 0, 1"));

	// DEFINE VARs
	$domain_name="http://".$rs1[0];
	$expire_date=$rs1[1];
	$generate_date=date("m/d/Y h:i:s a");
	$client_email=$rs1[2];

	// ATTRIBUTES AVAILABLE
	// {{domain_name}}
	// {{expire_date}}
	// {{generate_date}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{expire_date}}", $expire_date, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{expire_date}}", $expire_date, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($client_email, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}
	
// send the expired domain notice
function domain_expired($oid, $email_admin)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='22'"));
	$default_email=stripslashes(trim($rs[2]));

	$rs1=mysql_fetch_row(mysql_query("select hosting_order.domain_name, hosting_order.domain_expire, user.email from hosting_order, user where hosting_order.oid='".addslashes(trim($oid))."' and hosting_order.uid=user.uid order by hosting_order.oid asc limit 0, 1"));

	// DEFINE VARs
	$domain_name="http://".$rs1[0];
	$expire_date=$rs1[1];
	$generate_date=date("m/d/Y h:i:s a");
	$client_email=$rs1[2];

	// ATTRIBUTES AVAILABLE
	// {{domain_name}}
	// {{expire_date}}
	// {{generate_date}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{expire_date}}", $expire_date, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{expire_date}}", $expire_date, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($client_email, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}

	
// send the expired domain notice
function domain_expire_date_error($oid, $email_admin)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='23'"));
	$default_email=stripslashes(trim($rs[2]));

	$rs1=mysql_fetch_row(mysql_query("select hosting_order.domain_name, hosting_order.domain_expire, user.email from hosting_order, user where hosting_order.oid='".addslashes(trim($oid))."' and hosting_order.uid=user.uid order by hosting_order.oid asc limit 0, 1"));

	// DEFINE VARs
	$domain_name="http://".$rs1[0];
	$expire_date=$rs1[1];
	$generate_date=date("m/d/Y h:i:s a");
	$client_email=$rs1[2];

	// ATTRIBUTES AVAILABLE
	// {{domain_name}}
	// {{expire_date}}
	// {{generate_date}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{expire_date}}", $expire_date, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{expire_date}}", $expire_date, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($client_email, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}

function resolve_config_settings()
	{
	$query="select ";
	$query.="resolve_works ";
	$query.="from ";
	$query.="config ";
	$query.="limit 0, 1";

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

	$resolve_works=stripslashes(trim($rs[0]));

	return $resolve_works;
	}

function resolve_monitor()
	{
	$query="select ";
	$query.="cont_resolve ";
	$query.="from ";
	$query.="config ";
	$query.="limit 0, 1";

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

	$cont_resolve=stripslashes(trim($rs[0]));

	return $cont_resolve;
	}

function resolve_server()
	{
	$query="select ";
	$query.="resolve_server ";
	$query.="from ";
	$query.="config ";
	$query.="limit 0, 1";

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

	$resolve_server=stripslashes(trim($rs[0]));

	return $resolve_server;
	}

function domain_expire_check()
	{
	$query="select ";
	$query.="run_domain_expire ";
	$query.="from ";
	$query.="config ";
	$query.="limit 0, 1";

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

	$run_domain_expire=stripslashes(trim($rs[0]));

	return $run_domain_expire;
	}

function resolver_nslookup2ip($domain_name, $resolve_server)
	{
	GLOBAL $resolver_log;

	@exec("nslookup -type=A ".$domain_name." ".$resolve_server, $output);

	$buffer="";
	foreach ($output as $line) { $buffer.=$line; }
	unset($output);
	$returned_=$buffer;
	
	write_resolver_log($domain_name, $resolver_log, $buffer);
	
	$buffer=substr($buffer, -21);
	$buffer=trim($buffer);
	list($junk, $ip)=split("[ ]", $buffer);
	
	if (preg_match("/[A-za-z]/", $ip)) { return "0|".$returned_; }
	else { return $ip; }
	}

function resolver_gethostbyname2ip($domain_name)
	{
	$results=gethostbyname($domain_name);

	if ($domain_name==$results) { return 0; }
	else { return $results; }
	}

function write_resolver_log($domain_name, $resolver_log, $buffer)
	{
	$data="[";
	$data.=date("m/d/Y h:i:s a");
	$data.="][";
	$data.=$domain_name;
	$data.="][";
	$data.=str_replace("	", "", $buffer);
	$data.="]\n";

	$filename=date("m-d-y").".txt";

	$read=@fopen($resolver_log."/".$filename, "a+");
	@fwrite($read, $data);
	@fclose($read);
	@chmod($resolver_log."/".$filename, 0777);
	}

function mark_as_resolved($oid)
	{
	$query="update ";
	$query.="hosting_order ";
	$query.="set ";
	$query.="resolved='1', ";
	$query.="resolved_time='".time()."', ";
	$query.="ur_date='', ";
	$query.="ur_lock='0' ";
	$query.="where ";
	$query.="oid='".addslashes(trim($oid))."'";

	mysql_query($query);
	}

function set_unresolved_lock($oid)
	{
	$query="update ";
	$query.="hosting_order ";
	$query.="set ";
	$query.="ur_lock='1', ";
	$query.="ur_date='".time()."' ";
	$query.="where ";
	$query.="oid='".addslashes(trim($oid))."'";

	mysql_query($query);
	}

function get_expired_lock($oid)
	{
	$query="select  ";
	$query.="domain_lock ";
	$query.="from ";
	$query.="hosting_order ";
	$query.="where ";
	$query.="oid='".addslashes(trim($oid))."'";

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

	return $rs[0];
	}

function set_domain_lock($oid, $int)
	{
	$query="update ";
	$query.="hosting_order ";
	$query.="set ";
	$query.="domain_lock='".addslashes(trim($int))."' ";
	$query.="where ";
	$query.="oid='".addslashes(trim($oid))."'";

	mysql_query($query);
	}

function update_domain_due_date($oid, $domain_expire)
	{
	$m=date("m", $domain_expire);
	$d=date("d", $domain_expire);
	$y=date("y", $domain_expire);
	$new_expire=mktime(0, 0, 0, $m, $d, ($y+1));
	$new_expire=date("m/d/Y", $new_expire);

	$query="update ";
	$query.="hosting_order ";
	$query.="set ";
	$query.="domain_expire='".$new_expire."' ";
	$query.="where ";
	$query.="oid='".addslashes(trim($oid))."'";

	mysql_query($query);
	}

function resolver($email_admin)
	{
	$resolve_config_settings=resolve_config_settings();
	$resolve_server=resolve_server();
	$resolve_monitor=resolve_monitor();
	$reports_resolved="";
	$reports_monitor="";
	$reports_unresolved="";
	$report="";
	$domain_expire_30="";
	$domain_expire_5="";
	$domain_expired="";

	$query0="select ";
	$query0.="hosting_order.domain_name, ";		// 0
	$query0.="user.first_name, ";				// 1
	$query0.="user.last_name, ";				// 2
	$query0.="user.email, ";					// 3
	$query0.="hosting_order.domain_expire, ";	// 4
	$query0.="hosting_order.resolved, ";		// 5
	$query0.="hosting_order.ip, ";				// 6
	$query0.="hosting_order.ur_lock, ";			// 7
	$query0.="hosting_order.ur_date, ";			// 8
	$query0.="hosting_order.oid, ";				// 9
	$query0.="hosting_order.pid, ";				// 10
	$query0.="hosting_order.ogcreate ";			// 11
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.status='1'";
	$query0.="and ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="order by hosting_order.domain_name asc";
	
	$row0=mysql_query($query0);
	while ($rs0=mysql_fetch_row($row0))
		{
		$domain_name=stripslashes(trim($rs0[0]));
		$first_name=stripslashes(trim($rs0[1]));
		$last_name=stripslashes(trim($rs0[2]));
		$email=stripslashes(trim($rs0[3]));
		$domain_expire=stripslashes(trim($rs0[4]));
		list($m, $d, $y)=split("/", $domain_expire);
		$domain_expire=mktime(0, 0, 0, $m, $d, $y);
		$resolved=stripslashes(trim($rs0[5]));
		$account_ip=stripslashes(trim($rs0[6]));
		$ur_lock=stripslashes(trim($rs0[7]));
		$ur_date=stripslashes(trim($rs0[8]));
		$oid=stripslashes(trim($rs0[9]));
		$domain_lock=get_expired_lock($oid);
		$pid=stripslashes(trim($rs0[10]));

		$query3="select ";
		$query3.="dedicated ";
		$query3.="from ";
		$query3.="plan_specs ";
		$query3.="where ";
		$query3.="pid='".addslashes(trim($pid))."'";

		$rs3=mysql_fetch_row(mysql_query($query3));

		if ($rs3[0]==0)
			{
			if ($resolve_config_settings==0&&$resolved==0) { mark_as_resolved($oid); $resolved_inital=true; }
			else if ($resolved==0)
				{
				# get look results
				if ($resolve_config_settings==1) 
					{ 
					$xlookup_ip=resolver_nslookup2ip($domain_name, $resolve_server); 
					list($lookup_ip, $nslookup_results)=explode("|", $xlookup_ip);
					}
				else { $lookup_ip=resolver_gethostbyname2ip($domain_name); }
				
				# evalutate look results
				if ($lookup_ip==0) { $unresolved=true; }
				else 
					{ 
					if ($account_ip==$lookup_ip) { mark_as_resolved($oid); $logically_resolved=true; } 
					else { $unresolved=true; }
					}
				}
			else if ($resolve_monitor==1)
				{
				# get look results
				if ($resolve_config_settings==1) 
					{ 
					$xlookup_ip=resolver_nslookup2ip($domain_name, $resolve_server); 
					list($lookup_ip, $nslookup_results)=explode("|", $xlookup_ip);
					}
				else { $lookup_ip=resolver_gethostbyname2ip($domain_name); }

				# evalutate look results
				if ($lookup_ip==0) { $unresolved_monitor=true; }
				else 
					{ 
					if ($account_ip==$lookup_ip) { mark_as_resolved($oid); } 
					else { $unresolved_monitor=true; }
					}
				}

				# send emails, build reports!
				$seed=$domain_name." - Owner: ".$first_name." ".$last_name.", ".$email." - Installed on: ".date("m/d/Y", $rs0[11])."\n";
				if (isset($nslookup_results))
					{
					if (trim($nslookup_results)!="") { $seed.="[Lookup Results ] [".$nslookup_results."]\n\n"; }
					else { $seed.="[No Lookup Results]\n\n";}
					}
				else { $seed.="\n"; }

				if (isset($resolved_inital))
					{
					send_successfully_resolved_email($oid, $email_admin);
					unset($resolved_inital);

					$reports_resolved.=$seed."|";
					$reports_resolved_ct+=1;
					}
				else if (isset($logically_resolved))
					{
					send_successfully_resolved_email($oid, $email_admin);
					unset($logically_resolved);

					$reports_resolved.=$seed."|";
					$reports_resolved_ct+=1;
					}
				else if (isset($unresolved_monitor))
					{
					# add in timing
					if ($ur_lock==0) { set_unresolved_lock($oid); $send_=true;  }

					if (isset($send_))
						{
						send_admin_notice_domain_stopped_responding($oid, $email_admin);
						unset($send_);
						}

					unset($unresolved_monitor);

					$reports_monitor.=$seed."|";
					$reports_monitor_ct+=1;
					}
				else if (isset($unresolved))
					{
					# add in timing
					if ($ur_lock==0) { set_unresolved_lock($oid); $send_=true;  }
					else { if (($ur_date+172800)<time()) { $send_=true; } }

					if (isset($send_))
						{
						send_unresolved_email($oid, $email_admin);
						unset($send_);
						}

					unset($unresolved);

					$reports_unresolved.=$seed."|";
					$reports_unresolved_ct+=1;
					}

			# expire reports
			if (domain_expire_check()=="yes")
				{
				if (checkdate(date("m", $domain_expire), date("d", $domain_expire), date("Y", $domain_expire))==true)
					{
					# echo date("m/d/Y")."<br>";
					# echo date("m/d/Y", $domain_expire)."<br>"; die;
					if ($domain_expire<=time()) # expired
						{
						if ($domain_lock==5) 
							{
							domain_expired($oid, $email_admin);
							set_domain_lock($oid, 0);
							update_domain_due_date($oid, $domain_expire);

							$domain_expired=$seed."|";
							$domain_expired_ct+=1;
							}
						}

					if (($domain_expire-432000)<time()&&$domain_expire>time()) # expires in 5 days
						{
						if ($domain_lock==30) 
							{
							domain_expire_5_days($oid, $email_admin);
							set_domain_lock($oid, 5);

							$domain_expire_5=$seed."|";
							$domain_expire_5_ct+=1;
							}
						}
					
					if (($domain_expire-2592000)<time()&&$domain_expire>time()) # expires in a month
						{
						if ($domain_lock==0) 
							{
							domain_expire_30_days($oid, $email_admin);
							set_domain_lock($oid, 30);

							$domain_expire_30=$seed."|";
							$domain_expire_30_ct+=1;
							}
						}
					}
				else { domain_expire_date_error($oid, $email_admin); }
				}

			unset($seed);
			if (isset($skip)) { unset($skip); }
			}
		}

	# build reports
	if (trim($reports_resolved)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$reports_resolved; 
		$report.=$reports_resolved_ct."{";
		}

	if (trim($reports_unresolved)=="") { $report.="None for this run.|0{"; }
	else 
		{
		$report.=$reports_unresolved; 
		$report.=$reports_unresolved_ct."{";
		}

	if (trim($reports_monitor)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$reports_monitor; 
		$report.=$reports_monitor_ct."{";
		}

	if (trim($domain_expire_30)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$domain_expire_30; 
		$report.=$domain_expire_30_ct."{";
		}

	if (trim($domain_expire_5)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$domain_expire_5; 
		$report.=$domain_expire_5_ct."{";
		}

	if (trim($domain_expired)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$domain_expired; 
		$report.=$domain_expired_ct."{";
		}

	return $report;
	}

function resolver_individual($email_admin, $individual)
	{
	$resolve_config_settings=resolve_config_settings();
	$resolve_server=resolve_server();
	$resolve_monitor=resolve_monitor();
	$reports_resolved="";
	$reports_monitor="";
	$reports_unresolved="";
	$report="";
	$domain_expire_30="";
	$domain_expire_5="";
	$domain_expired="";

	$query0="select ";
	$query0.="hosting_order.domain_name, ";		// 0
	$query0.="user.first_name, ";				// 1
	$query0.="user.last_name, ";				// 2
	$query0.="user.email, ";					// 3
	$query0.="hosting_order.domain_expire, ";	// 4
	$query0.="hosting_order.resolved, ";		// 5
	$query0.="hosting_order.ip, ";				// 6
	$query0.="hosting_order.ur_lock, ";			// 7
	$query0.="hosting_order.ur_date, ";			// 8
	$query0.="hosting_order.oid ";				// 9
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.oid='".addslashes(trim($individual))."'";
	$query0.="and ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="order by hosting_order.domain_name asc";
	
	$rs0=mysql_fetch_row(mysql_query($query0));

	$domain_name=stripslashes(trim($rs0[0]));
	$first_name=stripslashes(trim($rs0[1]));
	$last_name=stripslashes(trim($rs0[2]));
	$email=stripslashes(trim($rs0[3]));
	$domain_expire=stripslashes(trim($rs0[4]));
	list($m, $d, $y)=split("/", $domain_expire);
	$domain_expire=mktime(0, 0, 0, $m, $d, $y);
	$resolved=stripslashes(trim($rs0[5]));
	$account_ip=stripslashes(trim($rs0[6]));
	$ur_lock=stripslashes(trim($rs0[7]));
	$ur_date=stripslashes(trim($rs0[8]));
	$oid=stripslashes(trim($rs0[9]));
	$domain_lock=get_expired_lock($oid);
	
	if ($resolve_config_settings==0&&$resolved==0) { mark_as_resolved($oid); $resolved_inital=true; }
	else if ($resolved==0)
		{
		# get look results
		if ($resolve_config_settings==1) { $lookup_ip=resolver_nslookup2ip($domain_name, $resolve_server); }
		else { $lookup_ip=resolver_gethostbyname2ip($domain_name); }
		
		# evalutate look results
		if ($lookup_ip==0) { $unresolved=true; }
		else 
			{ 
			if ($account_ip==$lookup_ip) { mark_as_resolved($oid); $logically_resolved=true; } 
			else { $unresolved=true; }
			}
		}
	else if ($resolve_monitor==1)
		{
		# get look results
		if ($resolve_config_settings==1) { $lookup_ip=resolver_nslookup2ip($domain_name, $resolve_server); }
		else { $lookup_ip=resolver_gethostbyname2ip($domain_name); }

		# evalutate look results
		if ($lookup_ip==0) { $unresolved_monitor=true; }
		else 
			{ 
			if ($account_ip==$lookup_ip) { mark_as_resolved($oid); } 
			else { $unresolved_monitor=true; }
			}
		}

	# send emails, build reports!
	$seed=$domain_name." - Owner: ".$first_name." ".$last_name.", ".$email." - Expires on: ".date("m/d/Y", $domain_expire)."\n";

	if (isset($resolved_inital))
		{
		send_successfully_resolved_email($oid, $email_admin);
		unset($resolved_inital);

		$reports_resolved.=$seed."|";
		$reports_resolved_ct+=1;
		}
	else if (isset($logically_resolved))
		{
		send_successfully_resolved_email($oid, $email_admin);
		unset($logically_resolved);

		$reports_resolved.=$seed."|";
		$reports_resolved_ct+=1;
		}
	else if (isset($unresolved_monitor))
		{
		# add in timing
		if ($ur_lock==0) { set_unresolved_lock($oid); $send_=true;  }

		if (isset($send_))
			{
			send_admin_notice_domain_stopped_responding($oid, $email_admin);
			unset($send_);
			}

		unset($unresolved_monitor);

		$reports_monitor.=$seed."|";
		$reports_monitor_ct+=1;
		}
	else if (isset($unresolved))
		{
		# add in timing
		if ($ur_lock==0) { set_unresolved_lock($oid); $send_=true;  }
		else { if (($ur_date+172800)<time()) { $send_=true; } }

		if (isset($send_))
			{
			send_unresolved_email($oid, $email_admin);
			unset($send_);
			}

		unset($unresolved);

		$reports_unresolved.=$seed."|";
		$reports_unresolved_ct+=1;
		}


	# expire reports
	if (domain_expire_check()=="yes")
		{
		if (checkdate(date("m", $domain_expire), date("d", $domain_expire), date("Y", $domain_expire))==true)
			{
			# echo date("m/d/Y")."<br>";
			# echo date("m/d/Y", $domain_expire)."<br>"; die;
			if ($domain_expire<=time()) # expired
				{
				if ($domain_lock==5) 
					{
					domain_expired($oid, $email_admin);
					set_domain_lock($oid, 0);
					update_domain_due_date($oid, $domain_expire);

					$domain_expired=$seed."|";
					$domain_expired_ct+=1;
					}
				}

			if (($domain_expire-432000)<time()&&$domain_expire>time()) # expires in 5 days
				{
				if ($domain_lock==30) 
					{
					domain_expire_5_days($oid, $email_admin);
					set_domain_lock($oid, 5);

					$domain_expire_5=$seed."|";
					$domain_expire_5_ct+=1;
					}
				}
			
			if (($domain_expire-2592000)<time()&&$domain_expire>time()) # expires in a month
				{
				if ($domain_lock==0) 
					{
					domain_expire_30_days($oid, $email_admin);
					set_domain_lock($oid, 30);

					$domain_expire_30=$seed."|";
					$domain_expire_30_ct+=1;
					}
				}
			}
		else { domain_expire_date_error($oid, $email_admin); }
		}

	unset($seed);

	# build reports
	if (trim($reports_resolved)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$reports_resolved; 
		$report.=$reports_resolved_ct."{";
		}

	if (trim($reports_unresolved)=="") { $report.="None for this run.|0{"; }
	else 
		{
		$report.=$reports_unresolved; 
		$report.=$reports_unresolved_ct."{";
		}

	if (trim($reports_monitor)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$reports_monitor; 
		$report.=$reports_monitor_ct."{";
		}

	if (trim($domain_expire_30)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$domain_expire_30; 
		$report.=$domain_expire_30_ct."{";
		}

	if (trim($domain_expire_5)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$domain_expire_5; 
		$report.=$domain_expire_5_ct."{";
		}

	if (trim($domain_expired)=="") { $report.="None for this run.|0{"; }
	else 
		{ 
		$report.=$domain_expired; 
		$report.=$domain_expired_ct."{";
		}

	return $report;
	}


// Display the resolver report
function send_newly_added_server_notification($email_admin, $server_name, $server_ip, $primary_ns, $primary_ns_ip, $secondary_ns, $secondary_ns_ip, $max_accounts)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='5'"));
	$default_email=stripslashes(trim($rs[2]));

	// DEFINE VARs
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE
	// {{server_name}}
	// {{server_ip}}
	// {{primary_ns}}
	// {{primary_ns_ip}}
	// {{secondary_ns}}
	// {{secondary_ns_ip}}
	// {{max_accounts}}
	// {{generate_date}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{server_name}}", $server_name, $subject);
	$subject=str_replace("{{server_ip}}", $server_ip, $subject);
	$subject=str_replace("{{primary_ns}}", $primary_ns, $subject);
	$subject=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $subject);
	$subject=str_replace("{{secondary_ns}}", $secondary_ns, $subject);
	$subject=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $subject);
	$subject=str_replace("{{max_accounts}}", $max_accounts, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{server_name}}", $server_name, $message);
	$message=str_replace("{{server_ip}}", $server_ip, $message);
	$message=str_replace("{{primary_ns}}", $primary_ns, $message);
	$message=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $message);
	$message=str_replace("{{secondary_ns}}", $secondary_ns, $message);
	$message=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $message);
	$message=str_replace("{{max_accounts}}", $max_accounts, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}
	
// Display the resolver report
function send_admin_max_accounts_reached($max_clients, $active_server_name, $email_admin)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='6'"));
	$default_email=stripslashes(trim($rs[2]));

	// DEFINE VARs
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE
	// {{max_clients}}
	// {{active_server_name}}
	// {{generate_date}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{max_clients}}", $max_clients, $subject);
	$subject=str_replace("{{active_server_name}}", $active_server_name, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{max_clients}}", $max_clients, $message);
	$message=str_replace("{{active_server_name}}", $active_server_name, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}

// End Resolver Functions --------------------------------------------------------------------

// check to see if we have exceeded our max account limit
function check_max_account_status($email_admin, $below_public)
	{
	$current_server=current_server_internal();
	if ($current_server!=99)
		{
		$rs=mysql_fetch_row(mysql_query("select max_accounts, server_name, server_ip from server_config where whm_id='".addslashes($current_server)."'"));
		
		// read in logins
		$temp=@read_log($current_server, $below_public);
		list($xwhm_username, $xwhm_password)=split("[|]", $temp);
		
		$server_ip=$rs[2];

		$installed_accounts=list_accounts($server_ip, $xwhm_username, $xwhm_password, 1); 
		$max_clients=$rs[0];
		$active_server_name=$rs[1];
		
		if ($installed_accounts>=$max_clients)
			{
			send_admin_max_accounts_reached($max_clients, $active_server_name, $email_admin);

			return 1;
			}
		else
			{
			return 0;
			}
		}
	else
		{
		return 0;
		}
	}

// Install a new hosting account notification to user
function new_account_installed($email_admin, $oid, $currency, $currency_type, $addon_choices)
	{	
	$query="select ";
	$query.="user.first_name, ";						// 0
	$query.="user.last_name, ";							// 1
	$query.="hosting_order.domain_name, ";				// 2
	# $query.="UNIX_TIMESTAMP(hosting_order.ogcreate), ";	// 3
	$query.="hosting_order.ogcreate, ";	// 3
	$query.="hosting_order.pid, ";						// 4
	$query.="hosting_order.addon_choices, ";			// 5
	$query.="hosting_order.payment_method, ";			// 6
	$query.="hosting_order.payment_term, ";				// 7
	$query.="hosting_order.total_due_today, ";			// 8
	$query.="hosting_order.total_due_reoccur, ";		// 9
	$query.="hosting_order.whm_id, ";					// 10
	$query.="hosting_order.domain_registration,  ";		// 11
	# added 6/20/2003 ---------------------------------------------
	$query.="user.advert, ";							// 12
	$query.="user.advert_other, ";						// 13
	$query.="hosting_order.promotion_code,  ";			// 14
	$query.="user.email,  ";							// 15
	$query.="hosting_order.pns1,  ";					// 16
	$query.="hosting_order.pns2,  ";					// 17
	$query.="hosting_order.client_notes  ";				// 18
	# added 6/20/2003 ---------------------------------------------
	$query.="from user, hosting_order ";
	$query.="where user.uid=hosting_order.uid ";
	$query.="and hosting_order.oid='".addslashes(trim($oid))."' ";
	$query.="order by user.uid asc";

	$rs1=mysql_fetch_row(mysql_query($query)); 
	
	$pns1="Private Nameserver Prefix 1:".stripslashes(trim($rs1[16]));
	$pns2="Private Nameserver Prefix 2:".stripslashes(trim($rs1[17]));
	$client_notes="Client Notes:\n".stripslashes(trim($rs1[18]));

	# added 6/20/2003 ---------------------------------------------
	$advert=stripslashes(trim($rs1[12]));
	$advert_other=stripslashes(trim($rs1[13]));
	$how_found=$advert."".((trim($advert_other)!="")?": ".$advert_other."":"")."";
	$promotion_code=stripslashes(trim($rs1[14]));
	if (trim($promotion_code)!="") { $coupon="Used Promotion Code / Coupon: ".$promotion_code; }
	else { $coupon=""; }
	$email_address=stripslashes(trim($rs1[15]));
	# added 6/20/2003 ---------------------------------------------

	$first_name=stripslashes(trim($rs1[0]));
	$last_name=stripslashes(trim($rs1[1]));
	$domain_name=stripslashes(trim($rs1[2]));
	$ogcreate=stripslashes(trim($rs1[3]));
	$pid=stripslashes(trim($rs1[4]));
	$addon_choices=stripslashes(trim($rs1[5]));
	$payment_method=stripslashes(trim($rs1[6]));
	$payment_term=stripslashes(trim($rs1[7]));
	if (trim(strtolower($payment_term))=="monthly") { $payment_term="per Month"; }
	else if (trim(strtolower($payment_term))=="quarterly") { $payment_term="per Quarter"; }
	else if (trim(strtolower($payment_term))=="semi-annual") { $payment_term="Semi-Annually"; }
	else if (trim(strtolower($payment_term))=="annually") { $payment_term="Annually"; }

	$total_due_today=stripslashes(trim($rs1[8]));
	$total_due_reoccur=stripslashes(trim($rs1[9]));
	$whm_id=stripslashes(trim($rs1[10]));
	$domain_registration=stripslashes(trim($rs1[11]));

	// added rockwell 3/27/2003
	// PULL IN TEMPLATE
	
	$rs0x=mysql_fetch_row(mysql_query("select default_email from payment_process where pid='".addslashes(trim($payment_method))."'"));
	
	if (trim($rs0x[0])!="")
		{
		list($sub, $body)=split("{{break}}", $rs0x[0]);
		$subject=base64_decode($sub);
		$message=base64_decode($body);
		}
	else
		{
		$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='7'"));
		$subject=stripslashes(trim($rs[0]));
		$message=stripslashes(trim($rs[1]));
		$default_email=stripslashes(trim($rs[2]));
		}
	// end added rockwell 3/27/2003

	// build addons array
	$addons="Addons Ordered:\n";
	$addons.=display_addons_ordered_email($addon_choices);
	if ($domain_registration!=0) { $addons.="Domain Registration"; }
	else if (str_replace("Addons Ordered:\n", "", $addons)=="") { $addons.=" - No account addons ordered"; }

	// get pay method
	$rs3=mysql_fetch_row(mysql_query("select name from payment_process where pid='".addslashes(trim($payment_method))."'"));
	
	if ($whm_id!=0)
		{
		// get server name
		$rs4=mysql_fetch_row(mysql_query("select server_name from server_config where whm_id='".addslashes(trim($whm_id))."'"));
		$active_server_name="was installed onto ".stripslashes(trim($rs4[0]));
		}
	else
		{
		$active_server_name="was created as a pending account.";
		}

	$queryx="select ";
	$queryx.="package_name ";
	$queryx.="from plan_specs ";
	$queryx.="where pid='".addslashes(trim($pid))."'";
	$rs2=mysql_fetch_row(mysql_query($queryx));

	// DEFINE VARs
	$order_date=date("m/d/Y", $ogcreate);
	$hosting_package=stripslashes(trim($rs2[0]));
	$payment_gateway=stripslashes(trim($rs3[0]));
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE

	// {{first_name}}
	// {{last_name}}
	// {{domain_name}}
	// {{order_date}}
	// {{hosting_package}}
	// {{addons}}
	// {{payment_gateway}}
	// {{total_due_today}}
	// {{total_reoccur}}
	// {{payment_term}}
	// {{active_server_name}}
	// {{generate_date}}

	// {{how_found}}
	// {{email_address}}
	// {{coupon}}

	// {{pns1}}
	// {{pns2}}
	// {{client_notes}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=str_replace("{{how_found}}", $how_found, $subject);
	$subject=str_replace("{{email_address}}", $email_address, $subject);
	$subject=str_replace("{{pns1}}", $pns1, $subject);
	$subject=str_replace("{{pns2}}", $pns2, $subject);
	$subject=str_replace("{{client_notes}}", $client_notes, $subject);
	$subject=str_replace("{{coupon}}", $coupon, $subject);
	$subject=str_replace("{{order_date}}", $order_date, $subject);
	$subject=str_replace("{{active_server_name}}", $active_server_name, $subject);
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{last_name}}", $last_name, $subject);
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{hosting_package}}", $hosting_package, $subject);
	$subject=str_replace("{{addons}}", $addons, $subject);
	$subject=str_replace("{{payment_gateway}}", $payment_gateway, $subject);
	$subject=str_replace("{{total_due_today}}", $total_due_today, $subject);
	$subject=str_replace("{{total_reoccur}}", $total_due_reoccur, $subject);
	$subject=str_replace("{{payment_term}}", $payment_term, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=str_replace("{{how_found}}", $how_found, $message);
	$message=str_replace("{{email_address}}", $email_address, $message);
	$message=str_replace("{{pns1}}", $pns1, $message);
	$message=str_replace("{{pns2}}", $pns2, $message);
	$message=str_replace("{{client_notes}}", $client_notes, $message);
	$message=str_replace("{{coupon}}", $coupon, $message);
	$message=str_replace("{{order_date}}", $order_date, $message);
	$message=str_replace("{{active_server_name}}", $active_server_name, $message);
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{last_name}}", $last_name, $message);
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{hosting_package}}", $hosting_package, $message);
	$message=str_replace("{{addons}}", $addons, $message);
	$message=str_replace("{{payment_gateway}}", $payment_gateway, $message);
	$message=str_replace("{{total_due_today}}", $total_due_today, $message);
	$message=str_replace("{{total_reoccur}}", $total_due_reoccur, $message);
	$message=str_replace("{{payment_term}}", $payment_term, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}

// Send order received successfully notification
function new_order_received($email_admin, $sid, $currency, $currency_type, $addon_choices)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='8'"));
	$default_email=stripslashes(trim($rs[2]));
	
	// build query
	$query="select ";
	$query.="first_name, ";			// 0
	$query.="last_name, ";			// 1
	$query.="domain_name, ";		// 2
	$query.="pid, ";				// 3
	$query.="addon_choices, ";		// 4
	$query.="payment_method, ";		// 5
	$query.="payment_term, ";		// 6
	$query.="total_due_today, ";	// 7
	$query.="total_due_reoccur, ";	// 8
	$query.="email, ";				// 9
	$query.="advert, ";				// 10
	$query.="advert_other, ";		// 11
	$query.="promotion_code, ";		// 12
	$query.="pns1, ";				// 13
	$query.="pns2, ";				// 14
	$query.="client_notes ";		// 15
	$query.="from session_history ";
	$query.="where sid='".trim($sid)."'";

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

	$pns1=stripslashes(trim($rs1[13]));
	$pns2=stripslashes(trim($rs1[14]));
	$client_notes=stripslashes(trim($rs1[15]));
	
	$first_name=stripslashes(trim($rs1[0]));
	$last_name=stripslashes(trim($rs1[1]));
	$domain_name=stripslashes(trim($rs1[2]));
	$pid=stripslashes(trim($rs1[3]));
	$addon_choices=stripslashes(trim($rs1[4]));
	$payment_method=stripslashes(trim($rs1[5]));
	$payment_term=stripslashes(trim($rs1[6]));
	$total_due_today=stripslashes(trim($rs1[7]));
	$total_due_reoccur=stripslashes(trim($rs1[8]));
	$email_address=stripslashes(trim($rs1[9]));
	$how_found=stripslashes(trim($rs1[10]));
	$advert_other=stripslashes(trim($rs1[11]));
	$promotion_code=stripslashes(trim($rs1[12]));
	if (trim($promotion_code)!="") { $coupon="Used Promotion Code / Coupon: ".$promotion_code; }
	else { $coupon=""; }
	if (trim($advert_other)!="") { $how_found.="\n -- ".$advert_other; }

	// build package query
	$queryx="select ";
	$queryx.="package_name ";
	$queryx.="from plan_specs ";
	$queryx.="where pid='".addslashes(trim($pid))."'";
	$rs2=mysql_fetch_row(mysql_query($queryx));
	
	// build addons array
	$addons="Addons Ordered:\n";
	$addons.=display_addons_ordered_email($addon_choices);
	if ($domain_registration!=0) { $addons.="Domain Registration"; }
	else if (str_replace("Addons Ordered:\n", "", $addons)=="") { $addons.=" - No account addons ordered"; }

	// get pay method
	$rs3=mysql_fetch_row(mysql_query("select name from payment_process where pid='".addslashes(trim($payment_method))."'"));
	
	// DEFINE VARs
	$hosting_package=stripslashes(trim($rs2[0]));
	$payment_gateway=stripslashes(trim($rs3[0]));
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE

	// {{first_name}}
	// {{last_name}}
	// {{domain_name}}
	// {{hosting_package}}
	// {{addons}}
	// {{payment_gateway}}
	// {{total_due_today}}
	// {{total_reoccur}}
	// {{payment_term}}
	// {{generate_date}}
	// {{email_address}}
	// {{how_found}}
	// {{coupon}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{coupon}}", $coupon, $subject);
	$subject=str_replace("{{pns1}}", $pns1, $subject);
	$subject=str_replace("{{pns2}}", $pns2, $subject);
	$subject=str_replace("{{client_notes}}", $client_notes, $subject);
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{email_address}}", $email_address, $subject);
	$subject=str_replace("{{how_found}}", $how_found, $subject);
	$subject=str_replace("{{last_name}}", $last_name, $subject);
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{hosting_package}}", $hosting_package, $subject);
	$subject=str_replace("{{addons}}", $addons, $subject);
	$subject=str_replace("{{payment_gateway}}", $payment_gateway, $subject);
	$subject=str_replace("{{total_due_today}}", $total_due_today, $subject);
	$subject=str_replace("{{total_reoccur}}", $total_due_reoccur, $subject);
	$subject=str_replace("{{payment_term}}", $payment_term, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{coupon}}", $coupon, $message);
	$message=str_replace("{{pns1}}", $pns1, $message);
	$message=str_replace("{{pns2}}", $pns2, $message);
	$message=str_replace("{{client_notes}}", $client_notes, $message);
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{email_address}}", $email_address, $message);
	$message=str_replace("{{how_found}}", $how_found, $message);
	$message=str_replace("{{last_name}}", $last_name, $message);
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{hosting_package}}", $hosting_package, $message);
	$message=str_replace("{{addons}}", $addons, $message);
	$message=str_replace("{{payment_gateway}}", $payment_gateway, $message);
	$message=str_replace("{{total_due_today}}", $total_due_today, $message);
	$message=str_replace("{{total_reoccur}}", $total_due_reoccur, $message);
	$message=str_replace("{{payment_term}}", $payment_term, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);
	
	mail($email_address, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());

	$subject.=" - Admin Copy";

	$message.="\n\n-------------------------------------------------------------";
	$message.="\n\nThis is a copy of the message that was sent to the client.\n";
	$message.="We have just sent the user off to make payment.\n";
	$message.="You will get a \"completed order\" email when they come back from making payment, if all goes well.\n";

	mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}
	
// send email to client for DNS config
function send_dns_config($email_admin, $oid)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='9'"));
	$default_email=stripslashes(trim($rs[2]));
	
	// build query
	$query="select ";
	$query.="hosting_order.domain_name, ";	// 0
	$query.="hosting_order.whm_id, ";		// 1
	$query.="user.email ";					// 2
	$query.="from user, hosting_order ";
	$query.="where user.uid=hosting_order.uid ";
	$query.="and hosting_order.oid='".trim($oid)."' ";
	$query.="order by user.uid asc";

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

	// build package query
	$query1="select ";
	$query1.="primary_ns, ";
	$query1.="primary_ns_ip, ";
	$query1.="secondary_ns, ";
	$query1.="secondary_ns_ip ";
	$query1.="from server_config ";
	$query1.="where whm_id='".addslashes(trim($rs1[1]))."'";

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

	// DEFINE VARs
	$domain_name=stripslashes(trim($rs1[0]));
	$primary_ns=stripslashes(trim($rs2[0]));
	$primary_ns_ip=stripslashes(trim($rs2[1]));
	$secondary_ns=stripslashes(trim($rs2[2]));
	$secondary_ns_ip=stripslashes(trim($rs2[3]));
	$user_email=stripslashes(trim($rs1[2]));
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE
	// {{domain_name}}
	// {{primary_ns}}
	// {{primary_ns_ip}}
	// {{secondary_ns}}
	// {{secondary_ns_ip}}
	// {{user_email}}
	// {{generate_date}}

	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{primary_ns}}", $primary_ns, $subject);
	$subject=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $subject);
	$subject=str_replace("{{secondary_ns}}", $secondary_ns, $subject);
	$subject=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{primary_ns}}", $primary_ns, $message);
	$message=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $message);
	$message=str_replace("{{secondary_ns}}", $secondary_ns, $message);
	$message=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	mail($user_email, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}
/*
// Send out newly installed account welcome email
function send_welcome_email_ip($email_admin, $oid, $unique_ip_ordered)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='10'"));
	$default_email=stripslashes(trim($rs[2]));
	
	// build query
	$query="select ";
	$query.="hosting_order.domain_name, ";				// 0
	$query.="hosting_order.ip, ";						// 1
	$query.="hosting_order.whm_username, ";				// 2
	$query.="hosting_order.whm_password, ";				// 3
	$query.="user.email, ";								// 4
	$query.="hosting_order.whm_id ";					// 5
	$query.="from user, hosting_order ";
	$query.="where user.uid=hosting_order.uid ";
	$query.="and hosting_order.oid='".addslashes(trim($oid))."' ";
	$query.="order by user.uid asc";

	$rs1=mysql_fetch_row(mysql_query($query));
	
	$whm_id=stripslashes(trim($rs1[5]));

	$dns=mysql_fetch_row(mysql_query("select primary_ns, primary_ns_ip, secondary_ns, secondary_ns_ip from server_config where whm_id='".addslashes(trim($whm_id))."'"));
	
	$primary_ns=$dns[0];
	$primary_ns_ip=$dns[1];
	$secondary_ns=$dns[2];
	$secondary_ns_ip=$dns[3];

	// DEFINE VARs
	$ip=stripslashes(trim($rs1[1]));
	$whm_username=stripslashes(trim($rs1[2]));
	$whm_password=stripslashes(trim($rs1[3]));
	$domain_name=stripslashes(trim($rs1[0]));
	$user_email=stripslashes(trim($rs1[4]));
	$generate_date=date("m/d/Y h:i:s a");
	if ($unique_ip_ordered==1) { $temporary_path="http://".$ip; }
	else { $temporary_path=$ip."/~".$whm_username."/"; }

	// ATTRIBUTES AVAILABLE
	// {{ip}}
	// {{whm_username}}
	// {{whm_password}}
	// {{domain_name}}
	// {{primary_ns}}
	// {{primary_ns_ip}}
	// {{secondary_ns}}
	// {{secondary_ns_ip}}
	// {{generate_date}}
	// {{temporary_path}}


	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{ip}}", $ip, $subject);
	$subject=str_replace("{{whm_username}}", $whm_username, $subject);
	$subject=str_replace("{{whm_password}}", $whm_password, $subject);
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{primary_ns}}", $primary_ns, $subject);
	$subject=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $subject);
	$subject=str_replace("{{secondary_ns}}", $secondary_ns, $subject);
	$subject=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);
	$subject=str_replace("{{temporary_path}}", $temporary_path, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{ip}}", $ip, $message);
	$message=str_replace("{{whm_username}}", $whm_username, $message);
	$message=str_replace("{{whm_password}}", $whm_password, $message);
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{primary_ns}}", $primary_ns, $message);
	$message=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $message);
	$message=str_replace("{{secondary_ns}}", $secondary_ns, $message);
	$message=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);
	$message=str_replace("{{temporary_path}}", $temporary_path, $message);

	mail($user_email, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());

	$subject.=" - Admin Copy";

	$message.="\n\nThis message is an exact copy of what was sent to the client.";

	mail($email_admin, $subject, $message, "From: ".$email_admin."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}
*/

// Send out newly installed account welcome email
function send_welcome_email_ip($email_admin, $oid, $unique_ip_ordered)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='10'"));
	$default_email=stripslashes(trim($rs[2]));
	
	// build query
	$query="select ";
	$query.="hosting_order.domain_name, ";				// 0
	$query.="hosting_order.ip, ";						// 1
	$query.="hosting_order.whm_username, ";				// 2
	$query.="hosting_order.whm_password, ";				// 3
	$query.="user.email, ";								// 4
	$query.="hosting_order.whm_id, ";					// 5
	$query.="user.username, ";							// 6
	$query.="user.temp_pw, ";							// 7
	$query.="config.http_web ";						// 8
	$query.="from user, hosting_order, config ";
	$query.="where user.uid=hosting_order.uid ";
	$query.="and hosting_order.oid='".addslashes(trim($oid))."' ";
	$query.="order by user.uid asc";

	$rs1=mysql_fetch_row(mysql_query($query));
	
	$whm_id=stripslashes(trim($rs1[5]));

	$dns=mysql_fetch_row(mysql_query("select primary_ns, primary_ns_ip, secondary_ns, secondary_ns_ip from server_config where whm_id='".addslashes(trim($whm_id))."'"));
	
	$primary_ns=$dns[0];
	$primary_ns_ip=$dns[1];
	$secondary_ns=$dns[2];
	$secondary_ns_ip=$dns[3];

	// DEFINE VARs
	$ip=stripslashes(trim($rs1[1]));
	$whm_username=stripslashes(trim($rs1[2]));
	$whm_password=stripslashes(trim($rs1[3]));
	$domain_name=stripslashes(trim($rs1[0]));
	$user_email=stripslashes(trim($rs1[4]));
	$user_username=stripslashes(trim($rs1[6]));
	$user_temppass=stripslashes(trim($rs1[7]));
	$client_login_temp=stripslashes(trim($rs1[8]));
	$client_login=$client_login_temp."/clogin.php";
	$generate_date=date("m/d/Y h:i:s a");
	if ($unique_ip_ordered==1) { $temporary_path="http://".$ip; }
	else { $temporary_path=$ip."/~".$whm_username."/"; }

	// ATTRIBUTES AVAILABLE
	// {{ip}}
	// {{whm_username}}
	// {{whm_password}}
	// {{domain_name}}
	// {{primary_ns}}
	// {{primary_ns_ip}}
	// {{secondary_ns}}
	// {{secondary_ns_ip}}
	// {{generate_date}}
	// {{temporary_path}}
	// {{clientarea_username}}
	// {{clientarea_password}}
	// {{clientarea_loginlink}}


	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{ip}}", $ip, $subject);
	$subject=str_replace("{{whm_username}}", $whm_username, $subject);
	$subject=str_replace("{{whm_password}}", $whm_password, $subject);
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{primary_ns}}", $primary_ns, $subject);
	$subject=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $subject);
	$subject=str_replace("{{secondary_ns}}", $secondary_ns, $subject);
	$subject=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);
	$subject=str_replace("{{temporary_path}}", $temporary_path, $subject);
	$subject=str_replace("{{clientarea_username}}", $user_username, $subject);
	$subject=str_replace("{{clientarea_password}}", $user_temppass, $subject);
	$subject=str_replace("{{clientarea_loginlink}}", $client_login, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{ip}}", $ip, $message);
	$message=str_replace("{{whm_username}}", $whm_username, $message);
	$message=str_replace("{{whm_password}}", $whm_password, $message);
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{primary_ns}}", $primary_ns, $message);
	$message=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $message);
	$message=str_replace("{{secondary_ns}}", $secondary_ns, $message);
	$message=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);
	$message=str_replace("{{temporary_path}}", $temporary_path, $message);
	$message=str_replace("{{clientarea_username}}", $user_username, $message);
	$message=str_replace("{{clientarea_password}}", $user_temppass, $message);
	$message=str_replace("{{clientarea_loginlink}}", $client_login, $message);

	mail($user_email, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());

	$subject.=" - Admin Copy";

	$message.="\n\nThis message is an exact copy of what was sent to the client.";

	mail($email_admin, $subject, $message, "From: ".$email_admin."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}

// backs up db based on /inc/connect.php
// sends an email with the db backpu
// set to 1 to display output
// set to zero to display nothing for cron, sends email
function dbbackup($email_admin, $server_backup, $dbhost, $dbuser, $dbpass, $db, $http_admin, $http_images, $which)
	{
	// if the dir is not avail, create it.
	if(!is_dir($server_backup)) { mkdir($server_backup, 0777);	}

	// build a timestamp
	$date = date("m-d-Y", time());

	// build a filename
	$filename=$db."_".$date."."."sql";

	// full name to the backup file
	$fullname = $server_backup."/".$filename;

	// build the query
	$dump_cmd = "mysqldump -h".$dbhost." -u".$dbuser." -p".$dbpass." ".$db." > ".$fullname;

	// run the query
	passthru($dump_cmd, $dump_status);

	// build message
	$dump_status="The database ";

	// check status of the query
	if ($dump_status==0)
		{
		$lines=file($fullname);

		// Read each line into an array
		foreach ($lines as $line_num => $line) 
			{
			if (ereg("--", $line)==true)
				{
				if (substr_count($line, "--")==1)
					{
					$result.=str_replace("--", "# --", $line);
					}
				else if (substr_count($line, "--")>=2)
					{
					$result.="# ".$line;
					}
				else
					{
					$result.=$line;
					}
				}
			else
				{
				$result.=$line;
				}
			}
		
		// rewrite the file
		$xread = fopen ($fullname, "w");
		fwrite ($xread, $result);
		fclose ($xread);

		// gzip the file
		$comp_cmd="gzip -f ".$fullname;

		// execute the command
		passthru($comp_cmd, $comp_status);

		// build status
		$dump_status.="was backed up successfully!\n\n";
		
		// was it backed up successfully?
		if ($comp_status==0)
			{
			// change the file permissions
			chmod($fullname.".gz", 0755);

			// display output
			$dump_status.="The backup was stored locally in:\n<img src='".$http_images."/error_arrow.gif'><a href='".$http_admin."/dbbackup/".$filename.".gz'>".$fullname.".gz</a>";
			}
		else
			{
			// change the file permissions
			chmod($fullname, 0755);

			// display output
			$dump_status.="The backup was stored locally in:\n<img src='".$http_images."/error_arrow.gif'><a href='".$http_admin."/dbbackup/".$filename."'>".$fullname."</a>";
			}
		}
	else
		{
		$dump_status.="backup failed!\n\n";
		}

	// display for the web or cron?
	if ($which==1)
		{
		$subject="DB Backup: ".date("m/d/Y h:i:s a");

		$message="Below are the results of the database backup:\n";
		$message.="You should save this email in case of disaster!\n";
		$message.="-------------------------------------------------\n\n\n";
		$message.=$result;

		// mail out db
		mail($email_admin, $subject, $message, "From: ".$email_admin."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());

		// web output
		return nl2br($dump_status);
		}
	else
		{
		// cron output
		$subject="DB Backup: ".date("m/d/Y h:i:s a");

		$message="Below are the results of the database backup:\n";
		$message.="You should save this email in case of disaster!\n";
		$message.="-------------------------------------------------\n\n\n";
		$message.=$result;

		// mail out db
		mail($email_admin, $subject, $message, "From: ".$email_admin."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
		}
	}

// start weekly reports -----------------------------------------------------

// get last ran time for weekly sales
function last_ran_weekly_sales_report($below_public, $which)
	{
	if ($which==1)
		{
		// not there, write it
		if (!file_exists($below_public."/weekly_sales_report.log"))
			{		
			$data=(time()-601200);

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

			chmod($below_public."/weekly_sales_report.log", 0777);

			return $data;
			}
		else
			{
			// read in log file
			$read = fopen ($below_public."/weekly_sales_report.log", "r");
			$result = fread ($read, filesize($below_public."/weekly_sales_report.log"));
			fclose ($read);

			return $result;
			}
		}
	else if ($which==0)
		{		
		$data=time();

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



// Send Weekly Sales Report
function reports_weekly_sales($email_admin, $site_name, $which, $currency, $currency_type)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='29'"));
	$default_email=stripslashes(trim($rs[2]));

	$x=0;
	# $row0=mysql_query("select UNIX_TIMESTAMP(ogcreate), domain_name, pid, total_due_reoccur, payment_term, uid from hosting_order where status='1' order by hosting_order.ogcreate asc");
	$row0=mysql_query("select ogcreate, domain_name, pid, total_due_reoccur, payment_term, uid from hosting_order where status='1' order by hosting_order.ogcreate asc");
	while ($rs0=mysql_fetch_row($row0))
		{
		if (($rs0[0]+604800)>=time())
			{
			$rs1=mysql_fetch_row(mysql_query("select package_name from plan_specs where pid='".addslashes(trim($rs0[2]))."'"));
			$rs2=mysql_fetch_row(mysql_query("select first_name, last_name from user where uid='".addslashes(trim($rs0[5]))."'"));
			if ($x==0)
				{
				$data="1. http://".$rs0[1].", ".$rs2[0]." ".$rs2[1].", ".$rs1[0].", ".$currency."".$rs0[3]."".$currency_type."\n";
				}
			else
				{
				$data.=$x.". http://".$rs0[1].", ".$rs2[0]." ".$rs2[1].", ".$rs1[0].", ".$currency."".$rs0[3]."".$currency_type."\n";
				}
			$x++;
			}
		}

	// DEFINE VARs
	$new_signups=$data;
	$signup_count=$x;
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE
	// {{site_name}}
	// {{generate_date}}
	// {{new_signups}}
	// {{signup_count}}


	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);
	$subject=str_replace("{{site_name}}", $site_name, $subject);
	$subject=str_replace("{{new_signups}}", $new_signups, $subject);
	$subject=str_replace("{{signup_count}}", $signup_count, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{generate_date}}", $generate_date, $message);
	$message=str_replace("{{site_name}}", $site_name, $message);
	$message=str_replace("{{new_signups}}", $new_signups, $message);
	$message=str_replace("{{signup_count}}", $signup_count, $message);

	
	if ($which==99)
		{
		echo nl2br($message);
		}
	else
		{
		mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
		}
	}

// get last ran time for weekly sales
function last_ran_monthly_revenue_report($below_public, $which)
	{
	if ($which==1)
		{
		// not there, write it
		if (!file_exists($below_public."/monthly_revenue_server_sales_report.log"))
			{		
			$data=(time()-2588400);

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

			chmod($below_public."/monthly_revenue_server_sales_report.log", 0777);

			return $data;
			}
		else
			{
			// read in log file
			$read = fopen ($below_public."/monthly_revenue_server_sales_report.log", "r");
			$result = fread ($read, filesize($below_public."/monthly_revenue_server_sales_report.log"));
			fclose ($read);

			return $result;
			}
		}
	else if ($which==0)
		{		
		$data=time();

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

// Send Weekly Sales Report
function reports_monthly_revenue_sales($email_admin, $site_name, $which, $currency, $currency_type)
	{
	GLOBAL $currency;
	GLOBAL $currency_type;

	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='30'"));

	$subject=stripslashes(trim($rs[0]));
	$message=stripslashes(trim($rs[1]));
	$default_email=stripslashes(trim($rs[2]));
	$generate_date=date("m/d/Y h:i:s a");
	
	# -------------------------------------------------------------------------------------------------------------
	
	// loop the server config database
	$return="";
	$query="select ";
	$query.="whm_id, ";
	$query.="server_name ";
	$query.="from ";
	$query.="server_config";

	$row0=mysql_query($query);
	while ($rs0=mysql_fetch_row($row0))
		{
		$query1="select ";
		$query1.="SUM(total_due_reoccur) ";
		$query1.="from ";
		$query1.="hosting_order ";
		$query1.="where ";
		$query1.="payment_term='monthly' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."'";
		$query1.="and ";
		$query1.="status='1' ";
		$query1.="or ";
		$query1.="payment_term='Monthly'";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1'";
		
		$rs1=mysql_fetch_row(mysql_query($query1));

		$monthly=$currency.sprintf("%01.2f", $rs1[0])." ".$currency_type;

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

		$query1="select ";
		$query1.="SUM(total_due_reoccur) ";
		$query1.="from ";
		$query1.="hosting_order ";
		$query1.="where ";
		$query1.="payment_term='quarterly' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."'";
		$query1.="and ";
		$query1.="status='1' ";
		$query1.="or ";
		$query1.="payment_term='Quarterly' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1'";

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

		$quarterly=$currency.sprintf("%01.2f", $rs1[0])." ".$currency_type;

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

		$query1="select ";
		$query1.="SUM(total_due_reoccur) ";
		$query1.="from ";
		$query1.="hosting_order ";
		$query1.="where ";
		$query1.="payment_term='Semi-Annual' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1' ";
		$query1.="or ";
		$query1.="payment_term='semi-annual' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1' ";
		$query1.="or ";
		$query1.="payment_term='semi_annual' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1' ";
		$query1.="or ";
		$query1.="payment_term='Semi_Annual' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1'";
		
		$rs1=mysql_fetch_row(mysql_query($query1));

		$semi_annual=$currency.sprintf("%01.2f", $rs1[0])." ".$currency_type;

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

		$query1="select ";
		$query1.="SUM(total_due_reoccur) ";
		$query1.="from ";
		$query1.="hosting_order ";
		$query1.="where ";
		$query1.="payment_term='annual' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1' ";
		$query1.="or ";
		$query1.="payment_term='Annual' ";
		$query1.="and ";
		$query1.="whm_id='".addslashes(trim($rs0[0]))."' ";
		$query1.="and ";
		$query1.="status='1'";

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

		$annual=$currency.sprintf("%01.2f", $rs1[0])." ".$currency_type;

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

		$return.="Server ".($g+=1)." - ".stripslashes(trim($rs0[1]))." [WHM ID: ".$rs0[0]."]\n";
		$return.="Income:\n";
		$return.="--&gt; ".$monthly." Billing Monthly\n";
		$return.="--&gt; ".$quarterly." Billing Quarterly\n";
		$return.="--&gt; ".$semi_annual." Billing Semi-Annually\n";
		$return.="--&gt; ".$annual." Billing Annually\n\n";

		unset($monthly);
		unset($quarterly);
		unset($semi_annual);
		unset($annual);
		unset($query1);
		unset($rs1);
		}

	// ATTRIBUTES AVAILABLE
	// {{site_name}}
	// {{generate_date}}
	// {{server_details}}


	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($subject));
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);
	$subject=str_replace("{{site_name}}", $site_name, $subject);
	$subject=str_replace("{{server_details}}", $return, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($message));
	$message=str_replace("{{generate_date}}", $generate_date, $message);
	$message=str_replace("{{site_name}}", $site_name, $message);
	$message=str_replace("{{server_details}}", $return, $message);

	
	if ($which==99)
		{
		echo nl2br($message);
		}
	else
		{
		mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
		}
	}

// get last ran time for weekly sales
function last_ran_monthly_new_account_report($below_public, $which)
	{
	if ($which==1)
		{
		// not there, write it
		if (!file_exists($below_public."/monthly_new_account_sales_report.log"))
			{		
			$data=(time()-2588400);

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

			chmod($below_public."/monthly_new_account_sales_report.log", 0777);

			return $data;
			}
		else
			{
			// read in log file
			$read = fopen ($below_public."/monthly_new_account_sales_report.log", "r");
			$result = fread ($read, filesize($below_public."/monthly_new_account_sales_report.log"));
			fclose ($read);

			return $result;
			}
		}
	else if ($which==0)
		{		
		$data=time();

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

// Send Weekly Sales Report
function reports_monthly_new_account($email_admin, $site_name, $which, $currency, $currency_type)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='31'"));
	$default_email=stripslashes(trim($rs[2]));
	
	$x=0;
	# $row0=mysql_query("select UNIX_TIMESTAMP(ogcreate), domain_name, pid, total_due_reoccur, payment_term, uid from hosting_order where status='1' order by hosting_order.ogcreate asc");
	$row0=mysql_query("select ogcreate, domain_name, pid, total_due_reoccur, payment_term, uid from hosting_order where status='1' order by hosting_order.ogcreate asc");
	while ($rs0=mysql_fetch_row($row0))
		{
		$record=date("mY", $rs0[0]);
		$current=date("mY");
		if (strcmp($record, $current)==0)
			{
			$rs1=mysql_fetch_row(mysql_query("select package_name from plan_specs where pid='".addslashes(trim($rs0[2]))."'"));
			$rs2=mysql_fetch_row(mysql_query("select first_name, last_name from user where uid='".addslashes(trim($rs0[5]))."'"));
			if ($x==0)
				{
				$data="1. http://".$rs0[1].", ".$rs2[0]." ".$rs2[1].", ".$rs1[0].", ".$currency."".$rs0[3]."".$currency_type."\n";
				}
			else
				{
				$data.=$x.". http://".$rs0[1].", ".$rs2[0]." ".$rs2[1].", ".$rs1[0].", ".$currency."".$rs0[3]."".$currency_type."\n";
				}
			$x++;
			}
		unset($record);
		}

	// DEFINE VARs
	$generate_date=date("m/d/Y h:i:s a");
	$month=date("F");
	$signup_details=$data;
	$signup_count=$x;

	// ATTRIBUTES AVAILABLE
	// {{site_name}}
	// {{month}}
	// {{signup_count}}
	// {{signup_details}}
	// {{generate_date}}


	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{site_name}}", $site_name, $subject);
	$subject=str_replace("{{month}}", $month, $subject);
	$subject=str_replace("{{signup_count}}", $signup_count, $subject);
	$subject=str_replace("{{signup_details}}", $signup_details, $subject);
	$subject=str_replace("{{server_details}}", $server_details, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{site_name}}", $site_name, $message);
	$message=str_replace("{{month}}", $month, $message);
	$message=str_replace("{{signup_count}}", $signup_count, $message);
	$message=str_replace("{{signup_details}}", $signup_details, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	
	if ($which==99)
		{
		echo nl2br($message);
		}
	else
		{
		mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
		}
	}

// Send notice to admin that the max accounts/server was reached and that the server was switched successfully
function send_successful_server_switch($email_admin, $max_clients, $previous_active_server_name, $new_active_server_name, $server_role)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='6'"));
	$default_email=stripslashes(trim($rs[2]));
	
	// DEFINE VARs
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE
	// {{max_clients}}
	// {{previous_active_server_name}}
	// {{new_active_server_name}}
	// {{server_role}}
	// {{generate_date}}


	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{max_clients}}", $max_clients, $subject);
	$subject=str_replace("{{previous_active_server_name}}", $previous_active_server_name, $subject);
	$subject=str_replace("{{new_active_server_name}}", $new_active_server_name, $subject);
	$subject=str_replace("{{server_role}}", $server_role, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{max_clients}}", $max_clients, $message);
	$message=str_replace("{{previous_active_server_name}}", $previous_active_server_name, $message);
	$message=str_replace("{{new_active_server_name}}", $new_active_server_name, $message);
	$message=str_replace("{{server_role}}", $server_role, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	
	mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}

// Send notice to admin that the max accounts/server was reached
function send_max_accounts_reached($email_admin, $max_accounts, $server_name)
	{
	// PULL IN TEMPLATE
	$rs=mysql_fetch_row(mysql_query("select subject, message, default_email from email_templates where emid='32'"));
	$default_email=stripslashes(trim($rs[2]));
	
	// DEFINE VARs
	$generate_date=date("m/d/Y h:i:s a");

	// ATTRIBUTES AVAILABLE
	// {{max_accounts}}
	// {{server_name}}
	// {{generate_date}}


	// parse out subject
	// this way is inefficient, but works on all OS's
	$subject=stripslashes(trim($rs[0]));
	$subject=str_replace("{{max_accounts}}", $max_accounts, $subject);
	$subject=str_replace("{{server_name}}", $server_name, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);

	// parse out subject
	// this way is inefficient, but works on all OS's
	$message=stripslashes(trim($rs[1]));
	$message=str_replace("{{max_accounts}}", $max_accounts, $message);
	$message=str_replace("{{server_name}}", $server_name, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);

	
	mail($email_admin, $subject, $message, "From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."\nReply-To: ".$email_admin."\nX-Mailer: PHP/" . phpversion());
	}
		
// START FUNCTIONS ------------------------------

// converts ex. Jan to 1
function convert_month($month)
	{
	$month=strtolower($month);
	if ($month=="jan") { return "01"; }
	else if ($month=="feb") { return "02"; }
	else if ($month=="mar") { return "03"; }
	else if ($month=="apr") { return "04"; }
	else if ($month=="may") { return "05"; }
	else if ($month=="jun") { return "06"; }
	else if ($month=="jul") { return "07"; }
	else if ($month=="aug") { return "08"; }
	else if ($month=="sep") { return "09"; }
	else if ($month=="oct") { return "10"; }
	else if ($month=="nov") { return "11"; }
	else if ($month=="dec") { return "12"; }
	}
		
// returns unix timestamp or mysql comp.
// timestamp format
// $which == 0 == unix time
// $which == 1 == mysql time
function display_time($ogcreate, $which)
	{
	$ogcreate=str_replace("  ", " ", $ogcreate);
	list($dayx, $mon, $day, $time, $year)=split(" ", $ogcreate." ");
	
	$mon=convert_month($mon);
	$time=str_replace(":", "", $time);
	
	if ($which==0)
		{
		$ogcreate=mktime(substr($time, 0, 2), substr($time, 2, 2), substr($time, -2), $mon, $day, $year);
		}
	else if ($which==1)
		{
		$ogcreate=trim($year).sprintf("%02.0f", trim($mon)).sprintf("%02.0f", trim($day)).trim($time);
		}
	
	return $ogcreate;
	}

// cleans up record from csv file
function clean_record($line)
	{
	$line=substr($line, 1, strlen(trim($line)));
	$line=str_replace(",,,,,,,,,", ",", $line);
	$line=str_replace(",,,,,,,", ",", $line);
	$line=str_replace(",,,,", ",", $line);
	$line=str_replace(",,", ",", $line);

	return $line;
	}
	
// END FUNCTIONS --------------------------------

// create the phpbb account
// goes anywhere you create a user
function migrate_user_to_phpbb($username, $password, $email)
	{
	$rs=mysql_fetch_row(mysql_query("select uid from user where username='".addslashes(trim($username))."'"));

	mysql_query("insert into phpbb_users set user_id='".addslashes(trim($rs[0]))."', username='".addslashes(trim($username))."', user_password='".addslashes(trim($password))."', user_email='".addslashes(trim($email))."', user_active='1', user_level='0', user_regdate='".time()."'");
	}

// reset the phpbb password
// clogin.php
// elogin.hp
// client_area.php
function reset_phpbb_pw($username, $password)
	{
	mysql_query("update phpbb_users set user_password='".addslashes(trim($password))."' where username='".addslashes(trim($username))."'");
	}

// reset the phpbb email
// client_area.php
function reset_phpbb_email($username, $email)
	{
	mysql_query("update phpbb_users set user_email='".addslashes(trim($email))."' where username='".addslashes(trim($username))."'");
	}

// suspend (ban) the user in phpbb
// /admin/client_view.php
function suspend_phpbb_user($username)
	{
	$rs=mysql_fetch_row(mysql_query("select user_id from phpbb_users where username='".addslashes(trim($username))."'"));
	mysql_query("insert into phpbb_banlist set ban_userid='".addslashes(trim($rs[0]))."'");
	}

// unsuspend (un-ban) the user in phpbb
// /admin/client_view.php
function unsuspend_phpbb_user($username)
	{
	$rs=mysql_fetch_row(mysql_query("select user_id from phpbb_users where username='".addslashes(trim($username))."'"));
	mysql_query("delete from phpbb_banlist where ban_userid='".addslashes(trim($rs[0]))."'");
	}

// delete the user in phpbb
// /admin/client_view.php
function delete_phpbb_user($uid)
	{
	@mysql_query("delete from phpbb_banlist where user_id='".addslashes(trim($uid))."'");
	@mysql_query("delete from phpbb_users where user_id='".addslashes(trim($uid))."'");
	}

// START WHOIS FUNCTIONS ------------------------------------------------------------- //

function get_whois_server($domain)
	{
	$whoisservers=array(array("ac","whois.nic.ac"),array("ac.cn","whois.cnnic.net.cn"),array("ac.jp","whois.nic.ad.jp"),array("ac.uk","whois.ja.net"),array("ad.jp","whois.nic.ad.jp"),array("adm.br","whois.nic.br"),array("adv.br","whois.nic.br"),array("aero","whois.information.aero"),array("ag","whois.nic.ag"),array("agr.br","whois.nic.br"),array("ah.cn","whois.cnnic.net.cn"),array("al","whois.ripe.net"),array("am.br","whois.nic.br"),array("arq.br","whois.nic.br"),array("at","whois.nic.at"),array("au","whois.aunic.net"),array("art.br","whois.nic.br"),array("as","whois.nic.as"),array("asn.au","whois.aunic.net"),array("ato.br","whois.nic.br"),array("be","whois.geektools.com"),array("bg","whois.digsys.bg"),array("bio.br","whois.nic.br"),array("biz","whois.biz"),array("bj.cn","whois.cnnic.net.cn"),array("bmd.br","whois.nic.br"),array("br","whois.registro.br"),array("ca","whois.cira.ca"),array("cc","whois.nic.cc"),array("cd","whois.cd"),array("ch","whois.nic.ch"),array("cim.br","whois.nic.br"),array("ck","whois.ck-nic.org.ck"),array("cl","whois.nic.cl"),array("cn","whois.cnnic.net.cn"),array("cng.br","whois.nic.br"),array("cnt.br","whois.nic.br"),array("com","rs.internic.net"),array("com.au","whois.aunic.net"),array("com.br","whois.nic.br"),array("com.cn","whois.cnnic.net.cn"),array("com.eg","whois.ripe.net"),array("com.hk","whois.hknic.net.hk"),array("com.mx","whois.nic.mx"),array("com.ru","whois.ripn.ru"),array("com.tw","whois.twnic.net"),array("conf.au","whois.aunic.net"),array("co.jp","whois.nic.ad.jp"),array("co.uk","whois.nic.uk"),array("cq.cn","whois.cnnic.net.cn"),array("csiro.au","whois.aunic.net"),array("cx","whois.nic.cx"),array("cz","whois.nic.cz"),array("de","whois.denic.de"),array("dk","whois.dk-hostmaster.dk"),array("ecn.br","whois.nic.br"),array("ee","whois.eenet.ee"),array("edu","rs.internic.net"),array("edu.au","whois.aunic.net"),array("edu.br","whois.nic.br"),array("eg","whois.ripe.net"),array("es","whois.ripe.net"),array("esp.br","whois.nic.br"),array("etc.br","whois.nic.br"),array("eti.br","whois.nic.br"),array("eun.eg","whois.ripe.net"),array("emu.id.au","whois.aunic.net"),array("eng.br","whois.nic.br"),array("far.br","whois.nic.br"),array("fi","whois.ripe.net"),array("fj","whois.usp.ac.fj"),array("fj.cn","whois.cnnic.net.cn"),array("fm.br","whois.nic.br"),array("fnd.br","whois.nic.br"),array("fo","whois.ripe.net"),array("fot.br","whois.nic.br"),array("fst.br","whois.nic.br"),array("fr","whois.nic.fr"),array("g12.br","whois.nic.br"),array("gd.cn","whois.cnnic.net.cn"),array("ge","whois.ripe.net"),array("ggf.br","whois.nic.br"),array("gl","whois.ripe.net"),array("gr","whois.ripe.net"),array("gr.jp","whois.nic.ad.jp"),array("gs","whois.adamsnames.tc"),array("gov","whois.nic.gov"),array("gs.cn","whois.cnnic.net.cn"),array("gov.au","whois.aunic.net"),array("gov.br","whois.nic.br"),array("gov.cn","whois.cnnic.net.cn"),array("gov.hk","whois.hknic.net.hk"),array("gob.mx","whois.nic.mx"),array("gs","whois.adamsnames.tc"),array("gz.cn","whois.cnnic.net.cn"),array("gx.cn","whois.cnnic.net.cn"),array("he.cn","whois.cnnic.net.cn"),array("ha.cn","whois.cnnic.net.cn"),array("hb.cn","whois.cnnic.net.cn"),array("hi.cn","whois.cnnic.net.cn"),array("hl.cn","whois.cnnic.net.cn"),array("hn.cn","whois.cnnic.net.cn"),array("hm","whois.registry.hm"),array("hk","whois.hknic.net.hk"),array("hk.cn","whois.cnnic.net.cn"),array("hu","whois.ripe.net"),array("id.au","whois.aunic.net"),array("ie","whois.domainregistry.ie"),array("ind.br","whois.nic.br"),array("imb.br","whois.nic.br"),array("inf.br","whois.nic.br"),array("info","whois.afilias.info"),array("info.au","whois.aunic.net"),array("it","whois.nic.it"),array("idv.tw","whois.twnic.net"),array("int","whois.iana.org"),array("is","whois.isnic.is"),array("il","whois.isoc.org.il"),array("jl.cn","whois.cnnic.net.cn"),array("jor.br","whois.nic.br"),array("jp","whois.nic.ad.jp"),array("js.cn","whois.cnnic.net.cn"),array("jx.cn","whois.cnnic.net.cn"),array("kr","whois.krnic.net"),array("la","whois.nic.la"),array("lel.br","whois.nic.br"),array("li","whois.nic.ch"),array("lk","whois.nic.lk"),array("ln.cn","whois.cnnic.net.cn"),array("lt","ns.litnet.lt"),array("lu","whois.dns.lu"),array("lv","whois.ripe.net"),array("ltd.uk","whois.nic.uk"),array("mat.br","whois.nic.br"),array("mc","whois.ripe.net"),array("med.br","whois.nic.br"),array("mil","whois.nic.mil"),array("mil.br","whois.nic.br"),array("mn","whois.nic.mn"),array("mo.cn","whois.cnnic.net.cn"),array("ms","whois.adamsnames.tc"),array("mus.br","whois.nic.br"),array("mx","whois.nic.mx"),array("name","whois.nic.name"),array("ne.jp","whois.nic.ad.jp"),array("net","rs.internic.net"),array("net.au","whois.aunic.net"),array("net.br","whois.nic.br"),array("net.cn","whois.cnnic.net.cn"),array("net.eg","whois.ripe.net"),array("net.hk","whois.hknic.net.hk"),array("net.lu","whois.dns.lu"),array("net.mx","whois.nic.mx"),array("net.uk","whois.nic.uk"),array("net.ru","whois.ripn.ru"),array("net.tw","whois.twnic.net"),array("nl","whois.domain-registry.nl"),array("nm.cn","whois.cnnic.net.cn"),array("no","whois.norid.no"),array("nom.br","whois.nic.br"),array("not.br","whois.nic.br"),array("ntr.br","whois.nic.br"),array("nx.cn","whois.cnnic.net.cn"),array("nz","whois.domainz.net.nz"),array("plc.uk","whois.nic.uk"),array("odo.br","whois.nic.br"),array("oop.br","whois.nic.br"),array("or.jp","whois.nic.ad.jp"),array("org","rs.internic.net"),array("org.au","whois.aunic.net"),array("org.br","whois.nic.br"),array("org.cn","whois.cnnic.net.cn"),array("org.hk","whois.hknic.net.hk"),array("org.lu","whois.dns.lu"),array("org.ru","whois.ripn.ru"),array("org.tw","whois.twnic.net"),array("org.uk","whois.nic.uk"),array("pl","nazgul.nask.waw.pl"),array("pp.ru","whois.ripn.ru"),array("ppg.br","whois.nic.br"),array("pro.br","whois.nic.br"),array("psi.br","whois.nic.br"),array("psc.br","whois.nic.br"),array("pt","whois.ripe.net"),array("qh.cn","whois.cnnic.net.cn"),array("qsl.br","whois.nic.br"),array("rec.br","whois.nic.br"),array("ro","whois.rotld.ro"),array("ru","whois.ripn.ru"),array("sc.cn","whois.cnnic.net.cn"),array("sd.cn","whois.cnnic.net.cn"),array("se","whois.nic-se.se"),array("sg","whois.nic.net.sg"),array("sh","whois.nic.sh"),array("sh.cn","whois.cnnic.net.cn"),array("si","whois.arnes.si"),array("sk","whois.ripe.net"),array("slg.br","whois.nic.br"),array("sm","whois.ripe.net"),array("sn.cn","whois.cnnic.net.cn"),array("srv.br","whois.nic.br"),array("st","whois.nic.st"),array("sx.cn","whois.cnnic.net.cn"),array("tc","whois.adamsnames.tc"),array("th","whois.nic.uk"),array("tj.cn","whois.cnnic.net.cn"),array("tmp.br","whois.nic.br"),array("to","whois.tonic.to"),array("tr","whois.ripe.net"),array("trd.br","whois.nic.br"),array("tur.br","whois.nic.br"),array("tv","whois.tv"),array("tv.br","whois.nic.br"),array("tw","whois.twnic.net"),array("tw.cn","whois.cnnic.net.cn"),array("uk","whois.thnic.net"),array("va","whois.ripe.net"),array("vet.br","whois.nic.br"),array("vg","whois.adamsnames.tc"),array("wattle.id.au","whois.aunic.net"),array("ws","whois.worldsite.ws"),array("xj.cn","whois.cnnic.net.cn"),array("xz.cn","whois.cnnic.net.cn"),array("yn.cn","whois.cnnic.net.cn"),array("zlg.br","whois.nic.br"),array("zj.cn","whois.cnnic.net.cn"),array("nu","whois.nic.nu"));
	$whocnt = count($whoisservers);
	for ($x=0;$x<$whocnt;$x++)
		{
		$artld = $whoisservers[$x][0];
		$tldlen = intval(0 - strlen($artld));
		if (substr($domain, $tldlen) == $artld) $whosrv = $whoisservers[$x][1];
		}
	return $whosrv;
	}

function parse_a($xbase, $search)
	{
	$xbase=str_replace($search, "", strtolower($xbase));
	$xbase=substr($xbase, 3, strlen($xbase));
	$m=substr(trim($xbase), 0, 3);
	$d=substr(trim($xbase), 4, 2);
	$y=substr(trim($xbase), 7, 4);
	$m=convert_month($m);

	return $m."|".$d."|".$y."|";
	}

function parse_b($xbase, $search)
	{
	$xbase=str_replace($search, "", strtolower($xbase));
	$xbase=substr($xbase, 0, 8);
	list($m, $d, $y)=split("[/]", $xbase);
	return $m."|".$d."|20".$y."|";
	}

function lookup($dom)
	{
	$lusrv = get_whois_server($dom);
	if (!$lusrv) return "";

	$fp = @fsockopen($lusrv,43);
	@fputs($fp, "$dom\r\n");
	$string="";
	while(@!feof($fp)){
		$string.= @fgets($fp,128);
	}
	@fclose($fp);

	$reg = "/Whois Server: (.*?)\n/i";
	@preg_match_all($reg, $string, $matches);
	$secondtry = $matches[1][0];

	if ($secondtry){
		$fp = @fsockopen($secondtry,43);
		@fputs($fp, "$dom\r\n");
		$string="";
		while(@!feof($fp)){
			$string.=@fgets($fp,128);
		}
		@fclose($fp);
	}	

	return $string;
	}

function whois_parse($base)
	{
	if (ereg("expiration date:", strtolower($base))==true)
		{
		$xbase=stristr(strtolower($base), "expiration date:");
		$xbase=substr($xbase, 0, 35);	

		if (ereg("200", $xbase)==true) { return parse_a($xbase, "expiration date: "); }
		else { return parse_b($xbase, "expiration date: "); }
		}
	else if (ereg("expires:", strtolower($base))==true)
		{
		// State 3: Expires: 11/30/03 00:00:00
		$xbase=stristr(strtolower($base), "expires:");
		$xbase=substr($xbase, 0, 35);	

		if (ereg("200", $xbase)==true) { return parse_a($xbase, "expires: "); }
		else { return parse_b($xbase, "expires: "); }
		}
	else if (ereg("registered through-", strtolower($base))==true)
		{
		$xbase=stristr(strtolower($base), "registered through-");
		$xbase=substr($xbase, 0, 35);	

		if (ereg("200", $xbase)==true) { return parse_a($xbase, "registered through- "); }
		else { return parse_b($xbase, "registered through- ");	}
		}
	}

// returns ns from whois
// which == 0 == return ns only
// which == 1 == return ns+ip
function whois_2_ns($base, $which)
	{
	$tok=strtok($base, "\n");
	while ($tok)
		{
		if (ereg("server", strtolower(trim($tok)))==true)
			{
			$g=1;
			}
		else if ($g==1)
			{
			if ($which==0)
				{
				$buffer=trim($tok)."|";
				}
			else
				{
				$tok=trim($tok);
				$buffer.=gethostbyname($tok)."@".$tok."|";
				}
			$g=2;
			}
		else if ($g==2)
			{
			if ($which==0)
				{
				$buffer.=trim($tok)."|";
				}
			else
				{
				$tok=trim($tok);
				$buffer.=gethostbyname($tok)."@".$tok."|";
				}
			unset($g);
			}

		$tok=strtok("\n");
		}
	
	return $buffer;
	}

// END WHOIS FUNCTIONS --------------------------------------------------------------- //

function domain_reg_notice($oid)
	{
	GLOBAL $email_admin;
	GLOBAL $currency;
	GLOBAL $currency_type;

	# query out template
	$query="select ";
	$query.="subject, ";
	$query.="message, ";
	$query.="default_email ";
	$query.="from ";
	$query.="email_templates ";
	$query.="where ";
	$query.="emid='43' ";
	$query.="limit 0, 1";
	
	$rs=mysql_fetch_row(mysql_query($query));

	$default_email=stripslashes(trim($rs[2]));
	$subject=stripslashes(trim($rs[0]));
	$message=stripslashes(trim($rs[1]));

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

	$query="select ";
	$query.="whm_id ";
	$query.="from ";
	$query.="hosting_order ";
	$query.="where ";
	$query.="oid='".addslashes(trim($oid))."'";
	
	$rs=mysql_fetch_row(mysql_query($query));

	$whm_id=stripslashes(trim($rs[0]));

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

	$query="select ";
	$query.="user.first_name, ";				// 0
	$query.="user.last_name, ";					// 1
	$query.="user.organization_name, ";			// 2
	$query.="user.street_address_1, ";			// 3
	$query.="user.street_address_2, ";			// 4
	$query.="user.city, ";						// 5
	$query.="user.state, ";						// 6
	$query.="user.zip_code, ";					// 7
	$query.="user.phone, ";						// 8
	$query.="user.fax, ";						// 9
	$query.="tld_chart.cost, ";					// 10
	$query.="hosting_order.domain_name, ";		// 11
	$query.="server_config.primary_ns, ";		// 12
	$query.="server_config.primary_ns_ip, ";	// 13
	$query.="server_config.secondary_ns, ";		// 14
	$query.="server_config.secondary_ns_ip, ";	// 15
	$query.="user.email ";						// 16
	$query.="from ";
	$query.="hosting_order, user, tld_chart, server_config ";
	$query.="where ";
	$query.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query.="and ";
	$query.="hosting_order.uid=user.uid ";
	$query.="and ";
	$query.="hosting_order.tld_id=tld_chart.tld_id ";
	
	if ($whm_id==0)
		{
		$query0="select ";
		$query0.="whm_id, ";
		$query0.="count(*) ";
		$query0.="from ";
		$query0.="server_config ";
		$query0.="where ";
		$query0.="active_server='1' ";
		$query0.="group by whm_id";

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

		if ($rs0[1]>0) { $whm_id=stripslashes(trim($rs0[0])); }
		else 
			{
			$query0="select ";
			$query0.="whm_id, ";
			$query0.="count(*) ";
			$query0.="from ";
			$query0.="server_config ";
			$query0.="where ";
			$query0.="manual_install_active='1' ";
			$query0.="group by whm_id";

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

			if ($rs0[1]>0) { $whm_id=stripslashes(trim($rs0[0])); }
			else
				{
				$query0="select ";
				$query0.="whm_id ";
				$query0.="from ";
				$query0.="server_config ";
				$query0.="limit 0, 1 ";

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

				$whm_id=stripslashes(trim($rs0[0]));
				}
			}

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

		$query.="and ";
		$query.="server_config.whm_id='".addslashes(trim($whm_id))."' ";
		}
	else
		{
		$query.="and ";
		$query.="hosting_order.whm_id=server_config.whm_id ";
		}

	$query.="order by hosting_order.oid asc ";
	$query.="limit 0, 1";
	
	$rs=mysql_fetch_row(mysql_query($query));

	$first_name=stripslashes(trim($rs[0]));
	$last_name=stripslashes(trim($rs[1]));
	$organization_name=stripslashes(trim($rs[2]));
	$street_address_1=stripslashes(trim($rs[3]));
	$street_address_2=stripslashes(trim($rs[4]));
	$city=stripslashes(trim($rs[5]));
	$state=stripslashes(trim($rs[6]));
	$zip_code=stripslashes(trim($rs[7]));
	$phone=stripslashes(trim($rs[8]));
	$fax=stripslashes(trim($rs[9]));
	$cost=stripslashes(trim($rs[10]));
	$cost=$currency.sprintf("%01.2f", $cost)." ".$currency_type;
	$domain_name=stripslashes(trim($rs[11]));
	$primary_ns=stripslashes(trim($rs[12]));
	$primary_ns_ip=stripslashes(trim($rs[13]));
	$secondary_ns=stripslashes(trim($rs[14]));
	$secondary_ns_ip=stripslashes(trim($rs[15]));
	$email_address=stripslashes(trim($rs[16]));
	$generate_date=date("m/d/Y h:i:s a");
	
	# -----------------------------------------------------------------------------------

	# {{first_name}}
	# {{last_name}}
	# {{company_name}} 
	# {{address_1}}
	# {{address_2}}
	# {{city}}
	# {{state}}
	# {{zip}}
	# {{phone}}
	# {{fax}}
	# {{domain_reg_price}}
	# {{domain_name}}
	# {{primary_ns}}
	# {{primary_ns_ip}}
	# {{secondary_ns}}
	# {{secondary_ns_ip}}
	# {{generate_date}}
	# {{email_address}}

	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{last_name}}", $last_name, $subject);
	$subject=str_replace("{{company_name}}", $organization_name, $subject);
	$subject=str_replace("{{address_1}}", $street_address_1, $subject);
	$subject=str_replace("{{address_2}}", $street_address_2, $subject);
	$subject=str_replace("{{city}}", $city, $subject);
	$subject=str_replace("{{state}}", $state, $subject);
	$subject=str_replace("{{zip}}", $zip_code, $subject);
	$subject=str_replace("{{phone}}", $phone, $subject);
	$subject=str_replace("{{fax}}", $fax, $subject);
	$subject=str_replace("{{domain_reg_price}}", $cost, $subject);
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{primary_ns}}", $primary_ns, $subject);
	$subject=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $subject);
	$subject=str_replace("{{secondary_ns}}", $secondary_ns, $subject);
	$subject=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $subject);
	$subject=str_replace("{{generate_date}}", $generate_date, $subject);
	$subject=str_replace("{{email_address}}", $email_address, $subject);

	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{last_name}}", $last_name, $message);
	$message=str_replace("{{company_name}}", $organization_name, $message);
	$message=str_replace("{{address_1}}", $street_address_1, $message);
	$message=str_replace("{{address_2}}", $street_address_2, $message);
	$message=str_replace("{{city}}", $city, $message);
	$message=str_replace("{{state}}", $state, $message);
	$message=str_replace("{{zip}}", $zip_code, $message);
	$message=str_replace("{{phone}}", $phone, $message);
	$message=str_replace("{{fax}}", $fax, $message);
	$message=str_replace("{{domain_reg_price}}", $cost, $message);
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{primary_ns}}", $primary_ns, $message);
	$message=str_replace("{{primary_ns_ip}}", $primary_ns_ip, $message);
	$message=str_replace("{{secondary_ns}}", $secondary_ns, $message);
	$message=str_replace("{{secondary_ns_ip}}", $secondary_ns_ip, $message);
	$message=str_replace("{{generate_date}}", $generate_date, $message);
	$message=str_replace("{{email_address}}", $email_address, $message);

	# -----------------------------------------------------------------------------------
	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();
	$headers.="\nX-Priority: 1"; 
	
	mail($email_admin, $subject, $message, $headers);
	}
?>