/************************************************************ * * Functions to support the Blog * * Functions: * printSingleBlogOnly($blogURL, &$blogTitle, &$url) * printSingleBlog($url) * printBlogForm($action, $id) * getBlogURLByID($id) * getNextAndPrevBlog($id,&$prev,&$next) * printBlogList($limit) * printBlogListRandom($limit) * fetchSingleBlog($id) * constructBlogPreviewContents($row, $count, $subCount) * getTotalBlogEntries() * printAllBlogTitles() * updateBlog($_POST) * createBlog($_POST) * ************************************************************/ include_once "../../passwd/mysql_connect.php"; /* * If ?p=__ is defined, and exists within the DB * Then, return "Yes" * Otherwise, return "No" */ function printSingleBlogOnly($blogURL, &$blogTitle, &$url) { global $conn; $status = "No"; $url = ""; if (isset($blogURL)) { if ($blogURL != "") { $sql = "SELECT * FROM `blog` WHERE url LIKE '{$blogURL}'"; $result = $conn->query($sql); if (!$result) { } else { if ($result->num_rows > 0) { $status = "Yes"; $row = $result->fetch_assoc(); $blogTitle = $row['title']; $url = $blogURL; } } } } return $status; } /* * Print a single blog with the page url of $url */ function printSingleBlog($url) { global $conn; // Don't show hidden blogs on the main page but allow them to be displayed in the blogadmin.php page $filterOutHidden = " AND hidden='No' "; if ($_SERVER['PHP_SELF'] == "/1/blogadmin.php") $filterOutHidden = ""; $sql = "SELECT * FROM `blog` WHERE url='{$url}' {$filterOutHidden} "; $result = $conn->query($sql); if (!$result) { print "
Whoops. Could not fetch blog.
"; } else { if ($result->num_rows > 0) { $output = ""; while ($row = $result->fetch_assoc()) { $id = $row['id']; getNextAndPrevBlog($id,$prev,$next); $date = $row['date']; $dateParts = explode("-",$date); $date_ = date('M d',mktime(0,0,0,$dateParts[1],$dateParts[2],$dateParts[0])); $title = $row['title']; $url = $row['url']; $author = $row['author']; $image = $row['image']; $imageSmall = $row['image-small']; $body = $row['body']; $bodyPreview = $row['body-preview']; $views = $row['views']; $hidden = $row['hidden']; // If there is no small image defined, use the large image if ($image == "" && $imageSmall != "") $image = $imageSmall; // Only display author if one is defined. if ($author != "") { $author = "Whoops. There doesn't seem to be a blog there.
"; } } } /* * Print the fields to edit or create a blog * * Input: * action = "edit" to edit/update an existing blog * action = "add" to create a new blog * id = only matters if action=edit. This is the id of the blog to edit. * */ function printBlogForm($action, $id) { global $conn; $fetchErrorFlag = 0; if ($action == "edit") { // Don't show hidden blogs on the main page but allow them to be displayed in the blogadmin.php page $filterOutHidden = " AND hidden='No' "; if ($_SERVER['PHP_SELF'] == "/1/blogadmin.php") $filterOutHidden = ""; $sql = "SELECT * FROM `blog` WHERE id='{$id}' {$filterOutHidden}"; $result = $conn->query($sql); if (!$result) { print "Whoops. Could not fetch blog.
"; } else { if ($result->num_rows > 0) { $output = ""; while ($row = $result->fetch_assoc()) { $id = $row['id']; $date = $row['date']; $dateParts = explode("-",$date); $date_ = date('M d',mktime(0,0,0,$dateParts[1],$dateParts[2],$dateParts[0])); $title = $row['title']; $url = $row['url']; $author = $row['author']; $image = $row['image']; $imageSmall = $row['image-small']; $body = $row['body']; $bodyPreview = $row['body-preview']; $views = $row['views']; $hidden = $row['hidden']; if ($hidden == "Yes") { $hiddenSelectedNo = ""; $hiddenSelectedYes = " CHECKED "; } else { $hiddenSelectedNo = " CHECKED "; $hiddenSelectedYes = ""; } $image_ = $row['image']; $imageSmall_ = $row['image-small']; } } else { print "Whoops. There doesn't seem to be a blog there.
"; $fetchErrorFlag = 1; } } } else { $date = date('Y-m-d'); $title = ""; $url = ""; $author = ""; $body = ""; $bodyPreview = ""; $hiddenSelectedNo = " CHECKED "; $hiddenSelectedYes = ""; $image_ = ""; $imageSmall_ = ""; } $tdTitleStyle = " width: 100px; font-weight:bold; line-height:30px; "; $inputTextStyle = " style='width:400px; text-align: left; padding-left:5px;' "; if (!$fetchErrorFlag) { $output .= " "; } print $output; } /* * Given an ID, get the URL of the blog */ function getBlogURLByID($id) { global $conn; $output = ""; // Don't show hidden blogs on the main page but allow them to be displayed in the blogadmin.php page $filterOutHidden = " AND hidden='No' "; if ($_SERVER['PHP_SELF'] == "/1/blogadmin.php") $filterOutHidden = ""; $sql = "SELECT * FROM `blog` WHERE id='{$id}' {$filterOutHidden}"; $result = $conn->query($sql); if (!$result) { print "Whoops. Could not fetch blog.
"; } else { if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $output = $row['url']; } } } return $output; } /* * Fetch the previous and the next blog url * Update the parameter variables. */ function getNextAndPrevBlog($id,&$prev,&$next) { global $conn; $prev = ""; $next = ""; // Don't show hidden blogs on the main page but allow them to be displayed in the blogadmin.php page $filterOutHidden = " WHERE hidden='No' "; if ($_SERVER['PHP_SELF'] == "/1/blogadmin.php") $filterOutHidden = ""; $sql = "SELECT * FROM `blog` {$filterOutHidden} ORDER BY id DESC"; $result = $conn->query($sql); if (!$result) { } else { if ($result->num_rows > 1) { $refIdLast = ""; $refIdThis = ""; $refIdNext = ""; $refUrlLast = ""; $refUrlThis = ""; $refUrlNext = ""; $foundID = 0; while ($row = $result->fetch_assoc()) { // We found the ID on the last cycle // Grab the ID from this cycle, which will be the next ID if ($foundID) { $refIdNext = $row['id']; $refUrlNext = $row['url']; break; } // Still looking for the ID of the currently displayed blog article else { $refIdLast = $refIdThis; $refIdThis = $row['id']; $refUrlLast = $refUrlThis; $refUrlThis = $row['url']; // We found it if ($refIdThis == $id) { $foundID = 1; continue; } } } // We found the ID. // Update the next and prev links if ($foundID) { if ($_SERVER['PHP_SELF'] == "/1/blogadmin.php") { if ($refIdLast != "") $prev = "Previous"; if ($refIdNext != "") $next = "Next"; } else { if ($refIdLast != "") $prev = "Previous"; if ($refIdNext != "") $next = "Next"; } } } } } /* * Print the blog entries */ function printBlogList($limit) { global $conn; // Limit the number of blog entries $limit_ = ""; if (isset($limit)) if ($limit > 0) $limit_ = " LIMIT {$limit} "; // Don't show hidden blogs on the main page but allow them to be displayed in the blogadmin.php page $filterOutHidden = " WHERE hidden='No' "; if ($_SERVER['PHP_SELF'] == "/1/blogadmin.php") $filterOutHidden = ""; $sql = "SELECT * FROM `blog` {$filterOutHidden} ORDER BY date DESC {$limit_}"; $result = $conn->query($sql); if (!$result) { print "Whoops. Could not fetch blog entries
"; } else { if ($result->num_rows > 0) { $count = 0; $subCount = 0; $output = ""; while ($row = $result->fetch_assoc()) { $count++; $subCount++; $output .= constructBlogPreviewContents($row, $count, $subCount); } print "No blog entries yet.
"; } } } /* * Return the blog preview. This is a small view of the blog. Small image and short text description. * * Parameters: * $row = The database row of the blog that we want to print. * $count = When printing a list of blog previews, this is the Nth blog in that list * $subCount = This is the Nth blog in this row of blogs. We print only 3 per line. * */ function constructBlogPreviewContents($row, $count, &$subCount) { $output = ""; // If this is not set then we don't have good content if (!isset($row['title'])) return; $date = $row['date']; $dateParts = explode("-",$date); $date_ = date('M d',mktime(0,0,0,$dateParts[1],$dateParts[2],$dateParts[0])); $title = $row['title']; $url = $row['url']; $author = $row['author']; $image = $row['image']; $imageSmall = $row['image-small']; $body = $row['body']; $bodyPreview = $row['body-preview']; $views = $row['views']; // If there is no small image defined, use the large image if ($imageSmall == "") $imageSmall = $image; // Only display author if one is defined. if ($author != "") { $author = "| {$count}. | {$hiddenText} |
Error updating blog
{$sql}
{$error}
";
}
}
/*
* Given a form submission, create a new blog entry
*/
function createBlog($post) {
global $conn;
$returnedID = "";
$hidden = $conn->real_escape_string(trim($post['hidden']));
$title = $conn->real_escape_string(trim($post['title']));
$date = $conn->real_escape_string(trim($post['date']));
$author = $conn->real_escape_string(trim($post['author']));
$url = $conn->real_escape_string(trim($post['url']));
$image = $conn->real_escape_string(trim($post['image']));
$imageSmall = $conn->real_escape_string(trim($post['image-small']));
$preview = $conn->real_escape_string(trim($post['preview']));
$body = $conn->real_escape_string(trim($post['body']));
$sql = "INSERT INTO `blog`
(`date`, `title`, `url`, `author`, `image`, `image-small`, `body`, `body-preview`, `hidden`)
VALUES
('{$date}', '{$title}', '{$url}', '{$author}', '{$image}', '{$imageSmall}', '{$body}', '{$preview}', '{$hidden}')";
$result = $conn->query($sql);
if (!$result) {
$error = $conn->error;
print "
Error creating blog
{$sql}
{$error}
";
}
else {
$sql = "SELECT id
FROM `blog`
ORDER BY id DESC
LIMIT 1";
$result = $conn->query($sql);
if (!$result) {
}
else {
$row = $result->fetch_assoc();
$returnedID = $row['id'];
}
}
return $returnedID;
}
?>