<?php
/* 
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	 
	
	Freeway eCommerce 
	http://www.openfreeway.org
	Copyright (c) 2007 ZacWare
	
	osConcert, Online Seat Booking 
  	http://www.osconcert.com

  	Copyright (c) 2009-2013 osConcert 
	
	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;
    osc_db_connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD'],  $db['DB_DATABASE']);

    if ($db_error == false) {
      osc_db_test_create_db_permission($db['DB_DATABASE']);
    }
	
	$current_step="install";
    if ($db_error != false) {?>
		<form name="install" action="install.php?step=2" method="post">
			<div class="card border-danger mb-3 mx-auto" style="max-width: 50rem;" id="txr_terms">
				<div class="card-header"><i class='fa fa-times-circle' style='color:red'></i>
				A test connection made to the database was unsuccessful.</div>
				<div class="card-body">
					<p class="card-text">osConcert is licensed under the terms of the GNU General Public License (GPL) Version 2</p>
					<p class="card-text">The error message returned is:</p>
					<p class="card-text alert-danger"><?php echo $db_error; ?></p>
					<p class="card-text">Please click on the <i>Back</i> button below to review your database server settings.</p>
					<p class="card-text">If you require help with your database server settings, please consult your hosting company.</p>
				</div>
			</div>
			<?php 
					reset($HTTP_POST_VARS);
					//FOREACH
					//while (list($key, $value) = each($HTTP_POST_VARS)) {
					foreach($HTTP_POST_VARS as $key => $value) {
						  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);
							}
						  }
					}
			?>
			<button type="button" class="btn btn-info" style="float: left; margin-top: 10px;" onClick="this.form.submit();"><i class='fas fa-arrow-alt-circle-left' style='color:white'></i> Back</button>
			<a class="btn btn-info" style="float: right; margin-top: 10px;" href="index.php" role="button"><i class='fas fa-times-circle' style='color:white'></i> Cancel</a>
		</form>
<?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="9_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)
		{
		$table_found='yes';
			// 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=mysqli_fetch_array($installed_query)){
					$installed_sqls[$installed_result["file_name"]]=1;
				}
			}else{//no sql_queries table
			$table_found='no';}
		} 
		// first get the last executed sql file
		osc_set_time_limit(5);
		$version_splt=explode("_",$version);
		$current_version=$version;
		while(true){
			$temp_file="osconcert_v" . $current_version;
			if (!file_exists($dir_fs_www_root . "install/" . $temp_file . ".sql")) break;//found the last file that exists
			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);
		}
?>
	<script>
		function do_action(step){
				alert(step);
			if (document.install.optReinstall && document.install.optReinstall[0].checked){
				document.install.action="install.php?step=3";
			} 
				else if (document.install.optReinstall && document.install.optReinstall[1].checked){
				document.install.action="install.php?step=3";
			}
			
			else {
				document.install.action="install.php?step="+step;
			}
		}
	</script>
				<div class="container" style="width:800px">
			<div class="row">
					<div class="col-md-12">
					<ul class="list-group list-group-horizontal justify-content-center">
					  <li class="list-group-item">Compatibility Test</li>
					  <li class="list-group-item">Installation Options</li>
					  <li class="list-group-item"><strong>Database Import</strong></li>
					  <li class="list-group-item">Test Connection</li>
					</ul> <br>
					</div>
			</div>
			<div class="row">
					<div class="col-md-12">
					<div class="progress progress-bar progress-bar-striped progress-bar-animated bg-info" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width: 50%">50%</div>
		</div><br>
					
			</div>
			<div class="row">
					<div class="col-md-12"><br>
					<form name="install" action="install.php?step=3" method="post">
				<h3><i class='fa fa-check' style='color:green'></i>
				A test connection made to the database was successful.</h3>
				<?php 
				$continue=3;
				if (count($sql_files)>0 && $table_found=='no') 
				{ //empty database - new install?>
					<ul>	
						<li>Please continue the installation process to execute the database import procedure.</li>
						<li>Please take essential backups if an upgrade is being made to the database.</li>
						<li>It is important this procedure is not interrupted, otherwise the database may end up corrupt.</li>
					</ul>
					<p>	The files to be import must be located at:<br /></p>
					<font color="#117a8b"><?php echo $dir_fs_www_root . "install/"; ?></font>
					<p>
						<?php  
							$splt_arr=array();
							$col=1;
							$row=1;
							for ($icnt=0,$n=count($sql_files);$icnt<$n;$icnt++)
							{
								$splt_arr[$row][$col]=$sql_files[$icnt];
								if (($icnt+1)>1 && ($icnt+1) % 10==0) 
								{
									$row=1;
									$col++;
								} else {
									$row++;
								}
							}
							if(count($sql_files>0)){echo '<div><div>The following files were located and will be installed:</div></div>';}

							for ($icnt=1,$n=count($splt_arr);$icnt<=$n;$icnt++)
							{
									echo '<div>';
									for ($jcnt=1,$n1=count($splt_arr[$icnt]);$jcnt<=$n1;$jcnt++)
									{
											echo '<div ' . ($jcnt>1?"style='padding-left:20px;'":'') .  '>' . $splt_arr[$icnt][$jcnt] . '.sql</div>';
									}
									echo '</div>';
							}
						?>
					</p>
		<?php 	} elseif (count($sql_files)>0 && $table_found=='yes') 
		{ ?>
					<p>
						A database upgrade is available for your osConcert installation.<br />
						Please consider making a  database backup before proceeding<br />
						It is important this procedure is not interrupted, otherwise the database may end up corrupt.
					</p>
					<p>The files to be imported must be located at:<br /><br />
						<font color="#117a8b"><?php echo $dir_fs_www_root . "install/"; ?></font>
					</p>
					<?php  
						$splt_arr=array();
						$col=1;
						$row=1;
						for ($icnt=0,$n=count($sql_files);$icnt<$n;$icnt++)
						{
							$splt_arr[$row][$col]=$sql_files[$icnt];
							if (($icnt+1)>1 && ($icnt+1) % 10==0) 
							{
								$row=1;
								$col++;
							} else {
								$row++;
							}
						}
						if(count($sql_files>0)){echo '<div><div>The following files were located and will be installed:</div></div>';}
						for ($icnt=1,$n=count($splt_arr);$icnt<=$n;$icnt++){
							echo '<div>';
							for ($jcnt=1,$n1=count($splt_arr[$icnt]);$jcnt<=$n1;$jcnt++){
								echo '<div ' . ($jcnt>1?"style='padding-left:20px;'":'') .  '>' . $splt_arr[$icnt][$jcnt] . '.sql</div>';
							}
							echo '</div>';
						}
				} else { 
						if (osc_in_array('configure', $HTTP_POST_VARS['install'])) {
							$continue=3;
						} else{
							$continue=8;
						}
					?>
						<p>The existing osConcert database appears to be up to date.</p>
						<p>Do you want to Reinstall the Database wiping all existing data? <input type="radio" name="optReinstall" value="Y" style="border:0"/>&nbsp;Yes&nbsp;<input type="radio" name="optReinstall" value="N" checked style="border:0"/>&nbsp;No&nbsp;</p>
						<p>Click 'Continue' to proceed</p>
						<input type="hidden" name="no_database_updates" value="Y">
		<?php 	}
				reset($HTTP_POST_VARS);
				//while (list($key, $value) = each($HTTP_POST_VARS)) {
				foreach($HTTP_POST_VARS as $key => $value) {
					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);
						}
					}
				}
		  ?>
				<a class="btn btn-info" style="float: left; margin-top: 10px;" href="index.php" role="button"><i class='fas fa-arrow-alt-circle-left' style='color:white'></i> Cancel</a>
				<button type="button" class="btn btn-info" style="float: right; margin-top: 10px;" onClick="this.form.submit();">Continue <i class='fas fa-arrow-alt-circle-right' style='color:white'></i></button>
			</form>
					</div>
			</div>
			</div><br>
	

<?php
    }
} else {
?>
		<div class="container" style="width:800px">
		<div class="row">
		<div class="col-md-12">
		<ul class="list-group list-group-horizontal justify-content-center">

					  <li class="list-group-item">Compatibility Test</li>
					  <li class="list-group-item">Installation Options</li>
					  <li class="list-group-item"><strong>Database Import</strong></li>
					  <li class="list-group-item">Test Connection</li>
					</ul><br> 
		</div>
		</div>
		<div class="row">
		<div class="col-md-12">
		<div class="progress progress-bar progress-bar-striped progress-bar-animated bg-info" role="progressbar" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%">25%</div>
		</div><br>
		</div>

		<div class="row">
		<div class="col-md-12">
		<form name="install" action="install.php?step=2" method="post">
			<h3 class="text-center text-info">Please enter the database server information:</h3>      
			<div style="width: 100%; float: left; padding-right:15px; margin: 10px 0px;">   		
				<div style="width: 30%; vertical-align: text-top; float: left;">Database Server:</div>
				<div style="width: 70%; float: left;">
					<?php echo osc_draw_input_field('DB_SERVER'); ?>
					<?php $tooltip_ip = "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" ?>
					<i class="fa fa-question-circle" style="color:#117a8b" data-toggle="tooltip" data-placement="bottom" title="<?php echo $tooltip_ip;?>">
						Hostame or IP-address of the database server
					</i>
				</div>
			</div>

			<div style="width: 100%; float: left; padding-right:15px; margin: 10px 0px;">                        
				 <div style="width: 30%; vertical-align: text-top; float: left;" >Username:</div>
				 <div style="width: 70%; float: left;">
					<?php echo osc_draw_input_field('DB_SERVER_USERNAME'); ?>
					<?php $tooltip_username = "The username used to connect to the database server. An example username is 'mysql_10'.\nNote: Create and Drop permissions are required at this point of the installation procedure." ?>
					<i class="fa fa-question-circle" style="color:#117a8b" data-toggle="tooltip" data-placement="bottom" title="<?php echo $tooltip_username;?>">
						Database username
					</i>
				 </div>    
			</div>

			<div style="width: 100%; float: left; padding-right:15px; margin: 10px 0px;">                        
				<div style="width: 30%; vertical-align: text-top; float: left;" >Password:</div>
				<div style="width: 70%; float: left;">
					<?php echo osc_draw_password_field('DB_SERVER_PASSWORD'); ?>
					<?php $tooltip_pass = "The password is used together with the username, which forms the database user account." ?>
					<i class="fa fa-question-circle" style="color:#117a8b" data-toggle="tooltip" data-placement="bottom" title="<?php echo $tooltip_pass;?>">
						Database password
					</i>
				</div>    
			</div>

			<div style="width: 100%; float: left; padding-right:15px; margin: 10px 0px;">                        
				<div style="width: 30%; vertical-align: text-top; float: left;" >Database Name:</div>
				<div style="width: 70%; float: left;">
					<?php echo osc_draw_input_field('DB_DATABASE'); ?>
					<?php $tooltip_db = "The database used to hold the data. An example database name is 'osconcert'." ?>
					<i class="fa fa-question-circle" style="color:#117a8b" data-toggle="tooltip" data-placement="bottom" title="<?php echo $tooltip_db;?>">
						Database Name
					</i>
				</div>    
			</div>

			<div style="width: 100%; float: left; padding-right:15px; margin: 10px 0px;">                        
				<div style="width: 30%; vertical-align: text-top; float: left;" >Session Storage:</div>
				<div style="width: 70%; float: left;">
					<div class="form-check-inline">
						<?php echo osc_draw_radio_field('STORE_SESSIONS', 'files'); ?>Files
					</div>
					<div class="form-check-inline">
						<?php echo osc_draw_radio_field('STORE_SESSIONS', 'mysql', true); ?>Database
					</div>				
					<?php $tooltip_ss = "Note: Due to security related issues, database session storage is recommended for shared servers." ?>
					<i class="fa fa-question-circle" style="color:#117a8b" data-toggle="tooltip" data-placement="bottom" title="<?php echo $tooltip_ss;?>">
						Store user session data as files on the server, or in the database.
					</i>
				</div>
			</div>
			<a class="btn btn-info" style="float: left; margin-top: 10px;" href="index.php" role="button"><i class='fas fa-arrow-alt-circle-left' style='color:white'></i> Cancel</a>
			<button type="button" class="btn btn-info" style="float: right; margin-top: 10px;" onClick="this.form.submit();">Continue <i class='fas fa-arrow-alt-circle-right' style='color:white'></i></button>
			<?php
			  reset($HTTP_POST_VARS);
			  //while (list($key, $value) = each($HTTP_POST_VARS)) {
			foreach($HTTP_POST_VARS as $key => $value) {
				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')) {
				  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');
			?>
		</form>
		</div>
		</div>
		</div>
<br>
<?php
  }
?>