refactor actions

fix index upon delete and rename
This commit is contained in:
quenousimporte 2024-03-07 12:48:31 +01:00
parent c13d688e90
commit bf1c9cc9d9
1 changed files with 35 additions and 67 deletions

102
index.php
View File

@ -20,66 +20,33 @@
session_start();
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;
file_put_contents($dir . '/' . $title, $content);
$key = array_search($dir . '/' . $title, $_SESSION['index']);
if ($key)
{
// handle the rename case
array_splice($_SESSION['index'], $key, 1);
array_unshift($_SESSION['index'], $dir . '/' . $title);
}
removefromindex($title);
array_unshift($_SESSION['index'], $dir . '/' . $title);
}
$action = '';
$nextpage = 'home';
if (isset($_GET['open']))
{
$action = 'open';
}
else if (isset($_GET['search']))
{
$action = 'search';
}
else if (isset($_GET['tags']))
{
$action = 'tags';
}
else if (isset($_GET['clip']))
{
$action = 'clip';
}
else if (isset($_POST['save']))
{
$action = 'save';
}
else if (isset($_POST['delete']))
{
$action = 'delete';
}
else if (isset($_POST['home']))
{
$action = 'save';
}
else if(isset($_GET['home']))
{
$action = '';
}
else if (isset($_POST['preview']))
{
$action = 'preview';
}
$content = '';
if ($action == 'clip' && $_GET['param'])
if (isset($_GET['clip']) && $_GET['param'])
{
$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo');
savenote('todo', $content);
$_GET['param'] = '';
}
if ($action == 'save')
else if (isset($_POST['save']) || isset($_POST['home']))
{
$title = $_POST['title'];
$content = $_POST['content'];
@ -89,26 +56,21 @@
if ($title != $previoustitle)
{
rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del');
removefromindex($previoustitle);
}
if (isset($_POST['home']))
if (!isset($_POST['home']))
{
$action = '';
}
else
{
$action = 'open';
$_GET['param'] = $title;
$nextpage = 'note';
}
}
else if ($action == 'delete')
else if (isset($_POST['delete']))
{
$title = $_POST['title'];
rename($dir . '/' . $title, $dir . '/' . $title . '.del');
// reindex!
removefromindex($title);
}
if ($action == 'preview')
else if (isset($_POST['preview']))
{
require 'libs/Parsedown.php';
$title = $_POST['title'];
@ -121,10 +83,16 @@
$Parsedown = new Parsedown();
$Parsedown->setBreaksEnabled(true);
echo $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos + 3));
$nextpage = '';
}
else if ($action == 'open')
else if (isset($_GET['open']))
{
$nextpage = 'note';
$title = $_GET['param'];
}
if ($nextpage == 'note')
{
$title = $_GET['param'];
$now = date("Y-m-d H.i.s", time());
if (!$title)
{
@ -175,7 +143,7 @@
echo '<span class="grey">' . $title . ' - ' . $now . ' - ' . $length . 'c - ' . $words . 'w - ' . $lines . 'l </span>';
}
else
else if ($nextpage == 'home')
{
if (isset($_GET['param']))
{
@ -201,18 +169,18 @@
$name = basename($path);
if (!str_ends_with($name, '.del'))
{
if ($action == 'search' || $action == 'tags')
if (isset($_GET['search']) || isset($_GET['tags']))
{
$content = file_get_contents($path);
if (($action == 'search' && !str_contains(strtolower($content), strtolower($param)) && !str_contains(strtolower($name), strtolower($param)))
|| ($action == 'tags' && !preg_match('/tags:.*' . $param . '/i', $content)))
if ((isset($_GET['search']) && !str_contains(strtolower($content), strtolower($param)) && !str_contains(strtolower($name), strtolower($param)))
|| (isset($_GET['tags']) && !preg_match('/tags:.*' . $param . '/i', $content)))
{
continue;
}
}
echo '<div><a href=index.php?open=true&param=' . urlencode($name) . '>' . $name .'</a>';
if ($action == 'tags')
if (isset($_GET['tags']))
{
$tags = array();
$hastags = preg_match_all('/tags:(.*)/i', $content, $tags, PREG_SET_ORDER);
@ -228,7 +196,7 @@
echo '</span>';
}
}
else if ($action == 'search' && str_contains(strtolower($content), strtolower($param)))
else if (isset($_GET['search']) && str_contains(strtolower($content), strtolower($param)))
{
$pos = strpos(strtolower($content), strtolower($param));
echo '<span class="grey"> ' . $param . substr($content, $pos + strlen($param), 42) . '</span>';