add download feature

This commit is contained in:
quenousimporte 2024-09-30 10:09:49 +02:00
parent 03821833b3
commit ab2cdcc400
1 changed files with 122 additions and 83 deletions

205
index.php
View File

@ -1,3 +1,124 @@
<?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 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 '<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&param=' . 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="https:' . $link[1] . '">https:' . $link[1] . '</a>
</div>';
}
}
$divcontent .= '</div>';
return $divcontent;
}
$nextpage = 'home';
if (isset($_GET['download']))
{
download();
}
?>
<!DOCTYPE html>
<html>
<head>
@ -9,89 +130,8 @@
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?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 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 '<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&param=' . 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="https:' . $link[1] . '">https:' . $link[1] . '</a>
</div>';
}
}
$divcontent .= '</div>';
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());