bbn/index.php

209 lines
5.3 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>bbn</title>
2024-02-19 17:28:43 +01:00
<link rel="manifest" href="manifest.json" />
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">
2024-02-20 16:23:04 +01:00
<link rel="stylesheet" type="text/css" href="style.css">
2024-02-14 14:23:25 +01:00
</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;
}
session_start();
function removefromindex($title)
{
global $dir;
$key = array_search($dir . '/' . $title, $_SESSION['index']);
if ($key !== FALSE)
{
array_splice($_SESSION['index'], $key, 1);
}
}
function savenote($title, $content)
2024-03-06 08:33:44 +01:00
{
global $dir;
file_put_contents($dir . '/' . $title, $content);
removefromindex($title);
array_unshift($_SESSION['index'], $dir . '/' . $title);
2024-03-06 08:33:44 +01:00
}
2024-02-20 16:23:37 +01:00
$nextpage = 'home';
if (isset($_GET['clip']) && $_GET['param'])
2024-03-06 08:33:44 +01:00
{
2024-03-06 14:44:52 +01:00
$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo');
savenote('todo', $content);
2024-03-06 08:33:44 +01:00
$_GET['param'] = '';
}
else if (isset($_POST['save']) || isset($_POST['home']))
2024-02-14 14:23:25 +01:00
{
$title = $_POST['title'];
$content = $_POST['content'];
savenote($title, $content);
$previoustitle = $_POST['previoustitle'];
if ($title != $previoustitle)
{
rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del');
removefromindex($previoustitle);
}
if (!isset($_POST['home']))
2024-02-20 16:23:37 +01:00
{
$nextpage = 'note';
2024-02-20 16:23:37 +01:00
}
2024-02-14 14:23:25 +01:00
}
else if (isset($_POST['delete']))
2024-02-14 14:23:25 +01:00
{
$title = $_POST['title'];
rename($dir . '/' . $title, $dir . '/' . $title . '.del');
removefromindex($title);
2024-02-14 14:23:25 +01:00
}
else if (isset($_POST['preview']))
2024-02-19 12:20:20 +01:00
{
2024-02-20 16:23:37 +01:00
require 'libs/Parsedown.php';
2024-02-19 12:20:20 +01:00
$title = $_POST['title'];
$content = $_POST['content'];
2024-03-06 08:33:44 +01:00
$pos = 0;
if (str_starts_with($content, '---'))
{
$pos = strpos($content, '---', 3) + 3;
2024-03-06 08:33:44 +01:00
}
2024-02-19 12:20:20 +01:00
$Parsedown = new Parsedown();
$Parsedown->setBreaksEnabled(true);
echo $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos));
$nextpage = '';
}
else if (isset($_GET['open']))
{
$nextpage = 'note';
$title = $_GET['param'];
2024-02-19 12:20:20 +01:00
}
if ($nextpage == 'note')
2024-02-14 14:23:25 +01:00
{
$now = date("Y-m-d H.i.s", time());
2024-03-06 08:33:44 +01:00
if (!$title)
2024-02-16 18:08:18 +01:00
{
2024-03-06 08:33:44 +01:00
$title = $now;
2024-02-16 18:08:18 +01:00
}
2024-03-06 08:33:44 +01:00
if (!file_exists($dir . '/' . $title))
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";
savenote($title, $content);
2024-02-14 14:23:25 +01:00
}
else if (!$content)
2024-02-14 14:23:25 +01:00
{
2024-03-06 08:33:44 +01:00
$content = file_get_contents($dir . '/' . $title);
2024-02-14 14:23:25 +01:00
}
2024-03-06 11:53:57 +01:00
2024-03-07 14:59:06 +01:00
$rows = max(20, substr_count($content, "\r\n")) * 2;
2024-03-06 11:53:57 +01:00
2024-03-06 08:33:44 +01:00
require('note.php');
2024-03-06 11:53:57 +01:00
$links = array();
if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER))
{
echo '<div>Internal links:</div>';
foreach($links as $link)
{
echo '<div>
<a href="index.php?open=true&param=' . urlencode($link[1]) . '">' . $link[1] . '</a>
</div>';
}
echo '<br>';
}
$links = array();
2024-03-08 09:19:27 +01:00
if (preg_match_all('/https:(.*)\b/', $content, $links, PREG_SET_ORDER))
2024-03-06 11:53:57 +01:00
{
echo '<div>External links:</div>';
foreach($links as $link)
{
echo '<div>
<a target="_blank" href="https:' . $link[1] . '">https:' . $link[1] . '</a>
</div>';
}
echo '<br>';
}
2024-02-14 14:23:25 +01:00
}
else if ($nextpage == 'home')
2024-02-14 14:23:25 +01:00
{
2024-03-06 08:33:44 +01:00
if (isset($_GET['param']))
2024-02-14 14:23:00 +01:00
{
2024-03-06 08:33:44 +01:00
$param = $_GET['param'];
2024-02-14 14:23:25 +01:00
}
2024-03-06 08:33:44 +01:00
require('home.php');
2024-03-06 11:53:57 +01:00
echo '<div>';
if (!isset($_SESSION['index']) || isset($_GET['reindex']))
2024-03-06 11:53:57 +01:00
{
$files = glob($dir . '/*');
usort($files, function($a, $b)
{
return filemtime($b) - filemtime($a);
});
$_SESSION['index'] = $files;
}
2024-03-06 11:53:57 +01:00
foreach($_SESSION['index'] as $path)
2024-03-06 11:53:57 +01:00
{
$tags = '';
$name = basename($path);
2024-03-06 12:14:08 +01:00
if (!str_ends_with($name, '.del'))
2024-03-06 11:53:57 +01:00
{
if ($_GET['param'] && (isset($_GET['search']) || isset($_GET['tags'])))
2024-03-06 11:53:57 +01:00
{
$content = file_get_contents($path);
if ((isset($_GET['search']) && !str_contains(strtolower($content), strtolower($param)) && !str_contains(strtolower($name), strtolower($param)))
|| (isset($_GET['tags']) && !preg_match('/tags:.*' . $param . '/i', $content)))
2024-03-06 11:53:57 +01:00
{
continue;
}
}
echo '<div><a href=index.php?open=true&param=' . urlencode($name) . '>' . $name .'</a>';
if (isset($content) && isset($_GET['tags']))
2024-03-06 11:53:57 +01:00
{
$tags = array();
$hastags = preg_match_all('/tags:(.*)/i', $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?tags=true&param=' . $tag . '">' . $tag . '</a> ');
}
echo '</span>';
}
}
else if (isset($_GET['search']) && str_contains(strtolower($content), strtolower($param)))
2024-03-06 11:53:57 +01:00
{
$pos = strpos(strtolower($content), strtolower($param));
2024-03-06 12:14:08 +01:00
echo '<span class="grey"> ' . $param . substr($content, $pos + strlen($param), 42) . '</span>';
2024-03-06 11:53:57 +01:00
}
echo'</div>';
}
}
echo '</div>';
2024-02-14 14:23:25 +01:00
}
?>
</body>
2024-02-13 22:57:28 +01:00
</html>