                                                                                                                                                                                    

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
  $sLibraryPath = '/usr50/home/retrovox/public_html/manage/php-lib';
  include "$sLibraryPath/RETROVOX-CONSTANT-VARIABLES.php";
  include "$sLibraryPath/fnRetrovoxStartHtml.php";

  $bNewFile = false;
  $sFilePath = '';
  $sFileContents = '';
  $aaTextLines = '';
  $sWebPathPrefix = '/usr50/home/retrovox/public_html';

  $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 fnRetrovoxStartHtml("Retrovox Notepad: no file name specified");
    echo "
	  <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 fnRetrovoxStartHtml("Retrovox Notepad: the path is a directory");    
    echo "
      <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 type = 'a'>
       <li>
   <a href = \"retrovox-notepad-choose-file.php?file=$sFilePathUrlEncoded\">
       Re-enter the file name</a></li>
        <li>Select a file in the directory (<em>$sFilePathEncoded</em>):</li>
          
	  <ol type = '1'>";

      //-- Not supported in php 4
      //foreach (glob("$sFilePath*") as $sMemberFileName) 
      $aaFiles = '';
      $dh  = opendir($sFilePath);
      while (false !== ($sMemberFileName = readdir($dh)))
      {

        if ($sMemberFileName != '.')
           { $aaFiles[] = $sMemberFileName; }
      } //--while
       
      sort($aaFiles);
      reset($aaFiles);
      
      foreach ($aaFiles as $sMemberFileName)
      {

        if ($sMemberFileName == '..')
        {
          $sParentDirectory = preg_replace('#[^/]+/$#', '', $sFilePath);

          echo "<li><font color = 'red'>[Parent Directory]</font>
            <a href ='retrovox-notepad.php?file=".
            htmlspecialchars(urlencode($sParentDirectory))
	    ."'> (view files)</a></li> \n";
        }
        else
        {
          if (is_dir($sFilePath.$sMemberFileName))
          {
            echo "<li><font color = 'red'>$sMemberFileName/</font>
              <a href ='retrovox-notepad.php?file=".
              htmlspecialchars(urlencode($sFilePath.$sMemberFileName))
	      ."'> (view files)</a></li> \n";
          }
          else
          {
            $sMemberFilePath = $sFilePath.$sMemberFileName;
            $sMemberFilePathUrlEncoded = 
               htmlspecialchars(urlencode($sMemberFilePath));
            $sMemberFileWebPathEncoded =
               htmlspecialchars(
                 str_replace($sWebPathPrefix, '', $sMemberFilePath));

            echo "
             <li>$sMemberFileName (<a href ='retrovox-notepad.php?".
              "file=$sMemberFilePathUrlEncoded'>edit</a> | 
              <a href = '$sMemberFileWebPathEncoded'>
              view</a> | <a href = '/manage/move-file-form.php?".
              "file=$sMemberFilePathUrlEncoded'>copy</a>)</li> \n";
          } //-- if is a sub-directory
        } //-- if parent dir reference
      } //-- foreach member file

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

  if (!file_exists($sFilePath))
  {

    echo fnRetrovoxStartHtml("Retrovox Notepad: the file doesn't exist");    
    echo "
      <p>
       The file-name<br>
       &nbsp;&nbsp;<em>$sFilePathEncoded</em><br>
       which you just specified doesn't exist (folder).
      </p>
       <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 doesn't exist

  if (!is_writable($sFilePath))
  {
    echo fnRetrovoxStartHtml("Retrovox Notepad: the file is not writable");    
    echo "
	  <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


   $aaTextLines = file($sFilePath);
   $sFileContents = implode('', $aaTextLines);
   $bNewFile = false;

  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 = '/manage/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> |
    <a href = '/manage/view-file-lines.php?file=<?=$sFilePathUrlEncoded?>'>
    View file with line numbers</a>

    </form>
    <form  action = 'retrovox-notepad.php'  method = 'get'  
             name = 'switchFileForm'>
    <input   type = 'submit'  
            value = 'Switch to a Different File'>
    <select  name = 'file'
         onChange = 'document.forms.switchFileForm.submit()'>
    <option value=''>-Choose A File To Switch To-</option>

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
      $sCurrentDirectory = dirname($sFilePath);

      $aaFiles = '';
      $dh  = opendir($sCurrentDirectory);
      while (false !== ($sMemberFileName = readdir($dh)))
      {
        if ($sMemberFileName != '.')
           { $aaFiles[] = $sMemberFileName; }
      }
       
      sort($aaFiles);
      reset($aaFiles);
      
      foreach ($aaFiles as $sMemberFileName)
      {
        if ($sMemberFileName == '..')
        {
          $sParentDirectory = preg_replace('#[^/]+$#', '', $sCurrentDirectory);
          echo "<option  value = '".htmlspecialchars($sParentDirectory)."'>
          [Parent Directory]</option> ";
        }
        else
        {
           echo "<option 
          value = '".htmlspecialchars($sCurrentDirectory."/".$sMemberFileName)."'>
          $sMemberFileName</option> ";
        }
      }


//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>
    </select>
    <a href = '/manage/add-registration-form.php?file=<?=$sFilePathUrlEncoded?>'>
    Register this file</a>
    </form>
    </nobr>
    <br>
</body>
</html>



                                                                                                                                                                                    