bbn/index.php

206 lines
5.7 KiB
PHP
Raw Normal View History

2024-02-13 22:57:28 +01:00
<!DOCTYPE html>
<html>
2024-02-14 14:23:25 +01:00
<head>
<title>
<?php
if (isset($_GET['action']) && $_GET['action'] == 'open')
2024-02-14 14:23:25 +01:00
{
echo $_GET['param'];
2024-02-14 14:23:25 +01:00
}
?>
</title>
2024-02-14 18:22:26 +01:00
<meta name="theme-color" content="white" />
2024-02-14 14:23:25 +01:00
<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;
}
.grey {
color: lightgrey;
2024-02-14 14:23:25 +01:00
}
</style>
</head>
<body>
<?php
require 'settings.php';
2024-02-14 14:23:25 +01:00
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;
}
$action = '';
if (isset($_POST['action'])) $action = $_POST['action'];
else if (isset($_GET['action'])) $action = $_GET['action'];
$param = '';
if (isset($_GET['param'])) $param = $_GET['param'];
$content = '';
if ($action == 'save')
2024-02-14 14:23:25 +01:00
{
$title = $_POST['title'];
$content = $_POST['content'];
file_put_contents($dir . '/' . $title, $content);
$previoustitle = $_POST['previoustitle'];
if ($title != $previoustitle)
{
rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del');
}
$action = 'open';
$param = $title;
2024-02-14 14:23:25 +01:00
}
else if ($action == 'delete')
2024-02-14 14:23:25 +01:00
{
$title = $_POST['title'];
rename($dir . '/' . $title, $dir . '/' . $title . '.del');
}
2024-02-13 22:57:28 +01:00
if ($action == 'open')
2024-02-14 14:23:25 +01:00
{
$now = date("Y-m-d H.i.s", time());
2024-02-16 18:08:18 +01:00
if (!$param)
{
$param = $now;
2024-02-16 18:08:18 +01:00
}
2024-02-16 18:08:18 +01:00
if (!file_exists($dir . '/' . $param))
2024-02-14 14:23:25 +01:00
{
2024-02-19 10:26:10 +01:00
$content = "---\r\ndate: " . substr($now, 0, 10) . "\r\ntags: \r\n---\r\n";
file_put_contents($dir . '/' . $param, $content);
2024-02-14 14:23:25 +01:00
}
else if (!$content)
2024-02-14 14:23:25 +01:00
{
2024-02-16 18:08:18 +01:00
$content = file_get_contents($dir . '/' . $param);
2024-02-14 14:23:25 +01:00
}
2024-02-13 22:57:28 +01:00
$lines = substr_count($content, "\r\n") + 1;
$words = $lines + substr_count($content, " ");
$rows = max(20, $lines) + 10;
$length = strlen($content);
2024-02-14 08:54:24 +01:00
2024-02-14 14:23:25 +01:00
echo '<form action="index.php" method="POST">
2024-02-14 14:23:00 +01:00
<div>
<a accesskey="h" href="' . $root . '">home</a>
<select name="action">
<option value="save">save</option>
<option value="delete">delete</option>
</select>
<input class="title" name="title" value="' . $param . '">
2024-02-16 17:55:21 +01:00
<input type="submit" name="go" value="go" accesskey="s">
2024-02-14 14:23:00 +01:00
</div>
<br>
2024-02-14 14:23:25 +01:00
<div>
<textarea rows="' . $rows. '" autofocus name="content" spellcheck="false">' . $content . '</textarea>
2024-02-14 14:23:25 +01:00
</div>
<input hidden name="previoustitle" value="' . $param . '">
2024-02-14 14:23:25 +01:00
</form>';
$links = array();
if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER)
|| preg_match_all('/http(.*)[ \r]/', $content, $links, PREG_SET_ORDER))
{
echo '<div>Internal links:</div>';
foreach($links as $link)
{
echo '<div>
<a href="index.php?action=open&param=' . urlencode($link[1]) . '">' . $link[1] . '</a>
</div>';
}
echo '<br>';
}
$links = array();
if (preg_match_all('/http(.*)[ \r]/', $content, $links, PREG_SET_ORDER))
{
echo '<div>External links:</div>';
foreach($links as $link)
{
echo '<div>
<a target="_blank" href="http' . $link[1] . '">http' . $link[1] . '</a>
</div>';
}
echo '<br>';
}
echo '<span class="grey">' . $param . ' - ' . $now . ' - ' . $length . 'c - ' . $words . 'w - ' . $lines . 'l </span>';
2024-02-14 14:23:25 +01:00
}
else
{
2024-02-14 14:23:00 +01:00
echo '<form action="index.php" method="GET">
<a accesskey="h" href="' . $root . '">home</a>
<select name="action">
<option ' . ($action == 'open' ? 'selected' : '') . ' value="open">open</option>
<option ' . ($action == 'filter' ? 'selected' : '') . ' value="filter">filter</option>
<option ' . ($action == 'search' ? 'selected' : '') . ' value="search">search</option>
<option ' . ($action == 'tags' ? 'selected' : '') . ' value="tags">tags</option>
</select>
<input autofocus name="param" value="' . $param . '">
2024-02-16 17:55:21 +01:00
<input accesskey="s" type="submit" value="go">
2024-02-14 14:23:00 +01:00
</form>';
2024-02-14 14:23:25 +01:00
echo '<br>';
2024-02-14 14:23:00 +01:00
2024-02-14 16:59:19 +01:00
$files = glob($dir . '/*');
usort($files, function($a, $b)
2024-02-14 14:23:00 +01:00
{
2024-02-14 16:59:19 +01:00
return filemtime($b) - filemtime($a);
});
foreach($files as $path)
{
$tags = '';
$name = basename($path);
if (!str_ends_with($name, '.del') && ($action != 'filter' || str_contains($name, $param)))
2024-02-14 14:23:00 +01:00
{
if ($action == 'search' || $action == 'tags')
2024-02-16 16:58:06 +01:00
{
$content = file_get_contents($path);
if (($action == 'search' && !str_contains($content, $param)) ||
($action == 'tags' && !preg_match('/tags:.*' . $param . '/', $content)))
2024-02-16 16:58:06 +01:00
{
continue;
}
}
echo '<div><a href=index.php?action=open&param=' . urlencode($name) . '>' . $name .'</a>';
if ($action == 'tags')
{
$tags = array();
$hastags = preg_match_all('/tags:(.*)/', $content, $tags, PREG_SET_ORDER);
if ($hastags)
{
$tagslist = explode(',', $tags[0][1]);
echo '<span class="grey"> ';
foreach ($tagslist as $tag)
{
$tag = trim($tag);
echo('<a class="grey" href="index.php?action=tags&param=' . $tag . '">' . $tag . '</a> ');
}
echo '</span>';
}
}
else if ($action == 'search')
{
$pos = strpos($content, $param);
echo '<span class="grey"> <b>' . $param . '</b>' . substr($content, $pos + strlen($param), 42) . '</span>';
}
echo'</div>';
2024-02-14 14:23:25 +01:00
}
}
2024-02-14 14:23:00 +01:00
echo '<br>';
2024-02-14 14:23:25 +01:00
}
?>
</body>
2024-02-13 22:57:28 +01:00
</html>