<?
if ( $_POST['action'] == 'Test Połączenia' )
{
  $mysqli = new mysqli( $_POST['host'], $_POST['user'], $_POST['password'],
          $_POST['name'] );
  if ( mysqli_connect_errno() )
    echo '<p class="conect_error">Nie można się połaczyć: <br>' . mysqli_connect_error() . '</p>';
  else
    echo '<p class="conect">Połączenie nawiązane</p>';
  mysqli_close( $mysqli );
}

if ( $_POST['action'] == 'install' )
{
  $mysqli = new mysqli( $_POST['host'], $_POST['user'], $_POST['password'], $_POST['name'] );
  $mysqli -> set_charset( 'utf8' );

  $file_sql = file_get_contents( 'https://shoppro.project-dc.pl/updates/shopPRO_sql.txt' );

  $sql_commands = explode( ';', $file_sql );
  for ( $i = 0; $i < count( $sql_commands ) - 1; $i++ )
    $result       = $mysqli -> query( $sql_commands[$i] );

  mysqli_close( $mysqli );
  $file = file_get_contents( 'https://shoppro.project-dc.pl/updates/shopPRO.zip' );

  $dlHandler = fopen( 'update.zip', 'w' );
  if ( !fwrite( $dlHandler, $file ) )
    fclose( $dlHandler );

  $file_name = 'update.zip';
  $path      = pathinfo( realpath( $file_name ), PATHINFO_DIRNAME );
  $path      = substr( $path, 0, strlen( $path ) );
  $zip       = new \ZipArchive;
  $res       = $zip -> open( $file_name );

  if ( $res === TRUE )
  {
    $zip -> extractTo( $path );
    $zip -> close();
    unlink( $file_name );
  }
  $config = "<?php\n"
          . "\$database['host'] = " . "'" . $_POST['host'] . "'" . ";\r\n"
          . "\$database['user'] = " . "'" . $_POST['user'] . "'" . ";\r\n"
          . "\$database['password'] = " . "'" . $_POST['password'] . "'" . ";\r\n"
          . "\$database['name'] = " . "'" . $_POST['name'] . "'" . ";\r\n"
          . "\$config['update'] = true;" . ";\r\n"
          . "\$config['devel'] = false;" . ";\r\n"
          . "\$config['cache'] = false;" . ";\r\n"
          . "\$config['salt'] = 'dd6c0ee59bf35b208b6d9bf42dd60769'" . ";\r\n"
          . "?>";
  file_put_contents( 'config.php', $config );

  unlink( 'install.php' );

  header( "location:/index.php?action=htaccess" );
}
?>
<!DOCTYPE html>
<html lang="pl">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>cmsPro - instalator</title>
    <link href="https://fonts.googleapis.com/css?family=Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i&amp;subset=latin-ext" rel="stylesheet">
    <style type="text/css">
      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }
      body {
        background: #f9f9f9;
        font-family: 'Montserrat', sans-serif;
      }
      .contener{
        max-width: 600px;
        margin:auto;
        width:100%;
        text-align: center;
      }
      .con-form {
        margin-top: 20px;
        overflow: hidden;
      }
      .form-header {
        background: #2d3e50;
        padding: 15px;
        color: #FFF;
      }
      .form-header h3 {
        font-weight: 300;
      }
      .form-title {
        margin:0px;
      }
      form {
        padding: 15px;
        position: relative;
        float: left;
        width: 100%;
        margin: 0px;
        border-left: 1px solid #2d3e50;
        border-right: 1px solid #2d3e50;
        border-bottom: 1px solid #2d3e50;
        font-size: 14px;
      }
      .form-group {
        margin-bottom: 15px;
        width: 100%;
        position:relative;
        display: block;
      }
      .form-group::after {
        content: '';
        display: block;
        clear: both;
      }
      .form-group label {
        line-height: 30px;
        padding-top: 0px;
        min-height: 1px;
        padding-left: 10px;
        padding-right: 10px;
        margin-bottom: 0;
        text-align: right;
        width: 33%;
        float: left;
        font-weight: 400;
        max-width: 100%;
        color: #656d78;
      }
      .form-group input{
        min-height: 1px;
        padding: 6px 12px;
        width: 66%;
        float: left;
        height: 30px;
        font-size: 12px;
        line-height: 18px;
        border: 1px solid #D5D5D5;
        background: #F9F9F9;
      }
      .form-footer {
        background: #F5F5F5;
        line-height: 30px;
        padding: 10px 0px;
        float: left;
        width: 100%;
        position: relative;
      }
      .testbaz {
        border: 1px solid #c6c6c6;;
        color: #333;
        background-color: #fff;
      }
      .testbaz, .inst {
        font-size: 12px;
        padding: 4px 15px;
        line-height: 20px;
        font-weight: 400;
        transition: all 200ms ease;
        display: inline-block;
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        cursor: pointer;
      }
      .inst{
        color: #fff;
        background-image: none;
        border: 1px solid transparent;
        background-color: #33414e;
        border-color: #33414e;
      }
      .conect_error {
        color: red;
        text-align: center;
        font-size: 16px;
        padding: 15px;
        margin-top: 10px;
        width: 600px;
        margin: 10px auto 0;
        background: #ffe1e1;
        border: 1px solid #fe9b9b;
      }
      .form-group::before, .form-group::after{
        content: " ";
        display: table;
      }
      .conect{
        color: green;
        text-align: center;
        font-size: 16px;
        padding: 15px;
        margin-top: 10px;
        width: 600px;
        margin: 10px auto 0;
        background: #e3f6d7;
        border: 1px solid #c4f0a8;
      }
    </style>
  </head>
  <body>
    <div class="contener">
      <div class="con-form">
        <div class="form-header">
          <h3 class="form-title">Instalacja</h3>
        </div>
        <form method="POST" action="" style="text-align: center">
          <input type="hidden" name="action" value="install" />
          <div class="form-group">
            <label form="host">Host:</label>
            <input type="text" name="host" value="<?= $_POST['host']?>">
          </div>
          <div class="form-group">
            <label form="name">Nazwa bazy danych:</label>
            <input type="text" value="<?= $_POST['name']?>" name="name">
          </div>
          <div class="form-group">
            <label form="user">Nazwa użytkownika:</label>
            <input type="text" value="<?= $_POST['user']?>" name="user">
          </div>
          <div class="form-group">
            <label form="password">Hasło:</label>
            <input type="text" name="password" value="<?= $_POST['password']?>">
          </div>
          <div class="form-footer">
            <input class="testbaz" type="submit" name="action" value="Test Połączenia">
            <input class="inst" type="submit" value="Instaluj">
          </div>
        </form>
      </div>
    </div>
  </body>
</html>