<?php
class Installer{
	function ExtractSql($host,$user,$pass,$db,$data){
		$conn = mysql_connect($host,$user,$pass);
		if(!$conn){
			return 'Error connecting to mysql!';
		}else{
			$database = mysql_select_db($db);
			if(!$database){
				return 'Error connecting to database!';
			}else{
				$query = null;
				foreach($data as $sql_line){
                  if(trim($sql_line) != "" && strpos($sql_line, "--") === false){
                    $query .= $sql_line;
                    if (substr(rtrim($query), -1) == ';'){
                      $result = mysql_query($query)or die(mysql_error());
                      $query = null;
                    }
                  }
                }
				file_put_contents('config.php','<?php'."\n".'$config[\'mysql\'][\'host\'] = \''.$host.'\' ;'."\n".'$config[\'mysql\'][\'user\'] = \''.$user.'\' ;'."\n".'$config[\'mysql\'][\'pass\'] = \''.$pass.'\' ;'."\n".'$config[\'mysql\'][\'db\'] = \''.$db.'\' ;'."\n\n".'$config[\'captcha_admin\'] = true;'."\n".'?>');
				unlink('dropshare.sql');
				unlink('install.php');
				header('Location: ./');
			}
		}
	}
	
	function Auto($data,$header,$else = null){
		if(isset($data[$header])){
			return $data[$header];
		}
		return $else;
	}
}

echo '<!DOCTYPE HTML>';
echo '<html>';
echo '<head>
	<title>Installation</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="description" content="" />
	<meta name="keywords" content="" />
	<link rel="stylesheet" href="template/css/main.css" />
	<link rel="stylesheet" href="template/css/alertboxes.css" />
	<script src="template/js/alertboxes.js"></script>
	<meta name="viewport" content="width=device-width, initial-scale=1" /> 
	<link rel="shortcut icon" href="template/img/favicon.ico" type="image/x-icon" />
</head>
<body><a href="./install.php"><h1 class="logo">Installation</h1></a>';
	
	
echo '<div class="box">';
if(file_exists('dropshare.sql')){
	$install = new Installer;
	
	echo '<form method="POST">';
	if(isset($_POST['host']) && isset($_POST['user']) && isset($_POST['pass']) && isset($_POST['db'])){
		$sql = file('dropshare.sql');
        echo '<div class="alert-message error" style="width:100%;margin-left:0px;">'.$install->ExtractSql($_POST['host'],$_POST['user'],$_POST['pass'],$_POST['db'],$sql).'</div>';
	}
	echo '<input type="text" name="host" value="'.$install->Auto($_POST,'host').'" placeholder="Host (def: localhost)"><br>';
	echo '<input type="text" name="user" value="'.$install->Auto($_POST,'user').'" placeholder="Username (def: root)"><br>';
	echo '<input type="password" name="pass" placeholder="Password"><br>';
	echo '<input type="text" name="db" value="'.$install->Auto($_POST,'db').'" placeholder="Database"><br>';
	echo '<input type="submit" value="Install" class="next action-button" style="margin-left:0px;width:100%;">';
	echo '</form>';
	
}else{
	echo 'dropshare.sql not exist!';
}
echo '</div></body></html>';

?>