<?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 
*/ 


  $start_time=time();
  // attempt to set the time limit to 30 seconds default  maximum 2mins(120)
  $time_extend=false;
  if (osc_set_time_limit(30)) $time_extend=true;

  $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) . '/';
  if (osc_in_array('database', $HTTP_POST_VARS['install'])) {
    $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']));

    osc_db_connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD']);

    $db_error = false;
	$sql_files=array();
	$version="1_0_0";
	$installed_sqls=array();
	// first check the database existance
	$database_query=osc_db_query("SHOW DATABASES like '" . $db['DB_DATABASE'] . "'");
	if (osc_db_num_rows($database_query)>0  && (!isset($HTTP_POST_VARS["optReinstall"]) || $HTTP_POST_VARS["optReinstall"]!="Y")){
		// 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
	$version_splt=split("_",$version);
	$current_version=$version;
	while(true){
		$temp_file="freeway_v" . $current_version;
		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;
		}
		$current_version=join("_",$version_splt);
	}
	$first_install=0;
	$last_sql="";
	for ($icnt=0;$icnt<count($sql_files);$icnt++){
		if ($sql_files[$icnt]=="freeway_v1_0_0") $first_install=1;
		$last_sql=$sql_files[$icnt];
	    osc_db_install($db['DB_DATABASE'], $dir_fs_www_root . "install/" . $sql_files[$icnt] . ".sql");
		if ($db_error) break;
	}

    if ($db_error != false) {
	  	$current_step="install";
?>
<table width="100%" cellpadding="0" cellspacing="0">
<form name="install" action="install.php?step=3" method="post">
	<tr>
		<td>
		<?php echo osc_draw_box_start(480);?>
		<table width="100%" border="0" cellpadding="2" class="formPage">
		  <tr>
			<td>
			  <p><b>The database import was <font color="#AB212E">unsuccessful.</font></b> <img src="images/setting_error.gif" align="absmiddle"></p>
			  <p>The following error has occurred:</p>
			   <p class="boxme"><?php echo $db_error; ?></p>
			   Sql file: <?php echo $last_sql;?>
			</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_retry.gif" border="0" alt="Retry" class="imageButton"></td>
		  </tr>
		</table>
		</td>
	</tr>
</form>
</table>
<?php
    } else {
	$current_step="install";
?>
<table width="100%" cellpadding="0" cellspacing="0">
<form name="install" action="install.php?step=4" method="post">
	<tr>
		<td>
		<?php echo osc_draw_box_start(480);?>
		<table width="95%" border="0" cellpadding="2" class="formPage">
		  <tr>
			<td>
			<p><b>The database import was <font color="#AB212E">successful.</font></b> <img src="images/setting_right.gif" align="absmiddle"></p>
			</td>
		  </tr>
		</table>
		<?php echo osc_draw_box_end();?>
		</td>
	</tr>
	<tr>
		<td>
		<?php
			 echo '<input type="hidden" name="first_install" value="' . $first_install . '">';
			  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="5">
		  <tr>
		<?php
			  if (osc_in_array('configure', $HTTP_POST_VARS['install'])) {
		?>
			<td align="right"><a href="javascript:doAction(1)"><img src="images/button_continue.gif" border="0" alt="Continue"></a></td>
		<?php
			  } else {
		?>
			<td align="right"><a href="javascript:doAction(2)"><img src="images/button_continue.gif" border="0" alt="Continue"></a></td>
		<?php
			  }
		?>
		  </tr>
		</table>
		</td>
	</tr>
	</form>
</table>
<script language="javascript">
	function doAction(type){
		if (type==1){
			document.install.action="install.php?step=4";
		} else {
			document.install.action="install.php?step=8";
		}
		document.install.submit();
	}
</script>
<?php
    }
  }
?>
