                                                                                                                                                                                            <?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
  include "/home/waggacwc/public_html/refurb/RETRO-GLOBALS.php";
  //test
  include "$LIBRARYPATH/RETROVOX-CONSTANT-VARIABLES.php";
  include "$LIBRARYPATH/fnRetrovoxStartHtml.php";
  include "$LIBRARYPATH/fnStartHtml.php";
  include "$LIBRARYPATH/fnSwitchFileSelect.php";
  include "$LIBRARYPATH/fnNumericHtmlSelect.php";
  include "$LIBRARYPATH/fnDebugArray.php";
  //include "$LIBRARYPATH/fnRetrovoxDirectoryListing.php";
  include "$LIBRARYPATH/fnDirectoryListing.php";

  //echo realpath('/apache2/ideas/product-data/lib');
  //echo "The realpath of / is ".realpath('/');
  $bNewFile = false;
  $sFilePath = '';
  $sFileContents = '';
  $aaTextLines = '';
  $sWebPathPrefix = '/ideas';
  $bChmodSucceeded = false;  //-- if php can make the file writeable

  $sConfigurationFilePath =
     "$WEBDOCUMENTROOT/refurb/manage/notepad/retrovox-notepad.conf";

  if (file_exists($sConfigurationFilePath))
  {
    $aaEditorSettings = parse_ini_file($sConfigurationFilePath);
    $iEditorFontSize = $aaEditorSettings['editor-font-size'];
    $iEditorColumns = $aaEditorSettings['editor-columns'];
    $iEditorRows = $aaEditorSettings['editor-rows'];
    //fnDebugArray($aaEditorSettings);
  }

  //-- Convert all file path indicators to forward slashes since this is the
  //-- easiest format for PHP to deal with. Otherwise it is necessary to 'escape'
  //-- all the MS Windows backslashes.
  //$sFilePath = stripslashes($_GET['file']);
  $sFilePath = $_GET['file'];
  $sFilePath = str_replace('\\', '/', $sFilePath);
  $sFilePath = preg_replace('#[/]+#', '/', $sFilePath);
  
//  if (substr($sFilePath, 0, 1) != "/")
//  {
//    $sFilePath = "/$sFilePath";  
//  }
  
  
  $sFilePathEncoded = htmlspecialchars($sFilePath);
  $sFilePathUrlEncoded = htmlspecialchars(urlencode($sFilePath));
  $sWebFilePathEncoded = htmlspecialchars(str_replace($WEBDOCUMENTROOT, "", $sFilePath));

  $sFileName = basename($sFilePath);
  $sFileNameEncoded = htmlspecialchars($sFileName);
  $sFileNameUrlEncoded = htmlspecialchars(urlencode($sFileName));

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

  $iEditorColumnsRequest = $_REQUEST['cols'];
  $iEditorRowsRequest = $_REQUEST['rows'];
  $bStripSlashes = $_REQUEST['stripslash'];
  $iEditorFontSizeRequest = $_REQUEST['fontsize'];
  $bSaveConfiguration = $_REQUEST['save'];

  if ($bSaveConfiguration == 'true')
    { $bSaveConfiguration = true; }
  else
    { $bSaveConfiguration = false; } 
  //-- if, else save config settings


  //-- Editor settings set through request variables have
  //-- precedence to those set through the configuration file
  if (!is_numeric($iEditorFontSize))
  { 
    if (is_numeric($iEditorFontSizeRequest))
    {
      $iEditorFontSize = $iEditorFontSizeRequest; 
    }
    else
    {
      $iEditorFontSize = "15"; 
    }
  }
  else
  { 
    if (is_numeric($iEditorFontSizeRequest))
    {
      $iEditorFontSize = $iEditorFontSizeRequest; 
    }
  } //-- if no font size in config file

  if (!is_numeric($iEditorColumns))
  { 
    if (is_numeric($iEditorColumnsRequest))
    {
      $iEditorColumns = $iEditorColumnsRequest; 
    }
    else
    {
      $iEditorColumns = "15"; 
    }
  }
  else
  { 
    if (is_numeric($iEditorColumnsRequest))
    {
      $iEditorColumns = $iEditorColumnsRequest; 
    }
  } //-- if no rows in config file

  if (!is_numeric($iEditorRows))
  { 
    if (is_numeric($iEditorRowsRequest))
    {
      $iEditorRows = $iEditorRowsRequest; 
    }
    else
    {
      $iEditorRows = "15"; 
    }
  }
  else
  { 
    if (is_numeric($iEditorRowsRequest))
    {
      $iEditorRows = $iEditorRowsRequest; 
    }
  } //-- if no rows number in config file



  if ($bSaveConfiguration)
  {
    $sConfigurationData = "\n".
      "editor-font-size=$iEditorFontSize\n".
      "editor-columns=$iEditorColumns\n".
      "editor-rows=$iEditorRows\n";

    $fileHandler = fopen($sConfigurationFilePath, "w");
    fwrite($fileHandler, $sConfigurationData);
    fclose($fileHandler);
  
  } //-- if save settings


  if (!file_exists($sFilePath))
  {
    //-- old
    if (file_exists($WEBDOCUMENTROOT.$sFilePath))
    {
      $sFilePath = $WEBDOCUMENTROOT.$sFilePath;
    }
    elseif (file_exists($WEBDOCUMENTROOT."/".$sFilePath))
    {
      $sFilePath = $WEBDOCUMENTROOT."/".$sFilePath;
    }
    else
    {
      echo fnRetrovoxStartHtml("Web 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, else web-relative path
  } //-- if file doesn't exist

  if (is_dir($sFilePath))
  {
    if (substr($sFilePath, -1) != "/")
    {
      $sFilePath = "$sFilePath/";
    }

    echo fnStartHtml("Web 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 Polyphone 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>
       <a href = '$MANAGEMENTWEBFOLDER/find-file-form.php?file=$sFileNameUrlEncoded'>
       Find a file on the server</a></li>
       <li>Select a file in the directory (<em>$sFilePathEncoded</em>):</li>";
         
       
       echo fnDirectoryListing(
          $sFilePath, 
          false, 
          "$NOTEPADWEBFOLDER/retrovox-notepad.php?file", 
          "$NOTEPADWEBFOLDER/retrovox-notepad.php?file",
          "$MANAGEMENTWEBFOLDER/move-file-form.php?file",
          "$NOTEPADWEBFOLDER/retrovox-notepad.php?file",
          $WEBDOCUMENTROOT);

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

  if (!is_writable($sFilePath))
  {
    //$bChmodSucceeded = chmod($sFilePath, 0407);
    copy($sFilePath, "$sFilePath-temp");
    rename("$sFilePath-temp", $sFilePath);

    if (!is_writable($sFilePath))
    {
      echo fnStartHtml("Web 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 may be able to overcome this problem by typing 
          <pre>
            chmod a+w $sFilePath
          </pre>
          if you can log into the web-server (assuming that it is a Unix variant 
          server). This sometimes works. Another trick is to rename or copy the 
          file to a different name and then rename it back again to its original name.
          The purpose of this is to make you, or the web-server the owner of the file,
          however, the current script has just attempted that trick and it FAILED.
          Nevertheless, you may have better luck if you can telnet or ssh into the 
          Web-server.
          </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 = \"$MANAGEMENTWEBFOLDER/move-file-form.php?file=$sFilePathUrlEncoded\">
           Copy the file</a></li>
          </ol>
           
      </body>
      </html>";
      exit; 
    } 
    else
    {
      $bRenameFileTrick = true;
    } //-- if renaming file trick was  successful

  } //-- 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='<?=ORGANISATIONNAME?>, edit, file'>
 <META HTTP-EQUIV='Description'
          CONTENT='Web notepad for <?=ORGANISATIONNAME?>'>
        <LINK REV='made' HREF='mailto:matth3wbishop@yahoo.com'>

<head><title>Web Notepad for <?=$ORGANISATIONNAME?></title>
<style>
  TEXTAREA
  {
    background: black;
    font-size: <?=$iEditorFontSize?>pt;
    color: white;
    font-weight: normal;
  }
</style>
</head>
   <body bgcolor='lightgreen'>
<center><big>Web Notepad for <?=$ORGANISATIONNAME?></big></center>
<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
   if ($bSaveConfiguration)
   {
     echo "
        <small><font color ='red'>
        Configuration Settings Saved</font></small><br>
          ";
   }

   if ($bRenameFileTrick)
   {
     echo "
        <small><font color ='red'>
        The Rename-file trick was used to make the file editable</font></small><br>
          ";
   }

//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>
    
    <form action = 'retrovox-notepad-save-SAFE.php' 
          method = 'post'>
    <input   type = 'submit'  value = 's a v e   f i l e'>
    <a href = '<?=$MANAGEMENTWEBFOLDER?>/move-file-form.php?file=<?=$sFilePath?>'>
    Copy or Rename 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 = '<?=$MANAGEMENTWEBFOLDER?>/move-file-form.php?file=<?=$sFilePath?>'>
    Copy or Rename the Current File</a> | 
    <a href = 'retrovox-notepad-choose-file.php?file=<?=$sFilePathUrlEncoded?>'>
    Select a Different File</a> |
    <a href = '<?=$MANAGEMENTWEBFOLDER?>/view-file-lines.php?file=<?=$sFilePathUrlEncoded?>'>
    View file with line numbers</a>

    </form>

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
  $sCurrentDirectory = dirname($sFilePath);
  echo fnSwitchFileSelect($sCurrentDirectory, "$NOTEPADWEBFOLDER/retrovox-notepad.php"); 
//--<--<--<--<--<--<--<--<--<--<--<--<--<--<
?>
  <form action = '<?=$NOTEPADWEBFOLDER?>/retrovox-notepad.php'
        method = 'get'>
  <input type = 'hidden'  name = 'file'
        value = '<?=$sFilePathEncoded?>'>
  <input type = 'hidden'  name = 'save'
        value = 'true'>
  <strong>Editor font size:</strong>
  <?= fnNumericHtmlSelect('30', 'fontsize', $iEditorFontSize, 1, 1, '') ?>
  <strong>Editor rows:</strong>
  <?= fnNumericHtmlSelect('45', 'rows', $iEditorRows, 5, 1, '') ?>
  <strong>Editor columns:</strong>
  <?= fnNumericHtmlSelect('100', 'cols', $iEditorColumns, 40, 1, '') ?>
  <input type = 'submit' value = 'Change Editor Settings'>
  </form>
  <a href = '<?=$MANAGEMENTWEBFOLDER?>/add-registration-form.php?file=<?=$sFilePathUrlEncoded?>'>
    Register this file</a>
<?php
?>
    </nobr>
    <br>
</body>
</html>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    


                                                                                                                                                                                                                                                                                                                                                                        
        
                                                                                                        
                                                    