getMessage()); } } function get_post_by_id($id) { $db = connect_db(); $stmt = $db->prepare('SELECT * FROM posts WHERE id = :id'); $stmt->bindValue(':id', $id, SQLITE3_INTEGER); $result = $stmt->execute(); $post = $result->fetchArray(SQLITE3_ASSOC); $stmt->close(); return format_post($post); } function get_posts_by_page($page, $posts_per_page, $parent_id = 1) { $offset = ($page - 1) * $posts_per_page; $db = connect_db(); $stmt = $db->prepare('SELECT * FROM posts WHERE parent_id = :parent_id ORDER BY id DESC LIMIT :limit OFFSET :offset'); $stmt->bindValue(':limit', $posts_per_page, SQLITE3_INTEGER); $stmt->bindValue(':offset', $offset, SQLITE3_INTEGER); $stmt->bindValue(':parent_id', $parent_id, SQLITE3_INTEGER); $result = $stmt->execute(); $posts = []; while ($row = $result->fetchArray(SQLITE3_ASSOC)) { $post = format_post($row); $posts[] = $post; } $stmt->close(); return $posts; } function get_total_posts($parent_id = 1) { $db = connect_db(); $stmt = $db->prepare('SELECT COUNT(*) AS total FROM posts WHERE parent_id = :parent_id'); $stmt->bindValue(':parent_id', $parent_id, SQLITE3_INTEGER); $result = $stmt->execute(); $row = $result->fetchArray(SQLITE3_ASSOC); $total = $row['total']; $stmt->close(); return $total; } function format_post($post) { $text = nl2br(htmlspecialchars($post['text'])); $text = preg_replace_callback('/\[(\d+)\]/', function ($matches) use ($post) { $linked_post = get_post_by_id($matches[1]); if ($linked_post) { return " Link"; } else { return "[Invalid Post]"; } }, $text); return [ 'id' => $post['id'], 'hash_name' => $post['hash_name'], 'ip_address' => $post['ip_address'], // Replace with actual function for screen reader friendliness 'parent_id' => $post['parent_id'], 'text' => $text, 'date_posted' => date('Y m d', strtotime($post['date_posted'])), ]; } function add_post($ip_address, $parent_id, $text) { $db = connect_db(); $stmt = $db->prepare('INSERT INTO posts ( ip_address, parent_id, text) VALUES ( :ip_address, :parent_id, :text)'); $stmt->bindValue(':ip_address', substr($ip_address, 0, 2048), SQLITE3_TEXT); $stmt->bindValue(':parent_id', $parent_id, SQLITE3_INTEGER); $stmt->bindValue(':text', substr($text, 0, 5048) , SQLITE3_TEXT); $stmt->execute(); $stmt->close(); } ?>