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

if (isset($submit))
	{
	$error_connect.="<font color='#990000'><b>";
	$error_connect="Cannot Connect to Db or";
	$error_connect.="the Database is Currently Unavailable.";
	$error_connect.="</b></font>";
	$error_connect.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	$error_connect.="[<a href='Javascript: history.back()'>Go Back</a>]";

	$error_select_db="<font color='#990000'><b>";
	$error_select_db.="Unable to Select Database.";
	$error_select_db.="</b></font>";
	$error_select_db.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	$error_select_db.="[<a href='Javascript: history.back()'>Go Back</a>]";

	$dblink=@MYSQL_CONNECT($xdbhost, $xdbuser, $xdbpass) OR DIE($error_connect);
	@mysql_select_db($xdb) or die($error_select_db);

	$query="select ";
	$query.="id, ";			// 0
	$query.="fname, ";		// 1
	$query.="lname, ";		// 2
	$query.="company, ";	// 3
	$query.="address, ";	// 4 
	$query.="city, ";		// 5
	$query.="state, ";		// 6
	$query.="zip, ";		// 7
	$query.="country, ";	// 8
	$query.="phone, ";		// 9
	$query.="email, ";		// 10
	$query.="username, ";	// 11
	$query.="signup_date ";	// 12
	$query.="from users ";
	$query.="order by ";
	$query.="id asc";

	$x=0;
	$row=mysql_query($query);
	while ($rs=mysql_fetch_row($row))
		{
		// build variables
		$uid=addslashes(trim($rs[0]));
		$first_name=addslashes(trim($rs[1]));
		$last_name=addslashes(trim($rs[2]));
		$organization_name=addslashes(trim($rs[3]));
		$street_address_1=addslashes(trim($rs[4]));
		$city=addslashes(trim($rs[5]));
		$state=addslashes(trim($rs[6]));
		$zip_code=addslashes(trim($rs[7]));
		$country=addslashes(trim($rs[8]));
		$phone=addslashes(trim($rs[9]));
		$email=addslashes(trim($rs[10]));
		$username=addslashes(trim($rs[11]));
		$signup_date=addslashes(trim($rs[12]))."/";
		list($month, $day, $year)=split("/", $signup_date);
		$signup_date=$year.$month.$day."000000";
		$tos=1;
		$status=0;
		$sid=md5(microtime());
		
		// insert query
		$insert="insert into user ";
		$insert.="set ";
		$insert.="uid='".$uid."', ";
		$insert.="first_name='".$first_name."', ";
		$insert.="organization_name='".$organization_name."', ";
		$insert.="street_address_1='".$street_address_1."', ";
		$insert.="city='".$city."', ";
		$insert.="state='".$state."', ";
		$insert.="zip_code='".$zip_code."', ";
		$insert.="country='".$country."', ";
		$insert.="phone='".$phone."', ";
		$insert.="email='".$email."', ";
		$insert.="username='".$username."', ";
		$insert.="ogcreate='".$signup_date."', ";
		$insert.="tos='".$tos."', ";
		$insert.="status='".$status."', ";
		$insert.="sid='".$sid."', ";
		$insert.="password='".md5("password")."'";

		if ($x==0)
			{
			$output=$insert.";\n";
			$x++;
			}
		else
			{
			$output.=$insert.";\n";
			}
		}

	if ($schema==2)
		{
		$show_output=$output;
		}
	else if ($schema==1)
		{
		if (isset($drop_table))
			{
			$structure="DROP TABLE IF EXISTS user;\n";
			}
		@$structure.=("CREATE TABLE user (
			  uid bigint(22) unsigned NOT NULL auto_increment,
			  sid varchar(8) NOT NULL default '',
			  status int(2) NOT NULL default '0',
			  first_name varchar(255) NOT NULL default '',
			  last_name varchar(255) NOT NULL default '',
			  organization_name varchar(255) NOT NULL default '',
			  street_address_1 varchar(255) NOT NULL default '',
			  street_address_2 varchar(255) NOT NULL default '',
			  city varchar(255) NOT NULL default '',
			  state varchar(100) NOT NULL default '',
			  zip_code varchar(25) NOT NULL default '',
			  country varchar(255) NOT NULL default '',
			  phone varchar(35) NOT NULL default '',
			  fax varchar(35) NOT NULL default '',
			  email varchar(255) NOT NULL default '',
			  username varchar(255) NOT NULL default '',
			  password varchar(255) NOT NULL default '',
			  advert varchar(255) NOT NULL default '',
			  advert_other varchar(255) NOT NULL default '',
			  tos int(1) NOT NULL default '0',
			  temp_pw varchar(10) NOT NULL default '',
			  ogcreate timestamp(14) NOT NULL,
			  PRIMARY KEY  (uid),
			  KEY sid (sid)
			) TYPE=MyISAM;
			");

		$show_output=$structure."\n";
		$show_output.=$output."\n";
		}
	else if ($schema==0)
		{
		if (isset($drop_table))
			{
			$structure="DROP TABLE IF EXISTS user;\n";
			}
		@$structure.=("CREATE TABLE user (
			  uid bigint(22) unsigned NOT NULL auto_increment,
			  sid varchar(8) NOT NULL default '',
			  status int(2) NOT NULL default '0',
			  first_name varchar(255) NOT NULL default '',
			  last_name varchar(255) NOT NULL default '',
			  organization_name varchar(255) NOT NULL default '',
			  street_address_1 varchar(255) NOT NULL default '',
			  street_address_2 varchar(255) NOT NULL default '',
			  city varchar(255) NOT NULL default '',
			  state varchar(100) NOT NULL default '',
			  zip_code varchar(25) NOT NULL default '',
			  country varchar(255) NOT NULL default '',
			  phone varchar(35) NOT NULL default '',
			  fax varchar(35) NOT NULL default '',
			  email varchar(255) NOT NULL default '',
			  username varchar(255) NOT NULL default '',
			  password varchar(255) NOT NULL default '',
			  advert varchar(255) NOT NULL default '',
			  advert_other varchar(255) NOT NULL default '',
			  tos int(1) NOT NULL default '0',
			  temp_pw varchar(10) NOT NULL default '',
			  ogcreate timestamp(14) NOT NULL,
			  PRIMARY KEY  (uid),
			  KEY sid (sid)
			) TYPE=MyISAM;
			");

		$show_output=$structure;
		}
	}

