                                                                                                                  <?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
  $MANAGEMENTWEBFOLDER =
    "/home/waggacwc/public_html/refurb/manage";
 
  $WEBROOT = "/home/waggacwc/public_html";


  $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

  //-- Convert all file-path specifications to forward-slashes since this avoids
  //-- all the 'escaping' nightmare of back-slashes
  $sFilePath = stripslashes($_POST['saveFilePath']);
  $sFilePath = str_replace('\\', '/', $sFilePath);
  $sFilePath = preg_replace('#[/]+#', '/', $sFilePath);

  $sFilePathEncoded = htmlspecialchars($sFilePath);
  $sFilePathUrlEncoded = htmlspecialchars(urlencode($sFilePath));

  $sCurrentDirectory = dirname($sFilePath);
  $sCurrentWebDirectory =   
    str_replace('c:/apache2/htdocs', '', $sCurrentDirectory);

  $sFileContents = $_POST['fileContents'];

  $sFileName = str_replace("$sCurrentDirectory/", '', $sFilePath);
  $sFileNameUrlEncoded = htmlspecialchars(urlencode($sFileName));

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


  $sStartHtml = "
      <!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='save, edit, file, unspecified'>
       <META HTTP-EQUIV='Description'
		CONTENT=''>

      <head><title>$ORGANISATIONNAME</title>
      </head>
      <body>";


  if (preg_match("/\./", $sFilePath))
  {
    $sHtmlFilePath = preg_replace("/\..*$/", ".html", $sFilePath);
  }
  else
  {
    $sHtmlFilePath = preg_replace("/$/", ".html", $sFilePath);
  }

  $sFileWebPath = htmlspecialchars(str_replace(
      $WEBDOCUMENTROOT, '', $sFilePath));

  $debug = "";
  if ($debug == "true")
  {
    echo "
      <pre>
      sFilePath=$sFilePath
      sFileName=$sFileName
      sCurrentDirectory=$sCurrentDirectory
      sFileWebPath=$sFileWebPath
      sCurrrentWebDirectory=$sCurrentWebDirectory
      sHtmlFilePath=$sHtmlFilePath
      </pre>";
    //exit;
  } //--if


 
  if ($sFilePath == '')
  {
    echo "
      $sStartHtml

      <center><h1>No file name specified</h1></center>
      <pre>
      You did not specify any file to be edited.
        
      This may indicate that the script <em>retrovox-notepad.php</em>
      is not working correctly.
          
      Or it may indicate that you have typed the address of this 
      page directly in your web-browser rather than arriving here after
      submiting the form in the script <em>retrovox-notepad.php
          
      No changes to any text file have been saved.
      </pre>    
      <ol>
          <li>
          <a href = 'retrovox-notepad-choose-file.php'>
          Choose a file to edit
          </a></li>
          <li>
          <a href = 'move-file-form.php'>
          Copy a file
          </a></li>
        </ol>
      </body>
      </html>";
      exit;
  } //-- if no file specified

  if (!is_writable($sFilePath))
  {
    echo "
      $sStartHtml


      <center><h1>The file is not writable</h1></center>
      <p>
       The file-name \"<em>$sFilePathEncoded</em>\" 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 $ORGANISATIONNAME server.</p>
       <p>
       This message may indicate that the script retrovox-notepad.php is not
       working properly. No changes to the text file have been saved.
       </p>
       <br>
       <ol>
         <li><a href = 'retrovox-notepad-choose-file.php'>
         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 "
      $sStartHtml


      <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.php'>
          Re-enter the file name</a>
      </body>
      </html>";
     return; 
  } //-- if file is a directory

  //$sBackupFileDirectory =
  //  'c:/apache2/htdocs/ideas/manage/old-data';

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

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

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

  $sCleanFileContents = stripslashes($sFileContents);

echo $sStartHtml
//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>


  <h1>File Changes Saved</h1>
    <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 = "$BACKUPFILEDIRECTORY/$sBackupFileBaseName$ii.txt";
       $sMisplacedBackupFileName = 
         "$sCurrentDirectory/$sBackupFileBaseName$ii.txt";

       $sCurrentPath = 
          htmlspecialchars(urlencode(
            $sBackupFileName));
       $sCurrentWebPath = 
          htmlspecialchars(
            str_replace(
              $WEBDOCUMENTROOT, '', $sBackupFileName));


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


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

    <ol type = 'a'>
      <li><a href = '<?=$sFileWebPath?>'>
      View the file as a web-page</a>
      (<a href = '<?=$sFileWebPath?>' target = '_blank'>in new window</a>)
      </li>
      <li>
      <a href = '<?=$MANAGEMENTWEBFOLDER?>/view-file-lines.php?file=<?=$sFilePathUrlEncoded?>'>
      View this file with line numbers</a></li>

      <li>
<a href = 'retrovox-notepad.php?file=<?=htmlspecialchars($sFilePath)?>'>
      Continue editing the same file</a></li>
      <li>
<a href = 'retrovox-notepad.php?file=<?=htmlspecialchars($sFilePath)?>'
   target = '_blank'>
      Edit same file in a new window</a></li>
      <li>
<a href = '<?=$MANAGEMENTWEBFOLDER?>/move-file-form.php?file=<?=htmlspecialchars($sFilePath)?>'>
      Copy or rename this file</a></li>
      <li>
<a href = 'retrovox-notepad-choose-file.php?file=<?=$sFilePathUrlEncoded?>'>
      Edit a different or new file</a></li>
      <li>
      <a href = '<?=$MANAGEMENTWEBFOLDER?>/find-file-form.php?file=<?=$sFileNameUrlEncoded?>'>
      Find a file on the server</a></li>
      <li>
      <a href = '<?=$LIBRARYPATH?>/fnTransformPlainTextDocumentToHtml-TEST.php?file=<?=$sFilePathUrlEncoded?>'>
      View this file as HTML (beta)</a></li>
    </ol>   


  <ul>
  <li><a href = '<?=$MANAGEMENTWEBFOLDER?>/admin.php'>
      Go to the server administration page</a></li>
  </ul>
</body>
</html>

                                                                
                                                                                        
                        
                                                