diff --git a/docs/common/utils.php b/docs/common/utils.php new file mode 100644 index 0000000..dfef57c --- /dev/null +++ b/docs/common/utils.php @@ -0,0 +1,15 @@ + diff --git a/docs/www/blog/article.php b/docs/www/blog/article.php index ce6e082..5f59fa0 100644 --- a/docs/www/blog/article.php +++ b/docs/www/blog/article.php @@ -1,32 +1,95 @@ title){ + printf(" +
+

Article not found

+
+

+ I am sorry, but I couldn't find an article with this ID. +

+
+ "); + return; + } + + // Begin the article + printf("
"); + + // Display the blogpost name + printf("

%s

", $this->title); + + // Display the blog metadata + printf("
"); + + // Display tags + for($i = 0; $i < count($this->tags); $i++) { + $tag = $this->tags[$i]; + printf(" + + %s + ", $tag["color"], $tag["name"]); + } + + // Display publish date and end metadata div + printf("Published on: %s
", + date("Y-m-d", strtotime($this->date_posted))); + + // Display hrule, article content and end the article + printf("

%s
", $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(){ - // Access global variables - global $conn; - global $blogTitle; - global $blogContent; - global $datePosted; - global $tags; - global $blogId; - +function get_blog_info($conn, $blogId){ // Check DB connection if($conn == null){ printf(" @@ -53,7 +116,7 @@ function get_blog_info(){ // If no post with given guid was found, // there is no information to gather, return. if(!$result){ - return; + return null; } // Prepare new statement for selecting the tags for a given blogpost @@ -74,47 +137,53 @@ function get_blog_info(){ $blogContent = $result["content"]; $datePosted = $result["date_posted"]; $tags = $tags_arr; + + return new Blogpost($blogId, $blogTitle, $blogContent, $datePosted, $tags); } -// If a blog with given ID was not found display warning message and die. -if(!$blogTitle){ - printf(" -
-

Article not found

-
-

- I am sorry, but I couldn't find an article with this ID. -

-
- "); - include_once($COMMONS."/footer.php"); - die(); +/** + * Display all the comments responding to a given article. + */ +function display_comments($conn, $blogId){ + // Check DB connection + if($conn == null){ + printf(" +
+

+ Failed to load comments due to database connection error! +

+ If you see this error in production, + please shoot me an email with helpful details. +
"); + 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); + } -// Begin the article -printf("
"); +// Get the blog id. +$blogId = sanitize_input($_GET["guid"]); -// Display the blogpost name -printf("

%s

", $blogTitle); - -// Display the blog metadata -printf("
"); - -// Display tags -for($i = 0; $i < count($tags); $i++) { - $tag = $tags[$i]; - printf(" - - %s - ", $tag["color"], $tag["name"]); +$blogPost = null; +// Try to get info about blog +if($blogId) { + $blogPost = get_blog_info($conn, $blogId); } -// Display publish date and end metadata div -printf("Published on: %s
", - date("Y-m-d", strtotime($datePosted))); +// Display the header with title being the blog name, or not found message. +display_header($blogPost ? $blogPost->title : "Article not found"); -// Display hrule, article content and end the article -printf("

%s
", $blogContent); +// Display the blog +$blogPost->display_article(); include_once($COMMONS."/footer.php"); ?> diff --git a/docs/www/login.php b/docs/www/login.php index be52252..c02ba97 100644 --- a/docs/www/login.php +++ b/docs/www/login.php @@ -2,25 +2,14 @@ $COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common"; include_once($COMMONS."/header.php"); +include_once($COMMONS."/utils.php"); + display_header("Login"); // Define previous attempt and error variables and set to empty values. $emailOld = $passwordOld = ""; $emailErr = $passwordErr = ""; -/** - * Sanitize a given input string to be safe to display and process. - */ -function sanitize_input($data) { - // Remove unnecessary whitespace characters - $data = trim($data); - // Remove backslashes - $data = stripslashes($data); - // Escape all special characters to HTML entities - $data = htmlspecialchars($data); - return $data; -} - /** * Process the information, and if there are no errors, log the user in. */