This repository has been archived on 2024-01-22. You can view files and clone it, but cannot push or open issues or pull requests.
speedie-page/blog.php
2023-07-01 21:38:59 +02:00

327 lines
9.5 KiB
PHP

<?php
/*
* speedie-blog
*
* Copyright (C) 2007-2011 Steven Frank <http://stevenf.com/>
* Copyright (C) 2023 speedie <speedie@speedie.site>
*
* See LICENSE.blog file for copyright and license details.
*/
spl_autoload_register(function($class){
require str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class, '\\')).'.php';
});
use md\MarkdownExtra;
define('BLOG_PATH', dirname(__FILE__). '/articles');
define('BLOG_EXT', 'md');
define('BLOG_TITLE', "speedie's blog");
define('BLOG_DESC', "speedie's blog, about stuff I want to talk about.");
define('LATEST_TEXT', "Latest blog post: ");
define('BLOG_URL', "https://speedie.site/blog");
define('DISPLAY_NUM', false);
define('DISPLAY_ID', false);
define('BASE_URI', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
define('SELF', $_SERVER['SCRIPT_NAME']);
define('VIEW', '');
define('ENABLE_TITLE', true);
define('ENABLE_HEAD', true);
define('ENABLE_FOOTER', true);
define('META_DESC', true);
define('META_ENC', true);
function __( $label, $alt_word = null ) {
return is_null($alt_word) ? $label : $alt_word;
}
function truncate($text, $chars) {
if (strlen($text) <= $chars) {
return $text;
}
$text = $text." ";
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}
function printHeader($title, $action, $html) {
// TODO: Improve this garbage, I suck at PHP and this is just stuff I found on the internet.
if (META_DESC) {
$nhtml = $html;
$nhtml = preg_replace( '@<p\b[^>]*>(?=.*?<a\b[^>]*>).*?<\@p>@si', '', $nhtml);
$nhtml = preg_replace( '@<(li)[^>]*?>.*?</\\1>@si', '', $nhtml);
$nhtml = preg_replace( '@<(ul)[^>]*?>.*?</\\1>@si', '', $nhtml);
$nhtml = strip_tags($nhtml);
$nhtml = preg_replace('/\s*$^\s*/m', "\n", $nhtml);
$nhtml = truncate($nhtml, 512);
}
if (ENABLE_HEAD) {
print "<!DOCTYPE html>\n";
print "<head>\n";
if (META_DESC && $action != 'home') {
print "<meta name=\"description\" content=\"$nhtml\">\n";
} else if (META_DESC) {
print "<meta name=\"description\" content=". BLOG_DESC .">\n";
}
if (ENABLE_TITLE) {
print "<title>$title</title>\n";
}
include("php/header.php");
print " </head>\n";
}
print " <body>\n";
print "<div class=\"content\">\n\n";
}
function printFooter() {
if (ENABLE_FOOTER) {
print " </body>\n";
print "<footer>\n";
include("php/footer.php");
print "</footer>\n";
}
}
function getDateForPage($pageName, $pageDate) {
$file = BLOG_PATH . "/$pageName." . BLOG_EXT . ".date";
if (file_exists($file)) {
return file_get_contents($file);
} else {
return 0;
}
}
function getAllPageNames($path = "") {
$filenames = array();
$dir = opendir(BLOG_PATH . "/$path" );
while ( $filename = readdir($dir) ) {
if ( $filename[0] == "." ) {
continue;
}
if ( is_dir( BLOG_PATH . "/$path/$filename" ) ) {
array_push($filenames, ...getAllPageNames( "$path/$filename" ) );
continue;
}
if ( preg_match("/".BLOG_EXT."$/", $filename) != 1) {
continue;
}
$filename = substr($filename, 0, -(strlen(BLOG_EXT)+1) );
$filenames[] = substr("$path/$filename", 1);
}
closedir($dir);
return $filenames;
}
function fileNameForPage($page) {
return BLOG_PATH . "/$page." . BLOG_EXT;
}
function dateForPage($page) {
$file = BLOG_PATH . "/$page." . BLOG_EXT . ".date";
if (file_exists($file)) {
return file_get_contents($file);
}
}
function numForPage($page) {
$file = BLOG_PATH . "/$page." . BLOG_EXT . ".num";
return file_get_contents($file);
}
function sanitizeFilename($inFileName) {
return str_replace(array('~', '..', '\\', ':', '|', '&', '.', '+', '!'), '-', $inFileName);
}
function pageURL($page) {
return SELF . VIEW . "/".str_replace("%2F", "/", str_replace("%23", "#", urlencode(sanitizeFilename($page))));
}
function pageLink($page, $title, $attributes="") {
return "<a href=\"" . pageURL($page) ."\"$attributes>$title</a>";
}
function toHTMLID($noid) {
return str_replace(" ", "-", $noid);
}
function toHTML($inText) {
$parser = new MarkdownExtra;
$parser->no_markup = true;
$outHTML = $parser->transform($inText);
preg_match_all("/\[\[(.*?)\]\]/", $outHTML, $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
$fullLinkText = $matches[1][$i];
$linkTitleSplit = explode('|', $fullLinkText);
$linkedPage = $linkTitleSplit[0]; // split away an eventual link text
$linkText = (count($linkTitleSplit) > 1) ? $linkTitleSplit[1] : $linkedPage;
$pagePart = explode('#', $linkedPage)[0]; // split away an eventual anchor part
$linkedFilename = fileNameForPage(sanitizeFilename($pagePart));
$exists = file_exists($linkedFilename);
$outHTML = str_replace("[[$fullLinkText]]",
pageLink($linkedPage, $linkText, ($exists? "" : " class=\"noexist\"")), $outHTML);
}
$outHTML = preg_replace("/\{\{(.*?)\}\}/", "<img src=\"" . BASE_URI . "/images/\\1\" alt=\"\\1\" />", $outHTML);
preg_match_all("/<h([1-4])>(.*?)<\/h\\1>/", $outHTML, $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
$prefix = "<h".$matches[1][$i].">";
$caption = $matches[2][$i];
$suffix = substr_replace($prefix, "/", 1, 0);
/*
$outHTML = str_replace("$prefix$caption$suffix",
"$prefix<a id=\"".toHTMLID($caption)."\">$caption</a>$suffix", $outHTML);
*/
}
return $outHTML;
}
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
$newPage = "";
$text = "";
$html = "";
$rss = "";
$page = preg_match('@^/@', @$_SERVER["PATH_INFO"]) ?
urldecode(substr($_SERVER["PATH_INFO"], 1)) : urldecode(@$_REQUEST['page']);
$page = sanitizeFilename($page);
if ($page != '') {
$filename = fileNameForPage($page);
if ( file_exists($filename) ) {
$text = file_get_contents($filename);
} else {
$newPage = NULL;
include('php/404.php');
die();
}
} else {
$action = 'home';
}
if ( $action === 'home') {
$pageNames = getAllPageNames();
$filelist = array();
$sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : 'name';
foreach($pageNames as $page) {
$filelist[$page] = numForPage($page);
}
arsort($filelist, SORT_NUMERIC);
// Create the RSS feed
$rss .= "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
$rss .= "<channel>\n";
$rss .= " <title>". BLOG_TITLE ."</title>\n";
$rss .= " <description>". BLOG_DESC ."</description>\n";
$rss .= " <atom:link href=\"". BLOG_URL ."\" rel=\"self\" type=\"application/rss+xml\" />\n";
// Print text before blog itself
$html .= "<h2>Blog</h2>\n";
$html .= " <p>This is a list of all my blog posts. If you prefer, you can also read them using your favorite RSS reader through <a href=\"rss.xml\">my feed.</a>\n";
$html .= " <p>If you wish, you can also get site updates, through the <a href=\"updates.xml\">updates feed.</a> The feed is updated whenever there are important things to announce with the site, or a new release for my software is out.</p>\n";
$html .= " <ul>\n";
$f = "0";
foreach ($filelist as $pageName => $pageDate) {
if (DISPLAY_NUM) {
$i += 1;
$num = "$i ";
}
if (DISPLAY_ID) {
$i = numForPage($pageName);
$num = "$i";
}
$pubDate = date('r', strtotime(getDateForPage($pageName, $pageDate)));
$link = pageURL($pageName, $pageName);
$description = toHTML(file_get_contents(fileNameForPage($pageName)));
$rss .= "<item>\n";
$rss .= " <title>$pageName</title>\n";
$rss .= " <link>$link</link>\n";
$rss .= " <guid>$link</guid>\n";
$rss .= " <pubDate>$pubDate</pubDate>\n";
$rss .= " <description>\n";
$rss .= " <![CDATA[\n";
$rss .= " $description\n";
$rss .= " ]]>\n";
$rss .= " </description>\n";
$rss .= "</item>\n";
$html .= "<li>";
if (!$f) {
$html .= " <p class=\"latest\"><strong>$num</strong>".LATEST_TEXT.pageLink($pageName, $pageName).", written ".getDateForPage($pageName, $pageDate)."</p>";
$f = 1;
} else {
$html .= " <p><strong>$num</strong> ".pageLink($pageName, $pageName).", written ".getDateForPage($pageName, $pageDate)."</p>";
}
$html .= "</li>\n";
}
$html .= "</ul>\n";
// Write end of the page
$html .= "<h3>Archived blog posts</h3>\n";
$html .= " <p>Some blog posts have been archived because they're written out of frustration, are irrelevant or just aren't worth reading. You can still access the blog posts through these RSS feeds:</p>\n";
$html .= "<ul>\n";
$html .= " <li><a href=\"rss-archived-01.xml\">Blog post archive feed (1-36)</a></li>\n";
$html .= " <li><a href=\"rss-archived-02.xml\">Blog post archive feed (37-49)</a></li>\n";
$html .= "</ul>\n";
// End the RSS feed
$rss .= "</channel>\n";
$rss .= "</rss>\n";
if (file_get_contents('rss.xml') != $rss) {
file_put_contents('rss.xml', $rss);
}
} else { // convert the page and view it
$html .= empty($text) ? '' : toHTML($text);
}
// All blog posts
if (($action === 'home')) {
$title = __("Blog posts");
} else if ($filename != '') {
$title = $page;
}
// print text
printHeader($title, $action, $html);
print " $html\n";
print " </div>\n";
printFooter();
print "</html>\n";