<?php
/**
 * wallabag, self hostable application allowing you to not miss any content anymore
 *
 * @category   wallabag
 * @author     Nicolas Lœuillet <nicolas@loeuillet.org>
 * @copyright  2013
 * @license    http://opensource.org/licenses/MIT see COPYING file
 */

$errors = array();
$successes = array();

/* Function taken from at http://php.net/manual/en/function.rmdir.php#110489
 * Idea : nbari at dalmp dot com
 * Rights unknown
 * Here in case of .gitignore files
 */
function delTree($dir) {
    $files = array_diff(scandir($dir), array('.','..'));
    foreach ($files as $file) {
      (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
    }
    return rmdir($dir);
  }

if (isset($_GET['clean'])) {
    if (is_dir('install')){
    delTree('install');
    header('Location: index.php');      
    }
}

if (isset($_POST['download'])) {
    if (!file_put_contents("cache/vendor.zip", fopen("http://static.wallabag.org/files/vendor.zip", 'r'))) {
        $errors[] = 'Impossible to download vendor.zip. Please <a href="http://wllbg.org/vendor">download it manually</a> and unzip it in your wallabag folder.';
    }
    else {
        if (extension_loaded('zip')) {
            $zip = new ZipArchive();
            if ($zip->open("cache/vendor.zip") !== TRUE){
                $errors[] = 'Impossible to open cache/vendor.zip. Please unzip it manually in your wallabag folder.';
            }
            if ($zip->extractTo(realpath(''))) {
                @unlink("cache/vendor.zip");
                $successes[] = 'twig is now installed, you can install wallabag.';
            }
            else {
                $errors[] = 'Impossible to extract cache/vendor.zip. Please unzip it manually in your wallabag folder.';
            }
            $zip->close();
        }
        else {
            $errors[] = 'zip extension is not enabled in your PHP configuration. Please unzip cache/vendor.zip in your wallabag folder.';
        }
    }
}
else if (isset($_POST['install'])) {
    if (!is_dir('vendor')) {
        $errors[] = 'You must install twig before.';
    }
    else {
        $continue = true;
        // Create config.inc.php
        if (!copy('inc/poche/config.inc.default.php', 'inc/poche/config.inc.php')) {
            $errors[] = 'Installation aborted, impossible to create inc/poche/config.inc.php file. Maybe you don\'t have write access to create it.';
            $continue = false;
        }
        else {
            function generate_salt() {
                mt_srand(microtime(true)*100000 + memory_get_usage(true));
                return md5(uniqid(mt_rand(), true));
            }

            $content = file_get_contents('inc/poche/config.inc.php');
            $salt = generate_salt();
            $content = str_replace("define ('SALT', '');", "define ('SALT', '".$salt."');", $content);
            file_put_contents('inc/poche/config.inc.php', $content);
        }

        if ($continue) {

            // User informations
            $username = trim($_POST['username']);
            $password = trim($_POST['password']);
            $salted_password = sha1($password . $username . $salt);

            // Database informations
            $moreQueries = array();
            if ($_POST['db_engine'] == 'sqlite') {
                if (!copy('install/poche.sqlite', 'db/poche.sqlite')) {
                    $errors[] = 'Impossible to create inc/poche/config.inc.php file.';
                    $continue = false;
                }
                else {
                    $db_path = 'sqlite:' . realpath('') . '/db/poche.sqlite';
                    $handle = new PDO($db_path);
                    $sql_structure = "";
                }
            }
            else {
                $content = file_get_contents('inc/poche/config.inc.php');

                if ($_POST['db_engine'] == 'mysql') {
                    $db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database'] . ';charset=utf8mb4';
                    $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['mysql_server']."');", $content);
                    $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['mysql_database']."');", $content);
                    $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['mysql_user']."');", $content);
                    $content = str_replace("define ('STORAGE_PASSWORD', 'poche');", "define ('STORAGE_PASSWORD', '".$_POST['mysql_password']."');", $content);
                    $handle = new PDO($db_path, $_POST['mysql_user'], $_POST['mysql_password'], array(
                        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
                    ));

                    $moreQueries[] = "INSERT INTO `entries` (`id`, `title`, `url`, `is_read`, `is_fav`, `content`, `user_id`) VALUES
(1, 'Framabag, un nouveau service libre et gratuit', 'http://www.framablog.org/index.php/post/2014/02/05/Framabag-service-libre-gratuit-interview-developpeur', 0, 0, [encoded data, 22668 characters], 1),
(2, 'wallabag/wallabag', 'https://github.com/wallabag/wallabag', 0, 0, 0x3c7370616e20636c6173733d226e616d65223e524541444d452e6d643c2f7370616e3e3c703e77616c6c6162616720697320612073656c6620686f737461626c65206170706c69636174696f6e20616c6c6f77696e6720796f7520746f206e6f74206d69737320616e7920636f6e74656e7420616e796d6f72652e20436c69636b2c20736176652c2072656164206974207768656e20796f752063616e2e20497420657874726163747320636f6e74656e7420736f207468617420796f752063616e2072656164206974207768656e20796f7520686176652074696d652e3c2f703e0a3c703e4d6f726520696e666f726d6174696f6e73206f6e206f757220776562736974653a203c6120687265663d22687474703a2f2f77616c6c616261672e6f7267223e77616c6c616261672e6f72673c2f613e3c2f703e0a3c68323e3c6120636c6173733d22616e63686f722220687265663d2268747470733a2f2f6769746875622e636f6d2f77616c6c616261672f77616c6c61626167236c6963656e7365223e3c2f613e4c6963656e73653c2f68323e0a3c703e436f7079726967687420c2a920323031302d32303134204e69636f6c6173204cc59375696c6c6574203c6120687265663d226d61696c746f3a6e69636f6c6173406c6f6575696c6c65742e6f7267223e6e69636f6c6173406c6f6575696c6c65742e6f72673c2f613e205468697320776f726b20697320667265652e20596f752063616e2072656469737472696275746520697420616e642f6f72206d6f6469667920697420756e64657220746865207465726d73206f662074686520446f205768617420546865204675636b20596f752057616e7420546f205075626c6963204c6963656e73652c2056657273696f6e20322c206173207075626c69736865642062792053616d20486f63657661722e205365652074686520434f5059494e472066696c6520666f72206d6f72652064657461696c732e3c2f703e0a, 1),
(3, 'a self hostable application for saving web pages | wallabag', 'https://www.wallabag.org/', 0, 1, 0x0a3c64697620636c6173733d22726f77223e0a3c64697620636c6173733d22636f6c2d6c672d3820636f6c2d6d642d313220636f6c2d78732d313220636f6c2d736d2d3132223e0a3c703e77616c6c616261672028666f726d65726c7920706f636865292069732061203c7374726f6e673e73656c6620686f737461626c65206170706c69636174696f6e20666f7220736176696e67207765622070616765733c2f7374726f6e673e2e20556e6c696b65206f746865722073657276696365732cc2a077616c6c6162616720697320667265652028617320696e2066726565646f6d2920616e64206f70656e20736f757263652e3c2f703e0a3c2f6469763e0a0a3c2f6469763e0a3c64697620636c6173733d22726f77223e0a3c64697620636c6173733d22636f6c2d6c672d3820636f6c2d6d642d313220636f6c2d78732d313220636f6c2d736d2d3132223e0a3c703e576974682074686973206170706c69636174696f6e20796f752077696c6c206e6f74206d69737320636f6e74656e7420616e796d6f72652e203c7374726f6e673e436c69636b2c20736176652c2072656164206974207768656e20796f752077616e743c2f7374726f6e673e2e2049742073617665732074686520636f6e74656e7420796f752073656c65637420736f207468617420796f752063616e2072656164206974207768656e20796f7520686176652074696d652e3c2f703e0a3c2f6469763e0a0a3c2f6469763e0a3c64697620636c6173733d22726f77223e0a3c64697620636c6173733d22636f6c2d6c672d3620636f6c2d6d642d313220636f6c2d78732d313220636f6c2d736d2d3132223e0a3c68323e486f7720697420776f726b733c2f68323e0a3c703e5468616e6b7320746f2074686520626f6f6b6d61726b6c6574206f72203c61207469746c653d22446f776e6c6f6164732220687265663d22687474703a2f2f7777772e77616c6c616261672e6f72672f646f776e6c6f6164732f223e74686972642d7061727479206170706c69636174696f6e733c2f613e2c20796f75207361766520616e2061727469636c6520696e20796f7572c2a077616c6c6162616720746f2072656164206974206c617465722e205468656e2c207768656e20796f75206f70656e20796f75722077616c6c616261672c203c7374726f6e673e796f752063616e20636f6d666f727461626c79207265616420796f75722061727469636c65733c2f7374726f6e673e2e3c2f703e0a3c68323e486f7720746f207573652077616c6c616261673c2f68323e0a3c703e5468657265206172652074776f207761797320746f207573652077616c6c616261673a20796f752063616e203c6120687265663d22687474703a2f2f7777772e77616c6c616261672e6f72672f6672657175656e746c792d61736b65642d7175657374696f6e732f23486f775f63616e5f495f696e7374616c6c5f77616c6c616261675f616e645f776861745f6172655f7468655f726571756972656d656e7473223e696e7374616c6c2069743c2f613ec2a06f6e20796f75722077656220736572766572206f7220796f752063616ec2a03c6120687265663d22687474703a2f2f6170702e696e746865706f6368652e636f6d223e63726561746520616e206163636f756e743c2f613ec2a06174204672616d616261672028776520696e7374616c6c20616e642075706772616465c2a077616c6c6162616720666f7220796f75292e3c2f703e0a3c2f6469763e0a0a3c2f6469763e0a, 1);";
                    $moreQueries[] = "INSERT INTO `tags` (`id`, `value`) VALUES (1, 'opensource');";
                    $moreQueries[] = "INSERT INTO `tags_entries` (`id`, `entry_id`, `tag_id`) VALUES (1, 2, 1);";

                    $sql_structure = file_get_contents('install/mysql.sql');
                }
                else if ($_POST['db_engine'] == 'postgres') {
                    $db_path = 'pgsql:host=' . $_POST['pg_server'] . ';dbname=' . $_POST['pg_database'];
                    $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['pg_server']."');", $content);
                    $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['pg_database']."');", $content);
                    $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['pg_user']."');", $content);
                    $content = str_replace("define ('STORAGE_PASSWORD', 'poche');", "define ('STORAGE_PASSWORD', '".$_POST['pg_password']."');", $content);
                    $handle = new PDO($db_path, $_POST['pg_user'], $_POST['pg_password']);
                    
                    $moreQueries[] = "INSERT INTO entries (title, url, is_read, is_fav, content, user_id) VALUES
