2024-09-30 10:09:49 +02:00
|
|
|
<?php
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
require 'settings.php';
|
2024-02-13 23:38:09 +01:00
|
|
|
|
2024-09-30 10:09:49 +02: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>';
|
|
|
|
exit;
|
|
|
|
}
|
2024-03-07 10:02:11 +01:00
|
|
|
|
2024-09-30 10:09:49 +02:00
|
|
|
session_start();
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-10-02 17:36:51 +02:00
|
|
|
function cleanstring($string)
|
|
|
|
{
|
|
|
|
return iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', strtolower($string));
|
|
|
|
}
|
|
|
|
|
2024-10-02 11:49:15 +02:00
|
|
|
function downloadnote($path)
|
|
|
|
{
|
|
|
|
header('Content-disposition: attachment; filename=' . basename($path));
|
|
|
|
header('Content-type: text/markdown');
|
|
|
|
readfile($path);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
function downloadall()
|
2024-09-30 10:09:49 +02:00
|
|
|
{
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
$zip_name = 'bbn.zip';
|
|
|
|
$zip->open($zip_name, ZipArchive::CREATE);
|
|
|
|
foreach($_SESSION['index'] as $path)
|
|
|
|
{
|
|
|
|
if (!str_ends_with($path, '.del'))
|
2024-03-07 10:02:11 +01:00
|
|
|
{
|
2024-09-30 10:09:49 +02:00
|
|
|
$path = $path . '.md';
|
|
|
|
if (file_exists($path))
|
|
|
|
{
|
|
|
|
$zip->addFile($path, basename($path));
|
|
|
|
}
|
|
|
|
else
|
2024-03-07 10:02:11 +01:00
|
|
|
{
|
2024-09-30 10:09:49 +02:00
|
|
|
echo"file does not exist";
|
2024-03-07 10:02:11 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-30 10:09:49 +02:00
|
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
|
|
|
|
header('Content-disposition: attachment; filename=' . $zip_name);
|
|
|
|
header('Content-type: application/zip');
|
|
|
|
readfile($zip_name);
|
|
|
|
|
|
|
|
unlink($zip_name);
|
|
|
|
exit;
|
|
|
|
}
|
2024-03-07 10:02:11 +01:00
|
|
|
|
2024-09-30 10:09:49 +02: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)
|
|
|
|
{
|
|
|
|
global $dir;
|
|
|
|
$path = $dir . '/' . $title;
|
|
|
|
$lastchanged = filemtime($path);
|
|
|
|
$previous = $_POST['lastchanged'];
|
|
|
|
if (!isset($_GET['clip']) && (int)$lastchanged > (int)$previous)
|
|
|
|
{
|
|
|
|
$tempcontent = file_get_contents($path . '.md');
|
|
|
|
if ($tempcontent != $content)
|
2024-03-06 08:33:44 +01:00
|
|
|
{
|
2024-09-30 10:09:49 +02:00
|
|
|
$temptitle = $title . '_' . $lastchanged;
|
|
|
|
file_put_contents($dir . '/' . $temptitle . '.md', $tempcontent);
|
|
|
|
array_unshift($_SESSION['index'], $dir . '/' . $temptitle);
|
|
|
|
echo '<div class="grey">Conflict detected. See backup: ' . $temptitle . '</div>';
|
2024-03-06 08:33:44 +01:00
|
|
|
}
|
2024-09-30 10:09:49 +02:00
|
|
|
}
|
|
|
|
file_put_contents($path . '.md', $content);
|
|
|
|
removefromindex($title);
|
|
|
|
array_unshift($_SESSION['index'], $path);
|
|
|
|
}
|
2024-02-20 16:23:37 +01:00
|
|
|
|
2024-09-30 10:09:49 +02:00
|
|
|
function linksdiv($content)
|
|
|
|
{
|
|
|
|
$divcontent = '<div class="grey">';
|
|
|
|
$links = array();
|
|
|
|
if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER))
|
|
|
|
{
|
|
|
|
foreach($links as $link)
|
2024-03-12 12:12:13 +01:00
|
|
|
{
|
2024-09-30 10:09:49 +02:00
|
|
|
$divcontent .= '<div>
|
|
|
|
<a href="index.php?open=true¶m=' . urlencode($link[1]) . '">' . $link[1] . '</a>
|
|
|
|
</div>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$links = array();
|
2024-10-01 09:03:59 +02:00
|
|
|
if (preg_match_all('/(https?:.*)\b/', $content, $links, PREG_SET_ORDER))
|
2024-09-30 10:09:49 +02:00
|
|
|
{
|
|
|
|
foreach($links as $link)
|
|
|
|
{
|
|
|
|
$divcontent .= '<div>
|
2024-10-01 09:03:59 +02:00
|
|
|
<a target="_blank" href="' . $link[1] . '">' . $link[1] . '</a>
|
2024-09-30 10:09:49 +02:00
|
|
|
</div>';
|
2024-03-12 12:12:13 +01:00
|
|
|
}
|
2024-09-30 10:09:49 +02:00
|
|
|
}
|
|
|
|
$divcontent .= '</div>';
|
|
|
|
return $divcontent;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nextpage = 'home';
|
2024-10-02 11:49:15 +02:00
|
|
|
$preview = '';
|
2024-09-30 10:09:49 +02:00
|
|
|
|
|
|
|
if (isset($_GET['download']))
|
|
|
|
{
|
2024-10-02 11:49:15 +02:00
|
|
|
downloadall();
|
2024-09-30 10:09:49 +02:00
|
|
|
}
|
2024-10-02 11:49:15 +02:00
|
|
|
else if (isset($_GET['clip']) && $_GET['param'])
|
2024-10-01 10:51:28 +02:00
|
|
|
{
|
|
|
|
$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo.md');
|
|
|
|
savenote('todo', $content);
|
|
|
|
$_GET['param'] = '';
|
|
|
|
}
|
2024-10-02 11:49:15 +02:00
|
|
|
else if (isset($_POST['save']) || isset($_POST['home']) || isset($_POST['links']) || isset($_POST['stats']) || isset($_POST['download']))
|
2024-10-01 10:51:28 +02: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);
|
|
|
|
}
|
|
|
|
|
2024-10-02 11:49:15 +02:00
|
|
|
if (isset($_POST['download']))
|
|
|
|
{
|
|
|
|
downloadnote($dir . '/' . $title . '.md');
|
|
|
|
}
|
|
|
|
else if (!isset($_POST['home']))
|
2024-10-01 10:51:28 +02:00
|
|
|
{
|
|
|
|
$nextpage = 'note';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isset($_POST['delete']))
|
|
|
|
{
|
|
|
|
$title = $_POST['title'];
|
|
|
|
rename($dir . '/' . $title . '.md', $dir . '/' . $title . '.md.del');
|
|
|
|
removefromindex($title);
|
|
|
|
}
|
|
|
|
else if (isset($_POST['preview']))
|
|
|
|
{
|
|
|
|
require 'libs/Parsedown.php';
|
|
|
|
$title = $_POST['title'];
|
|
|
|
$content = $_POST['content'];
|
|
|
|
$pos = 0;
|
|
|
|
if (str_starts_with($content, '---'))
|
|
|
|
{
|
|
|
|
$pos = strpos($content, '---', 3) + 3;
|
|
|
|
}
|
|
|
|
$Parsedown = new Parsedown();
|
|
|
|
$Parsedown->setBreaksEnabled(true);
|
|
|
|
$preview = $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos));
|
|
|
|
$nextpage = 'preview';
|
|
|
|
}
|
|
|
|
else if (isset($_GET['open']))
|
|
|
|
{
|
|
|
|
$nextpage = 'note';
|
|
|
|
$title = $_GET['param'];
|
|
|
|
}
|
2024-09-30 10:09:49 +02:00
|
|
|
?>
|
|
|
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2024-10-01 10:51:28 +02:00
|
|
|
<title>
|
|
|
|
<?php
|
|
|
|
if ($nextpage != 'home' && isset($title))
|
|
|
|
{
|
|
|
|
echo $title;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
echo 'bbn';
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
</title>
|
2024-09-30 10:09:49 +02:00
|
|
|
<link rel="manifest" href="manifest.json" />
|
|
|
|
<meta name="theme-color" content="white" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<link rel="stylesheet" type="text/css" href="style.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
2024-03-12 12:12:13 +01:00
|
|
|
|
2024-09-30 10:09:49 +02:00
|
|
|
<?php
|
2024-10-01 10:51:28 +02:00
|
|
|
if ($nextpage == 'preview')
|
2024-03-07 12:48:31 +01:00
|
|
|
{
|
2024-10-01 10:51:28 +02:00
|
|
|
echo $preview;
|
2024-02-19 12:20:20 +01:00
|
|
|
}
|
2024-10-01 10:51:28 +02:00
|
|
|
else if ($nextpage == 'note')
|
2024-02-14 14:23:25 +01:00
|
|
|
{
|
2024-02-19 10:18:10 +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-02-14 08:41:48 +01:00
|
|
|
|
2024-04-04 11:10:19 +02: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";
|
2024-03-07 10:02:11 +01:00
|
|
|
savenote($title, $content);
|
2024-02-14 14:23:25 +01:00
|
|
|
}
|
2024-02-19 10:18:10 +01:00
|
|
|
else if (!$content)
|
2024-02-14 14:23:25 +01:00
|
|
|
{
|
2024-04-04 11:10:19 +02: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();
|
2024-04-04 11:10:19 +02:00
|
|
|
$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
|
|
|
}
|
2024-03-07 12:48:31 +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
|
|
|
|
2024-03-07 10:02:11 +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
|
|
|
{
|
2024-03-20 12:04:00 +01:00
|
|
|
if (($_GET['param'] && isset($_GET['search'])) || isset($_GET['tags']))
|
2024-03-06 11:53:57 +01:00
|
|
|
{
|
2024-04-04 11:10:19 +02:00
|
|
|
$content = file_get_contents($path . '.md');
|
2024-10-01 09:21:11 +02:00
|
|
|
if (
|
|
|
|
// param does not match content or title
|
2024-10-02 17:36:51 +02:00
|
|
|
(isset($_GET['search']) && !str_contains(cleanstring($content), cleanstring($param)) && !str_contains(cleanstring($name), cleanstring($param))) ||
|
2024-10-01 10:51:28 +02:00
|
|
|
// param does not match tags
|
2024-10-01 09:21:11 +02:00
|
|
|
(isset($_GET['tags']) && !preg_match('/tags:.*' . $param . '/i', $content))
|
|
|
|
)
|
2024-03-06 11:53:57 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2024-10-01 09:21:11 +02:00
|
|
|
else if (isset($_GET['search']) && $param == $name)
|
|
|
|
{
|
|
|
|
header('Location: index.php?open=true¶m=' . urlencode($name));
|
|
|
|
die();
|
|
|
|
}
|
2024-03-06 11:53:57 +01:00
|
|
|
}
|
|
|
|
echo '<div><a href=index.php?open=true¶m=' . urlencode($name) . '>' . $name .'</a>';
|
2024-03-08 16:45:50 +01:00
|
|
|
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¶m=' . $tag . '">' . $tag . '</a> ');
|
|
|
|
}
|
|
|
|
echo '</span>';
|
|
|
|
}
|
|
|
|
}
|
2024-10-02 17:36:51 +02:00
|
|
|
else if (isset($_GET['search']) && str_contains(cleanstring($content), cleanstring($param)))
|
2024-03-06 11:53:57 +01:00
|
|
|
{
|
2024-10-02 17:36:51 +02:00
|
|
|
$pos = strpos(cleanstring($content), cleanstring($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>
|