326 lines
8.8 KiB
PHP
326 lines
8.8 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.
|
|
*/
|
|
|
|
// some settings
|
|
define('BLOG_EXT', "md");
|
|
define('BLOG_TITLE', "speedie.site updates");
|
|
define('BLOG_DESC', "Update feed, for quick short updates.");
|
|
define('BLOG_URL', "https://speedie.site/updates");
|
|
define('BLOG_LATEST_TEXT', "Latest update: ");
|
|
define('BLOG_DIR', "/updates/");
|
|
define('DISPLAY_NUM', false);
|
|
define('DISPLAY_ID', false);
|
|
define('ENABLE_TITLE', true);
|
|
define('ENABLE_HEAD', true);
|
|
define('ENABLE_FOOTER', true);
|
|
define('META_DESC', true);
|
|
define('META_ENC', true);
|
|
|
|
spl_autoload_register(function($class) {
|
|
require str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class, '\\')).'.php';
|
|
});
|
|
|
|
define('SELF', $_SERVER['SCRIPT_NAME']);
|
|
define('VIEW', '');
|
|
define('BASE_URI', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
|
|
define('BLOG_PATH', dirname(__FILE__). BLOG_DIR);
|
|
|
|
use md\MarkdownExtra;
|
|
|
|
function isntNull( $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 getDateForPost($postName, $postDate) {
|
|
$file = BLOG_PATH . "/$postName." . BLOG_EXT . ".date";
|
|
|
|
if (file_exists($file)) {
|
|
return file_get_contents($file);
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function getNameForPost($postName) {
|
|
$file = BLOG_PATH . "/$postName." . BLOG_EXT . ".title";
|
|
$title = $postName;
|
|
|
|
if (file_exists($file)) {
|
|
return rtrim(file_get_contents($file));
|
|
}
|
|
|
|
return $postName;
|
|
}
|
|
|
|
function getAllPostNames($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, ...getAllPostNames( "$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 fileNameForPost($post) {
|
|
return BLOG_PATH . "/$post." . BLOG_EXT;
|
|
}
|
|
|
|
function dateForPost($post) {
|
|
$file = BLOG_PATH . "/$post." . BLOG_EXT . ".date";
|
|
|
|
if (file_exists($file)) {
|
|
return file_get_contents($file);
|
|
}
|
|
}
|
|
|
|
function numForPost($post) {
|
|
$file = BLOG_PATH . "/$post." . BLOG_EXT . ".num";
|
|
|
|
if (file_exists($file)) {
|
|
return file_get_contents($file);
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function sanitizeFilename($inFileName) {
|
|
return str_replace(array('~', '..', '\\', ':', '|', '&', '.', '+', '!'), '-', $inFileName);
|
|
}
|
|
|
|
function postURL($post) {
|
|
return SELF . VIEW . "/".str_replace("%2F", "/", str_replace("%23", "#", urlencode(sanitizeFilename($post))));
|
|
}
|
|
|
|
function postLink($post, $title, $attributes="") {
|
|
return "<a href=\"" . postURL($post) ."\"$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);
|
|
$linkedPost = $linkTitleSplit[0]; // split away an eventual link text
|
|
$linkText = (count($linkTitleSplit) > 1) ? $linkTitleSplit[1] : $linkedPost;
|
|
$postPart = explode('#', $linkedPost)[0]; // split away an eventual anchor part
|
|
$linkedFilename = fileNameForPost(sanitizeFilename($postPart));
|
|
$exists = file_exists($linkedFilename);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return $outHTML;
|
|
}
|
|
|
|
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
|
|
$newPost = "";
|
|
$text = "";
|
|
$html = "";
|
|
$rss = "";
|
|
|
|
$post = preg_match('@^/@', @$_SERVER["PATH_INFO"]) ?
|
|
urldecode(substr($_SERVER["PATH_INFO"], 1)) : urldecode(@$_REQUEST['post']);
|
|
$post = sanitizeFilename($post);
|
|
|
|
if ($post != '') {
|
|
$filename = fileNameForPost($post);
|
|
|
|
if ( file_exists($filename) ) {
|
|
$text = file_get_contents($filename);
|
|
} else {
|
|
$newPost = NULL;
|
|
include('php/404.php');
|
|
die();
|
|
}
|
|
} else {
|
|
$action = 'home';
|
|
}
|
|
|
|
if ( $action === 'home') {
|
|
$postNames = getAllPostNames();
|
|
$filelist = array();
|
|
$sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : 'name';
|
|
|
|
foreach($postNames as $post) {
|
|
$filelist[$post] = strtotime(dateForPost($post));
|
|
}
|
|
|
|
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/updates.xml\" />\n";
|
|
|
|
$html .= "<h2>Updates</h2>\n";
|
|
$html .= " <p>This is a list of all my update posts. If you prefer, you can also read them using your favorite RSS reader through <a href=\"updates.xml\">my feed.</a>\n";
|
|
$html .= " <ul>\n";
|
|
|
|
$f = "0";
|
|
$num = "";
|
|
|
|
foreach ($filelist as $postName => $postDate) {
|
|
if (DISPLAY_NUM) {
|
|
$i += 1;
|
|
$num = "$i ";
|
|
}
|
|
|
|
if (DISPLAY_ID) {
|
|
$i = numForPost($postName);
|
|
$num = "$i";
|
|
}
|
|
|
|
$pubDate = date('r', strtotime(getDateForPost($postName, $postDate)));
|
|
$link = postURL($postName, $postName);
|
|
$description = toHTML(file_get_contents(fileNameForPost($postName)));
|
|
$title = getNameForPost($postName);
|
|
|
|
$rss .= "<item>\n";
|
|
$rss .= " <title>$title</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>".BLOG_LATEST_TEXT.postLink($postName, $title).", written ".getDateForPost($postName, $postDate)."</p>";
|
|
$f = 1;
|
|
} else {
|
|
$html .= " <p><strong>$num</strong> ".postLink($postName, $title).", written ".getDateForPost($postName, $postDate)."</p>";
|
|
}
|
|
|
|
$html .= "</li>\n";
|
|
}
|
|
|
|
$html .= "</ul>\n";
|
|
|
|
// End the RSS feed
|
|
$rss .= "</channel>\n";
|
|
$rss .= "</rss>\n";
|
|
|
|
if (file_get_contents('updates.xml') != $rss) {
|
|
file_put_contents('updates.xml', $rss);
|
|
}
|
|
} else { // convert the post and view it
|
|
$html .= empty($text) ? '' : toHTML($text);
|
|
}
|
|
|
|
// All blog posts
|
|
if (($action === 'home')) {
|
|
$title = isntNull("Update posts");
|
|
} else if ($filename != '') {
|
|
$title = $post;
|
|
}
|
|
|
|
// print text
|
|
printHeader($title, $action, $html);
|
|
|
|
print " $html\n";
|
|
print " </div>\n";
|
|
printFooter();
|
|
print "</html>\n";
|