Instantly share code, notes, and snippets.

@TechnoSparks /ts-BMS.php Secret
Last active Feb 18, 2018

Embed
What would you like to do?
This is the source code for the blog running on my website
<?php
function blog_printDatabaseEcho($entryData, $type = "default") {
if($type == "default") {
echo '<a href="/blog/entry/'.$entryData["filename"].'" class="entry-card white">'.'<h2 class="title">'.$entryData["title"].'</h3>'.'<p class="details">'.blog_timeparser($entryData["time"][0]).'<br>';
blog_tagsDisplayAllTags($entryData["tags"]); echo '</p>';
echo '</a>';
} elseif ($type == "rss") {
echo " <entry>\n";
echo ' <title>'.$entryData["title"].'</title>'."\n";
echo ' <link type="text/html" href="https://www.technosparks.net/blog/entry/'.$entryData["filename"].'"/>'."\n";
echo ' <updated>'.blog_timeparser($entryData["time"][0], "rss").'</updated>'."\n";
echo ' <content type="xhtml">'."\n";
echo ' <div xmlns="http://www.w3.org/1999/xhtml">'."\n";
echo ' <p><a href="https://www.technosparks.net/blog/entry/'.$entryData[1].'">Read this entry</a></p>'."\n";
echo ' </div>'."\n";
echo ' </content>'."\n";
echo ' <author><name>Shahmi Saidi</name></author>'."\n";
echo " </entry>\n\n";
}
}
function blog_printDatabase($type = "default") {
$fp = fopen("entries-list.txt", "r");
// Check for total number of entries
$entries = 0; $fpEntryofPg = []; $fpEntryofPgi = 1;
while(!feof($fp)) {
// Line 1
$fpBefore = ftell($fp);
if(empty(fgets($fp))) { break; }
// Line 2
if(fgets($fp) == "wip\n") {
if(!isset($_GET["all"])) {
$entries--;
}
}
if($entries % 6 == 0) {
$fpEntryofPg[$fpEntryofPgi++] = $fpBefore;
}
// Line 3 - 7
for($i = 1; $i <= 5; $i++) {
fgets($fp);
}
$entries++;
}
// Handle pagination
$maxPages = ceil($entries / 6);
if(!empty($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; }
if($page > $maxPages || $page <= 0) {
echo '<section>'.infobox("warn", ($page < 0 ? 'There&apos;s no such thing as negative pages! ' : '').'You attempted to access a page (page '.$page.') which does not exist! Showing you page 1 instead.').'</section>';
$page = 1;
}
// Output entries based on page
fseek($fp, $fpEntryofPg[$page]);
echo '<section class="bloglist">';
for($entry = 1; $entry <= 6; $entry++) {
// Line 1
if(empty(fgets($fp))) { break; }
// Line 2
if(fgets($fp) == "wip\n") {
if(!isset($_GET["all"])) {
for($i = 1; $i <= 5; $i++) {
fgets($fp);
}
$entry--;
continue;
}
}
// Line 3
$entryData["time"] = explode(" ", trim(fgets($fp), "\n"));
// Line 4
$entryData["filename"] = trim(fgets($fp), "\n");
// Line 5
$entryData["title"] = trim(fgets($fp), "\n");
// Line 6
$entryData["tags"] = explode(", ", trim(fgets($fp), "\n"));
// Line 7
fgets($fp);
blog_printDatabaseEcho($entryData, $type);
}
echo '</section>';
fclose($fp);
// Display pagination controls
if($type == "default") {
echo '<section style="text-align: center">';
echo '<a class="blog pagination button-link" href="?page='.($page - 1).((isset($_GET["all"])) ? '&all' : '').'" style="min-width: initial; visibility: ' . ($page > 1 ? 'visible' : 'hidden') . ';"><i class="fa fa-chevron-left"></i></a> ';
echo '<span class="blog pagination">Page ' . $page . '</span> ';
echo '<a class="blog pagination button-link" href="?page=' . ($page + 1) . ((isset($_GET["all"])) ? '$all' : '') . '" style="min-width: initial; visibility: ' . ($page < $maxPages ? 'visible' : 'hidden') . ';"><i class="fa fa-chevron-right"></i></a> ';
echo '</section>';
} elseif($type == "rss") {
if($page > 1) {
echo '<link rel="previous" href="https://www.technosparks.net/blog/atom-feed?page='.($page + 1).'"/>';
}
if($page < $maxPages) {
echo '<link rel="next" href="https://www.technosparks.net/blog/atom-feed?page='.($page - 1).'"/>';
}
}
}
function blog_processEntry($entryRequested) {
global $pgconfig_title, $blogSubtitle, $timestamp;
$entryFound = false;
$fp = fopen("entries-list.txt", "r");
while(!feof($fp)) {
// Line 1
$fpEntry = ftell($fp);
if(empty(fgets($fp))) { break; }
// Line 2 - 3
fgets($fp); fgets($fp);
// Line 4
if(trim(fgets($fp), "\n") == $entryRequested) {
$entryFound = true;
break;
}
// Line 5 - 7
fgets($fp); fgets($fp); fgets($fp);
}
if($entryFound == false) {
fclose($fp);
redirect(404);
}
fseek($fp, $fpEntry);
// Line 1 - 2
fgets($fp); fgets($fp);
// Line 3
$timestamp = explode(" ", trim(fgets($fp), "\n"));
// Line 4
fgets($fp);
// Line 5
$pgconfig_title = trim(fgets($fp), "\n");
// Line 6
fgets($fp);
// Line 7
$blogSubtitle = trim(fgets($fp), "\n");
fclose($fp);
}
function blog_timeparser($timestamp, $type = "standard") {
if(empty($timestamp)) return null;
if($type == "standard") {
$fullDate = date("l d F Y h:ia", $timestamp);
} elseif($type == "rss") {
$fullDate = date(DATE_ATOM, $timestamp);
} elseif($type == "blog-header") {
$fullDate = [ "d" => date("d", $timestamp), "m" => date("M", $timestamp), "y" => date("Y", $timestamp)];
}
return $fullDate;
}
function blog_tagsGetEntries($needtag = "") {
if(empty($needtag)) { return false; } else { $needtag = urldecode($needtag); }
$entryFound = false;
$fp = fopen("entries-list.txt", "r");
$entries = 0; $fpEntry = []; $fpEntryofPg = []; $fpEntryofPgi = 0; $fpEntryi = 0;
while(!feof($fp)) {
// Line 1
$fpBefore = ftell($fp);
if(empty(fgets($fp))) { break; }
// Line 2
if(fgets($fp) == "wip\n") {
if(!isset($_GET["all"])) {
for($i = 1; $i <= 5; $i++) {
fgets($fp);
}
continue;
}
}
// Line 3 - 5
fgets($fp); fgets($fp); fgets($fp);
// Line 6
$tags = explode(", ", trim(fgets($fp), "\n"));
foreach($tags as $tag) {
if($tag == $needtag) {
$fpEntry[$fpEntryi++] = $fpBefore;
$entryFound = true;
break;
}
}
// Line 7
fgets($fp);
}
if($entryFound == true) {
$entries = count($fpEntry);
$maxPages = ceil($entries / 6);
if(isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; }
if($page > $maxPages || $page <= 0) {
echo '<section>'.infobox("warn", ($page < 0 ? 'There&apos;s no such thing as negative pages! ' : '').'You attempted to access a page (page '.$page.') which does not exist! Showing you page 1 instead.').'</section>';
$page = 1;
}
$fpEntryi = ($page - 1) * 6;
$fpEntryMax = $fpEntryi + 5;
while($fpEntryi < $entries && $fpEntryi < $fpEntryMax) {
fseek($fp, $fpEntry[$fpEntryi]);
// Line 1 - 2
fgets($fp); fgets($fp);
// Line 3
$entryData["time"] = explode(" ", trim(fgets($fp), "\n"));
// Line 4
$entryData["filename"] = trim(fgets($fp), "\n");
// Line 5
$entryData["title"] = trim(fgets($fp), "\n");
// Line 6
$entryData["tags"] = explode(", ", trim(fgets($fp), "\n"));
// Line 7
fgets($fp);
blog_printDatabaseEcho($entryData, "default");
$fpEntryi++;
}
echo '<section style="text-align: center">';
echo '<a class="blog pagination button-link" href="?page='.($page - 1).((isset($_GET["all"])) ? '$all' : '').'" style="min-width: initial; visibility: ' . ($page > 1 ? 'visible' : 'hidden') . ';"><i class="fa fa-chevron-left"></i></a> ';
echo '<span class="blog pagination">Page ' . $page . '</span> ';
echo '<a class="blog pagination button-link" style="min-width: initial;" href="?page=' . ($page + 1) . ((isset($_GET["all"])) ? '$all' : '') . '" style="min-width: initial; visibility: ' . ($page < $maxPages ? 'visible' : 'hidden') . ';"><i class="fa fa-chevron-right"></i></a> ';
echo '</section>';
} else {
echo '<section>'.infobox("notify", 'Unfortunately, there are no articles with the tag “'.$needtag.'').'</section>';
}
fclose($fp);
}
function blog_tagsListAllTags() {
$tags = [];
$tagsi = 0;
$fp = fopen("entries-list.txt", "r");
while(!feof($fp)) {
// Line 1
if(empty(fgets($fp))) { break; }
// Line 2
if(fgets($fp) == "wip\n") {
if(!isset($_GET["all"])) {
for($i = 1; $i <= 5; $i++) {
fgets($fp);
}
continue;
}
}
// Line 3 - 5
fgets($fp); fgets($fp); fgets($fp);
// Line 6
$tagsfromEntry = explode(", ", trim(fgets($fp), "\n"));
foreach($tagsfromEntry as $tagsfromEntry) {
$tags[$tagsi++] = $tagsfromEntry;
}
// Line 7
fgets($fp);
}
fclose($fp);
$tags = array_unique($tags);
sort($tags);
foreach($tags as $tag) {
echo '<a href="?find='.urlencode($tag).((isset($_GET["all"])) ? '$all' : '').'" class="button-link" style="background-color: #6891bb;">'.$tag.'</a>';
}
}
function blog_tagsDisplayAllTags($tags) {
if(isset($_GET["all"])) $appendViewAll = "$all"; else $appendViewAll = "";
$arrSize = count($tags);
for($i = 0; $i < $arrSize; $i++) {
echo $tags[$i];
if($i + 1 != $arrSize) { echo ", "; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment