<?php
session_start();
	if(!$_GET['step']){
		$step="0";
	}else{
		$step=$_GET['step'];
	}
 include('header.php'); 
 
 $folder=$_SERVER['SCRIPT_NAME'];
 $folder=str_replace('install/index.php','',$folder);
 ?>

<?php
	if(!$_GET['step']){
?>
<div class="hero-unit">
	<h1>Wellcome</h1>
	<p>UPandSELL</p>
	<p>
		<a class="btn btn-primary btn-large" href="?step=1">Start Installation</a>
	</p>
</div>
<?php
}elseif($_GET['step']=="1"){
	if(isset($_POST['step1submit'])){
		$server=$_POST['db_host'];
		$dbname=$_POST['db_name'];
		$db_user=$_POST['db_user'];
		$db_pass=$_POST['db_pass'];
		$link = @mysql_connect($server, $db_user, $db_pass);
		if($link){
			$folder=$_SERVER['SCRIPT_NAME'];
			$folder=str_replace('/install/index.php','',$folder);
			$key = $_SESSION['key'];
			$secret= $_SESSION['secret'];

			$stringData = "<?php
					\n
					$"."localhost=\"$server\"; \n
					$"."dbuser=\"$db_user\"; \n
					$"."dbpass=\"$db_pass\"; \n
					$"."dbname=\"$dbname\"; \n
					\n
					$"."connect = @mysql_connect($"."localhost,$"."dbuser,$"."dbpass);\n
					$"."selectdb = mysql_select_db($"."dbname,$"."connect); \n
					\n
					?>";
			$fh = fopen("../connect.php", 'w');
			fwrite($fh, $stringData);
			fclose($fh);
			$hta = "# BEGIN YOURLS \n
<IfModule mod_rewrite.c> \n
Options +FollowSymlinks \n
RewriteEngine On \n
\n
RewriteCond %{REQUEST_FILENAME} !-f \n
RewriteCond %{REQUEST_FILENAME} !-d \n
RewriteCond %{REQUEST_URI} !^/buy \n
RewriteRule ^lang/(.*)$ ".$folder."/index.php?lang=$1 [L] \n
RewriteRule ^page/(.*)$ ".$folder."/index.php?id=$1 [L] \n
RewriteRule ^buy/([0-9]+) ".$folder."/index.php?id=buypage&id2=$1 [NC] \n
RewriteRule ^buyredirect/([0-9]+) ".$folder."/index.php?id=buypage&id2=$1&id3=redirect [NC] \n
RewriteRule ^download/([0-9]+) ".$folder."/index.php?id=download&id2=$1 [NC] \n
</IfModule>\n
# END YOURLS\n";
			$fh = fopen("../.htaccess", 'w');
			fwrite($fh, $hta);
			fclose($fh);
			include('../connect.php');
			include('db.php');
			//mysql_query($sql)or die(mysql_error());
				echo "<form action=\"index.php?step=2\" method=\"post\">	<input type=\"submit\" class=\"btn\" name=\"stdb\" value=\"Next Step\" /></form>";
				
		}else{
				echo "<script>alert(\"Failed to connect\")</script>";
		}
	}else{
?>
<form class="form-horizontal" method="post" action="">
	<legend>Step 1 : Mysql connection</legend>
  <div class="control-group">
    <label class="control-label" >MYSQL Server:</label>
    <div class="controls">
      <input type="text"  name="db_host" value="localhost">
	  <span class="help-inline">Default: localhost</span>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputEmail">MYSQL Username</label>
    <div class="controls">
      <input type="text" id="inputEmail" name="db_user" placeholder="Username">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">MYSQL Password</label>
    <div class="controls">
      <input type="password" name="db_pass" id="inputPassword" placeholder="Password">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">MYSQL Database</label>
    <div class="controls">
      <input type="text" name="db_name" id="inputPassword" placeholder="database">
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
      <button type="submit" class="btn" name="step1submit">Verify Connection</button>
    </div>
  </div>
</form>
<?php
	}
}elseif($_GET['step']=="2"){
	if(isset($_POST['step2submit'])){
	include('../connect.php');
		$siteurl=$_POST['siteurl'];
		$sitename=$_POST['sitename'];
		$admin_username=$_POST['admin_username'];
		$admin_password=md5($_POST['admin_password']);
		$_SESSION['admin_username']=$admin_username;
		$_SESSION['siteurl']=$siteurl;
		$insert="INSERT INTO `settings` (`value`, `type`) VALUES
('$siteurl', 'site_url'),
('$sitename', 'site_title');
";
mysql_query($insert)or die(mysql_error());
$time=time();
$timefix=date('d-m-Y');
mysql_query("INSERT INTO `members`(`username`,`password`,`name`,`paypalemail`,`email`,`role`,`register`,`registerfix`) VALUES('$admin_username','$admin_password','Admin','example@website.com','example@website.com','1','$time','$timefix')");
	
		echo "<form action=\"index.php?step=3\" method=\"post\">	<input type=\"submit\" class=\"btn\" name=\"stdb\" value=\"Finish\" /></form>";
				
	}else{
?>

<form class="form-horizontal" method="post" action="">
	<legend>Step 2 : Website config</legend>
  <div class="control-group">
    <label class="control-label" for="inputEmail">Site URL</label>
    <div class="controls">
      <input type="text" name="siteurl" id="inputEmail" placeholder="http://">
	  <span class="help-inline">Example: http://website.com (without "/" after )</span>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">Site Title</label>
    <div class="controls">
      <input type="text" name="sitename" id="inputPassword" placeholder="UPandSELL">
    </div>
  </div>
    <div class="control-group">
    <label class="control-label" for="inputPassword">Admin Username</label>
    <div class="controls">
      <input type="text" name="admin_username" id="inputPassword" placeholder="Username" >
    </div>
  </div>
    <div class="control-group">
    <label class="control-label" for="inputPassword">Admin Password</label>
    <div class="controls">
      <input type="password" name="admin_password" id="inputPassword" placeholder="Password">
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
      <button type="submit" class="btn" name="step2submit">Submit</button>
    </div>
  </div>
</form>
<?php
	}
}elseif($_GET['step']=="3"){
?>
<h2>
Admin Username: <b><?=$_SESSION['admin_username'];?></b><br/>
Link to Website: <b><a href="<?=$_SESSION['siteurl'];?>"><?=$_SESSION['siteurl'];?></a></b><br/>
Link to admin panel: <b><a href="<?=$_SESSION['siteurl'];?>/admin"><?=$_SESSION['siteurl'];?>/admin</a></b><br/>
<h2>
<?php
}else{
echo "ERROR";
}
?>
<?php include('footer.php'); ?>