<?php
$failure = false;
?>
<!DOCTYPE html>
<html>
<head>
<title>Install</title>
</head>
<body>
<h1>Installation check</h1>
<?php

// PDO
$ext = get_loaded_extensions();
$msg = '<p>PDO database interface: <span style="color:%s">%s</span>'."</p>\n";
$failure = ! in_array('PDO', $ext);
$res = $failure ? ['red', 'Error'] : ['green', 'OK'];
echo vsprintf($msg, $res);

// config file
if (! $failure) {
    $conffile = '../config.php';
    $perms = fileperms($conffile);
    if ($perms & 0x07) {
        echo '<p>Config file world readable: <span style="color:red">Error</span>', "</p>\n";
    }
    if ($perms & 0x10) {
        echo '<p>Config file writeable by webserver: <span style="color:red">Error</span>', "</p>\n";
    }
    $msg = '<p>Read config file: <span style="color:%s">%s</span>'."</p>\n";
    $failure = (! include($conffile));
    $res = $failure ? ['red', 'Error'] : ['green', 'OK'];
    echo vsprintf($msg, $res);
} else {
    echo "<p>Configfile correct?</p>";
}

// Database connection
if (! $failure) {
    try {
        $dbh = new PDO("mysql:host=$config_mysql_host", $config_mysql_username, $config_mysql_password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        $details = "<pre>" . $e->getMessage() . "</pre>\n";
        $failure = true;
    }
    $msg = '<p>Database connection: <span style="color:%s">%s</span>'."</p>\n";
    $res = $failure ? ['red', 'Error'] : ['green', 'OK'];
    echo vsprintf($msg, $res);
    if ($failure) {
        echo $details;
    }
} else {
    echo "<p>Database connection available?</p>\n";
}

// Ipreg database exists
if (! $failure) {
    $sql = "SELECT SCHEMA_NAME FROM
            INFORMATION_SCHEMA.SCHEMATA
            WHERE SCHEMA_NAME=?";
    $sth = $dbh->prepare($sql);
    $sth->execute([$config_mysql_dbname]);
    $failure = ! $sth->fetchColumn();
    $msg = '<p>Database exists: <span style="color:%s">%s</span>'."</p>\n";
    $res = $failure ? ['red', 'Error'] : ['green', 'OK'];
    echo vsprintf($msg, $res);
    $dbh->query("USE $config_mysql_dbname");
} else {
    echo "<p>Database available?</p>\n";
}
?>

<h2>Rights</h2>
<?php
// Admin-user?
if (! $failure) {
    $admincount = 0;

    // Admin count
    $sql = "SELECT user_id FROM user WHERE FIND_IN_SET('admin',user_role)>0";
    $sth = $dbh->query($sql);
    $adminlist = $sth->fetchAll(PDO::FETCH_ASSOC);
    $admincount = count($adminlist);
    if ($admincount == 0) {
        echo '<p>No admin user exists: <span style="color:red">Error</span>'."</p>\n";
    }

    // Default admin
    $sql = "SELECT user_pass FROM user WHERE user_name='admin' AND FIND_IN_SET('admin',user_role)>0";
    $sth = $dbh->query($sql);
    if ($rec = $sth->fetchColumn()) {
        // Check default password
        if ($rec == '$2y$10$HTs0lSaFrfr.q4Gmy5zWfeDg3jhYZkqEGZEnDkMiHZ641nso38mt6') {
           echo '<p>Password for default admin has not been changed: <span style="color:orange">Warning</span>'."</p>\n";
        } else {
           echo '<p>Default admin exists: <span style="color:green">OK</span>'."</p>\n";
        }
    } else {
        echo "<p>Default admin does not exist.</p>\n";
        if ($admincount > 0) {
            echo '<p>There are more admin accounts: <span style="color:green">OK</span>', "</p>\n";
        }
    }
} else {
    echo "<p>Administrative user available?</p>\n";
}

// Smarty
$compiledir = '../tpl_c';
$failure = ! is_writeable($compiledir);
$msg = '<p>Smarty compile directory writable: <span style="color:%s">%s</span>'."</p>\n";
$res = $failure ? ['red', 'Error'] : ['green', 'OK'];
echo vsprintf($msg, $res);

?>
<h2>Summary</h2>
<p>If everything here checks ok the installation directory <tt>install</tt>
should be removed.</p>
</body>
</html>