<!DOCTYPE html>
<html>
    <head>
        <title>
            <?php
                echo 'bbn';
                if (isset($_GET['open']))
                {
                    echo ' | ' . $_GET['open'];
                }
            ?>
        </title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <meta charset="UTF-8">
        <style type="text/css">
            body {
                font-family: helvetica;
            }
            textarea {
                width: 100%;
                border: none;
                outline: none;
                font-family: inherit;
                font-size: inherit;
            }
            .title {
                width: 100%;
                border: none;
                outline: none;
                font-weight: bold;
            }
        </style>
    </head>
    <body>
        <?php
            require 'settings.php';

            if ($password && (!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] != $password)) {
                header('WWW-Authenticate: Basic realm="bbn"');
                header('HTTP/1.0 401 Unauthorized');
                echo '<p>Access denied.</p>';
                exit;
            }

            echo '<a accesskey="l" href="index.php">List</a>&nbsp;<a accesskey="n" href="index.php?open=' . time() . '.md">New</a>';

            if (isset($_POST['savebutton']))
            {
                $title = $_POST['title'];
                $content = $_POST['content'];
                file_put_contents($dir . '/' . $title, $content);
                $_GET['open'] = $title;
            }
            else if (isset($_POST['deletebutton']))
            {
                $title = $_POST['title'];
                rename($dir . '/' . $title, $dir . '/' . $title . '.del');
            }

            if (isset($_GET['open']))
            {
                $title = $_GET['open'];

                $content = '';
                if (!file_exists($dir . '/' . $title))
                {
                    file_put_contents($dir . '/' . $title, $content);
                }
                else
                {
                    $content = file_get_contents($dir . '/' . $title);
                }

                $nblines = substr_count($content, "\r\n");

                echo '<form action="index.php" method="POST">
                        <br><div><input class="title" readonly name="title" value="' . $title . '"></div><br>
                        <div>
                            <textarea rows="' . $nblines. '" autofocus name="content" spellcheck="false">' . $content . '</textarea>
                        </div>
                        <input type="submit" name="savebutton" value="save" accesskey="s">
                        <input accesskey="d" type="submit" name="deletebutton" value="delete">
                    </form>';
            }
            else
            {
                echo '<br><br>';
                if ($dh = opendir($dir)) {
                    while (($file = readdir($dh)) !== false) {
                        if (str_ends_with($file, '.md'))
                        {
                            $mod = date("F d Y H:i:s", filemtime($dir . '/' . $file));
                            echo '<div>' . $mod . '&nbsp;<a href=index.php?open=' . urlencode($file) . '>' . $file .'</a></div>';
                        }
                    }
                    closedir($dh);
                }
            }

        ?>
    </body>
</html>