refactor
This commit is contained in:
parent
84baa60a2b
commit
58291bd62b
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
error_reporting(E_ALL);
|
||||
require 'settings.php';
|
||||
|
||||
function cleanstring($string)
|
||||
{
|
||||
return iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', strtolower($string));
|
||||
}
|
||||
|
||||
function downloadall()
|
||||
{
|
||||
$zip = new ZipArchive();
|
||||
$zip_name = 'bbn.zip';
|
||||
$zip->open($zip_name, ZipArchive::CREATE);
|
||||
foreach($_SESSION['index'] as $path)
|
||||
{
|
||||
if (!str_ends_with($path, '.del'))
|
||||
{
|
||||
$path = $path . '.md';
|
||||
if (file_exists($path))
|
||||
{
|
||||
$zip->addFile($path, basename($path));
|
||||
}
|
||||
else
|
||||
{
|
||||
echo"file does not exist";
|
||||
}
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
|
||||
header('Content-disposition: attachment; filename=' . $zip_name);
|
||||
header('Content-type: application/zip');
|
||||
readfile($zip_name);
|
||||
|
||||
unlink($zip_name);
|
||||
exit;
|
||||
}
|
||||
|
||||
function savenote($title, $content)
|
||||
{
|
||||
global $dir;
|
||||
$path = $dir . '/' . $title;
|
||||
$lastchanged = filemtime($path);
|
||||
$previous = $_POST['lastchanged'];
|
||||
if ((int)$lastchanged > (int)$previous)
|
||||
{
|
||||
$tempcontent = file_get_contents($path . '.md');
|
||||
if ($tempcontent != $content)
|
||||
{
|
||||
$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>';
|
||||
}
|
||||
}
|
||||
file_put_contents($path . '.md', $content);
|
||||
removefromindex($title);
|
||||
array_unshift($_SESSION['index'], $path);
|
||||
}
|
||||
|
||||
function removefromindex($title)
|
||||
{
|
||||
global $dir;
|
||||
$key = array_search($dir . '/' . $title, $_SESSION['index']);
|
||||
if ($key !== FALSE)
|
||||
{
|
||||
array_splice($_SESSION['index'], $key, 1);
|
||||
}
|
||||
}
|
||||
|
||||
function downloadnote($path)
|
||||
{
|
||||
header('Content-disposition: attachment; filename=' . basename($path));
|
||||
header('Content-type: text/markdown');
|
||||
readfile($path);
|
||||
exit;
|
||||
}
|
||||
|
||||
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¶m=' . 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="' . $link[1] . '">' . $link[1] . '</a>
|
||||
</div>';
|
||||
}
|
||||
}
|
||||
$divcontent .= '</div>';
|
||||
return $divcontent;
|
||||
}
|
||||
|
||||
function computepreview($title, $content)
|
||||
{
|
||||
require 'libs/Parsedown.php';
|
||||
$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));
|
||||
return $preview;
|
||||
}
|
||||
|
||||
// Main
|
||||
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;
|
||||
}
|
||||
|
||||
// reindex if needed
|
||||
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);
|
||||
}
|
||||
?>
|
9
home.php
9
home.php
|
@ -1,9 +0,0 @@
|
|||
<form action="index.php" method="GET">
|
||||
<div>
|
||||
<input hidden type="submit" name="search" value="search" accesskey="s">
|
||||
<input type="submit" name="new" value="new" accesskey="o">
|
||||
<input type="submit" name="download" value="download" accesskey="d">
|
||||
<br>
|
||||
<input placeholder="search..." autofocus autocomplete="off" class="title" name="param" value="<?php if (!isset($_GET['home']) && !isset($_POST['home'])) echo $param; ?>">
|
||||
</div>
|
||||
</form>
|
272
index.php
272
index.php
|
@ -1,204 +1,22 @@
|
|||
<?php
|
||||
error_reporting(E_ALL);
|
||||
require 'settings.php';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function cleanstring($string)
|
||||
{
|
||||
return iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', strtolower($string));
|
||||
}
|
||||
|
||||
function downloadnote($path)
|
||||
{
|
||||
header('Content-disposition: attachment; filename=' . basename($path));
|
||||
header('Content-type: text/markdown');
|
||||
readfile($path);
|
||||
exit;
|
||||
}
|
||||
|
||||
function downloadall()
|
||||
{
|
||||
$zip = new ZipArchive();
|
||||
$zip_name = 'bbn.zip';
|
||||
$zip->open($zip_name, ZipArchive::CREATE);
|
||||
foreach($_SESSION['index'] as $path)
|
||||
{
|
||||
if (!str_ends_with($path, '.del'))
|
||||
{
|
||||
$path = $path . '.md';
|
||||
if (file_exists($path))
|
||||
{
|
||||
$zip->addFile($path, basename($path));
|
||||
}
|
||||
else
|
||||
{
|
||||
echo"file does not exist";
|
||||
}
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
|
||||
header('Content-disposition: attachment; filename=' . $zip_name);
|
||||
header('Content-type: application/zip');
|
||||
readfile($zip_name);
|
||||
|
||||
unlink($zip_name);
|
||||
exit;
|
||||
}
|
||||
|
||||
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 ((int)$lastchanged > (int)$previous)
|
||||
{
|
||||
$tempcontent = file_get_contents($path . '.md');
|
||||
if ($tempcontent != $content)
|
||||
{
|
||||
$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>';
|
||||
}
|
||||
}
|
||||
file_put_contents($path . '.md', $content);
|
||||
removefromindex($title);
|
||||
array_unshift($_SESSION['index'], $path);
|
||||
}
|
||||
|
||||
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¶m=' . 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="' . $link[1] . '">' . $link[1] . '</a>
|
||||
</div>';
|
||||
}
|
||||
}
|
||||
$divcontent .= '</div>';
|
||||
return $divcontent;
|
||||
}
|
||||
|
||||
$nextpage = 'home';
|
||||
$preview = '';
|
||||
require('common.php');
|
||||
|
||||
if (isset($_GET['download']))
|
||||
{
|
||||
downloadall();
|
||||
}
|
||||
else if (isset($_POST['save']) || isset($_POST['home']) || isset($_POST['download']))
|
||||
{
|
||||
$title = $_POST['title'];
|
||||
$content = $_POST['content'];
|
||||
savenote($title, $content);
|
||||
|
||||
$previoustitle = $_POST['previoustitle'];
|
||||
if ($title != $previoustitle)
|
||||
$filter = '';
|
||||
if (isset($_GET['filter']))
|
||||
{
|
||||
rename($dir . '/' . $previoustitle . '.md', $dir . '/' . $previoustitle . '.md.del');
|
||||
removefromindex($previoustitle);
|
||||
}
|
||||
|
||||
if (isset($_POST['download']))
|
||||
{
|
||||
downloadnote($dir . '/' . $title . '.md');
|
||||
}
|
||||
else if (!isset($_POST['home']))
|
||||
{
|
||||
$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'];
|
||||
}
|
||||
else if (isset($_GET['new']))
|
||||
{
|
||||
$nextpage = 'note';
|
||||
$filter = $_GET['filter'];
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>
|
||||
<?php
|
||||
if ($nextpage != 'home' && isset($title))
|
||||
{
|
||||
echo $title;
|
||||
}
|
||||
else
|
||||
{
|
||||
echo 'bbn';
|
||||
}
|
||||
?>
|
||||
</title>
|
||||
<title>bbn</title>
|
||||
<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">
|
||||
|
@ -206,76 +24,46 @@
|
|||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<form action="index.php" method="GET" id="home">
|
||||
<input hidden type="submit" accesskey="h">
|
||||
</form>
|
||||
<form action="note.php" method="GET" id="note"></form>
|
||||
|
||||
<div>
|
||||
<input type="submit" name="new" value="new" accesskey="o" form="note">
|
||||
<input type="submit" name="download" value="download" accesskey="d" form="home">
|
||||
<br>
|
||||
<input placeholder="search..." autofocus autocomplete="off" class="title" name="filter" value="<?php echo $filter; ?>" form="home">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<?php
|
||||
if ($nextpage == 'preview')
|
||||
{
|
||||
echo $preview;
|
||||
}
|
||||
else if ($nextpage == 'note')
|
||||
{
|
||||
$now = date("Y-m-d H.i.s", time());
|
||||
if (!$title)
|
||||
{
|
||||
$title = $now;
|
||||
}
|
||||
|
||||
if (!file_exists($dir . '/' . $title . '.md'))
|
||||
{
|
||||
$content = "---\r\ndate: " . substr($now, 0, 10) . "\r\ntags: \r\n---\r\n";
|
||||
savenote($title, $content);
|
||||
}
|
||||
else if (!$content)
|
||||
{
|
||||
$content = file_get_contents($dir . '/' . $title . '.md');
|
||||
}
|
||||
|
||||
clearstatcache();
|
||||
$lastchanged = filemtime($dir . '/' . $title . '.md');
|
||||
$lines = substr_count($content, "\r\n");
|
||||
$words = substr_count($content, " ") + $lines;
|
||||
$chars = strlen($content);
|
||||
|
||||
$rows = max(20, $lines) * 2;
|
||||
|
||||
require('note.php');
|
||||
|
||||
}
|
||||
else if ($nextpage == 'home')
|
||||
{
|
||||
if (isset($_GET['param']))
|
||||
{
|
||||
$param = $_GET['param'];
|
||||
}
|
||||
require('home.php');
|
||||
|
||||
echo '<div>';
|
||||
|
||||
foreach($_SESSION['index'] as $path)
|
||||
{
|
||||
$name = basename($path);
|
||||
if (!str_ends_with($name, '.del'))
|
||||
if (str_ends_with($name, '.del'))
|
||||
{
|
||||
if (($_GET['param'] && isset($_GET['search'])))
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($filter)
|
||||
{
|
||||
$content = file_get_contents($path . '.md');
|
||||
if (isset($_GET['search']) && !str_contains(cleanstring($content), cleanstring($param)) && !str_contains(cleanstring($name), cleanstring($param)))
|
||||
if (!str_contains(cleanstring($content), cleanstring($filter)) && !str_contains(cleanstring($name), cleanstring($filter)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
echo '<div><a href=index.php?open=true¶m=' . urlencode($name) . '>' . $name .'</a>';
|
||||
if (isset($_GET['search']) && str_contains(cleanstring($content), cleanstring($param)))
|
||||
{
|
||||
$pos = strpos(cleanstring($content), cleanstring($param));
|
||||
echo '<span class="grey"> ' . $param . substr($content, $pos + strlen($param), 42) . '</span>';
|
||||
}
|
||||
echo'</div>';
|
||||
}
|
||||
}
|
||||
|
||||
echo '<div><a href=note.php?title=' . urlencode($name) . '>' . $name .'</a>';
|
||||
if ($filter && str_contains(cleanstring($content), cleanstring($filter)))
|
||||
{
|
||||
$pos = strpos(cleanstring($content), cleanstring($$filter));
|
||||
echo '<span class="grey"> ' . $filter . substr($content, $pos + strlen($filter), 42) . '</span>';
|
||||
}
|
||||
echo'</div>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
95
note.php
95
note.php
|
@ -1,8 +1,91 @@
|
|||
<form action="index.php" method="POST">
|
||||
<?php
|
||||
require('common.php');
|
||||
|
||||
// handle save actions
|
||||
$title = $_POST['title'];
|
||||
$content = $_POST['content'];
|
||||
|
||||
if (isset($_POST['save']) || isset($_POST['home']) || isset($_POST['download']))
|
||||
{
|
||||
savenote($title, $content);
|
||||
|
||||
$previoustitle = $_POST['previoustitle'];
|
||||
if ($title != $previoustitle)
|
||||
{
|
||||
rename($dir . '/' . $previoustitle . '.md', $dir . '/' . $previoustitle . '.md.del');
|
||||
removefromindex($previoustitle);
|
||||
}
|
||||
|
||||
if (isset($_POST['download']))
|
||||
{
|
||||
downloadnote($dir . '/' . $title . '.md');
|
||||
}
|
||||
else if (isset($_POST['home']))
|
||||
{
|
||||
header("Location: index.php");
|
||||
exit;
|
||||
}
|
||||
else
|
||||
{
|
||||
header("Location: note.php?title=" . $title);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
else if (isset($_POST['delete']))
|
||||
{
|
||||
rename($dir . '/' . $title . '.md', $dir . '/' . $title . '.md.del');
|
||||
removefromindex($title);
|
||||
|
||||
header("Location: index.php");
|
||||
exit;
|
||||
}
|
||||
else if (isset($_POST['preview']))
|
||||
{
|
||||
header("Location: note.php?preview=1&title=" . $title);
|
||||
exit;
|
||||
}
|
||||
|
||||
// main
|
||||
|
||||
$title = '';
|
||||
$content = '';
|
||||
if (isset($_GET['new']))
|
||||
{
|
||||
$now = date("Y-m-d H.i.s", time());
|
||||
$title = $now;
|
||||
$content = "---\r\ndate: " . substr($now, 0, 10) . "\r\ntags: \r\n---\r\n";
|
||||
savenote($title, $content);
|
||||
}
|
||||
else if (isset($_GET['title']))
|
||||
{
|
||||
$title = $_GET['title'];
|
||||
$content = file_get_contents($dir . '/' . $title . '.md');
|
||||
}
|
||||
|
||||
clearstatcache();
|
||||
$lastchanged = filemtime($dir . '/' . $title . '.md');
|
||||
$lines = substr_count($content, "\r\n");
|
||||
$words = substr_count($content, " ") + $lines;
|
||||
$chars = strlen($content);
|
||||
$rows = max(20, $lines) * 2;
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><? echo $title; ?></title>
|
||||
<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>
|
||||
<form action="note.php" method="POST" <? if(isset($_GET['preview'])) echo 'hidden'; ?>>
|
||||
<div>
|
||||
<input type="submit" name="home" value="home" accesskey="h">
|
||||
<input type="submit" name="save" value="save" accesskey="s">
|
||||
<input type="submit" name="preview" value="preview" accesskey="p" formtarget="_blank">
|
||||
<input type="submit" name="preview" value="preview" accesskey="p" formtarget="_bbn_preview">
|
||||
<input type="submit" name="download" value="download" accesskey="d">
|
||||
<input type="submit" name="delete" value="delete">
|
||||
<input hidden name="lastchanged" value="<? echo $lastchanged; ?>">
|
||||
|
@ -17,3 +100,11 @@
|
|||
echo linksdiv($content);
|
||||
?>
|
||||
</form>
|
||||
<div <? if(!isset($_GET['preview'])) echo 'hidden'; ?>>
|
||||
<?
|
||||
echo computepreview($title, $content);
|
||||
?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
Loading…
Reference in New Issue