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 .= '
';
+ }
+ }
+ $links = array();
+ if (preg_match_all('/(https?:.*)\b/', $content, $links, PREG_SET_ORDER))
+ {
+ foreach($links as $link)
+ {
+ $divcontent .= '
';
+ }
+ }
+ $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.