#!/usr/pkg/bin/php
iJozhaus Phlog - A place to share a few thoughts.       x       x       x       0
i       x       x       x       0
<?php
// Simple Gopher Blog / Phlog
// [email protected], 2016, public domain
// v0.2a, it may work, it may not, it works for me

// Simply serves up n recent posts, and allows the user to view older posts
// The site owner must cull/archive older posts manually if they don't wish
// for them to be included in the main listing.

//// [Settings / variables / globals]
$perPage = 5;
$phlogDir = "/users/tfurrows/phlog"; // relative to your gopher root, for generating links
$siteDir = "/users/tfurrows";
$gopherURL = "sdf.org";

//// [non-config variables]
$terminate = "\tx\tx\t0\n";

//// [Functions]
function getWords($string, $count = 10) {
       preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $string, $matches);
       return $matches[0];
}

//// [Main]

// Determine starting entry by "page"
$urlParam = getenv("QUERY_STRING");

if($urlParam!="") {
       $param = explode("=",$urlParam);
       $currentPage = $param[1];
       $startEntry = (($param[1] * $perPage)-$perPage);
} else {
       $startEntry = 0;
       $currentPage = 1;
}

// get all text files in the current working directory
// as phlog entries, sort by file modification time
$entries = glob("/sdf/ftp/pub/users/tfurrows/phlog/*.txt");
usort($entries, create_function('$b,$a', 'return filemtime($a) - filemtime($b);'));

$count=0;
foreach ($entries as $entry) {
       // Display a brief portion of n entries

       // Determine if they need to skip based on page num
       if($count<$startEntry) {
               $count++;
               continue;
       }

       // Start with some details about this post
       echo "i"."Posted on ".date("D F j, Y, g:i a",filemtime($entry)).$terminate;
       echo "i".str_repeat("-",50).$terminate;

       // get the first n words, format the output for gopher
       $summary = getWords(file_get_contents($entry),20);
       $summary = trim(preg_replace('/\s+/', ' ', $summary));
       $summary = wordwrap($summary,50,"\tx\tx\t0\ni");

       echo "i".$summary." ...".$terminate;
       echo "0Read this post\t".$phlogDir."/".str_replace("/sdf/ftp/pub/users/tfurrows/phlog/","",$entry)."\t".$gopherURL."\t70\tx\tx\t0\n";
       echo "i".$terminate;

       $count++;
       if($count>=($perPage+$startEntry)) break;
}

// If they have more than what is shown, give them a link to view more.
if(count($entries)>$perPage) {
       $totalPages = ceil(count($entries) / $perPage);

       echo "i".$terminate;
       for($i=1;$i<=$totalPages;$i++) {
               if($i == $currentPage) {
                       echo "iPage $i (current page)".$terminate;
               } else {
                       echo "1Page $i\t".$siteDir."/cgi-bin/phlog.php?page=$i\t".$gopherURL."\t70\n";
               }
       }
}
//echo "1Return to Jozhaus\t/\t$gopherURL\t70\n";
include("../footer.php");
?>