notes postgres backend

This commit is contained in:
bogale 2026-09-29 17:49:35 +09:00
commit 6519fa6827
9 changed files with 82 additions and 55 deletions

View file

@ -1,13 +1,11 @@
<?php <?php
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$_title = "Password generator"; $_title = "Password generator";
$root = $_SERVER["DOCUMENT_ROOT"]; require "$_shared/header.php";
require "$root/templates/header.php";
$str = strtr(base64_encode(random_bytes(15)), '+/', '-_'); $str = strtr(base64_encode(random_bytes(15)), '+/', '-_');
$str[random_int(0, 19)] = random_int(0, 1) ? '-' : '_'; $str[random_int(0, 19)] = random_int(0, 1) ? '-' : '_';
echo "<button onclick='navigator.clipboard.writeText(\"$str\").then( echo "<button onclick='navigator.clipboard.writeText(\"$str\").then(
() => { result.innerHTML = \"Copied\"; } );'>Copy</button><br>\n"; () => { result.innerHTML = \"Copied\"; } );'>Copy</button><br>\n";
echo "<span id='result'></span>\n"; echo "<span id='result'></span>\n";
require "$_shared/footer.php";
require "$root/templates/footer.php";
?> ?>

View file

@ -1,7 +1,7 @@
<?php <?php
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$_title = "Apps"; $_title = "Apps";
$root = $_SERVER["DOCUMENT_ROOT"]; require "$_shared/header.php";
require "$root/templates/header.php";
?> ?>
<h2>Apps</h2> <h2>Apps</h2>
<h4>Public</h4> <h4>Public</h4>
@ -14,5 +14,5 @@ require "$root/templates/header.php";
<a href="local/media/get.php">Media viewer</a><br> <a href="local/media/get.php">Media viewer</a><br>
<a href="local/media/load.php">Media downloader</a><br> <a href="local/media/load.php">Media downloader</a><br>
<?php <?php
require "$root/templates/footer.php"; require "$_shared/footer.php";
?> ?>

View file

@ -1,8 +1,7 @@
<?php <?php
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$_title = "Media viewer"; $_title = "Media viewer";
$root = $_SERVER["DOCUMENT_ROOT"]; require "$_shared/header.php";
require "$root/templates/header.php";
$urls_base = __DIR__ . "/urls"; $urls_base = __DIR__ . "/urls";
if (isset($_GET["list"])) { if (isset($_GET["list"])) {
$file = "$urls_base/" . $_GET["list"]; $file = "$urls_base/" . $_GET["list"];
@ -15,6 +14,5 @@ if (isset($_GET["list"])) {
echo "<a href='" . $_SERVER['PHP_SELF'] . "?list=$list'>Try $list</a><br>"; echo "<a href='" . $_SERVER['PHP_SELF'] . "?list=$list'>Try $list</a><br>";
} }
} }
require "$_shared/footer.php";
require "$root/templates/footer.php";
?> ?>

View file

@ -1,8 +0,0 @@
<?php
#create schema notes;
$pdo = new PDO("pgsql:dbname=php", "", "", [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
$pdo->query("create table mail(id int)");
?>

8
local/notes/db.sql Normal file
View file

@ -0,0 +1,8 @@
drop schema if exists notes cascade;
create schema notes;
create table notes.entries(
id int generated always as identity primary key,
title text not null,
note text not null,
date date default current_date not null
);

View file

@ -1,15 +1,18 @@
<?php <?php
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$_title = "Notes"; $_title = "Notes";
$root = $_SERVER["DOCUMENT_ROOT"]; require "$_shared/header.php";
require "$root/templates/header.php";
?> ?>
<a href="write.php">Write a note</a><br> <a href="write.php">Write a note</a><br>
<h4>Notes</h4> <h4>Notes</h4>
<?php <?php
foreach (scandir("notesDir") as $note) { require_once "$_shared/lib.php";
if ($note === '.' || $note === '..') $pdo = pdo_init();
continue; $entries = $pdo->query("select * from notes.entries order by date desc");
echo "<a href='write.php?title=$note'>$note</a><br>\n"; foreach ($entries as $entry) {
$id = $entry["id"];
$text = $entry["title"];
echo "<a href='write.php?id=$id'>$text</a><br>\n";
} }
require "$root/templates/footer.php"; require "$_shared/footer.php";
?> ?>

View file

@ -1,41 +1,51 @@
<?php <?php
$title = ""; $_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$note = ""; require_once "$_shared/lib.php";
switch ($_SERVER["REQUEST_METHOD"]) { switch ($_SERVER["REQUEST_METHOD"]) {
case "GET": case "GET":
if (isset($_GET["title"])) { if (isset($_GET["id"])) {
$title = $_GET["title"]; $note_id = $_GET["id"];
$note = file_get_contents("notesDir/" . base64_encode($title)); $pdo = pdo_init();
if (isset($_GET["delete"])) {
$stmt = $pdo->prepare("delete from notes.entries where id = ?");
$stmt->execute([$note_id]);
header("Location: .");
exit;
}
$stmt = $pdo->prepare("select * from notes.entries where id = ?");
$stmt->execute([$note_id]);
$entry = $stmt->fetch();
$title = $entry["title"];
$note = $entry["note"];
$date = $entry["date"];
$del_href = "<a href='?id=$note_id&delete'>Delele</a>";
}; };
break; break;
case "POST": case "POST":
file_put_contents("notesDir/" . base64_encode($_POST["title"]), $_POST["note"]); $pdo = pdo_init();
if ($_POST["id"] == "") {
$stmt = $pdo->prepare("insert into notes.entries
(title, note, date) values (?, ?, ?)");
$stmt->execute([$_POST["title"], $_POST["note"], $_POST["date"]]);
} else {
$stmt = $pdo->prepare("update notes.entries
set title = ?, note = ?, date = ? where id = ?");
$stmt->execute([$_POST["title"], $_POST["note"], $_POST["date"], $_POST["id"]]);
}
header("Location: ."); header("Location: .");
exit; exit;
} }
$_title = "Notes"; $_title = "Notes";
$root = $_SERVER["DOCUMENT_ROOT"]; require "$_shared/header.php";
require "$root/templates/header.php";
?> ?>
<form method="post"> <form method="post">
<input name="title" class="content" placeholder="Title" value="<?= $title ?>"> <input name="id" type="hidden" value="<?=$note_id?>">
<input name="date" type="date" class="content" value="<?=$date?>" required>
<input name="title" class="content" placeholder="Title" value="<?=$title?>" required>
<button type="submit" class="content">Save</button> <button type="submit" class="content">Save</button>
<button class="">Delete</button><br><br> <?=$del_href?>
<textarea name="note" class="note content" placeholder="Note"><?= $note ?></textarea> <textarea name="note" class="note content" placeholder="Note"><?=$note?></textarea>
</form> </form>
<style>
button {
padding: 5px 10px;
}
.content {
color: #ffffff;
background-color: #303030;
}
.note {
width: 98%;
height: 280px;
}
</style>
<?php <?php
require "$root/templates/footer.php"; require "$_shared/footer.php";
?> ?>

View file

@ -1,7 +1,18 @@
a {
color: #00ffff;
}
button {
padding: 5px 10px;
}
.note {
width: 98%;
height: 280px;
}
body { body {
color: #ffffff; color: #ffffff;
background: #000000; background: #000000;
} }
a { .content {
color: #00ffff; color: #ffffff;
background-color: #303030;
} }

7
templates/lib.php Normal file
View file

@ -0,0 +1,7 @@
<?php
function pdo_init() {
return new PDO("pgsql:dbname=php", null, null, [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
}
?>