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

View file

@ -1,7 +1,7 @@
<?php
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$_title = "Apps";
$root = $_SERVER["DOCUMENT_ROOT"];
require "$root/templates/header.php";
require "$_shared/header.php";
?>
<h2>Apps</h2>
<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/load.php">Media downloader</a><br>
<?php
require "$root/templates/footer.php";
require "$_shared/footer.php";
?>

View file

@ -1,8 +1,7 @@
<?php
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$_title = "Media viewer";
$root = $_SERVER["DOCUMENT_ROOT"];
require "$root/templates/header.php";
require "$_shared/header.php";
$urls_base = __DIR__ . "/urls";
if (isset($_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>";
}
}
require "$root/templates/footer.php";
require "$_shared/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
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
$_title = "Notes";
$root = $_SERVER["DOCUMENT_ROOT"];
require "$root/templates/header.php";
require "$_shared/header.php";
?>
<a href="write.php">Write a note</a><br>
<h4>Notes</h4>
<?php
foreach (scandir("notesDir") as $note) {
if ($note === '.' || $note === '..')
continue;
echo "<a href='write.php?title=$note'>$note</a><br>\n";
require_once "$_shared/lib.php";
$pdo = pdo_init();
$entries = $pdo->query("select * from notes.entries order by date desc");
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
$title = "";
$note = "";
$_shared = $_SERVER["DOCUMENT_ROOT"] . "/templates";
require_once "$_shared/lib.php";
switch ($_SERVER["REQUEST_METHOD"]) {
case "GET":
if (isset($_GET["title"])) {
$title = $_GET["title"];
$note = file_get_contents("notesDir/" . base64_encode($title));
if (isset($_GET["id"])) {
$note_id = $_GET["id"];
$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;
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: .");
exit;
}
$_title = "Notes";
$root = $_SERVER["DOCUMENT_ROOT"];
require "$root/templates/header.php";
require "$_shared/header.php";
?>
<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 class="">Delete</button><br><br>
<?=$del_href?>
<textarea name="note" class="note content" placeholder="Note"><?=$note?></textarea>
</form>
<style>
button {
padding: 5px 10px;
}
.content {
color: #ffffff;
background-color: #303030;
}
.note {
width: 98%;
height: 280px;
}
</style>
<?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 {
color: #ffffff;
background: #000000;
}
a {
color: #00ffff;
.content {
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,
]);
}
?>