* Copyright (C) 2023 speedie * * See LICENSE.blog file for copyright and license details. */ // some settings define('BLOG_EXT', "md"); define('BLOG_TITLE', "speedie's blog"); define('BLOG_DESC', "speedie's blog, about stuff I want to talk about."); define('BLOG_URL', "https://speedie.site/blog"); define('BLOG_LATEST_TEXT', "Latest blog post: "); define('BLOG_DIR', "/articles"); 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>@si', '', $nhtml); $nhtml = preg_replace( '@<(li)[^>]*?>.*?@si', '', $nhtml); $nhtml = preg_replace( '@<(ul)[^>]*?>.*?@si', '', $nhtml); $nhtml = strip_tags($nhtml); $nhtml = preg_replace('/\s*$^\s*/m', "\n", $nhtml); $nhtml = truncate($nhtml, 512); } if (ENABLE_HEAD) { print "\n"; print "\n"; if (META_DESC && $action != 'home') { print "\n"; } else if (META_DESC) { print "\n"; } if (ENABLE_TITLE) { print "$title\n"; } include("php/header.php"); print " \n"; } print " \n"; print "
\n\n"; } function printFooter() { if (ENABLE_FOOTER) { print " \n"; print "
\n"; include("php/footer.php"); print "
\n"; } } function getDateForPost($postName) { $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 "$title"; } 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>/", $outHTML, $matches, PREG_PATTERN_ORDER); for ($i = 0; $i < count($matches[0]); $i++) { $prefix = ""; $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 .= "\n"; $rss .= "\n"; $rss .= " ". BLOG_TITLE ."\n"; $rss .= " ". BLOG_DESC ."\n"; $rss .= " \n"; // Print text before blog itself $html .= "

Blog

\n"; $html .= "

This is a list of all my blog posts. If you prefer, you can also read them using your favorite RSS reader through my feed.\n"; $html .= "

If you wish, you can also get site updates, through the updates feed or your web browser. The feed is updated whenever there are important things to announce with the site, or a new release for my software is out.

\n"; $html .= "
    \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))); $link = postURL($postName, $postName); $description = toHTML(file_get_contents(fileNameForPost($postName))); $title = getNameForPost($postName); $rss .= "\n"; $rss .= " $title\n"; $rss .= " $link\n"; $rss .= " $link\n"; $rss .= " $pubDate\n"; $rss .= " \n"; $rss .= " \n"; $rss .= " \n"; $rss .= "\n"; $html .= "
  • "; if (!$f) { $html .= "

    $num".BLOG_LATEST_TEXT.postLink($postName, $title).", written ".getDateForPost($postName)."

    "; $f = 1; } else { $html .= "

    $num ".postLink($postName, $title).", written ".getDateForPost($postName)."

    "; } $html .= "
  • \n"; } $html .= "
\n"; // End the RSS feed $rss .= "
\n"; $rss .= "
\n"; if (file_get_contents('rss.xml') != $rss) { file_put_contents('rss.xml', $rss); } } else { // convert the post and view it $name = getNameForPost($post); $date = getDateForPost($post); $html .= "

$name

\n"; $html .= "

$date

\n"; $html .= empty($text) ? '' : toHTML($text); } // All blog posts if (($action === 'home')) { $title = isntNull("Blog posts"); } else { $title = getNameForPost($post); } // print text printHeader($title, $action, $html); print " $html\n"; print "
\n"; printFooter(); print "\n";