#!/usr/pkg/bin/php
<?php
// Gophermap.php, file list with descriptions
// 04/29/16, [email protected] for questions

$thisRoot = str_replace("/sdf/ftp/pub","",__DIR__);
$siteUrl = $_SERVER['SERVER_NAME'];
$SERVER_PORT = "70";

$thisDescr = "_descr.txt"; // text flat-file with descriptions (filename|description, one per line)
$thisMapHead = "_mapheader.txt"; // whatever you want included before the file listing
//$terminate = "\tx\tx\t0\n";
$terminate = "\n";

// Specific file names to ignore in this listing
$ignoreList[] = ".";
$ignoreList[] = "..";
$ignoreList[] = "gophermap";
$ignoreList[] = "gophermap.php.txt";
$ignoreList[] = $thisDescr;
$ignoreList[] = $thisMapHead;

// Gopher file types
$gfile['txt'] = "0";
$gfile['htm'] = "0";    // Serve my HTM, and HTML files as text
$gfile['html'] = "0";
$gfile['php'] = "-";    // Hidden file type example

// Images
$gfile['gif'] = "g";
$gfile['jpg'] = "I";
$gfile['jpeg'] = "I";
$gfile['png'] = "I";
$gfile['bmp'] = "I";
$gfile['tiff'] = "I";
$gfile['pcx'] = "I";
$gfile['ico'] = "I";
$gfile['tif'] = "I";
$gfile['svg'] = "I";
$gfile['eps'] = "I";

// Audio
$gfile['mp3'] = "s";
$gfile['mp2'] = "s";
$gfile['wav'] = "s";
$gfile['mid'] = "s";
$gfile['wma'] = "s";
$gfile['flac'] = "s";
$gfile['mpc'] = "s";
$gfile['aiff'] = "s";
$gfile['aac'] = "s";

$gfile['pdf'] = "P"; // may need to be "d"

// Process the descriptions
$descLines = explode("\n",file_get_contents("./".$thisDescr));
foreach($descLines as $descLine) {
       $desc = explode("|",$descLine);
       $description[$desc[0]]=$desc[1];
}

// Get the listing
$fileList = scandir("./", SCANDIR_SORT_NONE);
usort($fileList, 'strnatcasecmp');

foreach($fileList as $fileName) {
       // Skip ignore list files/directories
       if(in_array($fileName,$ignoreList)) {
               continue;
       }

       // File or directory
       if(is_dir("./".$fileName)) {
               $dirDisp[] = "1$fileName        $thisRoot/$fileName     $siteUrl        ".$SERVER_PORT."\n";
       } else {
               // Serve based on file type
               $ext = strtolower(pathinfo("./".$fileName,PATHINFO_EXTENSION));

               if(array_key_exists($ext,$gfile)) {
                       // Hidden filetypes
                       if($gfile[$ext]=="-") continue;

                       $leadChar = $gfile[$ext];
               } else {
                       // Everything not specified is binary
                       $leadChar = "9";
               }

               // Display the filename, with description if available
               $fileDispAdd = "";
               if(array_key_exists($fileName,$description)) {
                       $fileDispAdd = "   ^".$description[$fileName].$terminate;
               }
               $fileDisp[] = $leadChar."$fileName      $thisRoot/$fileName     $siteUrl        ".$SERVER_PORT."\n".$fileDispAdd;
       }
}

//// Output
if($thisMapHead!="") include($thisMapHead);

// Directories first
echo implode("",$dirDisp);
echo implode("",$fileDisp);

echo " ".$terminate;
//echo "0View the PHP for this script   $thisRoot/gophermap.php.txt     $siteUrl        ".$SERVER_PORT."\r\n";


//include('/var/gopher/footer.php');

?>