                                                                    

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
   
  $bNewFile = false;     //-- 
  $sFilePath = '';       //-- the name and full path to the text file
  $sFileContents = '';   //-- the contents of the text file
  $aaTextLines = '';     //--
  $iBackups = 4;         //-- the number of backup files to create

  $sFilePath = $_POST['saveFilePath'];
  $sFilePathEncoded = htmlspecialchars($sFilePath);
  $sFilePathUrlEncoded = htmlspecialchars(urlencode($sFilePath));

  $sCurrentDirectory = dirname($sFilePath);
  $sCurrentWebDirectory =   
    str_replace('/usr50/home/retrovox/public_html', '', $sCurrentDirectory);

  $sFileContents = $_POST['fileContents'];

  //-- This is the base-name for the backup files
  $sBackupFileBaseName = preg_replace("/\..*$/", "-old", $sFilePath);

  if ($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, 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. This probably indicates
          that the script retrovox-notepad.php is not working correctly.
          No changes to any text file have been saved.</p>
          <br>
          <a href = 'retrovox-notepad-choose-file.html'>Try Again</a>
      </body>
      </html>";
      return;
  } //-- if no file specified

  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>
      <center><h1>The file is not writable</h1></center>
	  <p>
          The file-name \"<em>$sFilePathEncoded</em>\" which you just specified has its permissions
          set so that is cannot be edited (updated or modified in any way).
          You may be able to overcome this problem by typing 
          <pre>
            chmod a+w $sFilePath
          </pre>
          when you are logged into the Retrovox server.</p>
          <p>
          This probably indicated that the script retrovox-notepad.php is not
          working properly. No changes to any text file have been recorded.
          </p>
          <br>
          <ol>
           <li><a href = 'retrovox-notepad-choose-file.html'>Re-enter the file name</a></li>
           <li><a href = \"$sFilePath\">
           View the contents of the directory (folder)</a></li>
          </ol>
           
      </body>
      </html>";
      return;
  } 
  //-- if file not writeable

  if (is_dir($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 \"<em>$sFilePathEncoded</em>\" for the file which you
          have been
          editing is a directory (folder). This probably indicates that the 
          script retrovox-notepad.php or retrovox-notepad-new.php 
          is not working properly.
          The Retrovox Notepad system is for editing text files located on the
          server. 
          </p>
          <br>
          <a href = 'retrovox-notepad-choose-file.html'>
          Re-enter the file name</a>
      </body>
      </html>";
     return; 
  } //-- if file is a directory

  if (file_exists($sFilePath))
  {
    for ($ii = $iBackups; $ii > 0; $ii--)
    {
       $sNewName = "$sBackupFileBaseName$ii.txt";
       $sOldName = "$sBackupFileBaseName".($ii-1).".txt";
       if (file_exists($sOldName))
       {
          rename($sOldName, $sNewName);
       } //-- if
    } //-- for

    rename($sFilePath, $sBackupFileBaseName."1.txt");
    //unlink($sFilePath);
  } //-- if file exists

  //-- save the changes
  $fileHandler = fopen($sFilePath, "w");
  fwrite($fileHandler, stripslashes($sFileContents));
  fclose($fileHandler);


//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>

<!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, save'>
 <META HTTP-EQUIV='Description'
          CONTENT='Retrovox notepad, save file changes'>
        <LINK REV='made' HREF='mailto:matth3wbishop@yahoo.com'>

<head><title>Retrovox Notepad  (File changes saved)</title>
</head>
<body>
<center><h1>The file has been saved:</h1></center>
    <p>
    The changes which you made to the text file<br>
    &nbsp;&nbsp;<em><?=$sFilePathEncoded?></em><br>
    have been saved.
    </p>
    <br>

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
    echo "
     <strong>The following backup files for this text file currently exist
     </strong>
     <ul>";
    for ($ii = $iBackups; $ii > 0; $ii--)
    {
       $sBackupFileName = "$sBackupFileBaseName$ii.txt";
       $sCurrentPath = 
          htmlspecialchars(urlencode(
            $sBackupFileName));
       $sCurrentWebPath = 
          htmlspecialchars(
            str_replace(
              '/usr50/home/retrovox/public_html', '', $sBackupFileName));

       if (file_exists($sBackupFileName))
       {
          echo "
            <li>$sBackupFileName 
          (<a href = \"$sCurrentWebPath\">view</a>|
          <a href = \"/manage/notepad/retrovox-notepad.php?file=$sCurrentPath\">edit</a>)</li>";
       } //-- if
    } //-- for
    echo "</ul>";

    $sFileWebPath = 
          htmlspecialchars(
            str_replace(
              '/usr50/home/retrovox/public_html', '', $sFilePath));

//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>

    <ol>
      <li><a href = '<?=$sFileWebPath?>'>
      View the file as a web-page</a></li>
      <li>
<a href = 'retrovox-notepad.php?file=<?=htmlspecialchars($sFilePath)?>'>
      Continue editing the same file</a></li>
      <li>
<a href = 'retrovox-notepad-choose-file.php?file=<?=$sFilePathUrlEncoded?>'>
      Edit a different or new file</a></li>
    </ol>   

    <form  action = 'retrovox-notepad.php'  method = 'get'>
    <input   type = 'submit'  
            value = 'Switch to a Different File'>
    <select  name = 'file'>
<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->

      $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>

</body>
</html>



                                                                    