<?php 
if(file_exists('system/configuration.php')){
	die('System: Already installed!');
}
session_start();

?>

<!DOCTYPE html>
<html lang="en"><head>
  <meta charset="utf-8">
  <title>Shortify</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="Plan flat skin for Bootstrap">
  <meta name="author" content="Jozef Vojtáš" >
  
  <!-- Google Web Font -->
  <link href="http://fonts.googleapis.com/css?family=Open+Sans:300,300italic,700&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  
  <!-- Bootstrap 3 Plan styles -->
  <link id="plan-theme" href="data/templates/frontend/superhero/assets/css/bootstrap.css" rel="stylesheet">
  <link href="data/templates/frontend/superhero/assets/fonts/font-awesome.css" rel="stylesheet" type="text/css" />

  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Docs Custom styles -->
  <style>

    body {
      padding: 120px 0 0 0; 
    }
    footer {
      background-color: white;
      border-top: 1px solid #ddd;
      padding: 30px;
      margin-top: 50px;
    }  
    .row > [class*="col-"] {
      margin-bottom: 30px;
    }
    .navbar-container {
      position: relative;
      min-height: 100px;
    }
    .navbar-container .navbar.navbar-fixed-top,
    .navbar-container .navbar.navbar-fixed-bottom {
      position: absolute;
      top: 50px;
      z-index: 0;
    }
    .navbar-container .navbar.navbar-fixed-top .container,
    .navbar-container .navbar.navbar-fixed-bottom .container {
      max-width: 90%;
    }
    .btn-group {
      margin-bottom: 10px
    }
    .form-inline select, 
    .form-inline input[type="text"], 
    .form-inline input[type="password"] {
      width: 180px;
    }
    .input-group {
      margin-bottom: 10px;
    }
    .pagination { margin-top :0;}
    .navbar-inverse {margin: 110px 0} 
    input[type="radio"].toggle {
    display: none;
}

input[type="radio"].toggle:checked + label {
    background-color: #3B4B5C;
    cursor: default;
    color: #E6E6E6;
    border-color: transparent;
    text-shadow: 0 1px 1px rgba(40, 40, 40, 0.75);
}

input[type="radio"].toggle + label {
    width: 20%;
}

input[type="radio"].toggle:checked + label.btn:hover {
    background-color: #445260;
    background-position: 0 0;
    -webkit-transition: none;
    -moz-trainsition: none;
    transition: none;
}

input[type="radio"].toggle-left + label {
    border-right: 0;
    -webkit-border-top-right-radius: 0;
    -webkit-border-bottom-right-radius: 0;
    -moz-border-top-right-radius: 0;
    -moz-border-bottom-right-radius: 0;
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}

input[type="radio"].toggle-right + label {
    -webkit-border-top-left-radius: 0;
    -webkit-border-bottom-left-radius: 0;
    -moz-border-top-left-radius: 0;
    -moz-border-bottom-left-radius: 0;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
}
  </style>
  
</head>

<body onload="runOnLoad()">

  <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" style="margin: 0"><!-- WHITE navbar-superhero -->
    <div class="container">
		<a class="navbar-brand" href="f.home">Shortify - Installation</a>
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header navbar-right">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-nav">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
      </div>
    </div>
  </nav>
  
  <div class="container">

    <div class="text-center">
      <h1>Shortify <small>/ Install proccess</small></h1>
    </div>
	</br>