('Framabag, un nouveau service libre et gratuit', 'http://www.framablog.org/index.php/post/2014/02/05/Framabag-service-libre-gratuit-interview-developpeur', false, false, convert_from(decode('[encoded data, 22664 characters]', 'hex'), 'UTF8'), 1),
('wallabag/wallabag', 'https://github.com/wallabag/wallabag', false, false, convert_from(decode('3c7370616e20636c6173733d226e616d65223e524541444d452e6d643c2f7370616e3e3c703e77616c6c6162616720697320612073656c6620686f737461626c65206170706c69636174696f6e20616c6c6f77696e6720796f7520746f206e6f74206d69737320616e7920636f6e74656e7420616e796d6f72652e20436c69636b2c20736176652c2072656164206974207768656e20796f752063616e2e20497420657874726163747320636f6e74656e7420736f207468617420796f752063616e2072656164206974207768656e20796f7520686176652074696d652e3c2f703e0a3c703e4d6f726520696e666f726d6174696f6e73206f6e206f757220776562736974653a203c6120687265663d22687474703a2f2f77616c6c616261672e6f7267223e77616c6c616261672e6f72673c2f613e3c2f703e0a3c68323e3c6120636c6173733d22616e63686f722220687265663d2268747470733a2f2f6769746875622e636f6d2f77616c6c616261672f77616c6c61626167236c6963656e7365223e3c2f613e4c6963656e73653c2f68323e0a3c703e436f7079726967687420c2a920323031302d32303134204e69636f6c6173204cc59375696c6c6574203c6120687265663d226d61696c746f3a6e69636f6c6173406c6f6575696c6c65742e6f7267223e6e69636f6c6173406c6f6575696c6c65742e6f72673c2f613e205468697320776f726b20697320667265652e20596f752063616e2072656469737472696275746520697420616e642f6f72206d6f6469667920697420756e64657220746865207465726d73206f662074686520446f205768617420546865204675636b20596f752057616e7420546f205075626c6963204c6963656e73652c2056657273696f6e20322c206173207075626c69736865642062792053616d20486f63657661722e205365652074686520434f5059494e472066696c6520666f72206d6f72652064657461696c732e3c2f703e0a', 'hex'), 'UTF8'), 1),
('a self hostable application for saving web pages | wallabag', 'https://www.wallabag.org/', false, true, convert_from(decode('3c64697620636c6173733d22726f77223e0a3c64697620636c6173733d22636f6c2d6c672d3820636f6c2d6d642d313220636f6c2d78732d313220636f6c2d736d2d3132223e0a3c703e77616c6c616261672028666f726d65726c7920706f636865292069732061203c7374726f6e673e73656c6620686f737461626c65206170706c69636174696f6e20666f7220736176696e67207765622070616765733c2f7374726f6e673e2e20556e6c696b65206f746865722073657276696365732cc2a077616c6c6162616720697320667265652028617320696e2066726565646f6d2920616e64206f70656e20736f757263652e3c2f703e0a3c2f6469763e0a0a3c2f6469763e0a3c64697620636c6173733d22726f77223e0a3c64697620636c6173733d22636f6c2d6c672d3820636f6c2d6d642d313220636f6c2d78732d313220636f6c2d736d2d3132223e0a3c703e576974682074686973206170706c69636174696f6e20796f752077696c6c206e6f74206d69737320636f6e74656e7420616e796d6f72652e203c7374726f6e673e436c69636b2c20736176652c2072656164206974207768656e20796f752077616e743c2f7374726f6e673e2e2049742073617665732074686520636f6e74656e7420796f752073656c65637420736f207468617420796f752063616e2072656164206974207768656e20796f7520686176652074696d652e3c2f703e0a3c2f6469763e0a0a3c2f6469763e0a3c64697620636c6173733d22726f77223e0a3c64697620636c6173733d22636f6c2d6c672d3620636f6c2d6d642d313220636f6c2d78732d313220636f6c2d736d2d3132223e0a3c68323e486f7720697420776f726b733c2f68323e0a3c703e5468616e6b7320746f2074686520626f6f6b6d61726b6c6574206f72203c61207469746c653d22446f776e6c6f6164732220687265663d22687474703a2f2f7777772e77616c6c616261672e6f72672f646f776e6c6f6164732f223e74686972642d7061727479206170706c69636174696f6e733c2f613e2c20796f75207361766520616e2061727469636c6520696e20796f7572c2a077616c6c6162616720746f2072656164206974206c617465722e205468656e2c207768656e20796f75206f70656e20796f75722077616c6c616261672c203c7374726f6e673e796f752063616e20636f6d666f727461626c79207265616420796f75722061727469636c65733c2f7374726f6e673e2e3c2f703e0a3c68323e486f7720746f207573652077616c6c616261673c2f68323e0a3c703e5468657265206172652074776f207761797320746f207573652077616c6c616261673a20796f752063616e203c6120687265663d22687474703a2f2f7777772e77616c6c616261672e6f72672f6672657175656e746c792d61736b65642d7175657374696f6e732f23486f775f63616e5f495f696e7374616c6c5f77616c6c616261675f616e645f776861745f6172655f7468655f726571756972656d656e7473223e696e7374616c6c2069743c2f613ec2a06f6e20796f75722077656220736572766572206f7220796f752063616ec2a03c6120687265663d22687474703a2f2f6170702e696e746865706f6368652e636f6d223e63726561746520616e206163636f756e743c2f613ec2a06174204672616d616261672028776520696e7374616c6c20616e642075706772616465c2a077616c6c6162616720666f7220796f75292e3c2f703e0a3c2f6469763e0a0a3c2f6469763e0a', 'hex'), 'UTF8'), 1)";
                    $moreQueries[] = "INSERT INTO tags (value) VALUES ('opensource')";
                    $moreQueries[] = "INSERT INTO tags_entries (entry_id, tag_id) VALUES (2, 1)";

                    $sql_structure = file_get_contents('install/postgres.sql');
                }

                $content = str_replace("define ('STORAGE', 'sqlite');", "define ('STORAGE', '".$_POST['db_engine']."');", $content);
                file_put_contents('inc/poche/config.inc.php', $content);
            }

            if ($continue) {

                function executeQuery($handle, $sql, $params) {
                    try
                    {
                        $query = $handle->prepare($sql);
                        $query->execute($params);
                        return $query->fetchAll();
                    }
                    catch (Exception $e)
                    {
                        return FALSE;
                    }
                }

                if ($_POST['db_engine'] != "sqlite") {
                    // create database structure
                    $query = $handle->exec($sql_structure);
                }

                // Create user
                $handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $sql = "INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, '')";
                $params = array($username, $salted_password, $username);
                $query = executeQuery($handle, $sql, $params);

                $id_user = (int)$handle->lastInsertId('users_id_seq');

                $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
                $params = array($id_user, 'pager', '10');
                $query = executeQuery($handle, $sql, $params);

                $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
                $params = array($id_user, 'language', 'en_EN.UTF8');
                $query = executeQuery($handle, $sql, $params);

                foreach ($moreQueries as $query) {
                    executeQuery($handle, $query, array());
                }

                $successes[] = 'wallabag is now installed. You can now <a href="index.php?clean=0">access it !</a>';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0">
        <meta charset="utf-8">
        <!--[if IE]>
        <meta http-equiv="X-UA-Compatible" content="IE=10">
        <![endif]-->
        <title>wallabag - installation</title>
        <link rel="shortcut icon" type="image/x-icon" href="themes/baggy/img/favicon.ico" />
        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="themes/baggy/img/apple-touch-icon-144x144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="themes/baggy/img/apple-touch-icon-72x72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="themes/baggy/img/apple-touch-icon-precomposed.png">
        <link href='//fonts.googleapis.com/css?family=PT+Sans:700' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="themes/baggy/css/ratatouille.css" media="all">
        <link rel="stylesheet" href="themes/baggy/css/font.css" media="all">
        <link rel="stylesheet" href="themes/baggy/css/main.css" media="all">
        <link rel="stylesheet" href="themes/baggy/css/messages.css" media="all">
        <link rel="stylesheet" href="themes/baggy/css/print.css" media="print">
        <script src="themes/default/js/jquery-2.0.3.min.js"></script>
        <script src="themes/baggy/js/init.js"></script>
    </head>
    <body>
        <header class="w600p center mbm">
            <h1 class="logo">
                <img width="100" height="100" src="themes/baggy/img/logo-w.png" alt="logo poche" />
            </h1>
        </header>
        <div id="main">
            <button id="menu" class="icon icon-menu desktopHide"><span>Menu</span></button>
            <ul id="links" class="links">
                <li><a href="http://www.wallabag.org/frequently-asked-questions/">FAQ</a></li>
                <li><a href="http://doc.wallabag.org/">doc</a></li>
                <li><a href="http://www.wallabag.org/help/">help</a></li>
                <li><a href="http://www.wallabag.org/">wallabag.org</a></li>
            </ul> 
            <?php if (!empty($errors)) : ?>
                <div class='messages error install'>
                    <p>Errors during installation:</p>
                    <p>
                        <ul>
                        <?php foreach($errors as $error) :?>
                            <li><?php echo $error; ?></li>
                        <?php endforeach; ?>
                        </ul>
                    </p>
                    <p><a href="index.php">Please reload</a> this page when you think you resolved these problems.</p>
                </div>
            <?php endif; ?>
            <?php if (!empty($successes)) : ?>
                <div class='messages success install'>
                    <p>
                        <ul>
                        <?php foreach($successes as $success) :?>
                            <li><?php echo $success; ?></li>
                        <?php endforeach; ?>
                        </ul>
                    </p>
                </div>
            <?php else : ?>
                <?php if (file_exists('inc/poche/config.inc.php') && is_dir('vendor')) : ?>
                <div class='messages success install'>
                    <p>
                        wallabag seems already installed. If you want to update it, you only have to delete install folder, then <a href="index.php">reload this page</a>.
                    </p>
                </div>
                <?php endif; ?>    
            <?php endif; ?>
            <p>To install wallabag, you just have to fill the following fields. That's all.</p>
            <p>Don't forget to check your server compatibility <a href="install/wallabag_compatibility_test.php?from=install">here</a>.</p>
            <form method="post">
                <fieldset>
                    <legend><strong>Technical settings</strong></legend>
                    <?php if (!is_dir('vendor')) : ?>
                        <div class='messages notice install'>wallabag needs twig, a template engine (<a href="http://twig.sensiolabs.org/">?</a>). Two ways to install it:<br />
                        <ul>
                            <li>automatically download and extract vendor.zip into your wallabag folder. 
                            <p><input type="submit" name="download" value="Download vendor.zip" /></p>
                            <?php if (!extension_loaded('zip')) : ?>
                                <b>Be careful, zip extension is not enabled in your PHP configuration. You'll have to unzip vendor.zip manually.</b>
                            <?php endif; ?>
                                <em>This method is mainly recommended if you don't have a dedicated server.</em></li>
                            <li>use <a href="http://getcomposer.org/">Composer</a> :<pre><code>curl -s http://getcomposer.org/installer | php
php composer.phar install</code></pre></li>
                        </ul>
                        </div>
                    <?php endif; ?>
                    <p>
                        Database engine:
                        <ul>
                            <li><label for="sqlite">SQLite</label> <input name="db_engine" type="radio" checked="" id="sqlite" value="sqlite" />
                            <div id="pdo_sqlite" class='messages error install'>
                                <p>You have to enable <a href="http://php.net/manual/ref.pdo-sqlite.php">pdo_sqlite extension</a>.</p>
                            </div>
                            </li>
                            <li>
                                <label for="mysql">MySQL</label> <input name="db_engine" type="radio" id="mysql" value="mysql" />
                                <ul id="mysql_infos">
                                    <li><label for="mysql_server">Server</label> <input type="text" placeholder="localhost" id="mysql_server" name="mysql_server" /></li>
                                    <li><label for="mysql_database">Database</label> <input type="text" placeholder="wallabag" id="mysql_database" name="mysql_database" /></li>
                                    <li><label for="mysql_user">User</label> <input type="text" placeholder="user" id="mysql_user" name="mysql_user" /></li>
                                    <li><label for="mysql_password">Password</label> <input type="text" placeholder="p4ssw0rd" id="mysql_password" name="mysql_password" /></li>
                                </ul>
                            </li>
                            <li>
                                <label for="postgres">PostgreSQL</label> <input name="db_engine" type="radio" id="postgres" value="postgres" />
                                <ul id="pg_infos">
                                    <li><label for="pg_server">Server</label> <input type="text" placeholder="localhost" id="pg_server" name="pg_server" /></li>
                                    <li><label for="pg_database">Database</label> <input type="text" placeholder="wallabag" id="pg_database" name="pg_database" /></li>
                                    <li><label for="pg_user">User</label> <input type="text" placeholder="user" id="pg_user" name="pg_user" /></li>
                                    <li><label for="pg_password">Password</label> <input type="text" placeholder="p4ssw0rd" id="pg_password" name="pg_password" /></li>
                                </ul>
                            </li>
                        </ul>
                    </p>
                </fieldset>

                <fieldset>
                    <legend><strong>User settings</strong></legend>
                    <p>
                        <label for="username">Username</label>
                        <input type="text" required id="username" name="username" value="wallabag" />
                    </p>
                    <p>
                        <label for="password">Password</label>
                        <input type="password" required id="password" name="password" value="wallabag" />
                        <label for="show">Show password:</label> <input style="margin-left:-80px;" name="show" id="show" type="checkbox" onchange="document.getElementById('password').type = this.checked ? 'text' : 'password'">
                    </p>
                    <p>
                        <label for="email">Email (not required)</label>
                        <input type="email" id="email" name="email" />
                    </p>
                </fieldset>

                <input type="submit" id="install_button" value="Install wallabag" name="install" />
            </form>
        </div>
        <script>
            $("#mysql_infos").hide();
            $("#pg_infos").hide();

            <?php
            if (!extension_loaded('pdo_sqlite')) : ?>
            $("#install_button").hide();
            <?php
            else :
            ?>
            $("#pdo_sqlite").hide();
            <?php
            endif;
            ?>

            $("input[name=db_engine]").click(function() 
                {
                    if ( $("#mysql").prop('checked')) {
                        $("#mysql_infos").show();
                        $("#pg_infos").hide();
                        $("#pdo_sqlite").hide();
                        $("#install_button").show();
                    }
                    else {
                        if ( $("#postgres").prop('checked')) {
                            $("#mysql_infos").hide();
                            $("#pg_infos").show();
                            $("#pdo_sqlite").hide();
                            $("#install_button").show();
                        }
                        else {
                            $("#mysql_infos").hide();
                            $("#pg_infos").hide();
                            <?php
                            if (!extension_loaded('pdo_sqlite')) : ?>
                            $("#pdo_sqlite").show();
                            $("#install_button").hide();
                            <?php
                            endif;
                            ?>
                        }
                    }
                });
        </script>
    </body>
</html>