<?php
$ServerErrors = array();
$config_file_name = '../config.php';
if (!empty($_POST['install'])) {
   $con = @mysqli_connect($_POST['sql_host'], $_POST['sql_user'], $_POST['sql_pass'], $_POST['sql_name']);
   if (mysqli_connect_errno()) {
     $ServerErrors[] = "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   if (!filter_var($_POST['site_url'], FILTER_VALIDATE_URL)) {
     $ServerErrors[] = "Invalid site url";
   }
   if (empty($_POST['admin_username']) || empty($_POST['admin_password'])) {
       $ServerErrors[] = "Please provide right admin username/password";
   }
   if (empty($ServerErrors)) {
      $file_content = 
'<?php
// +------------------------------------------------------------------------+
// | @author Deen Doughouz (DoughouzForest)
// | @author_url 1: http://www.wowonder.com
// | @author_url 2: http://codecanyon.net/user/doughouzforest
// | @author_email: wowondersocial@gmail.com   
// +------------------------------------------------------------------------+
// | WoWonder - The Ultimate PHP Social Networking Platform
// | Copyright (c) 2016 WoWonder. All rights reserved.
// +------------------------------------------------------------------------+
// MySQL Hostname
$sql_db_host = "'  . $_POST['sql_host'] . '";
// MySQL Database User
$sql_db_user = "'  . $_POST['sql_user'] . '";
// MySQL Database Password
$sql_db_pass = "'  . $_POST['sql_pass'] . '";
// MySQL Database Name
$sql_db_name = "'  . $_POST['sql_name'] . '";

// Site URL
$site_url = "' . $_POST['site_url'] . '"; // e.g (http://example.com)
?>';
$success = '';
$config_file = file_put_contents($config_file_name, $file_content);
    if ($config_file) {
        $filename = '../wowonder.sql';
        // 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;
           $query = false;
           // If it has a semicolon at the end, it's the end of the query
           if (substr(trim($line), -1, 1) == ';') {
              // Perform the query
              $query = mysqli_query($con, $templine);
              // Reset temp variable to empty
              $templine = ''; 
           }
        }
        if ($query) {
           $query_one  = mysqli_query($con, "UPDATE `Wo_Config` SET `value` = '" . mysqli_real_escape_string($con, $_POST['siteName']). "' WHERE `name` = 'siteName'");
           $query_one .= mysqli_query($con, "UPDATE `Wo_Config` SET `value` = '" . mysqli_real_escape_string($con, $_POST['siteTitle']). "' WHERE `name` = 'siteTitle'");
           $query_one .= mysqli_query($con, "UPDATE `Wo_Config` SET `value` = '" . mysqli_real_escape_string($con, $_POST['siteEmail']). "' WHERE `name` = 'siteEmail'");
           $query_one .= mysqli_query($con, "INSERT INTO `Wo_Users` (
            `username`,`password`, `email`, `admin`, `active`, `verified`, `registered`, `start_up`, `start_up_info`, `startup_follow`, `startup_image`)
            VALUES ('" . mysqli_real_escape_string($con, $_POST['admin_username']). "', '" . mysqli_real_escape_string($con, md5($_POST['admin_password'])) . "','" . mysqli_real_escape_string($con, $_POST['siteEmail']) . "'
                ,'1', '1', '1', '00/0000', '1', '1', '1', '1')");
           $_SESSION['user_id'] = 1;
           $success = 'WoWonder successfully installed, please wait ..';
        } else {
          $ServerErrors[] = "Error found while installing, please contact us.";
        }
      }
   }
}
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>WoWonder | Installation - www.20script.ir</title>
        <link rel="shortcut icon" type="image/png" href="../themes/wowonder/img/icon.png"/>
        <link rel="stylesheet" href="../themes/wowonder/stylesheet/font-awesome-4.5.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="../themes/wowonder/stylesheet/bootstrap.min.css">
        <link rel="stylesheet" href="../themes/wowonder/stylesheet/style.css">
        <script type="text/javascript" src="../themes/wowonder/javascript/jquery-1.11.3.js"></script>
        <script type="text/javascript" src="../themes/wowonder/javascript/init.js"></script>
        <script type="text/javascript" src="../themes/wowonder/javascript/jquery.form.min.js"></script>
    </head>
    <body>
        <?php 
            $page = 'terms';
            $pages_array = array(
               'req',
               'terms',
               'installation',
               'finish'
            );
            if (!empty($_GET['page'])) {
               if (in_array($_GET['page'], $pages_array)) {
                  $page = $_GET['page'];
               }
            }
            $page_icon = '';
            $page_name = '';
            if ($page == 'terms') {
            	$page_name = 'Terms of use';
                $page_icon = 'bars';
            } else if ($page == 'req') {
            	$page_name = 'Requirements';
                $page_icon = 'cog';
            } else if ($page == 'installation') {
            	$page_name = 'Installation';
                $page_icon = 'download';
            }else if ($page == 'finish') {
            	$page_name = 'Finish';
                $page_icon = 'check';
            }
            $cURL = true;
            $php = true;
            $gd = true;
            $disabled = false;
            $mysqli = true;
            $is_writable = true;
            $mbstring = true;
            if (!function_exists('curl_init')) {
            $cURL = false;
            $disabled = true;
            }
            if (!function_exists('mysqli_connect')) {
            $mysqli = false;
            $disabled = true;
            }
            if (!extension_loaded('mbstring')) {
            $mbstring = false;
            $disabled = true;
            }
            if (!extension_loaded('gd') && !function_exists('gd_info')) {
            $gd = false;
            $disabled = true;
            }
            if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
            $php = false;
            $disabled = true;
            }
            if (!is_writable('../config.php')) {
            $is_writable = false;
            $disabled = true;
            }
