bbn/index.php

222 lines
5.5 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>
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;
}
$action = '';
2024-03-06 08:33:44 +01:00
if (isset($_GET['open']))
{
$action = 'open';
}
else if (isset($_GET['search']))
{
$action = 'search';
}
else if (isset($_GET['tags']))
{
$action = 'tags';
}
else if (isset($_GET['clip']))
{
$action = 'clip';
}
else if (isset($_POST['save']))
{
$action = 'save';
}
else if (isset($_POST['delete']))
{
$action = 'delete';
}
else if (isset($_POST['home']))
2024-02-20 16:23:37 +01:00
{
$action = 'save';
}
2024-03-06 08:33:44 +01:00
else if(isset($_GET['home']))
{
$action = '';
}
else if (isset($_POST['preview']))
{
$action = 'preview';
}
2024-02-20 16:23:37 +01:00
$content = '';
2024-03-06 08:33:44 +01:00
if ($action == 'clip' && $_GET['param'])
{
2024-03-06 14:44:52 +01:00
$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo');
file_put_contents($dir . '/todo', $content);
2024-03-06 08:33:44 +01:00
$_GET['param'] = '';
}
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';
2024-03-06 08:33:44 +01:00
$_GET['param'] = $title;
2024-02-20 16:23:37 +01:00
if (isset($_POST['home']))
{
header('Location: index.php');
}
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
2024-02-19 12:20:20 +01:00
if ($action == 'preview')
{
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);
}
2024-02-19 12:20:20 +01:00
$Parsedown = new Parsedown();
$Parsedown->setBreaksEnabled(true);
2024-03-06 08:33:44 +01:00
echo $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos + 3));
2024-02-19 12:20:20 +01:00
}
else if ($action == 'open')
2024-02-14 14:23:25 +01:00
{
2024-03-06 08:33:44 +01:00
$title = $_GET['param'];
$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";
2024-03-06 08:33:44 +01:00
file_put_contents($dir . '/' . $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
$lines = substr_count($content, "\r\n") + 1;
$words = $lines + substr_count($content, " ");
$rows = max(20, $lines) * 2;
$length = strlen($content);
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();
if (preg_match_all('/https:(.*)[ \r]/', $content, $links, PREG_SET_ORDER))
{
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>';
}
echo '<span class="grey">' . $title . ' - ' . $now . ' - ' . $length . 'c - ' . $words . 'w - ' . $lines . 'l </span>';
2024-02-14 14:23:25 +01:00
}
else
{
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 class="editor">';
$files = glob($dir . '/*');
usort($files, function($a, $b)
{
return filemtime($b) - filemtime($a);
});
foreach($files as $path)
{
$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 ($action == 'search' || $action == 'tags')
{
$content = file_get_contents($path);
2024-03-06 12:14:08 +01:00
if (($action == 'search' && !str_contains(strtolower($content), strtolower($param)) && !str_contains(strtolower($name), strtolower($param)))
|| ($action == '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 ($action == 'tags')
{
$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>';
}
}
2024-03-06 12:14:08 +01:00
else if ($action == '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>