The following files exists in this folder. Click to view.
Foogler_blog/src
CHTMLPage.php
FDestroySession.php
<?php
//--------------------------------
//CHTMLPage.php
//Author: Susanne Lindberg
//
//Class for printing HTML for Foogler blog
//--------------------------------
class CHTMLPage
{
protected $iMenu;
protected $iStylesheet;
//---------------------------------
//Constructor
public function __construct($aSheet = WS_STYLESHEET)
{
$this->iStylesheet = $aSheet; //Gets chosen stylesheet file
$this->iMenu = unserialize(WS_MENU); //Gets menu from config file ----------
}
//---------------------------------
//Destructor
public function __destruct()
{
}
//---------------------------------
//Prints head with chosen title
public function printHTMLHeader($aTitle = WS_TITLE)
{
echo <<<END
<!doctype html>
<html lang=sv>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<link rel="shortcut icon" href="images/FooglerIcon.png" type="image/x-icon" />
<title>{$aTitle}</title>
</head>
END;
}
//---------------------------------
//Prints header with chosen headline
public function printPageHeader()
{
$menuHTML = "";
foreach($this->iMenu as $key => $value)
{
$menuHTML .="\n <li><a href='{$value}'>{$key}</a></li>";
}
$htmlLoggedInName = $this->getLoggedInName(); //Gets name of logged in user
$htmlLoginMenu = $this->getLoginLogoutMenu(); //Gets menu for logging in / out etc.
echo <<<END
<body>
<div class="header">
<a href="."><img class="logo" src="images/FooglerLogo.png" alt="Foogler Blog" /></a>
{$htmlLoggedInName}
<img class="line" src="images/verticalLine.jpg" alt="" />
<ul class="menuList">
{$htmlLoginMenu}
{$menuHTML}
</ul>
</div><!-- header -->
END;
}
//---------------------------------
//Prints body with chosen text
public function printPageBody($aBody)
{
$htmlErrorMessage = $this->getErrorMessage(); //Gets error message from session variable
echo <<<END
<div class="postContainer">
{$aBody}
</div><!-- postContainer -->
END;
}
//---------------------------------
//Prints left column with list of tags
public function printTagList()
{
//-----------------------
//Handles DB stuff
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); //New DB object
$tableTag = DB_PREFIX . 'Tag';
$tablePostTag = DB_PREFIX . 'PostTag';
if (mysqli_connect_error()) {
echo "Connect failed: ".mysqli_connect_error()."<br>";
exit();
}
$mysqli->set_charset("utf8");
$query = <<<END
--
-- Gets name of and counts times used of all tags
--
SELECT
tagName,
COUNT(idTag) AS 'noOfUsed'
FROM {$tableTag}
JOIN {$tablePostTag} ON (PostTag_idTag = idTag)
GROUP BY tagName
ORDER BY tagName ASC;
END;
//Performs query
$res = $mysqli->query($query) or die("Could not query database");
//HTML
echo <<<END
<ul class="tagList">
<li style="margin-left:-20px;">Tags:</li>
END;
//For each returned row, prints list item
while($row = $res->fetch_object()) {
echo "\n <li><a href='?p=index&tag={$row->tagName}'>{$row->tagName} ($row->noOfUsed)</a></li>";
}
$res->close();
$mysqli->close(); //Closes DB connection
echo <<<END
</ul>
END;
}
//---------------------------------
//Prints right column
public function printRightColumn()
{
//-----------------------
//DB stuff
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); //New DB object
$tablePost = DB_PREFIX . 'Post';
$tableAuthor = DB_PREFIX . 'Author';
if (mysqli_connect_error()) {
echo "Connect failed: ".mysqli_connect_error()."<br>";
exit();
}
$mysqli->set_charset("utf8");
//------------------------
//SQL queries
$query = <<<END
--
-- Gets titles of ten latests posts
--
SELECT idPost, postTitle FROM {$tablePost}
ORDER BY postDate DESC
LIMIT 10;
--
-- Gets timestamp for all months that have posts
--
SELECT UNIX_TIMESTAMP(postDate) AS 'postDate'
FROM {$tablePost}
GROUP BY MONTH(postDate)
ORDER BY postDate DESC;
--
-- Gets screenname and number of posts of all authors
--
SELECT screenname, COUNT(screenname) AS 'numPosts'
FROM {$tableAuthor}
JOIN {$tablePost} ON (idAuthor = Post_idAuthor)
GROUP BY screenname
ORDER BY screenname ASC;
--
-- Gets post stats (10, 30, 365 days)
--
SELECT DISTINCT (
SELECT COUNT(idPost) FROM {$tablePost} WHERE postDate BETWEEN DATE_SUB(NOW(), INTERVAL 10 DAY) AND NOW()
) AS 'tenDays',
(
SELECT COUNT(idPost) FROM {$tablePost} WHERE postDate BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
) AS 'thirtyDays',
(
SELECT COUNT(idPost) FROM {$tablePost} WHERE postDate BETWEEN DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW()
) AS 'year'
FROM {$tablePost};
END;
$res = $mysqli->multi_query($query) or die("Could not query database"); //Performs query
//----------------------------
//Gets result from first query
$res = $mysqli->store_result() or die("Failed to retrieve result from query.");
echo <<<END
<div class="rightColumn">
<ul class="list">
<li style="margin:40px 0 5px 0;">10 latest posts</li>
END;
//For each returned row, adds list item
while($row = $res->fetch_object()) {
echo "\n <li><a href='?p=post&id={$row->idPost}'>{$row->postTitle}</a></li>";
}
$res->close();
//------------------------------
//Gets result from second query
($mysqli->next_result() && ($res = $mysqli->store_result()) )
or die("Failed to retrive result from query.");
echo <<<END
</ul>
<ul class="list">
<li style="margin:40px 0 5px 0;">Archive</li>
END;
//For eac returned row, adds list item
while($row = $res->fetch_object()) {
echo "\n <li><a href='?p=index&date=" . date('Y-m', $row->postDate) . "'>" . date('F Y', $row->postDate) . "</a></li>";
}
$res->close();
//----------------------------
//Gets result from third query
($mysqli->next_result() && ($res = $mysqli->store_result()) )
or die("Failed to retrive result from query.");
echo <<<END
</ul>
<ul class="list">
<li style="margin:40px 0 5px 0;">Posts by authors</li>
END;
//For each returned row, adds list item
while($row = $res->fetch_object()) {
echo <<<END
<li>
<a href='?p=index&author={$row->screenname}'>{$row->screenname} ({$row->numPosts})</a> ·
<a href="?p=author&id={$row->screenname}">info</a>
</li>
END;
}
$res->close();
//Checks if user is logged in
if(isset($_SESSION['accountUser'])) {
//-----------------------------
//Gets result from fourth query
($mysqli->next_result() && ($res = $mysqli->store_result()) )
or die("Failed to retrive result from query.");
$row = $res->fetch_object();
echo <<<END
<ul class="list">
<li style="margin:40px 0 5px 0;">Statistics</li>
<li>Last 10 days: <b>{$row->tenDays}</b></li>
<li>Last 30 days: <b>{$row->thirtyDays}</b></li>
<li>Last year: <b>{$row->year}</b></li>
</ul>
END;
$res->close();
}
$mysqli->close(); //Closes DB connection
//Prints HTML for other stuff + end tags
echo <<<END
</ul>
<ul class="list">
<li style="margin:40px 0 5px 0;">
<a href="?p=rss">
<img src="images/RSS-icon.png" title="RSS Feed" />
RSS Feed
</a>
</li>
<li style="margin:40px 0 5px 0;">
<a href="?p=validate">
Validate blog
</a>
</li>
</ul>
</div><!-- rightColumn -->
</body>
</html>
END;
}
//---------------------------------
//Returns chosen post with correct HTML
public function getFormattedPost($postId) {
//--------------------
//DB stuff
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); //New DB object
$tablePost = DB_PREFIX . 'Post';
$tableAuthor = DB_PREFIX . 'Author';
$tableComment = DB_PREFIX . 'Comment';
$tableTag = DB_PREFIX . 'Tag';
$tablePostTag = DB_PREFIX . 'PostTag';
if (mysqli_connect_error()) {
echo "Connect failed: ".mysqli_connect_error()."<br>";
exit();
}
$mysqli->set_charset("utf8");
//-----------------------
//SQL queries
$query = <<<END
--
-- Selects the chosen post
--
SELECT * FROM {$tablePost}
WHERE idPost = {$postId};
--
-- Selects username of post author
--
SELECT screenname, idAuthor FROM {$tableAuthor}
WHERE idAuthor = (
SELECT Post_idAuthor FROM {$tablePost}
WHERE idPost = {$postId}
);
--
-- Counts number of comments for chosen post
--
SELECT COUNT(*) AS 'noOfComments' FROM {$tableComment}
WHERE Comment_idPost = {$postId};
--
-- Selects tag for chosen post
--
SELECT PostTag_idPost, idTag, tagName FROM {$tableTag}
JOIN {$tablePostTag} ON (idTag = PostTag_idTag)
WHERE PostTag_idPost = {$postId};
END;
//Performs queries
$mysqli->multi_query($query) or die("Could not query database");
//----------------------------
//Gets result from first query
$res = $mysqli->store_result() or die("Failed to retrieve result from query.");
$row = $res->fetch_object();
//Assigns variable values
$title = $row->postTitle;
$authorId = $row->Post_idAuthor;
$text = $row->postText;
$date = $row->postDate;
$text = nl2br($text); //Changes /n to <br />
$res->close();
//------------------------------
//Gets result from second query
($mysqli->next_result() && ($res = $mysqli->store_result()) )
or die("Failed to retrive result from query.");
$row = $res->fetch_object();
//Assigns variable values
$author = $row->screenname;
$authorId = $row->idAuthor;
$res->close();
//----------------------------
//Gets result from third query
($mysqli->next_result() && ($res = $mysqli->store_result()))
or die("Failed to retrive result from query.");
$row = $res->fetch_object(); //Gets first returned row
//Assigns variable values
$noOfComments = $row->noOfComments;
$res->close();
//-----------------------------
//Gets result from fourth query
($mysqli->next_result() && ($res = $mysqli->store_result()))
or die("Failed to retrive result from query.");
//For each returned row adds tag to string
$tagString = "";
while($row = $res->fetch_object()) {
$tagString .= "<a href='?p=index&tag={$row->tagName}'>{$row->tagName}</a>, ";
}
$tagString = substr($tagString, 0, -2); //Removes last comma on the end
$res->close();
$mysqli->close(); //Close DB connection
$editHTML = $this->getEditLinks($authorId, $postId); //If user is logged in and is the author of the post, adds edit links
//Post HTML
$html = <<<END
<!-- START POST -->
<h2><a href="?p=post&id={$postId}">{$title}</a></h2><span class="author"><a href="?p=index&author={$author}">{$author}</a></span>
<span class="post">
{$text}
</span><!-- post -->
<hr style="margin-top:15px;" />
<span class="comment">
<a href="?p=post&id={$postId}#comment">Comments ({$noOfComments})</a> ·
<a href="?p=post&id={$postId}#comment">Write comment</a>
</span>
<span class="date">Written {$date}</span>
<span class="tags">Tags: {$tagString}</span>
{$editHTML}
<hr style="margin-bottom:120px;" />
<!-- END POST -->
END;
return $html;
}
//---------------------------------
//Returns chosen comment with HTML
public function getFormattedComment($commId, $postId) {
//----------------------
//DB stuff
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); //New DB object
$tableComment = DB_PREFIX . 'Comment';
if (mysqli_connect_error()) {
echo "Connect failed: ".mysqli_connect_error()."<br>";
exit();
}
$mysqli->set_charset("utf8");
//----------------------
//SQL query
$query = <<<END
--
-- Selects chosen comment
--
SELECT * FROM {$tableComment}
WHERE idComment = {$commId}
END;
$res = $mysqli->query($query) or die("Could not query database"); //Performs query
$row = $res->fetch_object(); //Gets first row
$emailName = substr($row->commEmail, 0, (strpos($row->commEmail, '@'))); //Gets first part of e-mail
$deleteHTML = $this->getDeleteCommentLink($row->idComment, $postId); //If user is logged in, get delete link
//HTML
$html = <<<END
<!-- START COMMENT -->
<h3>{$row->commTitle} </h3>
<span class="post">
{$row->commText}
</span><!-- post -->
<hr />
<span class="tags">Written by: {$row->commSignature} ({$emailName})</span>
<span class="date">{$row->commDate}</span>
{$deleteHTML}
<hr style="margin-bottom:50px;" />
<!-- END COMMENT -->
END;
return $html;
}
//---------------------------------
// Prints the login-menu, changes links if user is logged in or not
public function getLoginLogoutMenu() {
$htmlMenu = "";
// If user is logged in
if(isset($_SESSION['accountUser'])) {
$htmlMenu = <<<EOD
<li><a href="?p=logout">Log out</a></li>
<li><a href="?p=new">New post</a></li>
<li><a href="?p=stats">Statistics</a></li>
EOD;
} else {
// If user is not logged in, show link to login-page
$htmlMenu = <<<EOD
<li><a href='?p=login'>Login</a></li>
EOD;
}
return $htmlMenu;
}
//---------------------------------
//Returns link for editing and deleting post
public function getEditLinks($authorId, $postId) {
//If user is logged in and the user is the author of the post
if(isset($_SESSION['accountUser']) && $_SESSION['accountId'] == $authorId) {
return '<span class="edit"><a href="?p=edit&id=' . $postId . '">Edit</a> · <a href="?p=delete&id=' . $postId . '">Delete</a></span>';
} else {
return "";
}
}
//---------------------------------
//Returns link to delete comment
public function getDeleteCommentLink($commentId, $postId) {
//Checks if user is logged in
if(isset($_SESSION['accountUser'])) {
return "<span class='edit'><a href='?p=cdeletep&id={$commentId}&post={$postId}'>Delete</a></span>";
} else {
return "";
}
}
//---------------------------------
//Returns string with username if logged in
public function getLoggedInName() {
// If user is logged in
if(isset($_SESSION['accountUser'])) {
return "<p class='loginName'>Logged in as {$_SESSION['accountUser']}</p>";
} else {
return "";
}
}
//---------------------------------
// Create a errormessage if its set in the SESSION
public function getErrorMessage() {
$html = "";
//Checks if error message is set
if(isset($_SESSION['errorMessage'])) {
$html = <<<EOD
<div class='errorMessage'>
{$_SESSION['errorMessage']}
</div>
EOD;
unset($_SESSION['errorMessage']); //Empties error message
}
return $html;
}
}