?>
        <div class="content-container container">
                <div class="row admin-panel">
                    <div class="col-md-3">
                        <ul class="list-group">
                            <li class="list-group-item black-list <?php echo ($page == 'terms') ? 'active-list': '';?>"><i class="fa fa-fw fa-bars"></i> Terms of use</li>
                            <li class="list-group-item black-list <?php echo ($page == 'req') ? 'active-list': '';?>"><i class="fa fa-fw fa-cog"></i> Requirements</li>
                            <li class="list-group-item black-list <?php echo ($page == 'installation') ? 'active-list': '';?>"><i class="fa fa-fw fa-download"></i> Installation</li>
                            <li class="list-group-item black-list <?php echo ($page == 'finish') ? 'active-list': '';?>"><i class="fa fa-fw fa-check"></i> Finish</li>
                        </ul>
                    </div>
                    <div class="col-md-9">
                        <div class="list-group">
                            <div class="list-group-item"><i class="fa fa-fw fa-<?php echo $page_icon?>"></i> <?php echo $page_name?></div>
                            <div class="setting-well">
                                <?php if ($page == 'terms') { ?>
                                <div class="terms">
                                    <h5>LICENSE AGREEMENT: one (1) Domain (site) Install</h5>
                                    <br>
                                    <b>You CAN:</b><br> 1) Use on one (1) domain only, additional license purchase required for each additional domain.<br> 2) Modify or edit as you see fit.<br> 3) Delete sections as you see fit.<br> 4) Translate to your choice of language.<br>
                                    <br><b>You CANNOT:</b> <br>1) Resell, distribute, give away or trade by any means to any third party or individual without permission.<br> 2) Use on more than one (1) domain.
                                    <br><br>Unlimited Licenses are available.
                                    <hr>
                                    <form action="?page=req" method="post">
                                        <div class="form-group">
                                            <input type="checkbox" id="agree" name="agree"> I agree to the terms of use and privacy policy
                                        </div>
                                        <div class="row">
                                            <div class="col-sm-2 pull-left last-btn">
                                                <button type="submit" class="form-control btn-main" id="next-terms" disabled>
                                                <i class="fa fa-arrow-right progress-icon" data-icon="paper-plane-o"></i> Next
                                                </button>
                                            </div>
                                            <div class="setting-saved-update-alert milinglist"></div>
                                        </div>
                                    </form>
                                </div>
                                <?php } else if ($page == 'req') { ?>
                                <div class="req">
                                    <h5>PHP 5.4+  <?php echo ($php == true) ? '<font color="green">Installed</font>' : '<font color="red">Not installed</font>'?></h5>
                                    <h5>cURL  <?php echo ($cURL == true) ? '<font color="green">Installed</font>' : '<font color="red">Not installed</font>'?></h5>
                                    <h5>MySQLi  <?php echo ($mysqli == true) ? '<font color="green">Installed</font>' : '<font color="red">Not installed</font>'?></h5>
                                    <h5>GD Library  <?php echo ($gd == true) ? '<font color="green">Installed</font>' : '<font color="red">Not installed</font>'?></h5>
                                    <h5>Mbstring  <?php echo ($mbstring == true) ? '<font color="green">Installed</font>' : '<font color="red">Not installed</font>'?></h5>
                                    <h5>Config.php permission <?php echo ($is_writable == true) ? '<font color="green">Writable</font>' : '<font color="red">Not writable</font>'?></h5>
                                    <br>
                                    <form action="?page=installation" method="post">
                                        <div class="row">
                                            <div class="col-sm-2 pull-left last-btn">
                                                <button type="submit" class="form-control btn-main" id="next-terms" <?php echo ($disabled == true) ? 'disabled': '';?>>
                                                <i class="fa fa-arrow-right progress-icon" data-icon="paper-plane-o"></i> Next
                                                </button>
                                            </div>
                                            <div class="setting-saved-update-alert milinglist"></div>
                                        </div>
                                    </form>
                                </div>
                                <?php } else if ($page == 'finish') { ?>
                                <div class="req">
                                    <h5>WoWonder successfully installed, if you have any qeastion, please let us <a href="mailto:wowondersocial@gmail.com">know</a></h5>
                                    <br>
                                    <h5><a href="../">Let's Start !</a></h5>
                                </div>
                                <?php } else if ($page == 'installation') { ?>
                                <div class="req">
                                   <?php
                                   if (!empty($ServerErrors)) {
                                   ?>
                                   <div class="alert alert-danger">
                                    <?php
                                       foreach ($ServerErrors as  $value) {
                                           echo '- ' . $value . "<br>";
                                       }
                                    ?>
                                   </div>
                                   <?php } else if (!empty($success)) { ?>
                                   <div class="alert alert-success">
                                    <i class="fa fa-check"></i> <?php echo $success;?>
                                    <script type="text/javascript">
                                    var URL = '?page=finish';
                                    var delay = 1000; //Your delay in milliseconds
                                    setTimeout(function(){ window.location = URL; }, delay);
                                    </script>
                                   </div>
                                   <?php } ?>
                                    <form action="?page=installation" method="post" class="form-horizontal install-site-setting">
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteName">SQL host name </label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="sql_host" value="<?php echo (!empty($_POST['sql_host'])) ? $_POST['sql_host']: '';?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteTitle">SQL username</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="sql_user" value="<?php echo (!empty($_POST['sql_user'])) ? $_POST['sql_user']: '';?>"> 
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteName">SQL password </label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="sql_pass" value="<?php echo (!empty($_POST['sql_pass'])) ? $_POST['sql_pass']: '';?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteTitle">SQL database name</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="sql_name" value="<?php echo (!empty($_POST['sql_name'])) ? $_POST['sql_name']: '';?>"> 
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="site_url">Site url</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="site_url" placeholder="http://siteurl.com" value="<?php echo (!empty($_POST['site_url'])) ? $_POST['site_url']: '';?>"> 
                                            </div>
                                        </div>
                                        <hr>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteEmail">Site name</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="siteName" value="<?php echo (!empty($_POST['siteName'])) ? $_POST['siteName']: '';?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteEmail">Site title</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="siteTitle" value="<?php echo (!empty($_POST['siteTitle'])) ? $_POST['siteTitle']: '';?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteEmail">Site E-mail</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="siteEmail" value="<?php echo (!empty($_POST['siteEmail'])) ? $_POST['siteEmail']: '';?>">
                                            </div>
                                        </div>
                                        <hr>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteEmail">Admin username</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="admin_username" value="<?php echo (!empty($_POST['admin_username'])) ? $_POST['admin_username']: '';?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteEmail">Admin passowrd</label>  
                                            <div class="col-md-6">
                                                <input type="text" class="form-control" name="admin_password" value="<?php echo (!empty($_POST['admin_password'])) ? $_POST['admin_password']: '';?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3" for="siteEmail"></label>  
                                            <div class="col-md-6">
                                                Note: Installation process may take few minutes.
                                            </div>
                                        </div>
                                        
                                        <input type="hidden" name="install" value="install">
                                        <div class="form-group last-btn">
                                           <label class="col-md-3"></label>  
                                           <div class="col-md-6">
                                              <button type="submit" onclick="Wo_SubmitButton();" class="btn btn-main" <?php echo ($disabled == true) ? 'disabled': '';?>>
                                              <i class="fa fa-download progress-icon" data-icon="download"></i> Install
                                              </button>   
                                           </div>
                                        </div>
                                    </form>
                                </div>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </div>
        </div>
    </body>
</html>
<style>
    button:disabled {
        color: #fff !important;
    }
</style>
<script>
function Wo_SubmitButton() {
    $('button').attr('disabled', true);
    $('button').text('Please wait..');
    $('form').submit();
}
    $(function() {
        $('#agree').change(function() {
            if($(this).is(":checked")) {
                $('#next-terms').attr('disabled', false);
            } else {
            	$('#next-terms').attr('disabled', true);
            }       
        });
    });
</script>
<style>
    body {
    background: #f9f9f9;
    }
    form {
    margin-bottom: 0;
    }
    .btn-main {
    color: #ffffff;
    background-color: #a84849;
    border-color: #a84849;
    }
    .btn-main:disabled {
    color: #333;
    border: none;
    }
    .btn-main:hover {
    color: #ffffff;
    background-color: #c45a5b;
    border-color: #c45a5b;
    }
    .admin-panel .col-md-9 .list-group-item:first-child,
    .setting-panel .col-md-8 .list-group-item:first-child,
    .profile-lists .list-group-item:first-child,
    .col-md-8 .list-group-item:first-child,
    .col-sm-4 .list-group-item:first-child,
    .red-list .list-group-item:first-child {
    color: #ffffff;
    background-color: #a84849;
    }
    .admin-panel .col-md-9 .list-group-item:first-child a,
    .setting-panel .col-md-8 .list-group-item:first-child a,
    .profile-lists .list-group-item:first-child a,
    .col-md-8 .list-group-item:first-child a {
    color: #ffffff !important;
    }
    .list-group-item.black-list.active-list {
    color: #ffffff;
    background-color: #a84849;
    }
    .list-group-item.black-list {
    background: #ffffff;
    }
    .profile-top-line {
    background-color: #a84849;
    }
    #bar {
    background-color: #a84849;
    }
    .list-group-item.black-list a {
    color: #444444;
    }
    .list-group-item.black-list.active-list a {
    color: #ffffff;
    }
    .main-color,
    .small-text a {
    color: #a84849 !important;
    }
    .search-advanced-container a:hover {
    text-decoration: none;
    color: #ffffff;
    background-color: #a84849;
    }
    .nav-tabs>li.active>a,
    .nav-tabs>li.active>a:focus,
    .nav-tabs>li.active>a:hover {
    color: #ffffff;
    cursor: default;
    color: #a84849;
    border-bottom: 1px solid #a84849;
    background-color: transparent
    }
    .btn-active {
    color: #ffffff;
    background: #a84849;
    outline: none;
    border: 1px solid #a84849
    }
    .btn-active:hover,
    .btn-active:focus {
    border: 1px solid #c45a5b;
    color: #ffffff;
    background: #c45a5b;
    }
    .btn-active-color:hover {
    background: #c45a5b;
    }
    .chat-container .online-toggle {
    background: #a84849;
    }
    .chat-tab .online-toggle {
    background: #a84849;
    }
    .profile-style .user-follow-button button.btn-active,
    .btn-login,
    .btn-register {
    background: #a84849;
    color: #ffffff;
    }
    .profile-style .user-follow-button button.btn-active:hover,
    .btn-login:hover,
    .btn-login:focus,
    .btn-register:hover,
    .btn-register:focus {
    color: #ffffff;
    background: #c45a5b;
    }
    .panel-login button:disabled {
    background-color: #A33E40;
    }
    .panel-login>.panel-heading a.active {
    color: #a84849;
    font-size: 18px;
    }
</style>