120 lines
2.9 KiB
PHP
120 lines
2.9 KiB
PHP
<?php
|
|
// Declare global variables for the blog content
|
|
$blogTitle = $blogContent = $datePosted = $tags = "";
|
|
$blogId = $_GET["guid"];
|
|
|
|
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
|
|
|
|
include_once($COMMONS."/header.php");
|
|
|
|
// Try to get info about blog
|
|
if($blogId)
|
|
get_blog_info();
|
|
|
|
// Display the header with title being the blog name, or not found message.
|
|
display_header($blogTitle ? $blogTitle : "Article not found");
|
|
|
|
/**
|
|
* Try to load info about the blog with guid in GET and set global
|
|
* variables accordingly.
|
|
*/
|
|
function get_blog_info(){
|
|
// Access global variables
|
|
global $conn;
|
|
global $blogTitle;
|
|
global $blogContent;
|
|
global $datePosted;
|
|
global $tags;
|
|
global $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;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// If a blog with given ID was not found display warning message and die.
|
|
if(!$blogTitle){
|
|
printf("
|
|
<article>
|
|
<h2> Article not found </h2>
|
|
<hr>
|
|
<p>
|
|
I am sorry, but I couldn't find an article with this ID.
|
|
</p>
|
|
</article>
|
|
");
|
|
include_once($COMMONS."/footer.php");
|
|
die();
|
|
}
|
|
|
|
// Begin the article
|
|
printf("<article>");
|
|
|
|
// Display the blogpost name
|
|
printf("<h2>%s</h2>", $blogTitle);
|
|
|
|
// Display the blog metadata
|
|
printf("<div class=\"blog-metadata\">");
|
|
|
|
// Display tags
|
|
for($i = 0; $i < count($tags); $i++) {
|
|
$tag = $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($datePosted)));
|
|
|
|
// Display hrule, article content and end the article
|
|
printf("</article><hr><article>%s</article>", $blogContent);
|
|
|
|
include_once($COMMONS."/footer.php");
|
|
?>
|