<?php
if (!$step) { header('Location: ./install.php'); die(); }

$file=dirname(__FILE__) . '/../libs/dbconnect.php';

if(!isset($dbuser)){
  $dbuser = $_POST['dbuser'];
  $dbpass = $_POST['dbpass'];
  $dbname = $_POST['dbname'];
  $dbhost = $_POST['dbhost'];
}

if($conn = @mysql_connect($dbhost,$dbuser,$dbpass)) {
	$output.= "<p>" . $lang['ConnectionEstab'] . "</p>\n";
	if(mysql_select_db($dbname, $conn)) {
	$output.= "<p>" . $lang['FoundDb'] . "</p>\n";
		if($handle = fopen($file, 'w')) {
			$str  = "<?php\n";
			$str .= "define(\"EZSQL_DB_USER\", '".$dbuser."');"."\n";
			$str .= "define(\"EZSQL_DB_PASSWORD\", '".$dbpass."');"."\n";
			$str .= "define(\"EZSQL_DB_NAME\", '".$dbname."');"."\n";
			$str .= "define(\"EZSQL_DB_HOST\", '".$dbhost."');"."\n";
			$str .= "if (!function_exists('gettext')) {"."\n";
			$str .= '	function _($s) {return $s;}'."\n";
			$str .= '}'."\n";
			$str .= "?>";

			if(fwrite($handle, $str)) {
				$output.= "<p>" . $lang['dbconnect'] . "</p>\n";
				fclose($handle);
			} 
			else { $errors[] = $lang['Error2-1']; }
		} 
		else { $errors[] = $lang['Error2-2']; }
	}
	else { $errors[] = $lang['Error2-3']; }
}
else { $errors[] = $lang['Error2-4']; }

if($check_errors !== false){
  if (!$errors) {
  	$output.='<p>' . $lang['NoErrors'] . '</p>
  	<form id="form2" name="form2" method="post">
  	  <input type="hidden" name="dbuser" value="'.$_POST['dbuser'].'" />
  	  <input type="hidden" name="dbpass" value="'.$_POST['dbpass'].'" />
  	  <input type="hidden" name="dbname" value="'.$_POST['dbname'].'" />
  	  <input type="hidden" name="dbhost" value="'.$_POST['dbhost'].'" />
  	  <input type="hidden" name="tableprefix" value="'.$_POST['tableprefix'].'" />
	  <input type="hidden" name="language" value="' . $_REQUEST['language'] . '">
  	  <input type="hidden" name="step" value="4">
  	  <input type="submit" name="Submit" value="' . $lang['Next'] . '" />
  	  </form>';
  }
  else {
    $output=DisplayErrors($errors);
    $output.='<form id="thisform">
    <input type=button onclick="window.history.go(-1)" value="' . $lang['GoBack'] . '" />
    </form>';
  }
  echo $output;
} else {
  header("Location: $url_install3"); 
}
?>
