From 08c2c31644d050b57f98784a7a1147c29f440bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Borovec?= Date: Sat, 20 Jul 2024 13:47:48 +0200 Subject: [PATCH] Commenting uses address, iff available. --- docs/www/blog/article.php | 251 +++++++++++++++++++++++++------------- 1 file changed, 168 insertions(+), 83 deletions(-) diff --git a/docs/www/blog/article.php b/docs/www/blog/article.php index 095a460..60d028c 100644 --- a/docs/www/blog/article.php +++ b/docs/www/blog/article.php @@ -7,6 +7,7 @@ class BlogpostComment { public $comment_id; public $blogpost_id; + public $blogpost_addr; public $poster_id; public $poster_name; public $parent_id; @@ -18,47 +19,88 @@ class BlogpostComment * Display the comment, and recursively it's children */ public function display_comment() { - printf(" -
-
- By: %s - On: %s - - -
-
- %s -
-
-
- - - -
- -
- -
-
+ if(is_null($this->blogpost_addr)) { + printf(" +
+
+ By: %s + On: %s + + +
+
+ %s +
+
+
+ + + +
+
-
+ + +
+
+
", - $this->comment_id, - $this->poster_name, - date("Y-m-d H:i", strtotime($this->timestamp)), - $this->comment_id, - $this->comment_id, - $this->content, - htmlspecialchars($_SERVER["PHP_SELF"]), - $this->blogpost_id, - $this->comment_id); + $this->comment_id, + $this->poster_name, + date("Y-m-d H:i", strtotime($this->timestamp)), + $this->comment_id, + $this->comment_id, + $this->content, + htmlspecialchars($_SERVER["PHP_SELF"]), + $this->blogpost_id, + $this->comment_id); + } + else { + printf(" +
+
+ By: %s + On: %s + + +
+
+ %s +
+
+
+ + + + +
+ +
+ +
+
+
+
+ ", + $this->comment_id, + $this->poster_name, + date("Y-m-d H:i", strtotime($this->timestamp)), + $this->comment_id, + $this->comment_id, + $this->content, + htmlspecialchars($_SERVER["PHP_SELF"]), + $this->blogpost_id, + $this->blogpost_addr, + $this->comment_id); + } if($this->children != null) { for($i = 0; $i < count($this->children); $i++) @@ -118,7 +160,8 @@ class BlogpostComment $commentObj = new BlogpostComment($com["comment_id"], $com["poster_id"], $username, $this->blogpost_id, - $com["timestamp"], $com["content"], $this->comment_id); + $this->blogpost_addr, $com["timestamp"], $com["content"], + $this->comment_id); $comments_arr[] = $commentObj; $commentObj->load_children($conn); } @@ -132,19 +175,22 @@ class BlogpostComment * $poster_id GUID of the comment author. * $poster_name Name of the comment author. * $blogpost_id GUID of the blogpost this comment is under. + * $blogpost_addr Human-readable address of the blogpost this + comment is under. * $timestamp Timestamp at comment creation. * $content Content of the comment. * $parent_id GUID of the comment this is a reply to (or NULL). */ public function __construct($comment_id, $poster_id, $poster_name, - $blogpost_id, $timestamp, $content, $parent_id) { - $this->comment_id = $comment_id; - $this->blogpost_id = $blogpost_id; - $this->poster_id = $poster_id; - $this->poster_name = $poster_name; - $this->timestamp = $timestamp; - $this->content = $content; - $this->parent_id = $parent_id; + $blogpost_id, $blogpost_address, $timestamp, $content, $parent_id) { + $this->comment_id = $comment_id; + $this->blogpost_id = $blogpost_id; + $this->blogpost_addr = $blogpost_address; + $this->poster_id = $poster_id; + $this->poster_name = $poster_name; + $this->timestamp = $timestamp; + $this->content = $content; + $this->parent_id = $parent_id; } } @@ -152,6 +198,7 @@ class BlogpostComment class Blogpost { public $blogpost_id; + public $address; public $title; public $content; public $date_posted; @@ -228,6 +275,7 @@ class Blogpost /** * Constructor for the blogpost. * $blogpost_id GUID of the blogpost in the database. + * $address Readable address of the blogpost. * $title Title of the blogpost. * $content Content of the blogpost article. * $date_posted Timestamp at publishing of article. @@ -236,15 +284,16 @@ class Blogpost * $comments Array of Blogpostcomment objects, * the comments of this article. */ - public function __construct($blogpost_id, $title, $content, - $date_posted, $date_edited, $tags, $comments){ - $this->blogpost_id = $blogpost_id; - $this->title = $title; - $this->content = $content; - $this->date_posted = $date_posted; - $this->date_edited = $date_edited; - $this->tags = $tags; - $this->comments = $comments; + public function __construct($blogpost_id, $address, $title, + $content, $date_posted, $date_edited, $tags, $comments){ + $this->blogpost_id = $blogpost_id; + $this->address = $address; + $this->title = $title; + $this->content = $content; + $this->date_posted = $date_posted; + $this->date_edited = $date_edited; + $this->tags = $tags; + $this->comments = $comments; } } @@ -291,7 +340,7 @@ function send_comment($conn, $blogId, $posterId, $content, $parentId) { * Load comments under a given blog. * Returns array of BlogpostComment objects. */ -function load_comments($conn, $blogId) { +function load_comments($conn, $blogId, $blogAddress) { // Prepare new statement for selecting all the child comments. $stmt = $conn->prepare("SELECT comment_id, poster_id, timestamp, content FROM blogpost_comments WHERE blogpost_id = :blogpost_id @@ -334,7 +383,8 @@ function load_comments($conn, $blogId) { } $commentObj = new BlogpostComment($com["comment_id"], $com["poster_id"], - $username, $blogId, $com["timestamp"], $com["content"], NULL); + $username, $blogId, $blogAddress, $com["timestamp"], + $com["content"], NULL); $commentObj->load_children($conn); $comments_arr[] = $commentObj; } @@ -348,8 +398,9 @@ function load_comments($conn, $blogId) { */ function load_blog($conn, $blogId){ // Prepare and bind statement for gathering blogpost info - $stmt = $conn->prepare("SELECT title, content, date_posted, date_edited - FROM blogposts WHERE blogpost_id = :blogpost_id;"); + $stmt = $conn->prepare("SELECT readable_address, title, content, + date_posted, date_edited FROM blogposts WHERE + blogpost_id = :blogpost_id;"); $stmt->bindParam(":blogpost_id", $blogId); // Execute the statement @@ -379,13 +430,14 @@ function load_blog($conn, $blogId){ // Set the variables $blogTitle = $result["title"]; + $blogAddress = $result["readable_address"]; $blogContent = $result["content"]; $datePosted = $result["date_posted"]; $dateEdited = $result["date_edited"]; $tags = $tags_arr; - $comments = load_comments($conn, $blogId); + $comments = load_comments($conn, $blogId, $blogAddress); - return new Blogpost($blogId, $blogTitle, $blogContent, + return new Blogpost($blogId, $blogAddress, $blogTitle, $blogContent, $datePosted, $dateEdited, $tags, $comments); } @@ -407,14 +459,22 @@ if(isset($_POST["submit"])) { $parentId = isset($_POST["comment_id"]) ? $_POST["comment_id"] : "NULL"; $posterId = isset($_SESSION["current_user"]) ? $_SESSION["current_user"]->user_id : "NULL"; + $address = isset($_POST["address"]) ? + sanitize_input($_POST["address"]) : NULL; // Try to send the comment $commentId = send_comment($conn, $blogId, $posterId, $commentContent, $parentId); // Redirect to this page with GET - header("Location: http://www.zdenekborovec-dev.cz/blog/". - "article?blogpost_id=".$blogId."#comment-".$commentId); + if(is_null($address)) { + header("Location: http://www.zdenekborovec-dev.cz/blog/". + "article?blogpost_id=".$blogId."#comment-".$commentId); + } + else { + header("Location: http://www.zdenekborovec-dev.cz/blog/". + "article?address=".$address."#comment-".$commentId); + } die(); } @@ -462,24 +522,49 @@ $blogPost->display_article(); printf("
"); // Display post comment form. -printf(" -
-

Comments:

-
- - -
- -
- -
-
-", - htmlspecialchars($_SERVER["PHP_SELF"]), $blogId, - isset($_SESSION["current_user"]) ? $_SESSION["current_user"]->user_name : - "Guest"); - +if(is_null($blogPost->address)) +{ + printf(" +
+

Comments:

+
+ + +
+ +
+ +
+
+ ", + htmlspecialchars($_SERVER["PHP_SELF"]), $blogId, + isset($_SESSION["current_user"]) ? + $_SESSION["current_user"]->user_name : "Guest"); +} +else +{ + printf(" +
+

Comments:

+
+ + + +
+ +
+ +
+
+ ", + htmlspecialchars($_SERVER["PHP_SELF"]), $blogId, $blogPost->address, + isset($_SESSION["current_user"]) ? + $_SESSION["current_user"]->user_name : "Guest"); +} // Display the blog comments $blogPost->display_comments();