                                                                                                                                                                                                                                    <?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
  include "/home/waggacwc/public_html/refurb/RETRO-GLOBALS.php";
  //test1
  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/fnDirectoryListing.php";

  include "$LIBRARYPATH/fnFillWebTemplate.php";
  include "$LIBRARYPATH/fnMessageToHtml.php";
  include "$LIBRARYPATH/fnListBlockToHtml.php";
  include "$LIBRARYPATH/fnCodeBlockToHtml.php";
  include "$LIBRARYPATH/fnJournalToHtml.php";
  include "$LIBRARYPATH/fnFaqToHtml.php";
  include "$LIBRARYPATH/fnHyperlinkPlainText.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

  $aaTextLines = file($ERRORTEMPLATE);
  $sErrorTemplateContents = implode('', $aaTextLines);

  $aaTextLines = file($ADMINTEMPLATE);
  $sAdminTemplateContents = implode('', $aaTextLines);


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

      $sPageTitle = "Web Notepad: the file doesn't exist";
      $sPageContent = "
          = Web Notepad: the file doesn't exist

       The file-name $sFilePathEncoded which you just specified doesn't 
       exist.

       'Re-enter the file name'
         link://retrovox-notepad-choose-file.php?file=$sFilePathUrlEncoded

       'Jump to a different folder'
         link://$MANAGEMENTWEBFOLDER/select-folder.php
 
       ";

      $sPageContent = fnMessageToHtml($sPageContent);
      $sOutputPage = fnFillWebTemplate(
        $sAdminTemplateContents, $sPageContent, $sPageTitle);   
      echo $sOutputPage;
      exit;
    } //-- if, else web-relative path
  } //-- if file doesn't exist

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

    $sPageTitle = "Web Notepad: the path is a directory";
    $sPageContent = "
          = Web Notepad: the path is a directory

       The file-name $sFilePathEncoded which you just specified is a 
       'directory' or 'folder' on the web-server.

     a- 'Jump to a different folder'
         link://$MANAGEMENTWEBFOLDER/select-folder.php
      - 'Re-enter the file name'
         link://retrovox-notepad-choose-file.php?file=$sFilePathUrlEncoded
      - 'Find a file on the server'
         link://$MANAGEMENTWEBFOLDER/find-file-form.php?file=$sFilePathUrlEncoded

       Select a file in the directory $sFilePathEncoded
         
 
       ";

      $sPageContent = fnMessageToHtml($sPageContent);

      $sPageContent = $sPageContent.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);

      $sOutputPage = fnFillWebTemplate(
        $sAdminTemplateContents, $sPageContent, $sPageTitle);   
      echo $sOutputPage;
      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))
    {

      $sPageTitle = "Web Notepad: the file is not writable";
      $sPageContent = "
          = Web Notepad: the file is not writable

      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 
          
            chmod a+w $sFilePath
          
      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.

      'Re-enter the file name'
         link://retrovox-notepad-choose-file.php?file=$sFilePathUrlEncoded
 
      'Copy the file (to try to make it editable)'
         link://$MANAGEMENTWEBFOLDER/move-file-form.php?file=$sFilePathUrlEncoded

      'Jump to a different folder'
         link://$MANAGEMENTWEBFOLDER/select-folder.php
       ";

      $sPageContent = fnMessageToHtml($sPageContent);
      $sOutputPage = fnFillWebTemplate(
        $sAdminTemplateContents, $sPageContent, $sPageTitle);   
      echo $sOutputPage;
      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.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 = '<?=$MANAGEMENTWEBFOLDER?>/select-folder.php'>
    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 = '<?=$MANAGEMENTWEBFOLDER?>/select-folder.php'>
    Select a Different File</a> |
    <a href = 'retrovox-notepad-choose-file.php?file=<?=$sFilePathUrlEncoded?>'>
    Re-enter the file name</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>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    


                                                                                                                                                                                                                                                                                                                                                                        
        
                                                                                                        
                                                                                            