                                                                                                                                                                                                                                                                        <?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
  include_once "/home/waggacwc/public_html/refurb/RETRO-GLOBALS.php";
  //test
  include_once "$LIBRARYPATH/RETROVOX-CONSTANT-VARIABLES.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/fnListBlockToHtml.php";
  include_once "$LIBRARYPATH/fnCodeBlockToHtml.php";
  include_once "$LIBRARYPATH/fnCodeBlockFromHtml.php";

  include_once "$LIBRARYPATH/fnHyperlinkPlainText.php";


  $sFilePath = '';
  $sFileContents = '';
  $aaTextLines = '';
  $bChmodSucceeded = false;  //-- if php can make the file writeable
  $EDITOR = "webpad";



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

  $sRegenerateDocument = $_REQUEST['regenerate'];
  $sRegenerateDocument = trim($sRegenerateDocument);

  $sFilePath = $_REQUEST['file'];
  $sFilePath = str_replace('\\', '/', $sFilePath);
  $sFilePath = preg_replace('#[/]+#', '/', $sFilePath);
  

  $sFilePathEncoded = htmlspecialchars($sFilePath);
  $sFilePathUrlEncoded = htmlspecialchars(urlencode($sFilePath));
  $sWebFilePathEncoded = htmlspecialchars(str_replace($WEBDOCUMENTROOT, "", $sFilePath));

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

  if (preg_match("/\./", $sFilePath))
  {
    $sConfigurationFilePath = preg_replace("/\.[^\.]*$/", ".conf", $sFilePath);
  }
  else
  {
    $sConfigurationFilePath = preg_replace("/$/", ".conf", $sFilePath);
  }


  $sTemplateFile = $_REQUEST['template-file'];
  //$iEditorRowsRequest = $_REQUEST['rows'];
  //$bStripSlashes = $_REQUEST['stripslash'];
  //$iEditorFontSizeRequest = $_REQUEST['fontsize'];
  //$bSaveConfiguration = $_REQUEST['save'];



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

      $sPageTitle = "Config Document: the document doesn't exist";
      $sPageContent = "
          = Config Document: the document 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 = "Document Settings: the path is a directory";
    $sPageContent = "
          = Document Settings: 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 (file_exists($sConfigurationFilePath) &&
      !is_writable($sConfigurationFilePath))
  {
    //$bChmodSucceeded = chmod($sFilePath, 0407);
    copy($sConfigurationFilePath, "$sConfigurationFilePath-temp");
    rename("$sConfigurationFilePath-temp", $sConfigurationFilePath);

    if (!is_writable($sConfigurationFilePath))
    {

      $sPageTitle = "Document Settings: the configuration file is not writable";
      $sPageContent = "
          = The configuration file is not writeable

      The config file-name [$sConfigurationFilePath]
      which was 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 $sConfigurationFilePath
          
      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.

 
      'Copy the file (to try to make it editable)'
         link://$MANAGEMENTWEBFOLDER/move-file-form.php?file=$sConfigurationFilePath
       ";

      $sPageContent = fnMessageToHtml($sPageContent);
      $sOutputPage = fnFillWebTemplate(
        $sAdminTemplateContents, $sPageContent, $sPageTitle);   
      echo $sOutputPage;
      exit;

    } 
    else
    {
      $bRenameFileTrick = true;
    } //-- if renaming file trick was  successful

  } //-- if configuration file not writeable

   $sConfigurationData = "\n".
      "document-template-path=$sTemplateFile\n";

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


    $fileHandler = fopen($sConfigurationFilePath, "w");
    fwrite($fileHandler, $sConfigurationData);
    fclose($fileHandler);

 
  if ($sRegenerateDocument == "true")
  {
    echo "";
  }


  $sPageContent = "
   
      = ($sFileName) Configuration Settings Updated

    The configuration settings for the document $sFilePathEncoded
    were updated, the following data was written to the configuration file
    $sConfigurationFilePath

        $sConfigurationData

    a-
   'Edit document' link://$WEBPADWEBFOLDER/webpad.php?file=$sFilePathUrlEncoded
     -

        ";

   $sPageContent = fnMessageToHtml($sPageContent);
   $sOutputPage = fnFillWebTemplate(
      $sAdminTemplateContents, $sPageContent, $sPageTitle);   
   echo $sOutputPage;

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

                                                                                                                                                                                                                                                                                                                                                                        
        
                                                                                                        
                                                                                                                                