(string) ($n['id'] ?? ''), 'text' => $text, 'updated' => (string) ($n['updated'] ?? ''), ]; } return $out; } function notes_save(array $notes): bool { $path = notes_path(); $dir = dirname($path); if (!is_dir($dir)) { @mkdir($dir, 0775, true); } $json = json_encode(array_values($notes), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); if ($json === false) return false; $tmp = $path . '.tmp'; $ok = @file_put_contents($tmp, $json . "\n", LOCK_EX); if ($ok === false) return false; return @rename($tmp, $path); } function notes_json(array $data, int $status = 200): never { http_response_code($status); header('Content-Type: application/json; charset=utf-8'); header('X-Content-Type-Options: nosniff'); echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); exit; } function notes_id(): string { return bin2hex(random_bytes(8)); } // Handle AJAX/API requests when called directly $isDirect = basename((string) ($_SERVER['SCRIPT_FILENAME'] ?? '')) === basename(__FILE__); $action = (string) ($_GET['action'] ?? $_POST['action'] ?? ''); if ($isDirect && $action !== '') { try { $contentType = $_SERVER['CONTENT_TYPE'] ?? ''; $input = $_POST; if (stripos($contentType, 'application/json') !== false) { $raw = file_get_contents('php://input'); $decoded = json_decode($raw ?: '', true); if (is_array($decoded)) $input = $decoded; } if ($action === 'list') { notes_json(['ok' => true, 'notes' => notes_load()]); } if ($action === 'add') { $text = trim((string) ($input['text'] ?? '')); if ($text === '') notes_json(['ok' => false, 'error' => 'Note is empty.'], 400); if (strlen($text) > 10000) notes_json(['ok' => false, 'error' => 'Note too long (max 10000 chars).'], 400); $notes = notes_load(); $note = ['id' => notes_id(), 'text' => $text, 'updated' => gmdate('c')]; array_unshift($notes, $note); if (!notes_save($notes)) notes_json(['ok' => false, 'error' => 'Failed to save note.'], 500); notes_json(['ok' => true, 'note' => $note, 'notes' => $notes]); } if ($action === 'edit') { $id = (string) ($input['id'] ?? ''); $text = trim((string) ($input['text'] ?? '')); if ($id === '') notes_json(['ok' => false, 'error' => 'Missing note id.'], 400); if ($text === '') notes_json(['ok' => false, 'error' => 'Note is empty.'], 400); if (strlen($text) > 10000) notes_json(['ok' => false, 'error' => 'Note too long (max 10000 chars).'], 400); $notes = notes_load(); $found = false; foreach ($notes as &$n) { if (($n['id'] ?? '') === $id) { $n['text'] = $text; $n['updated'] = gmdate('c'); $found = true; break; } } unset($n); if (!$found) notes_json(['ok' => false, 'error' => 'Note not found.'], 404); if (!notes_save($notes)) notes_json(['ok' => false, 'error' => 'Failed to save note.'], 500); notes_json(['ok' => true, 'notes' => $notes]); } if ($action === 'delete') { $id = (string) ($input['id'] ?? ''); if ($id === '') notes_json(['ok' => false, 'error' => 'Missing note id.'], 400); $notes = notes_load(); $before = count($notes); $notes = array_values(array_filter($notes, fn($n) => ($n['id'] ?? '') !== $id)); if (count($notes) === $before) notes_json(['ok' => false, 'error' => 'Note not found.'], 404); if (!notes_save($notes)) notes_json(['ok' => false, 'error' => 'Failed to save note.'], 500); notes_json(['ok' => true, 'notes' => $notes]); } notes_json(['ok' => false, 'error' => 'Unknown action.'], 400); } catch (Throwable $e) { notes_json(['ok' => false, 'error' => 'Server error.'], 500); } } // Render mode (when included) if ($isDirect && $action === '') { header('Content-Type: text/plain; charset=utf-8'); echo "notes.php endpoint"; return; } $notesInitial = []; try { $notesInitial = notes_load(); } catch (Throwable $e) { $notesInitial = []; } ?> Notes & Commands + Add Note New note Stored in data/notes.json. Use for commands/snippets you want quick access to. Save Cancel
Stored in data/notes.json. Use for commands/snippets you want quick access to.
data/notes.json