include $server_admin_inc."/header.php";

if (isset($submit))
	{
	echo("
		<table width='725' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
				<td width='99%' align='left' valign='top'>
				<table width='100%' cellpadding='2' cellspacing='0' border='0'>
					<tr>
						<td><b>P*rlBill User Migration Tool</b></td>
					</tr>
					<tr>
						<td><br></td>
					</tr>
					<tr>
						<td><font color='#990000'>Copy and Paste the following code into PHPmyAdmin:</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='Javascript: history.back()'>Go Back</a>]</td>
					</tr>
					<tr>
						<td><textarea rows='25' cols='75'>".$show_output."</textarea></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		");
	}
else
	{
	echo("
		<form action='".$PHP_SELF."' method='POST' name='form1'>
		<table width='725' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
				<td width='99%' align='left' valign='top'>
				<table width='100%' cellpadding='2' cellspacing='0' border='0'>
					<tr>
						<td colspan='2'><img src='".$http_images."/menu_arrow.gif'><b>P*rlBill User Migration Tool</b>&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/csv.php?sid=".$sid."'>Go Back</a>]</td>
					</tr>
					<tr>
						<td colspan='2'><img src='".$http_images."/space.gif' width='15' height='6'></td>
					</tr>
					<tr>
						<td colspan='2'><font color='#990000'>Connect to P*rlBill Database:</font></td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>P*rlBill DB Host:</td>
						<td width='75%' align='left' valign='top'><input ".$input_style." size='40' maxlength='255' type='text' name='xdbhost' value='".((isset($xdbhost))?"".$xdbhost."":"")."'></td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>P*rlBill DB Name:</td>
						<td width='75%' align='left' valign='top'><input ".$input_style." size='40' maxlength='255' type='text' name='xdb' value='".((isset($xdb))?"".$xdb."":"")."'></td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>P*rlBill DB Username:</td>
						<td width='75%' align='left' valign='top'><input ".$input_style." size='40' maxlength='255' type='text' name='xdbuser' value='".((isset($xdbuser))?"".$xdbuser."":"")."'></td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>P*rlBill DB Password:</td>
						<td width='75%' align='left' valign='top'><input ".$input_style." size='40' maxlength='255' type='text' name='xdbpass' value='".((isset($xdbpass))?"".$xdbpass."":"")."'></td>
					</tr>
					<tr>
						<td colspan='2'><br></td>
					</tr>
					<tr>
						<td colspan='2'><font color='#990000'>View dump (schema) of migration:</font></td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='9'>Output Options:</td>
						<td width='75%' align='left' valign='top'><input onclick='document.form1.drop_table.disabled=true;' type='radio' name='schema' value='2'".((isset($schema))?"".(($schema==2)?" checked":"")."":" checked").">I want the data only.</td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'></td>
						<td width='75%' align='left' valign='top'><input onclick='document.form1.drop_table.disabled=false;' type='radio' name='schema' value='1'".((isset($schema))?"".(($schema==1)?" checked":"")."":"").">I want the structure and data.</td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'></td>
						<td width='75%' align='left' valign='top'><input onclick='document.form1.drop_table.disabled=false;' type='radio' name='schema' value='0'".((isset($schema))?"".(($schema==0)?" checked":"")."":"").">I want the structure only.</td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'></td>
						<td width='75%' align='left' valign='top'><input type='checkbox' name='drop_table' value='1'".((isset($schema))?"".(($schema==2)?" disabled":"".((isset($drop_table))?"".(($drop_table==1)?" checked":"")."":"")."")."":"".((isset($drop_table))?"".(($drop_table==1)?" checked":"")."":"")."").">Add a drop table.</td>
					</tr>
					<tr>
						<td colspan='2'><br></td>
					</tr>
					<tr>
						<td width='25%' align='left' valign='top'></td>
						<td width='75%' align='left' valign='top'><input ".$button_style." type='submit' name='submit' value='View SQL'></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		</form>
		");
	}
include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>