<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
   
  $bNewFile = false;
  $sFilePath = '';
  $sFileContents = '';
  $aaTextLines = '';

  $sFilePath = $_GET['file'];

  if (substr($sFilePath, 0, 1) != "/")
  {
    $sFilePath = "/$sFilePath";  
  }
  
  if (!strstr($sFilePath, "/usr50/home/retrovox"))
  {
    $sFilePath = "/usr50/home/retrovox".$sFilePath;
  }
  
  $sFilePathEncoded = htmlspecialchars($sFilePath);
  $sFilePathUrlEncoded = htmlspecialchars(urlencode($sFilePath));
  $sWebFilePathEncoded = htmlspecialchars(str_replace("public_html", "", $sFilePath));

  $iEditorColumns = $_GET['cols'];
  $iEditorRows = $_GET['rows'];
  $bStripSlashes = $_GET['stripslash'];

  if ($iEditorColumns == '')
  {
     $iEditorColumns = 80;
  }
  
  if ($iEditorRows == '')
  {
     $iEditorRows = 20;
  }
  
  if ($sFilePath == '/usr50/home/retrovox')
  {
    echo "
      <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2//EN'>
      <html>
       <META HTTP-EQUIV='Content-Type' CONTENT='text/html; CHARSET='iso-8859-1'>
       <META HTTP-EQUIV='Keywords'
		CONTENT='Retrovox, edit, file, unspecified'>
       <META HTTP-EQUIV='Description'
		CONTENT='Retrovox notepad, file-name unspecified'>
	      <LINK REV='made' HREF='mailto:matth3wbishop@yahoo.com'>

      <head><title>Retrovox Notepad, (No file name specified)</title>
      </head>
      <body>
      <center><h1>No file name specified</h1></center>
	  <p>You did not specify any file to be edited</p>
          <br>
          <a href = 'retrovox-notepad-choose-file.html'>Try Again</a>
      </body>
      </html>";
     //-- is this possible 
     return; 
  } //-- if no file specified
 
  
  
  if (is_dir($sFilePath))
  {
    if (substr($sFilePath, -1) != "/")
    {
      $sFilePath = "$sFilePath/";
    }
    
    echo "
      <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2//EN'>
      <html>
       <META HTTP-EQUIV='Content-Type' CONTENT='text/html; CHARSET='iso-8859-1'>
       <META HTTP-EQUIV='Keywords'
		CONTENT='Retrovox, edit, file, directory'>
       <META HTTP-EQUIV='Description'
		CONTENT='Retrovox notepad, the path is a directory'>
	      <LINK REV='made' HREF='mailto:matth3wbishop@yahoo.com'>

      <head><title>Retrovox Notepad, (The path is a directory)</title>
      </head>
      <body>
      <center><h1>The path is a directory</h1></center>
      <p>
       The file-name<br>
       &nbsp;&nbsp;<em>$sFilePathEncoded</em><br>
       which you just specified is a directory (folder).
       The Retrovox Notepad system is for editing text files located on the
       server. 
      </p>
          <br>
       <ol>
        <li>
   <a href = \"retrovox-notepad-choose-file.php?file=$sFilePathUrlEncoded\">
       Re-enter the file name</a></li>
        <li>Select a file in the directory (folder):</li>
          
	  <ol type = '1'>";

      //-- Not supported in php 4
      //foreach (glob("$sFilePath*") as $sMemberFileName) 
      $aaFiles = '';
      $dh  = opendir($sFilePath);
      while (false !== ($sMemberFileName = readdir($dh)))
      {
         $aaFiles[] = $sMemberFileName;
      }
       
      sort($aaFiles);
      reset($aaFiles);
      
      foreach ($aaFiles as $sMemberFileName)
      {
        echo "<li><a href ='retrovox-notepad.php?file=".
	  htmlspecialchars(urlencode($sFilePath.$sMemberFileName))
	  ."'>$sFilePath$sMemberFileName</a></li> \n";
      }

      echo "  
        </ol>
        </ol>
        </body>
        </html>";
      
     return;
  } //-- if file is a directory

  if (!is_writable($sFilePath))
  {
    echo "
      <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2//EN'>
      <html>
       <META HTTP-EQUIV='Content-Type' CONTENT='text/html; CHARSET='iso-8859-1'>
       <META HTTP-EQUIV='Keywords'
		CONTENT='Retrovox, edit, file, unwritable'>
       <META HTTP-EQUIV='Description'
		CONTENT='Retrovox notepad, file unwritable'>
	      <LINK REV='made' HREF='mailto:matth3wbishop@yahoo.com'>

      <head><title>Retrovox Notepad, (File is not writable)</title>
      </head>
      <body bgcolor = 'lightgreen'>
      <center><h1>The file is not writable</h1></center>
	  <p>
          The file-name [$sFilePathEncoded] which you just specified has its permissions
          set so that is cannot be edited (updated or modified in any way).
          You can overcome this problem by typing 
          <pre>
            chmod a+w $sFilePath
          </pre>
          when you are logged into the retrovox server. This sometimes works.
          However, the web-server (Apache) runs as the user 'nobody' which
          seems to cause some permissions headaches. You can also try copying
          the old file to a new one.
          </p>
          <br>
          <ol  type = 'a'>
          <li>
   <a href = \"retrovox-notepad-choose-file.php?file=$sFilePathUrlEncoded\">
           Re-enter the file name</a></li>
          <li>
   <a href = \"/manage/move-file-form.php?file=$sFilePathUrlEncoded\">
           Copy the file</a></li>
          </ol>
           
      </body>
      </html>";
      return; 
  } //-- if file not writeable


  if (file_exists($sFilePath))
  {
     $aaTextLines = file($sFilePath);
     $sFileContents = implode('', $aaTextLines);
     $bNewFile = false;
  }
  else
  {
     $bNewFile = true;
  } 

  if ($bStripSlashes != '')
  {
    $sEditorContents = stripslashes(htmlspecialchars($sFileContents));
  }
  else
  {
    $sEditorContents = htmlspecialchars($sFileContents);
  }    
