<?php
    session_start();     
    
    include_once('functions.php');

    $config = array();
    $config['domain'] = $_SERVER["HTTP_HOST"];
    $config['sql'] = 'vcms.sql';
    $config['dbconfig'] = 'db.inc.php';
    $config['dbconfigDest'] = '../_includes/db.inc.php';
    $config['install'] = '../install';
    $config['phpversion'] = '5.0.0';
    $config['faf'] = array(
        '_includes/',
        'install/',
        'upload/',
        'tmp/',
        'tmp/cache/',
        'tmp/templates_c/'
    );
    
    $_SESSION['reqs'] = true;
    
    if($_POST) {
        foreach($_POST as $k => $v) if (!is_array($v)) $_POST[$k] = trim($v);
    }
    
    if (PHP_VERSION >= $config['phpversion']) {
        $version_res = true;
        $version = PHP_VERSION;
    } else {
        $version_res = false;
        $_SESSION['reqs'] = false;
        $version = PHP_VERSION;
    }
    
    if(ini_get('register_globals') == 1) {
        $rg_res = true;
        $_SESSION['reqs'] = false;
    } 
    else if(ini_get('register_globals') == 0) {
        $rg_res = false;
    }
    else {
        $rg_res = false;
    }

    $mysql_res = extension_loaded('mysql');
    if (!$mysql_res) $_SESSION['reqs'] = false;
    
    $mysqli_res = extension_loaded('mysqli');
    if (!$mysqli_res) $_SESSION['reqs'] = false;
    
    // make install
    if ($_POST['action'] == 'Install') {
        
        if($_POST['dbhost'] && $_POST['dbusername'] && $_POST['dbpassword'] && $_POST['dbname']) {
        
            $db_error = false;
            $db = @mysqli_connect($_POST['dbhost'], $_POST['dbusername'], $_POST['dbpassword'], $_POST['dbname']);        
        
        } else {
            
            $db_error = true;
            
        }                        
        
        if (!$db) {
            $db_error = true;
        }
        else {                    
            mysqli_close($db);
            
            $db = new mysqli($_POST['dbhost'], $_POST['dbusername'], $_POST['dbpassword'], $_POST['dbname']);
            importSQLFile($db, $config['sql']);
            
            $dbCfgFile = file_get_contents($config['dbconfig']);
            $dbCfgFile = str_replace('localhost', $_POST['dbhost'], $dbCfgFile);
            $dbCfgFile = str_replace('DB_USERNAME_HERE', $_POST['dbusername'], $dbCfgFile);
            $dbCfgFile = str_replace('DB_NAME_HERE', $_POST['dbname'], $dbCfgFile);
            $dbCfgFile = str_replace('DB_PASSWORD_HERE', $_POST['dbpassword'], $dbCfgFile);
            $h = fopen($config['dbconfigDest'], 'w');		
            fwrite($h, $dbCfgFile);
            fclose($h);
            
            rename($config['install'], $config['install'] . md5(microtime(true)));
            
            $installOK = true;
        }
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Internet SpeedTest CMS Installer - V-SPEED.EU</title>
        <meta name="description" content="Internet SpeedTest CMS Installer - V-SPEED.EU" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta http-equiv="Content-Style-Type" content="text/css" />

        <!-- Google WebFonts -->                
        <link href='http://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic,500,500italic' rel='stylesheet' type='text/css'>

        <!-- Stylesheets -->
        <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
    </head>
    <body>
        <h1>Welcome to the Internet SpeedTest CMS Installer!</h1>
        
<?php
    if ($installOK) {
?>
        <div class="">
            <h2>Installation is sucessful!</h2>
            You can go to see your webpage: <a href="http://<?php print $config['domain'] . str_replace('install/', '', $_SERVER['REQUEST_URI']); ?>" title="Main Page" target="_blank">http://<?php print $config['domain'] . str_replace('install/', '', $_SERVER['REQUEST_URI']); ?></a><br />
            Or you can go to the admin panel: <a href="http://<?php print $config['domain'] . str_replace('install/', '', $_SERVER['REQUEST_URI']); ?>admin/" title="Main Page" target="_blank">http://<?php print $config['domain'] . str_replace('install/', '', $_SERVER['REQUEST_URI']); ?>admin/</a><br /><br />
            Default login: <b>admin</b><br />
            Default password: <b>vcms</b><br /><br />
            <span class="bad">This installer will not be accessible anymore.</span><br><br>
            <span class="bad"><b>IMPORTANT:</b> In case of the Internal Server Error (500) uncomment the "RewriteBase /" directive in .htaccess file.</span>
        </div>
<?php
    }
    else {
?>
        <div class="reqs">            
            <h2>Required PHP settings</h2>
            <table>
                <tr>
                    <td>PHP version:</td>
                    <td><?php print $version . ' '; print ($version_res) ? '<span class="ok">ok</span>' : '<span class="bad">failed (minimum: ' . $config['phpversion'] . ')</span>'; ?></td>
                </tr>
                <tr>
                    <td>Register globals:</td>
                    <td><?php print ($rg_res) ? '<span class="bad">failed (please disable)</span>' : '<span class="ok">ok</span>'; ?></td>
                </tr>
            </table>
            <hr>
            <h2>Required PHP modules</h2>
            <table>
                <tr>
                    <td>MySQL:</td>
                    <td><?php print ($mysql_res) ? '<span class="ok">ok</span>' : '<span class="bad">failed (please enable)</span>'; ?></td>
                </tr>
                <tr>
                    <td>MySQLi:</td>
                    <td><?php print ($mysqli_res) ? '<span class="ok">ok</span>' : '<span class="bad">failed (please enable)</span>'; ?></td>
                </tr>
            </table>
            <hr>
            <h2>Files and folder permissions</h2>
            <table>
                <?php
                foreach($config['faf'] as $k => $v) {  
                ?>
                <tr>
                    <td><?php print $v; ?></td>
                    <td><?php print is_writable('../' . $v) ? '<span class="ok">is writable</span>' : '<span class="bad">is not writable (please enable)</span>'; ?></td>
                </tr>
                <?php
                }
                ?>
            </table>
            <hr>
        </div>
<?php
        if (!$_SESSION['reqs']) {
?>
        <div class="check-reqs"><br>Your server does not meet presented requirements. Please fix the configuration and click the "Refresh" button.<br><br></div>
        <form action="" method="post"><input type="submit" name="action" value="Refresh"/></form>
<?php
        }
        else {
            if ($_POST['action'] == 'Install') {
                if ($db_error) {
                    print '<span class="bad">Could not connect to database. Check provided data.</span><br />';
                }
            }
?>
        <br />
        <div class="text">Please, fill form and click "Install" button.</div>
        <form action="" method="post">
            <table>
                <tr>
                    <td><label for="dbhost">Database host</label></td>
                    <td><input autocomplete="off" type="text" name="dbhost" value="<?php print $_POST['dbhost'] ? $_POST['dbhost'] : "127.0.0.1"; ?>" /></td>
                </tr>
                <tr>
                    <td><label for="dbname">Database name</label></td>
                    <td><input autocomplete="off" type="text" name="dbname" value="<?php print $_POST['dbname']; ?>" /></td>
                </tr>
                <tr>
                    <td><label for="dbusername">Database username</label></td>
                    <td><input autocomplete="off" type="text" name="dbusername" value="<?php print $_POST['dbusername']; ?>" /></td>
                </tr>
                <tr>
                    <td><label for="dbpassword">Database password</label></td>
                    <td><input autocomplete="off" type="password" name="dbpassword" value="<?php print $_POST['dbpassword']; ?>" /></td>
                </tr>
            </table>
            
            <input type="submit" name="action" value="Install" />
        </form>
<?php
        }
    }
?>
        
        <br>You can also run Cloud Speed Test without CMS. Click the url: <a href="http://<?php print $config['domain'] . str_replace('install/', '', $_SERVER['REQUEST_URI']); ?>speedtest/">http://<?php print $config['domain'] . str_replace('install/', '', $_SERVER['REQUEST_URI']); ?>speedtest/</a>
        
    </body>
</html>