personal-website/docs/www/blog/article.php

189 lines
4.5 KiB
PHP

<?php
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php");
include_once($COMMONS."/utils.php");
class BlogpostComment
{
public $comment_id;
public $parent_id;
public $blogpost_id;
public $poster_id;
public $timestamp;
public $content;
}
class Blogpost
{
public $blogpost_id;
public $title;
public $content;
public $date_posted;
public $tags;
/**
* Display the article, or a warning message.
*/
function display_article(){
// If a blog with given ID was not found display warning message.
if(!$this->title){
printf("
<article>
<h2> Article not found </h2>
<hr>
<p>
I am sorry, but I couldn't find an article with this ID.
</p>
</article>
");
return;
}
// Begin the article
printf("<article>");
// Display the blogpost name
printf("<h2>%s</h2>", $this->title);
// Display the blog metadata
printf("<div class=\"blog-metadata\">");
// Display tags
for($i = 0; $i < count($this->tags); $i++) {
$tag = $this->tags[$i];
printf("
<span class=\"blog-tag\" style=\"background-color: %s\">
%s
</span>", $tag["color"], $tag["name"]);
}
// Display publish date and end metadata div
printf("<span class=\"blog-publish-date\">Published on: %s</span></div>",
date("Y-m-d", strtotime($this->date_posted)));
// Display hrule, article content and end the article
printf("</article><hr><article>%s</article>", $this->content);
}
/**
* Constructor for the blogpost.
* $blogpost_id GUID of the blogpost in the database.
* $title Title of the blogpost.
* $content Content of the blogpost article.
* $date_posted Timestamp at publishing of article.
* $tags Array of the tags this article has.
*/
public function __construct($blogpost_id, $title, $content,
$date_posted, $tags){
$this->blogpost_id = $blogpost_id;
$this->title = $title;
$this->content = $content;
$this->date_posted = $date_posted;
$this->tags = $tags;
}
}
/**
* Try to load info about the blog with guid in GET and set global
* variables accordingly.
*/
function get_blog_info($conn, $blogId){
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
include_once($COMMONS."/footer.php");
die();
}
// Prepare and bind statement for gathering blogpost info
$stmt = $conn->prepare("SELECT title, content, date_posted
FROM blogposts WHERE blogpost_id = :blogpost_id;");
$stmt->bindParam(":blogpost_id", $blogId);
// Execute the statement
$stmt->execute();
// Fetch the blogpost
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// If no post with given guid was found,
// there is no information to gather, return.
if(!$result){
return null;
}
// Prepare new statement for selecting the tags for a given blogpost
$stmt = $conn->prepare("SELECT name, color FROM
blogpost_tags INNER JOIN blogpost_has_tag ON
blogpost_tags.tag_id = blogpost_has_tag.tag_id WHERE
blogpost_id = :blogpost_id;");
// Bind and execute the tag select
$stmt->bindParam(":blogpost_id", $blogId);
$stmt->execute();
// Fetch the tags
$tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
// Set the variables
$blogTitle = $result["title"];
$blogContent = $result["content"];
$datePosted = $result["date_posted"];
$tags = $tags_arr;
return new Blogpost($blogId, $blogTitle, $blogContent, $datePosted, $tags);
}
/**
* Display all the comments responding to a given article.
*/
function display_comments($conn, $blogId){
// Check DB connection
if($conn == null){
printf("
<article>
<h1>
Failed to load comments due to database connection error!
</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
return;
}
// Prepare statement for selecting all coments replying to a given article.
$stmt = $conn->prepare("SELECT;");
// Bind and execute the tag select
$stmt->bindParam(":blogpost_id", $blogId);
$stmt->execute();
// Fetch the tags
$tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
}
// Get the blog id.
$blogId = sanitize_input($_GET["guid"]);
$blogPost = null;
// Try to get info about blog
if($blogId) {
$blogPost = get_blog_info($conn, $blogId);
}
// Display the header with title being the blog name, or not found message.
display_header($blogPost ? $blogPost->title : "Article not found");
// Display the blog
$blogPost->display_article();
include_once($COMMONS."/footer.php");
?>