From ab2cdcc40077f79cca75190412d0561f3af2753c Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Mon, 30 Sep 2024 10:09:49 +0200 Subject: [PATCH] add download feature --- index.php | 205 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 122 insertions(+), 83 deletions(-) diff --git a/index.php b/index.php index 2dce2a0..0ffa284 100644 --- a/index.php +++ b/index.php @@ -1,3 +1,124 @@ +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 download() + { + $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 (!isset($_GET['clip']) && (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 .= '
+ https:' . $link[1] . ' +
'; + } + } + $divcontent .= '
'; + return $divcontent; + } + + $nextpage = 'home'; + + if (isset($_GET['download'])) + { + download(); + } + +?> + @@ -9,89 +130,8 @@ + 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 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) - { - $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 .= '
- https:' . $link[1] . ' -
'; - } - } - $divcontent .= '
'; - return $divcontent; - } - - $nextpage = 'home'; if (isset($_GET['clip']) && $_GET['param']) { @@ -143,7 +183,6 @@ $nextpage = 'note'; $title = $_GET['param']; } - if ($nextpage == 'note') { $now = date("Y-m-d H.i.s", time());