<?php
$m  =   "";

if(isset($_POST['dbhost'], $_POST['dbname'], $_POST['dbusername'], $_POST['dbpass'])){
    try {
        $db = new PDO('mysql:host=' . $_POST['dbhost'] . ';dbname=' . $_POST['dbname'], $_POST['dbusername'], $_POST['dbpass']);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $dbSQL   =   file_get_contents('db.sql.txt');
        $createSite =   $db->prepare($dbSQL);
        $createSite->execute();
        $createSite->closeCursor();

        // Create Directory
        if ( !mkdir("uploads/a/1/admin", 0777, true) ) {
            die("Unable to make admin directory");
        }

        if(empty($_POST['site_dir'])){
            $_POST['site_dir']  =   "/";
        }

        if(substr($_POST['site_dir'], -1) != "/"){
            $_POST['site_dir'] .= "/";
        }

        $insertQuery    =   $db->prepare(
            "
            INSERT INTO `settings` (`site_name`, `site_desc`,`site_domain`,`site_dir` )
            VALUES('Owl Chat', 'Owl Chat Description','" . $_POST['site_domain'] . "','" . $_POST['site_dir'] . "');

            INSERT INTO `users` (`username`, `email`, `pass`,`upl_dir`,`mem_level`,`time_created`,`header_img`) VALUES
            ('admin', 'admin@admin.com', '9660de0dfbac70576eb520968bbc1b768d04072c5f7719f85ce2a7023a74ac59b81bb36cf70b8dbe2ec30bac4be521a2fefc76a44822d8171d4796c089c2979d', 'a/1/admin', '2','" . time() . "', 'headers/4.jpg');

            INSERT INTO `emotes` (`id`, `img`, `e_cmd`, `img_name`)
            VALUES
            (1, '9sEo42qVEgSfWEib.png', ':D', 'Really Happy'),
            (2, 'znftqBWai2vehtUq.png', ':)', 'Happy'),
            (3, 'yFbyyyrNO3Ldn4Wk.png', ':|', 'Neutral'),
            (4, 'vD7sHXrhHozXL9RV.png', ':o', 'Surprised'),
            (5, 'BdkHe43UAQTcEuFe.png', ':(', 'sad'),
            (6, 'VlE52gwbPrP8Jvbk.png', '>:]', 'Evil'),
            (7, 'kX5vdEecx7mee54Q.png', 'x_x', 'Dead'),
            (8, 'lUPCBvxHfOuMepRQ.png', ':s', 'Confused'),
            (9, 'ZVksEvGpllY2id9r.png', ':@', 'Angry'),
            (10, 'R9gqxjrX3pff8bw5.png', '>_>', 'Annoyed'),
            (11, 'bs4nN27tPgVGeF8E.png', 'D:', 'Shocked');
            "
        );
        $insertQuery->execute();

        unlink("db.sql.txt");

        $file   =   file_get_contents('inc/db-sample.php');
        $file   =   str_replace("OCHOST",trim($_POST["dbhost"]),$file);
        $file   =   str_replace("OCDBNAME",trim($_POST["dbname"]),$file);
        $file   =   str_replace("OCUSER",trim($_POST["dbusername"]),$file);
        $file   =   str_replace("OCPASS",trim($_POST["dbpass"]),$file);
        $fh     =   fopen('inc/db.php', 'w') or die("Can't open config.php. Make sure it is writable.");
        fwrite($fh, $file);
        fclose($fh);
        $m  =   '
        <div class="alert alert-success text-center">
            <strong>Owl Chat hass been installed successfully!</strong><br>
            To get started, please log into the account we created for you during installation.<br>
            <strong>Username: </strong> admin <br>
            <strong>Password: </strong> admin<br>
            <strong>PLEASE DELETE THE install.php FILE! THIS IS VERY IMPORTANT</strong>
        </div>';
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Owl Chat Install - www.20script.ir</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <!-- Google webfont -->
    <link href='http://fonts.googleapis.com/css?family=Noto+Sans:400,700' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
    <!-- FontAwesome css -->
    <link href="//netdna.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- Core bootstrap -->
    <link rel="stylesheet" href="assets/bs/css/bootstrap.min.css">
    <!-- Kanda's extended style -->
    <link rel="stylesheet" href="assets/bs/css/extended.min.css">
    <!-- Modernizr -->
    <!--[if IE 8]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.1/modernizr.min.js"></script>
    <![endif]-->
</head>
<body>

<div class="container-fluid">
    <!--left-->
    <div class="col-sm-12">
        <h1 class="lobster text-center"><i class="fa fa-comments-o"></i>Install Owl Chat - www.20Script.ir</h1>
        <div class="panel sky">
            <div class="panel-heading">Configuration</div>
            <div class="panel-body">
                <?php echo $m; ?>
                <form method="POST">
                    <div class="form-group">
                        <label>Database Host</label>
                        <input type="text" class="form-control" name="dbhost">
                    </div>
                    <div class="form-group">
                        <label>Database Name</label>
                        <input type="text" class="form-control" name="dbname">
                    </div>
                    <div class="form-group">
                        <label>Database Username</label>
                        <input type="text" class="form-control" name="dbusername">
                    </div>
                    <div class="form-group">
                        <label>Database Password</label>
                        <input type="text" class="form-control" name="dbpass">
                    </div>
                    <div class="form-group">
                        <label>Site Domain</label>
                        <input type="text" class="form-control" name="site_domain">
                    <span id="helpBlock" class="help-block">
                        The domain name of your site. Do not include http:// or a forward slash after the domain.
                        Example: euphonize-demo.com
                    </span>
                    </div>
                    <div class="form-group">
                        <label>Site Directory</label>
                        <input type="text" class="form-control" name="site_dir">
                    <span id="helpBlock" class="help-block">
                        The directory your site is installed. If you're installing on the root, then
                        just leave it as / If your site is installed in a folder called music then you would input
                        /music/
                    </span>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-sky btn-block">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div><!--/left-->
</div><!--/container-fluid-->

<!-- jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Core Bootstrap -->
<script src="assets/bs/js/bootstrap.min.js"></script>
<!-- Kanda's extended script -->
<script src="assets/bs/js/extended.min.js"></script>
<script>
    // initialize Kanda extended script
    extended = new Extended();
</script>
</body>
</html>