more object-oriented approach
This commit is contained in:
parent
a1075b0d5e
commit
42de24e677
3 changed files with 138 additions and 65 deletions
15
docs/common/utils.php
Normal file
15
docs/common/utils.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,32 +1,95 @@
|
|||
<?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");
|
||||
include_once($COMMONS."/utils.php");
|
||||
|
||||
// Try to get info about blog
|
||||
if($blogId)
|
||||
get_blog_info();
|
||||
class BlogpostComment
|
||||
{
|
||||
public $comment_id;
|
||||
public $parent_id;
|
||||
public $blogpost_id;
|
||||
public $poster_id;
|
||||
public $timestamp;
|
||||
public $content;
|
||||
}
|
||||
|
||||
// Display the header with title being the blog name, or not found message.
|
||||
display_header($blogTitle ? $blogTitle : "Article not found");
|
||||
|
||||
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(){
|
||||
// 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){
|
||||
/**
|
||||
* Display all the comments responding to a given article.
|
||||
*/
|
||||
function display_comments($conn, $blogId){
|
||||
// Check DB connection
|
||||
if($conn == null){
|
||||
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();
|
||||
<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);
|
||||
|
||||
}
|
||||
|
||||
// Begin the article
|
||||
printf("<article>");
|
||||
// Get the blog id.
|
||||
$blogId = sanitize_input($_GET["guid"]);
|
||||
|
||||
// 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"]);
|
||||
$blogPost = null;
|
||||
// Try to get info about blog
|
||||
if($blogId) {
|
||||
$blogPost = get_blog_info($conn, $blogId);
|
||||
}
|
||||
|
||||
// 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 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("</article><hr><article>%s</article>", $blogContent);
|
||||
// Display the blog
|
||||
$blogPost->display_article();
|
||||
|
||||
include_once($COMMONS."/footer.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.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue