                                                                                                                                            

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
  $sLibraryPath = 'c:\apache2\htdocs\ideas\product-data\lib';
  require "$sLibraryPath\fnRetrovoxStartHtml.php";
  include "$sLibraryPath\fnSwitchFileSelect.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('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);

  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.
        <ul>
          <li>
          This may indicate that the script <em>retrovox-notepad.php</em>
          is not working correctly.</li>
          <li>
          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</em></li>
          <li>
          No changes to any text file have been saved.</li>
        </ul>
        <ol  type = 'a'>
          <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 "
      <!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>\" 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 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 "
      <!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.php'>
          Re-enter the file name</a>
      </body>
      </html>";
     return; 
  } //-- if file is a directory

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

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

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

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


//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>
<?= fnRetrovoxStartHtml("Retrovox Notepad: File changes saved", '/product-data/admin-style.css') ?>

    <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 = "$sBackupFileDirectory\\$sBackupFileBaseName$ii.txt";
       $sMisplacedBackupFileName = 
         "$sCurrentDirectory\\$sBackupFileBaseName$ii.txt";

       $sCurrentPath = 
          htmlspecialchars(urlencode(
            $sBackupFileName));
       $sCurrentWebPath = 
          htmlspecialchars(
            str_replace(
              '/usr50/home/retrovox/public_html', '', $sBackupFileName));

       //-- get rid of unwanted backups which are clogging up 
       //-- my directories.
       if (file_exists($sMisplacedBackupFileName))
       { 
          unlink($sMisplacedBackupFileName);
       } //-- if

       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 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 = '/ideas/manage/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 = '/ideas/manage/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 = '/ideas/manage/find-file-form.php?file=<?=$sFileNameUrlEncoded?>'>
      Find a file on the server</a></li>
      <li>
      <a href = '/ideas/product-data/lib/fnTransformPlainTextDocumentToHtml-TEST.php?file=<?=$sFilePathUrlEncoded?>'>
      Transform this file into HTML (beta)</a></li>
  

    </ol>   

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

  <ul>
  <li><a href = '/manage/admin.php'>
      Go to the Retrovox Administration page</a></li>
  </ul>
</body>
</html>



                                                                                                                                            