<?php
if(isset($_POST['gotoadmin'])) {
	$_SESSION['username'] = 'admin';
   $_SESSION['password'] = '21232f297a57a5a743894a0e4a801fc3';
   $_SESSION['acctype'] = 'admin';
   $_SESSION['hash'] = 'adminsecret';
	?>
		<script language="JavaScript">
			location.replace("admin.Dashboard");
		</script>
	<?php
}
if(isset($_POST['install'])) {
		$mysqlhost = $_POST['mysqlhost'];
		$mysqlusername = $_POST['mysqlusername'];
		$mysqlpassword = $_POST['mysqlpassword'];
		$mysqldatabase = $_POST['mysqldatabase'];
		$filename = 'install/database.sql';
		// Connect to MySQL server
		mysql_connect($mysqlhost, $mysqlusername, $mysqlpassword) or die('Error connecting to MySQL server: ' . mysql_error());
		// Select database
		mysql_select_db($mysqldatabase) or die('Error selecting MySQL database: ' . mysql_error());
		
		$filecontent1=file_get_contents('install/1.php');
		$filecontent2=file_get_contents('install/2.php');
		$filecontent3=file_get_contents('install/3.php');
		$filecontent4=file_get_contents('install/4.php');
		$filecontent5=file_get_contents('install/5.php');
		$nothing="";
		$pos=strpos($filecontent, '?>');
		$filecontent1a=substr($filecontent1, 0, $pos).substr($filecontent1, $pos).$mysqlhost;
		$filecontent2a=substr($filecontent2, 0, $pos).substr($filecontent2, $pos).$mysqlusername;
		$filecontent3a=substr($filecontent3, 0, $pos).substr($filecontent3, $pos).$mysqlpassword;
		$filecontent4a=substr($filecontent4, 0, $pos).substr($filecontent4, $pos).$mysqldatabase;
		$filecontent5a=substr($filecontent5, 0, $pos).substr($filecontent5, $pos).$nothing;
		$filecontent=$filecontent1a . $filecontent2a . $filecontent3a . $filecontent4a . $filecontent5a;
		if (file_put_contents("system/configuration.php", $filecontent)){
			echo "Config file generated and writed successfully</br>";
			// Temporary variable, used to store current query
			$templine = '';
			// Read in entire file
			$lines = file($filename);
			// Loop through each line
			foreach ($lines as $line)
			{
			// Skip it if it's a comment
			if (substr($line, 0, 2) == '--' || $line == '')
				continue;
			
			// Add this line to the current segment
			$templine .= $line;
			// If it has a semicolon at the end, it's the end of the query
			if (substr(trim($line), -1, 1) == ';')
			{
				// Perform the query
				if (mysql_query($templine)){
					if($said == 0){
						echo "MYSQL database imported successfully</br>";
						$said = 1;
					}
				}else{
					print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
				}
				// Reset temp variable to empty
				$templine = '';
			}
			}
		}else{
			echo "ERROR - Config writting</br>";
		}
	?>
	<form name="csrf_form" id="shrinkform" action="" method="POST">
		<input value="Go to admin panel" class="btn btn-default" style="width: 150px;" name="gotoadmin" type="submit">
	</form>
	<?php
}else {
?>
    <!-- BASE CSS   =========================================================== -->
	<div class="row">
      <div class="col-md-8 col-md-offset-2">
		<div class="well">
			<div class="row">
				<p>Welcome in installation proccess of our script Shortify.</p>
				<p>This is only basic step, to connect MYSQL database with script.</p>
				<p>More settings will be available for you in admin panel after installation.</p>
				<p>you can download more free scripts in persianscript.ir ;).</p>
				<form name="csrf_form" id="shrinkform" action="" method="POST">
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="" class="input-group">
							<input class="form-control" placeholder="MYSQL HOST" name="mysqlhost" type="text">
							<input class="form-control" placeholder="MYSQL USERNAME" name="mysqlusername" type="text">
							<input class="form-control" placeholder="MYSQL PASSWORD" name="mysqlpassword" type="text">
							<input class="form-control" placeholder="MYSQL DATABASE" name="mysqldatabase" type="text">
							<span class="input-group-btn">
								<input value="Install" class="btn btn-default" style="width: 150px;" name="install" type="submit">
							</span>				
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
			</div>
        </div>
      </div>
    </div>
    <!-- Tables -->
<?php
}
?>
	</div>
  <!-- jQuery CDN and fallback -->
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <script>window.jQuery || document.write('<script src="data/templates/frontend/superhero/assets/js/jquery.js">\x3C/script>')</script>

  <!-- Main Scripts-->
  <script src="data/templates/frontend/superhero/assets/js/bootstrap.min.js"></script>
  <script src="data/templates/frontend/superhero/assets/js/plan.ui.js"></script>
  <!-- Bootstrap Select -->
  <script src="data/templates/frontend/superhero/assets/js/bootstrap-select.min.js"></script>

  <script src="system/general/components/jsoncookies/jquery.cookie.js"></script>
</body></html>