<?php
/* 
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare 
	
	Released under the GNU General Public License 
*/ 

?>
<?php 
  if (isset($HTTP_POST_VARS['DB_SERVER']) && !empty($HTTP_POST_VARS['DB_SERVER']) && isset($HTTP_POST_VARS['DB_TEST_CONNECTION']) && ($HTTP_POST_VARS['DB_TEST_CONNECTION'] == 'true')) {
    $db = array();
    $db['DB_SERVER'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER']));
    $db['DB_SERVER_USERNAME'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER_USERNAME']));
    $db['DB_SERVER_PASSWORD'] = trim(stripslashes($HTTP_POST_VARS['DB_SERVER_PASSWORD']));
    $db['DB_DATABASE'] = trim(stripslashes($HTTP_POST_VARS['DB_DATABASE']));
    $db_error = false;
	$upgrade_error=false;
	$source_link="";
	$add=0;
    osc_db_connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD']);

    if ($db_error == false) {
      osc_db_test_create_db_permission($db['DB_DATABASE']);
    }
	if($db_error == false){
		$source_link=mysql_connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD']);
		$content_tables=array('events','orders','admin_files');
		 $tables_query=mysql_query("show tables",$source_link);
		 while($tables=mysql_fetch_array($tables_query)){
			 if (in_array($tables[0],$content_tables)){
				$add=$add+1;
			 } else continue;
		 }
		 if($add!=3){
			 $db_error='The Given Database is incorrect';
			 $upgrade_error=true;
		 }
	} 
	$current_step="install";
    if ($db_error != false) {
?>
<table width="100%" cellpadding="0" cellspacing="0">
	<?php if($upgrade_error!=false){
			$action = "install.php?step=2&upg=upgrade";
			}else{
			$action = "install.php?step=3";
	} ?>
	<form name="install" action="<?php echo $action; ?>" method="post">
	<tr>
		<td valign="top">
		<?php echo osc_draw_box_start();?>

		<table width="100%" border="0" cellpadding="5" class="formPage">
		  <tr>
			<td>
			  <p><b>A test connection made to the database was <font color="#AB212E">unsuccessful.</font></b> <img src="images/setting_error.gif" align="absmiddle"></p>
			  <p>The error message returned is:</p>
			  <p class="boxme"><?php echo $db_error; ?></p>
			  <p>Please click on the <i>Back</i> button below to review your database server settings.</p>
			  <p>If you require help with your database server settings, please consult your hosting company.</p>
			</td>
		  </tr>
		</table>
		<?php echo osc_draw_box_end();?>
		</td>
	</tr>
	<tr>
		<td>
		<?php 
			  reset($HTTP_POST_VARS);
			  while (list($key, $value) = each($HTTP_POST_VARS)) {
				if (($key != 'x') && ($key != 'y') && ($key != 'DB_TEST_CONNECTION')) {
				  if (is_array($value)) {
					for ($i=0; $i<sizeof($value); $i++) {
					  echo osc_draw_hidden_field($key . '[]', $value[$i]);
					}
				  } else {
					echo osc_draw_hidden_field($key, $value);
				  }
				}
			  }
		?>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
		  <tr>
			<td><a href="index.php"><img src="images/button_cancel.gif" border="0" alt="Cancel"></a></td>
			<td align="right"><input type="image" src="images/button_back.gif" border="0" alt="Back" class="imageButton"></td>
		  </tr>
		</table>
		</td>
	</tr>
	</form>
</table>
<?php
    } else {
      $script_filename = getenv('PATH_TRANSLATED');

      if (empty($script_filename)) {
        $script_filename = getenv('SCRIPT_FILENAME');
      }
      if (empty($script_filename)) {
        $script_filename = $_SERVER["SCRIPT_FILENAME"];
      }

      $script_filename = str_replace('\\', '/', $script_filename);
      $script_filename = str_replace('//', '/', $script_filename);

      $dir_fs_www_root_array = explode('/', dirname($script_filename));
      $dir_fs_www_root = array();
      for ($i=0, $n=sizeof($dir_fs_www_root_array)-1; $i<$n; $i++) {
        $dir_fs_www_root[] = $dir_fs_www_root_array[$i];
      }
      $dir_fs_www_root = implode('/', $dir_fs_www_root) . '/';
	
	$sql_files=array();
	$installed_sqls=array();
	$version="1_0_0";
	// first check the database existance
	$database_query=osc_db_query("SHOW DATABASES like '" . $db['DB_DATABASE'] . "'"); 
	if (osc_db_num_rows($database_query)>0){
		// first sql_queries table exists
		$check_query=osc_db_query("SHOW TABLES FROM " . $db['DB_DATABASE'] . " like 'sql_queries'");
		if (osc_db_num_rows($check_query)>0){
			osc_db_select_db($db['DB_DATABASE']);
			$installed_query=osc_db_query("SELECT file_name from sql_queries order by file_name");
			while($installed_result=osc_db_fetch_array($installed_query)){
				$installed_sqls[$installed_result["file_name"]]=1;
			}
		}
	} 
	// first get the last executed sql file
	osc_set_time_limit(5);
	//$version_splt=split("_",$version);
	//$current_version=$version;
	
	/*while(true){
		$temp_file="freeway_v" . $current_version;
		//$temp_file="freeway_upgrade";
		if (!file_exists($dir_fs_www_root . "install/" . $temp_file . ".sql")) break;
		if (!isset($installed_sqls[$temp_file . ".sql"])){
			$sql_files[]=$temp_file;
		}
		for ($icnt=2;$icnt>=0;$icnt--){
			$version_splt[$icnt]+=1;
			if ($version_splt[$icnt]<=9 || $icnt==0) break;
			$version_splt[$icnt]=0;
		}
		$sql_files[] = $temp_file;
		$current_version=join("_",$version_splt);
	} unset($sql_files);*/
	$sql_files[] = 'freeway_upgrade';
	
	
?>
<script>
	function do_action(step){ 
		if (document.install.optReinstall && document.install.optReinstall[0].checked){
			document.install.action="install.php?step=3";
		} else {
			document.install.action="install.php?step="+step+"&upg=upgrade";
		}
	}
</script>
<table width="100%" cellpadding="0" cellspacing="0">
    <form name="install" action="install.php?step=3" method="post">
	<tr>
		<td valign="top">
		<?php echo osc_draw_box_start((count($sql_files)>0?400:200));?>
		<table width="100%" border="0" cellpadding="5" class="formPage">
		  <tr>
			<td>
			  <p><b>A test connection made to the database was <font color="#3F7B86">successful</font></b>  <img src="images/setting_right.gif" align="absmiddle"></p>
			  <?php 
				$continue=3;
			  if (count($sql_files)>0) { ?>
				  <p>Please continue the installation process to execute the database import procedure.<br />
				     Please take essential backups if an upgrade is being made to the Freeway database. <br />
				     It is important this procedure is not interrupted, otherwise the database may end up corrupt.</p>
				  <p>The files to be import must be located at:<br /><img src="images/pixel_trans.gif"  height="10"/><br />
				  <font color="#3F7B86"><?php echo $dir_fs_www_root . "install/"; ?></font>
				  <p>
				  <div style="height:200;width:600;overflow-x:auto;border:solid 1px #CFCFCF;margin:2px;padding:10px;">
				  <table border="0" cellpadding="0" cellspacing="0">
				<?php  
						echo '<tr><td>'.$sql_files[0].".sql".'</td></tr>';
						echo '</table>';
					?>
				  </div>
				<?php
					} else { 
						if (osc_in_array('configure', $HTTP_POST_VARS['install'])) {
							$continue=4;
						} else{
							$continue=8;
						}
				?>
					<p>Freeway Database already contains the recent database changes. 
					<p>Do you want to Reinstall the Database <input type="radio" name="optReinstall" value="Y" style="border:0"/>Yes&nbsp;<input type="radio" name="optReinstall" value="N" checked style="border:0"/>No</p>
				<?php } ?>
			</td>
		  </tr>
		</table>
		<?php echo osc_draw_box_end();?>
		</td>
	</tr>
	<tr>
		<td>
		<?php 
			  reset($HTTP_POST_VARS);
			  while (list($key, $value) = each($HTTP_POST_VARS)) {
				if (($key != 'x') && ($key != 'y') && ($key != 'DB_TEST_CONNECTION')) {
				  if (is_array($value)) {
					for ($i=0; $i<sizeof($value); $i++) {
					  echo osc_draw_hidden_field($key . '[]', $value[$i]);
					}
				  } else {
					echo osc_draw_hidden_field($key, $value);
				  }
				}
			  }
		?>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
		  <tr>
			<td><a href="index.php"><img src="images/button_cancel.gif" border="0" alt="Cancel"></a></td>
			<td align="right"><input type="image" src="images/button_continue.gif" border="0" alt="Continue" class="imageButton" onClick="do_action(<?php echo $continue;?>)"></td>
		  </tr>
		</table>
		</td>
	</tr>
	</form>
</table>
<?php
    }
  } else {
?>
<table width="100%" cellpadding="0" cellspacing="0">
	<form name="install" action="install.php?step=2&upg=upgrade" method="post">
	<tr>
		<td valign="top">
		<?php echo osc_draw_box_start();?>
		<table width="100%" cellpadding="5" cellspacing="0">
			<tr>
				<td><?php echo osc_title_bar('Please enter the database server information:');?></td>
			</tr>
			<tr>
				<td>
				<table width="100%" border="0" cellpadding="8" class="formPage" align="center">
				  <tr>
					<td width="30%" valign="top">Database Server:</td>
					<td width="70%" class="smallDesc">
					  <?php echo osc_draw_input_field('DB_SERVER'); ?>
					  <img src="images/layout/help_icon.gif" onClick="toggleBox('dbHost');" align="absmiddle"><br>
					  <div id="dbHostSD">Hostame or IP-address of the database server</div>
					  <div id="dbHost" class="longDescription">The database server can be in the form of a hostname, such as db1.myserver.com, or as an IP-address, such as 192.168.0.1</div>
					</td>
				  </tr>
				  <tr>
					<td width="30%" valign="top">Username:</td>
					<td width="70%" class="smallDesc">
					  <?php echo osc_draw_input_field('DB_SERVER_USERNAME'); ?>
					  <img src="images/layout/help_icon.gif"  onClick="toggleBox('dbUser');"  align="absmiddle"><br>
					  <div id="dbUserSD">Database username</div>
					  <div id="dbUser" class="longDescription">The username used to connect to the database server. An example username is 'mysql_10'.<br><br>Note: Create and Drop permissions <b>are required</b> at this point of the installation procedure.</div>
					</td>
				  </tr>
				  <tr>
					<td width="30%" valign="top">Password:</td>
					<td width="70%" class="smallDesc">
					  <?php echo osc_draw_password_field('DB_SERVER_PASSWORD'); ?>
					  <img src="images/layout/help_icon.gif" onClick="toggleBox('dbPass');"  align="absmiddle"><br>
					  <div id="dbPassSD">Database password</div>
					  <div id="dbPass" class="longDescription">The password is used together with the username, which forms the database user account.</div>
					</td>
				  </tr>
				  <tr>
					<td width="30%" valign="top">Database Name:</td>
					<td width="70%" class="smallDesc">
					  <?php echo osc_draw_input_field('DB_DATABASE'); ?>
					  <img src="images/layout/help_icon.gif" onClick="toggleBox('dbName');"  align="absmiddle"><br>
					  <div id="dbNameSD">Database Name</div>
						<div id="dbName" class="longDescription">The database used to hold the 
						  data. An example database name is 'Freeway'.</div>
					</td>
				  </tr>
				 <!-- <tr>
					<td width="30%" valign="top">Persistent Connections:</td>
					<td width="70%" class="smallDesc">
					  <?php echo osc_draw_checkbox_field('USE_PCONNECT', 'true'); ?>
					  <img src="images/layout/help_icon.gif" onClick="toggleBox('dbConn');"  align="absmiddle"><br>
					  <div id="dbConnSD">Persistent Connections</div>
					  <div id="dbConn" class="longDescription">Enable persistent database connections.<br><br />Note: Persistent connections should be disabled for shared servers and in general are not recommended.</div>
					</td>
				  </tr>-->
				  <tr>
					<td width="30%" valign="top">Session Storage:</td>
					<td width="70%" class="smallDesc">
					  <?php echo osc_draw_radio_field('STORE_SESSIONS', 'files', true); ?>&nbsp;Files&nbsp;&nbsp;<?php echo osc_draw_radio_field('STORE_SESSIONS', 'mysql'); ?>&nbsp;Database&nbsp;&nbsp;
					  <img src="images/layout/help_icon.gif" onClick="toggleBox('dbSess');"  align="absmiddle"><br>
					  <div id="dbSessSD"></div>
					  <div id="dbSess" class="longDescription">Store user session data as files on the server, or in the database.<br><br>Note: Due to security related issues, database session storage is recommended for shared servers.</td></div>
					</td>
				  </tr>
				</table>
				</td>
			</tr>
		</table>		
		<?php echo osc_draw_box_end();?>
		</td>
	<tr>
	<tr>
		<td>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
		  <tr>
			<td><a href="index.php"><img src="images/button_cancel.gif" border="0" alt="Cancel"></a></td>
			<td align="right"><input type="image" src="images/button_continue.gif" border="0" alt="Continue" class="imageButton"></td>
		  </tr>
		</table>
		<?php 
		  reset($HTTP_POST_VARS);
		  while (list($key, $value) = each($HTTP_POST_VARS)) {
			if (($key != 'x') && ($key != 'y') && ($key != 'DB_SERVER') && ($key != 'DB_SERVER_USERNAME') && ($key != 'DB_SERVER_PASSWORD') && ($key != 'DB_DATABASE') && ($key != 'STORE_SESSIONS') && ($key != 'DB_TEST_CONNECTION')) {//&& ($key != 'USE_PCONNECT') 
			  if (is_array($value)) {
				for ($i=0; $i<sizeof($value); $i++) {
				  echo osc_draw_hidden_field($key . '[]', $value[$i]);
				}
			  } else {
				echo osc_draw_hidden_field($key, $value);
			  }
			}
		  }
		
		  echo osc_draw_hidden_field('DB_TEST_CONNECTION', 'true');
		?>
		</td>
	</tr>
	</form>
</table>
<?php
  }
?>