From 58291bd62b6803ecc243c2a4b2e0a293b971710b Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Mon, 7 Oct 2024 16:06:35 +0200 Subject: [PATCH] refactor --- common.php | 140 +++++++++++++++++++++++++++ home.php | 9 -- index.php | 278 +++++++---------------------------------------------- note.php | 129 +++++++++++++++++++++---- style.css | 2 +- 5 files changed, 284 insertions(+), 274 deletions(-) create mode 100644 common.php delete mode 100644 home.php diff --git a/common.php b/common.php new file mode 100644 index 0000000..b8c41b1 --- /dev/null +++ b/common.php @@ -0,0 +1,140 @@ +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 '
Conflict detected. See backup: ' . $temptitle . '
'; + } + } + 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 = '
'; + $links = array(); + if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER)) + { + foreach($links as $link) + { + $divcontent .= '
+ ' . $link[1] . ' +
'; + } + } + $links = array(); + if (preg_match_all('/(https?:.*)\b/', $content, $links, PREG_SET_ORDER)) + { + foreach($links as $link) + { + $divcontent .= '
+ ' . $link[1] . ' +
'; + } + } + $divcontent .= '
'; + 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 '

Access denied.

'; + 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); + } +?> \ No newline at end of file diff --git a/home.php b/home.php deleted file mode 100644 index 02df927..0000000 --- a/home.php +++ /dev/null @@ -1,9 +0,0 @@ -
-
- - - -
- -
-
diff --git a/index.php b/index.php index 5a963c7..9b137ad 100644 --- a/index.php +++ b/index.php @@ -1,204 +1,22 @@ Access denied.

'; - 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 '
Conflict detected. See backup: ' . $temptitle . '
'; - } - } - file_put_contents($path . '.md', $content); - removefromindex($title); - array_unshift($_SESSION['index'], $path); - } - - function linksdiv($content) - { - $divcontent = '
'; - $links = array(); - if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER)) - { - foreach($links as $link) - { - $divcontent .= '
- ' . $link[1] . ' -
'; - } - } - $links = array(); - if (preg_match_all('/(https?:.*)\b/', $content, $links, PREG_SET_ORDER)) - { - foreach($links as $link) - { - $divcontent .= '
- ' . $link[1] . ' -
'; - } - } - $divcontent .= '
'; - 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) - { - 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'])) + $filter = ''; + if (isset($_GET['filter'])) { - $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']; } ?> - - <?php - if ($nextpage != 'home' && isset($title)) - { - echo $title; - } - else - { - echo 'bbn'; - } - ?> - + bbn @@ -206,76 +24,46 @@ +
+ +
+
+
+ + +
+ +
+ +
'; - - foreach($_SESSION['index'] as $path) - { - $name = basename($path); - if (!str_ends_with($name, '.del')) + $content = file_get_contents($path . '.md'); + if (!str_contains(cleanstring($content), cleanstring($filter)) && !str_contains(cleanstring($name), cleanstring($filter))) { - if (($_GET['param'] && isset($_GET['search']))) - { - $content = file_get_contents($path . '.md'); - if (isset($_GET['search']) && !str_contains(cleanstring($content), cleanstring($param)) && !str_contains(cleanstring($name), cleanstring($param))) - { - continue; - } - } - echo '
' . $name .''; - if (isset($_GET['search']) && str_contains(cleanstring($content), cleanstring($param))) - { - $pos = strpos(cleanstring($content), cleanstring($param)); - echo ' ' . $param . substr($content, $pos + strlen($param), 42) . ''; - } - echo'
'; + continue; } } - echo '
'; + echo '
' . $name .''; + if ($filter && str_contains(cleanstring($content), cleanstring($filter))) + { + $pos = strpos(cleanstring($content), cleanstring($$filter)); + echo ' ' . $filter . substr($content, $pos + strlen($filter), 42) . ''; + } + echo'
'; } ?> + diff --git a/note.php b/note.php index b98e4a1..da6de10 100644 --- a/note.php +++ b/note.php @@ -1,19 +1,110 @@ -
-
- - - - - - -
-
-
- -
- - ' . $lines . ' ' . $words . ' ' . $chars . ' ' . $title . '.md'; - echo linksdiv($content); - ?> -
+ + + + + + <? echo $title; ?> + + + + + + + +
> +
+ + + + + + +
+
+
+ +
+ + ' . $lines . ' ' . $words . ' ' . $chars . ' ' . $title . '.md'; + echo linksdiv($content); + ?> +
+
> + +
+ + + diff --git a/style.css b/style.css index e98ffd4..dedc8f4 100644 --- a/style.css +++ b/style.css @@ -37,7 +37,7 @@ a { width: 100%; } -form input[type="submit"] { +input[type="submit"] { background: none; border: none; color: lightgrey;