<?php
/*
#****************************************************************************#
# Ultimate Locator                                                              #
# Professional Zip Code Locator Systems          support@ultimatelocator.com #
# Copyright 2004 PageSkills.com Internet Services    www.ultimatelocator.com #
# Release Date: 2/01/2004                                                    #
#                                                                            #
#   Ultimate Locator is protected by the copyright                           #
#   laws and international copyright treaties, as well as other              #
#   intellectual property laws and treaties.                                 #
#   Distribution without approved license is illegal and is                  #
#   punishable by law.                                                       #
#                                                                            #
#   A license is good toward one domain only.  You must purchase             #
#   additional licenses if you wish to install this software on              #
#   additional domains (websites). We do offer Reseller Packages.            #
#****************************************************************************#
*/

session_start();
if(isset($_SESSION['MEMBER_USERNAME']))
{
include("config.php");


error_reporting(0);
?>
<html>
<head>
<title>Export Locations to .CSV File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body background="#FFFFFF" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<script>
function noEntry() {
	if (document.form.username.value=="") {
		alert("You did not enter your MySql Username.");
		document.form.username.focus();
		return false;
	}
	if (document.form.password.value=="") {
		alert("You did not enter your MySql password.");
		document.form.password.focus();
		return false;
	}
	if (document.form.server.value=="") {
		alert("You did not enter your MySql host address.");
		document.form.server.focus();
		return false;
	}
	if (document.form.database.value=="") {
		alert("You did not enter the name of the database your table resides in.");
		document.form.database.focus();
		return false;
	}
	if (document.form.table.value=="") {
		alert("You did not enter the name of the table you wish to convert.");
		document.form.table.focus();
		return false;
	}
	if (document.form.csvfile.value=="") {
		alert("You must specify a name to give the output file.");
		document.form.csvfile.focus();
		return false;
	}
return true;
}
</script>
<br>
<?php

function file_put_contents_php4 ($location, $whattowrite) {
    if (file_exists($location)) {
        unlink($location);
    }
    $fileHandler = fopen ($location, "w");
    fwrite ($fileHandler, $whattowrite);
    fclose ($fileHandler);
}

if(isset($HTTP_POST_VARS['submit'])){
    echo "Connecting to database... ";
    flush();
    $result = mysql_connect(addslashes($HTTP_POST_VARS['server']), addslashes($HTTP_POST_VARS['username']), addslashes($HTTP_POST_VARS['password']));
    if( !$result )
    {
        echo "<span class=\"error\">FAILED!</span><br />Could not connect to the database, please check your username, password, and server, and try again.</span></body></html>";
        flush();
        die();
    }
    echo "<span class=\"success\">Done!</span>";
    flush();
    echo "<br>";
    echo "Selecting database... ";
    $result = mysql_select_db(addslashes($HTTP_POST_VARS['database']));
    if( !$result ){
        echo "<span class=\"error\">FAILED!</span><br>Could not select databse, please check your database name and try again.</span></body></html>";
        flush();
        die();
    }
    echo "<span class=\"success\">Done!</span>";
    flush();
    echo "<br>";
    echo "Converting table... ";

	$output = "";
	$addcomma = "false";
	$query = mysql_query("SELECT * FROM `".$HTTP_POST_VARS['table']."`");
	while($row = mysql_fetch_array($query, MYSQL_ASSOC)){
		foreach($row as $x) {
			if ($addcomma == "true"){ $output .= ","; }
			$x = str_replace("\r\n", "",$x);
			$output .= $x;
			$addcomma = "true";
		}
	$output .= "
";
	$addcomma = "false";
	}
	$pos = strpos($HTTP_POST_VARS['csvfile'], ".csv");
	if ($pos == false) {
		$csvfile = $HTTP_POST_VARS['csvfile'].".csv";
	}
	file_put_contents_php4($path."/".$csvfile, $output);
    if( !$query )
    {
        echo "<span class=\"error\">Oops, something went wrong while converting the table.  The exact error MySQL reported was<br>";
        echo "MySQL Error Number: " . mysql_errorno();
        echo "MySQL Error: " . mysql_error();
        echo "</span></body></html>";
        die();
    }
    echo "<span class=\"success\">Done!</span>";
    flush();
    echo "<br>";
    echo "Export complete. Now closing the MySQL link.";
    mysql_close();
    flush();
	echo "<br><br><br>Your .csv file is now available from your server:<br><a href=\"$url_path/".$csvfile."\">Download your ".$csvfile."</a>
	<br><br><br><font size=\"1\"><a href=\"javascript:window.close()\">Close This Window</a></font>";
	
}
else
{
?>
<form name="form" id="form" action="<?php echo $PHP_SELF; ?>" method="post" onSubmit="return noEntry()">
<input type="hidden" name="username" value="<?php echo $username_zip; ?>">
<input type="hidden" name="password" value="<?php echo $password_zip; ?>">
<input type="hidden" name="server" value="<?php echo $hostname_zip; ?>">
<input type="hidden" name="database" value="<?php echo $database_zip; ?>">
<input type="hidden" name="table" value="location">

<table border="0" align="center" cellpadding="1" cellspacing="0">
  <tr>
    <td nowrap><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><strong>Export 
              your Locations to a .csv File</strong></font><br>
              <br>
          <table border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
      <tr>
        <td valign="top">
        <table  border="0" cellspacing="0" cellpadding="4">
            <tr valign="middle">
              <td colspan="2" bgcolor="#CCCCCC"><div align="center"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
              <strong>Please enter your new .cvs file name below</strong></font></div>
              </td>
            </tr>
            <tr valign="bottom">
              <td>&nbsp</td>
              <td>&nbsp;</td>
            </tr>
            <tr valign="bottom">
              <td nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
              &nbsp;Table name : </td>
              <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> &nbsp;Location
              </td>
            </tr>
            <tr valign="bottom">
              <td nowrap><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
              &nbsp;File Name : </td>
              <td> &nbsp;<input size="13" type="text" name="csvfile" ><font size="2" face="Verdana, Arial, Helvetica, sans-serif">.cvs
              </td>
            </tr>
            <tr valign="bottom">
              <td height="19" colspan="2">&nbsp;</td>
              </tr>
          </table>
        </td>
      </tr>
              </table>
            <p align="left">
            <font size="2" face="Times New Roman, Helvetica, sans-serif">
            A .csv file is a comma delimited file which can be imported into<br>
            Microsoft Excel for viewing and printing your locations.<br><br>
            After exporting, the new file will be saved to the server in<br>
            your <strong>/locator/dbackup</strong> directory. You will see a link on the next page<br>
            to download the file to your computer.</font>
          <div align="center"><br>
            <input type="submit" name="submit" value="Export Locations" >
            <br>
            </div>
    </div></td>
  </tr>
</table>
</form>
<?php
}

?>
<p>&nbsp;</p>
</body>
</html>

<?php
}
else { Header ("Location: logout.php?msg=Invalid%20Session."); }
?>