SWS PHP Cross Reference Web Portal Systems

Source: /showfilelist.php - 213 lines - 7659 bytes - Summary - Text - Print

   1  <?php
   2  /*#################################################################
   3  #
   4  # Name:    showfilelist.php
   5  #
   6  #   Use this to show an annotated list of files within the context
   7  #   of an SWS web site.
   8  #
   9  # Parameters:
  10  #   dir - directory where file lives. Relative to DOCUMENT_ROOT
  11  #   file - file containing a list of files to show
  12  #
  13  # Format of '$file' is a sequence of key=values where KEY may be:
  14  #
  15  #   pagetitle=string     Becomes title for HTML page
  16  #
  17  #   header=string        Page header (enclosed in a <h3></h3> tag)
  18  #
  19  #   section=string       Section header (enclosed in a <h3></h3> tag)
  20  #
  21  #   item=string          Starts a new link (enclosed in <li></li> tag)
  22  #   item2=string         Starts a second level new link (enclosed in <li></li> tag)
  23  #   url=string           Optional URL for <a href></a> tag
  24  #   comment=string       Optional comments added after <a href></a> tag
  25  #
  26  #   Jul 23, 2004  tpg      Initial coding begun
  27  #   Jan 20, 2005  tpg      Introduce new format for input file
  28  #
  29  # Copyright (C) 2004-2005 Terry Gliedt
  30  # This is free software; you can redistribute it and/or modify it under the
  31  # terms of the GNU General Public License as published by the Free Software
  32  # Foundation; See http://www.gnu.org/copyleft/gpl.html
  33  #################################################################*/
  34  //ini_set('error_reporting', E_ALL ^ E_NOTICE);
  35  $SWSDIR = $_SERVER['DOCUMENT_ROOT'] . '/sws';   // Where all my files are
  36  include_once "$SWSDIR/common.php";
  37  
  38  session();                          //  Watch for authorization to expire
  39  
  40  //  Get parameters passed in via normal invocation
  41  extract(isolate_parms( 'dir', 'file'));
  42  
  43  list($title,$html) = showfilelist($dir, $file);
  44  do_page($title, $html);
  45  
  46  exit;
  47  
  48  
  49  /*#################################################################
  50  #
  51  # Name:    showfilelist($dir, $file)
  52  #
  53  #   Show an HTML list of lines in a simple file.
  54  #
  55  # Parameters:
  56  #   dir - name of directory relative to HTDOCS
  57  #   file - file in $dir
  58  #
  59  # Returns:
  60  #   title of HTML
  61  #   string of HTML
  62  #
  63  #################################################################*/
  64  function showfilelist ($dir, $file) {
  65      global $SWSDIR, $REVISIONDATE;
  66      $pagetitle = '';                    // Must have an HTML page title
  67      $html = '';
  68      $openitem = "<ul>\n";               // HTML to open/close lists
  69      $closeitem = "</ul>\n";  
  70      $openitem2 = "<ul>\n";  
  71      $closeitem2 = "</ul>\n";  
  72  
  73      //  Remove attempts to screw with path
  74      $dir = str_replace('..','bogus',$dir);
  75  
  76      //  Deal with old broken PHP sites
  77      $p = strrpos($SWSDIR, '/');
  78      $documentroot = substr($SWSDIR,0,$p);
  79  
  80      //  Read file of URLs
  81      $f = "$documentroot/$dir/$file";
  82      if ($ff = file($f)) {
  83          $REVISIONDATE = "<br>Last Revision: " . date('M d, Y', filemtime($f));
  84      }
  85  
  86      $title = '';                    // Required
  87      $url = '';                      // Optional
  88      $comment = '';                  // Optional
  89      $iteminprogress = 0;            // If set, we have a item list going
  90      $item2inprogress = 0;
  91      $item = '';
  92      $item2 = '';
  93      while (list ($line_num, $line) = each($ff)) {
  94          if (preg_match ("/^\s*$/", $line)) { continue; }
  95  
  96          //  For old files, the first line will be both kinds of title
  97          if ($line_num == 0 && ! preg_match("/^#/", $line)) {
  98              $pagetitle = $line;
  99              $html .= "<h3>$line</h3>\n";
 100              continue;
 101          }
 102  
 103          //  Page title or for old files, the first line
 104          if (preg_match ("/^pagetitle=(.+)\s*$/", $line, $m)) {
 105              $pagetitle = $m[1];
 106              continue;
 107          }
 108          //  title=string
 109          if (preg_match ("/^header=(.+)\s*$/", $line, $m)) {
 110              $html .= "<h3>$m[1]</h3>\n";
 111              continue;
 112          }
 113          //  section=string
 114          if (preg_match ("/^section=(.+)\s*$/", $line, $m)) {
 115              if ($item2) { $html .= builditem($item2, $url, $comment); $item2 = ''; }
 116              if ($item2inprogress) { $item2inprogress = 0; $html .= $closeitem2; }
 117              if ($item)  { $html .= builditem($item, $url, $comment); $item = ''; }
 118              if ($iteminprogress)  { $iteminprogress = 0;  $html .= $closeitem; }
 119              $html .= "<h4>$m[1]</h4>\n";
 120              continue;
 121          }
 122          //  item=string
 123          if (preg_match ("/^item=(.+)\s*$/", $line, $m)) {
 124              if ($item2) { $html .= builditem($item2, $url, $comment); $item2 = ''; }
 125              if ($item2inprogress)  { $item2inprogress = 0; $html .= $closeitem2; }
 126              if (! $iteminprogress) { $iteminprogress = 1;  $html .= $openitem; }
 127              if ($item)  { $html .= builditem($item, $url, $comment); $item = ''; }
 128              $item = $m[1];
 129              $url = '';
 130              $comment = '';
 131              continue;
 132          }
 133          //  item2=string
 134          if (preg_match ("/^item2=(.+)\s*$/", $line, $m)) {
 135              if ($item)  { $html .= builditem($item, $url, $comment); $item = ''; }
 136              if (! $item2inprogress) { $item2inprogress = 1; $html .= $openitem2; }
 137              if ($item2) { $html .= builditem($item2, $url, $comment); $item2 = ''; }
 138              $item2 = $m[1];
 139              $url = '';
 140              $comment = '';
 141              continue;
 142          }
 143          //  url=string
 144          if (preg_match ("/^url=(.+)\s*$/", $line, $m)) {
 145              $url = $m[1];
 146              $c = substr($url,0,1);          // Check for relative path
 147              $c4 = substr($url,0,4);
 148              if ($c != '/' && $c4 != 'http') { $url = "/$dir/$url"; }
 149              continue;
 150          }
 151          //  comment=string
 152          if (preg_match ("/^comment=(.+)\s*$/", $line, $m)) {
 153              $comment = $m[1];
 154              continue;
 155          }
 156  
 157          //  Old format is still supported
 158          if (preg_match ("/^# section\s+(.+)/", $line, $m)) {
 159              if ($iteminprogress)  { $iteminprogress = 0;  $html .= $closeitem; }
 160              $html .= "<h4>$m[1]</h4>\n";
 161              continue;
 162          }
 163          if (preg_match ("/^#/", $line)) { continue; }
 164          if (preg_match("/^(\S+)\s+(.+)/", $line, $m)) {
 165              if (! $iteminprogress) { $iteminprogress = 1; $html .= $openitem; }
 166              if ($item) { $html .= builditem($item, $url, $comment); }
 167              $url = $m[1];
 168              $item = $m[2];
 169              $c = substr($url,0,1);          // Check for relative path
 170              $c4 = substr($url,0,4);
 171              if ($c != '/' && $c4 != 'http') { $url = "/$dir/$url"; }
 172              $html .= builditem($item, $url, '');
 173              $item = '';                     // Start new item
 174              $item2 = '';
 175              $url = '';
 176              $comment = '';
 177              continue;
 178          }
 179  
 180          $html .= "<b>Unable to parse line $line_num of '$file'</b><br>\n";
 181      }
 182  
 183      //  Finish any item in progress, finish item lists
 184      if ($item)  { $html .= builditem($item, $url, $comment); }
 185      if ($item2) { $html .= builditem($item2, $url, $comment); }
 186      if ($item2inprogress) { $html .= $closeitem2; }
 187      if ($iteminprogress) { $html .= $closeitem; }
 188  
 189      //  We must have seen pagetitle - our check for a reasonable file
 190      if (! $pagetitle) {
 191          return array('Illegal file', 'Never found "pagetitle keyword"');
 192      }
 193  
 194      $html .= "<br><br>\n";
 195      return array($pagetitle, $html);
 196  }
 197  
 198  /*#################################################################
 199  #
 200  # Name:    builditem($item, $url, $comment)
 201  #
 202  #################################################################*/
 203  function builditem ($item, $url, $comment) {
 204      $h = "<li>";
 205      if ($url) { $h .= "<a href=\"$url\">"; }
 206      $h .= "<b>$item</b>";
 207      if ($url) { $h .= "</a>"; }
 208      if ($comment) { $h .= " $comment"; }
 209      $h .= "</li>\n";
 210      return $h;
 211  }
 212  
 213  ?>

title

Description

title

Description

title

Description

title

title

Body