//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2//EN'>
<html>
 <META HTTP-EQUIV='Content-Type' CONTENT='text/html; CHARSET='iso-8859-1'>
 <META HTTP-EQUIV='Keywords'
          CONTENT='Retrovox, edit, file'>
 <META HTTP-EQUIV='Description'
          CONTENT='Retrovox notepad'>
        <LINK REV='made' HREF='mailto:matth3wbishop@yahoo.com'>

<head><title>Retrovox Notepad</title>
</head>
      <body bgcolor='lightgreen'>
<center><big>The Retrovox Notepad</big></center>
    <p></p>
    <form action = 'retrovox-notepad-save.php' 
          method = 'post'>
    <input   type = 'submit'  value = 's a v e   f i l e'>
    <a href = '../move-file-form.php?file=<?=$sFilePath?>'>
    Copy the Current File</a> | 
    <a href = 'retrovox-notepad-choose-file.php?file=<?=$sFilePathUrlEncoded?>'>
    Select a Different File</a>
    <br>
    Editing file: <em><?= htmlspecialchars($sFilePath) ?></em>
    <br>
    <textarea name = 'fileContents' 
              cols = '<?=$iEditorColumns?>' 
              rows = '<?=$iEditorRows?>'>
    <?= $sEditorContents ?>
    </textarea>
    <br>
    <nobr>
    <input   type = 'submit'  value = 's a v e   f i l e'>
    <input   type = 'hidden'  name = 'saveFilePath'
            value = '<?=htmlspecialchars($sFilePath)?>'>
    <a href = '../move-file-form.php?file=<?=$sFilePath?>'>
    Copy the Current File</a> | 
    <a href = 'retrovox-notepad-choose-file.php?file=<?=$sFilePathUrlEncoded?>'>
    Select a Different File</a>

    </form>
    <form  action = 'retrovox-notepad.php'  method = 'get'>
    <input   type = 'submit'  
            value = 'Switch to a Different File'>
    <select  name = 'file'>
<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
      $sCurrentDirectory = dirname($sFilePath);

      $aaFiles = '';
      $dh  = opendir($sCurrentDirectory);
      while (false !== ($sMemberFileName = readdir($dh)))
      {
         $aaFiles[] = $sMemberFileName;
      }
       
      sort($aaFiles);
      reset($aaFiles);
      
      foreach ($aaFiles as $sMemberFileName)
      {
        echo "
        <option 
        value = '".htmlspecialchars($sCurrentDirectory."/".$sMemberFileName)."'>
          $sMemberFileName</option> ";
      }


//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>
    </select>
    </form>
    </nobr>
    <br>
</body>
</html>