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

  include_once "$LIBRARYPATH/fnFillWebTemplate.php";
  include_once "$LIBRARYPATH/fnMessageToHtml.php";
  include_once "$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
  $EDITOR = "webpad";


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

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

  $aaTextLines = file($WEBPADTEMPLATE);
  $sWebpadTemplateContents = 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

  $sWikiFolder = "/home/waggacwc/public_html/refurb/docs";

  //-- this needs to be fixed
  /*
  if (!stristr($sFilePath, ".txt"))
  {
      $sPageTitle = "$EDITOR, (File not editable)";
      $sPageContent = "
          = $EDITOR, (File not editable)

      The file which you selected to edit with the $EDITOR is not 
      registered as being editable. You may edit it with the management
      web editor but you will need a password in order to access that editor. 
      The current editor is for maintaining documents which can then be 
      published either on the Internet as web pages or in another format.

      Currently (feb 2004) only documents in the 
      '/refurb/docs/' link://webpad.php?file=/refurb/docs/
      directory are editable using this interface.

      It is true that, this being the case I really shouldnt allow you to choose
      a file in a different directory, since that can only cause frustration
      when you receive this error message. To do.

      'Choose another file to edit' link://webpad-choose-file.php

 
       ";

      $sPageContent = fnMessageToHtml($sPageContent);
      $sOutputPage = fnFillWebTemplate(
        $sWebpadTemplateContents, $sPageContent, $sPageTitle);   
      echo $sOutputPage;
      exit;
  }
  */



  if (!file_exists($sFilePath))
  {
    //-- old
    if (file_exists($WEBDOCUMENTROOT.$sFilePath))
    {
      $sFilePath = $WEBDOCUMENTROOT.$sFilePath;
    }
    elseif (file_exists($WEBDOCUMENTROOT."/".$sFilePath))
    {
      $sFilePath = $WEBDOCUMENTROOT."/".$sFilePath;
    }
    else
    {

      $sPageTitle = "webpad: the file doesn't exist";
      $sPageContent = "
          = Webpad: the file doesn't exist

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

       'Re-enter the file name'
         link://webpad-choose-file.php?file=$sFilePathUrlEncoded
 
       ";

      $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 = "
          = Webpad: the path is a directory

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

       a-
       'Re-enter the file name'
         link://webpad-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, 
          "$WEBPADWEBFOLDER/webpad.php?file", 
          "$WEBPADWEBFOLDER/webpad.php?file",
          "$MANAGEMENTWEBFOLDER/move-file-form.php?file",
          "$WEBPADWEBFOLDER/webpad.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 = "Webpad: the file is not writable";
      $sPageContent = "
          = Webpad: 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://webpad-choose-file.php?file=$sFilePathUrlEncoded
 
      'Copy the file (to try to make it editable)'
         link://$MANAGEMENTWEBFOLDER/move-file-form.php?file=$sFilePathUrlEncoded
       ";

      $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='green'>
<center><big>Webpad 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 = 'webpad-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 = 'webpad-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 = 'webpad-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, "$WEBPADWEBFOLDER/webpad.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>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    


                                                                                                                                                                                                                                                                                                                                                                        
        
                                                                                                        
                                                                                                