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
|
<?php
|
||||||
// Declare global variables for the blog content
|
|
||||||
$blogTitle = $blogContent = $datePosted = $tags = "";
|
|
||||||
$blogId = $_GET["guid"];
|
|
||||||
|
|
||||||
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
|
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
|
||||||
|
|
||||||
include_once($COMMONS."/header.php");
|
include_once($COMMONS."/header.php");
|
||||||
|
include_once($COMMONS."/utils.php");
|
||||||
|
|
||||||
// Try to get info about blog
|
class BlogpostComment
|
||||||
if($blogId)
|
{
|
||||||
get_blog_info();
|
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
|
* Try to load info about the blog with guid in GET and set global
|
||||||
* variables accordingly.
|
* variables accordingly.
|
||||||
*/
|
*/
|
||||||
function get_blog_info(){
|
function get_blog_info($conn, $blogId){
|
||||||
// Access global variables
|
|
||||||
global $conn;
|
|
||||||
global $blogTitle;
|
|
||||||
global $blogContent;
|
|
||||||
global $datePosted;
|
|
||||||
global $tags;
|
|
||||||
global $blogId;
|
|
||||||
|
|
||||||
// Check DB connection
|
// Check DB connection
|
||||||
if($conn == null){
|
if($conn == null){
|
||||||
printf("
|
printf("
|
||||||
|
@ -53,7 +116,7 @@ function get_blog_info(){
|
||||||
// If no post with given guid was found,
|
// If no post with given guid was found,
|
||||||
// there is no information to gather, return.
|
// there is no information to gather, return.
|
||||||
if(!$result){
|
if(!$result){
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare new statement for selecting the tags for a given blogpost
|
// Prepare new statement for selecting the tags for a given blogpost
|
||||||
|
@ -74,47 +137,53 @@ function get_blog_info(){
|
||||||
$blogContent = $result["content"];
|
$blogContent = $result["content"];
|
||||||
$datePosted = $result["date_posted"];
|
$datePosted = $result["date_posted"];
|
||||||
$tags = $tags_arr;
|
$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.
|
||||||
printf("
|
*/
|
||||||
<article>
|
function display_comments($conn, $blogId){
|
||||||
<h2> Article not found </h2>
|
// Check DB connection
|
||||||
<hr>
|
if($conn == null){
|
||||||
<p>
|
printf("
|
||||||
I am sorry, but I couldn't find an article with this ID.
|
<article>
|
||||||
</p>
|
<h1>
|
||||||
</article>
|
Failed to load comments due to database connection error!
|
||||||
");
|
</h1>
|
||||||
include_once($COMMONS."/footer.php");
|
If you see this error in production,
|
||||||
die();
|
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
|
// Get the blog id.
|
||||||
printf("<article>");
|
$blogId = sanitize_input($_GET["guid"]);
|
||||||
|
|
||||||
// Display the blogpost name
|
$blogPost = null;
|
||||||
printf("<h2>%s</h2>", $blogTitle);
|
// Try to get info about blog
|
||||||
|
if($blogId) {
|
||||||
// Display the blog metadata
|
$blogPost = get_blog_info($conn, $blogId);
|
||||||
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
|
// Display the header with title being the blog name, or not found message.
|
||||||
printf("<span class=\"blog-publish-date\">Published on: %s</span></div>",
|
display_header($blogPost ? $blogPost->title : "Article not found");
|
||||||
date("Y-m-d", strtotime($datePosted)));
|
|
||||||
|
|
||||||
// Display hrule, article content and end the article
|
// Display the blog
|
||||||
printf("</article><hr><article>%s</article>", $blogContent);
|
$blogPost->display_article();
|
||||||
|
|
||||||
include_once($COMMONS."/footer.php");
|
include_once($COMMONS."/footer.php");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -2,25 +2,14 @@
|
||||||
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
|
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
|
||||||
|
|
||||||
include_once($COMMONS."/header.php");
|
include_once($COMMONS."/header.php");
|
||||||
|
include_once($COMMONS."/utils.php");
|
||||||
|
|
||||||
display_header("Login");
|
display_header("Login");
|
||||||
|
|
||||||
// Define previous attempt and error variables and set to empty values.
|
// Define previous attempt and error variables and set to empty values.
|
||||||
$emailOld = $passwordOld = "";
|
$emailOld = $passwordOld = "";
|
||||||
$emailErr = $passwordErr = "";
|
$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.
|
* Process the information, and if there are no errors, log the user in.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue