bbn/index.php

233 lines
6.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
error_reporting(E_ALL);
2024-02-14 14:23:25 +01:00
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></body></html>';
2024-02-14 14:23:25 +01:00
exit;
}
session_start();
2024-03-18 17:22:22 +01:00
if (!isset($_SESSION['index']) || isset($_GET['reindex']) || empty($_SESSION['index']))
{
$files = glob($dir . '/*');
usort($files, function($a, $b)
{
return filemtime($b) - filemtime($a);
});
$_SESSION['index'] = array_map(fn($value): string => str_replace('.md', '', $value), $files);
2024-03-18 17:22:22 +01:00
}
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;
2024-03-15 15:11:51 +01:00
$path = $dir . '/' . $title;
$lastchanged = filemtime($path);
$previous = $_POST['lastchanged'];
2024-04-04 10:04:04 +02:00
if (!isset($_GET['clip']) && (int)$lastchanged > (int)$previous)
2024-03-15 15:11:51 +01:00
{
$tempcontent = file_get_contents($path . '.md');
2024-03-15 15:11:51 +01:00
if ($tempcontent != $content)
{
$temptitle = $title . '_' . $lastchanged;
file_put_contents($dir . '/' . $temptitle . '.md', $tempcontent);
2024-03-15 15:11:51 +01:00
array_unshift($_SESSION['index'], $dir . '/' . $temptitle);
echo '<div class="grey">Conflict detected. See backup: ' . $temptitle . '</div>';
}
}
file_put_contents($path . '.md', $content);
removefromindex($title);
2024-03-15 15:11:51 +01:00
array_unshift($_SESSION['index'], $path);
2024-03-06 08:33:44 +01:00
}
2024-02-20 16:23:37 +01:00
2024-03-12 12:12:13 +01:00
function linksdiv($content)
{
$divcontent = '<div class="grey">';
$links = array();
if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER))
{
foreach($links as $link)
{
$divcontent .= '<div>
<a href="index.php?open=true&param=' . urlencode($link[1]) . '">' . $link[1] . '</a>
</div>';
}
}
$links = array();
if (preg_match_all('/https:(.*)\b/', $content, $links, PREG_SET_ORDER))
{
foreach($links as $link)
{
$divcontent .= '<div>
<a target="_blank" href="https:' . $link[1] . '">https:' . $link[1] . '</a>
</div>';
}
}
$divcontent .= '</div>';
return $divcontent;
}
$nextpage = 'home';
if (isset($_GET['clip']) && $_GET['param'])
2024-03-06 08:33:44 +01:00
{
$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo.md');
savenote('todo', $content);
2024-03-06 08:33:44 +01:00
$_GET['param'] = '';
}
2024-03-12 14:09:49 +01:00
else if (isset($_POST['save']) || isset($_POST['home']) || isset($_POST['links']) || isset($_POST['stats']))
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 . '.md', $dir . '/' . $previoustitle . '.md.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 . '.md', $dir . '/' . $title . '.md.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
}
if (!file_exists($dir . '/' . $title . '.md'))
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
{
$content = file_get_contents($dir . '/' . $title . '.md');
2024-02-14 14:23:25 +01:00
}
2024-03-12 12:12:13 +01:00
2024-03-15 15:11:51 +01:00
clearstatcache();
$lastchanged = filemtime($dir . '/' . $title . '.md');
2024-03-11 13:58:27 +01:00
$lines = substr_count($content, "\r\n");
$words = substr_count($content, " ") + $lines;
$chars = strlen($content);
2024-03-06 11:53:57 +01:00
2024-03-11 13:58:27 +01:00
$rows = max(20, $lines) * 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
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>';
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 . '.md');
2024-03-06 11:53:57 +01:00
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>