true, 'cookie_samesite' => 'Lax']); $_SESSION['csrf'] ??= bin2hex(random_bytes(32)); $error = null; $title = ''; try { $storage = dirname(__DIR__) . '/storage'; if (!is_dir($storage) && !mkdir($storage, 0775) && !is_dir($storage)) { throw new RuntimeException('Cannot create the SQLite storage directory.'); } $db = new PDO('sqlite:' . $storage . '/reading-list.sqlite', null, null, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); $db->exec('PRAGMA busy_timeout = 5000'); $db->exec('CREATE TABLE IF NOT EXISTS books ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, is_read INTEGER NOT NULL DEFAULT 0 CHECK (is_read IN (0, 1)) )'); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $csrf = $_POST['csrf'] ?? ''; if (!is_string($csrf) || !hash_equals($_SESSION['csrf'], $csrf)) { http_response_code(403); exit('The form expired. Reload the page and try again.'); } switch ($_POST['action'] ?? '') { case 'add': $title = is_string($_POST['title'] ?? null) ? trim($_POST['title']) : ''; // Match up to 120 Unicode characters using PHP's built-in regex support. if (preg_match('/\A.{1,120}\z/us', $title) !== 1) { $error = 'Enter a title between 1 and 120 characters.'; http_response_code(422); break; } $insert = $db->prepare('INSERT INTO books (title) VALUES (:title)'); $insert->execute(['title' => $title]); break; case 'read': $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); if (!$id) { http_response_code(400); exit('Choose a valid book.'); } $update = $db->prepare('UPDATE books SET is_read = 1 WHERE id = :id'); $update->execute(['id' => $id]); break; default: http_response_code(400); exit('Unknown form action.'); } if ($error === null) { header('Location: /', true, 303); exit; } } $books = $db->query('SELECT id, title, is_read FROM books ORDER BY id DESC')->fetchAll(); } catch (RuntimeException $exception) { error_log($exception->getMessage()); http_response_code(500); exit('SQLite could not complete the request. Check the development server log and storage directory.'); } ?> Reading List

Reading List

Books

Your list is empty. Add a book to get started.