<?php
    //settings neccesary to run this thing
    $me="index.php";
    $title  = "Example Title" ;
    $preview_size=250 ;
    $items_per_page=10;
    $default_path="example";
    $site_paths=array("example","folder");
    date_default_timezone_set("Europe/London");
    header_remove("X-Powered-By");
 
   //retrieve the variables we need
   $dir = isset($_GET['dir']) ? $_GET['dir'] : $default_path;
   $post = isset($_GET['post']) ? $_GET['post'] : null;
   $page = isset($_GET['page']) ? $_GET['page'] : 0;
   $page = max($page,0);
        
   //verify that our user isn't fucking with us
   $dir = in_array ($dir,$site_paths,TRUE) ? $dir : $default_path;
   
   #get all entries (txt files)
   $entries =  array_diff(scandir($dir), array('..', '.'));
   $entries = preg_grep("(.*[^/]*.txt)", $entries);
   
   $sorted_entries = array();
   $post = in_array($post . ".txt",$entries,TRUE) ? $post : null ;
   $in_dir=$post==null;
   
   $title=$post==null?$title:str_replace("_"," ",$post);
?>

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv='content-type' content='text/html; charset=UTF-8'>
    <title><?php echo $title ?></title>
      
      <style>
         * {
            margin: 0;
            padding: 0;
            font-family: Consolas, 'Courier New', monospace;
            color: grey;
          }

          a {
            font-weight: bold;
            text-decoration: none;
            }

          a:hover {
            background-color: white;
            color: black;
          }

          h1 {
            padding-top: 2px;
            margin-bottom: 10px;
            padding-left: 4px;
            background-color: black;
            font-size: 1em;
            color: white;
          }

          div#top {
            left: 0;
            top: 0;
            width: 100%;
            height: 24px;
            position: fixed;
            overflow: hidden;
            padding-top: 2px;
            line-height: 20px;
            background-color: black;
          }

          span#title {
            padding-left: 5px;
            font-size: 1.5em;
            color: white;
          }
          
          div.item {
            width: 800px;
            margin-top: 40px;
            margin-left: auto;
            margin-right: auto;
            margin-top: 40px;
            text-align: justify;
          }
          
      </style>
    </head>

    <body>
<?php 
        foreach ($entries as $current) {
           $current_name=basename($current, ".txt");
           $sorted_entries[-file_get_contents($dir . DIRECTORY_SEPARATOR .   $current_name . ".date")] = $current_name ;
        }
        
	ksort($sorted_entries);

        
        $sorted_entries=array_slice($sorted_entries, $page * $items_per_page, $items_per_page);
        $sorted_entries=$post==null?$sorted_entries:array($post);
        
        echo "      <div id='top'>\n";
         
        foreach ($site_paths as $current) {
          echo "        <span class='title'>\n          <a href='",$me,"?dir=",$current,"'>[",$current,"]</a>\n        </span>\n";
        }        
        
        echo "      </div>\n";
        
        foreach ($sorted_entries as $current) {
            $currentfile=$dir . DIRECTORY_SEPARATOR .  $current;
            
            $current_entry=file_get_contents ($currentfile . ".txt");
            
            $date=date("F d Y H:i:s", (int)trim(file_get_contents ($currentfile. ".date")));
            
            if($in_dir){
               $current_entry= substr($current_entry,0,$preview_size).'...'. "<a href='$me?dir=$dir&amp;post=$current'>[read more]</a>";
            }
            
            echo "      <div class='item'>\n        <h1>",str_replace("_"," ",$current),"<span style='float: right;'>",$date, "</span></h1>\n","        ",$current_entry,"\n      </div>\n";
            
        }
       echo "      <br><br>\n";
       
       if( $in_dir){
         echo  "      <div class='item'>\n";
         echo "        <a href='$me?dir=$dir&amp;page=",max($page - 1,0),"'>Previous $items_per_page entries</a>\n";
         echo "        <a href='$me?dir=$dir&amp;page=",max($page + 1,0),"' style='float: right;'>Next $items_per_page entries</a>\n";
         echo "      </div>\n";
       }
        
?>
    </body>
</html>


