                                                                                                                                                                    

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
//--Description:
//--  The purpose of this script is to remove a directory and all
//--  its sub-directories by compressing the directory and placing the
//--  compressed file into a garbage or trash directory. The idea behind
//--  this proceedure is to make this operation slightly less drastic
//--  than it would otherwise be. 
//--
  include_once "/home/waggacwc/public_html/refurb/RETRO-GLOBALS.php";

  include_once "$LIBRARYPATH/fnRetrovoxStartHtml.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/fnJournalToHtml.php";
  include_once "$LIBRARYPATH/fnFaqToHtml.php";
  include_once "$LIBRARYPATH/fnHyperlinkPlainText.php";

  $bNewFile = false;
  $sFilePath = '';
  $sFilePath = $_GET['directory'];
  $sFilePath = str_replace('\\', '/', $sFilePath);
  $sFilePath = preg_replace('#[/]+#', '/', $sFilePath);


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

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

  

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


  if ($sFilePath == '')
  {

      $sPageTitle = "Remove directory tree, (No file name specified)";
      $sPageContent = "
          = Remove directory tree, (No file name specified)

      You did not specify any folder to be removed.

      This may indicate that the script remove-directory-tree-form.php
      is not working correctly

 
       ";

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


  } //-- if no file specified




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

      $sPageTitle = "Remove: the folder doesn't exist";
      $sPageContent = "
          = Remove folder tree: the folder doesnt exist

       The file-name $sFilePathEncoded which you just specified doesn't 
       exist. This situation really shouldnt arise because you the
       folder should have been chosen from a drop down list in the 
       link://remove-directory-tree-form.php .So this message may indicate
       that that script is not working correctly.

       a-
       'Try again'
         link://remove-directory-tree-form.php
       - 
       'Do something else'
         link://$MANAGEMENTWEBFOLDER/admin.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 = "the path is not a directory";
    $sPageContent = "
          = Web Notepad: the path is not a directory

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

       a-
       'Try again'
         link://remove-directory-tree-form.php
       - 
       'Do something else'
         link://$MANAGEMENTWEBFOLDER/admin.php

       ";

      $sPageContent = fnMessageToHtml($sPageContent);


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

  } //-- if file is not a directory

  if ($sFilePath == "$WEBROOT")
  {

    $sPageTitle = "the path is the entire web-server folder";
    $sPageContent = "
          = The path is the web-server document root

       The file-name $sFilePathEncoded which you just specified is 
       the document root of the web-server. It seems extremely 
       unwise to be trying to remove this directory. Basically there
       will be nothing left on the web-server. Sorry but this script
       is not going to let you do that.

       a-
       'Try again'
         link://remove-directory-tree-form.php
       - 
       'Do something else'
         link://$MANAGEMENTWEBFOLDER/admin.php

       ";

      $sPageContent = fnMessageToHtml($sPageContent);


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

  } //-- if the folder is the web document root

 
  
  
  //$sBackupFileName = "junk.tar.gz";
  $sLeafFolderName = preg_replace('#^.*/([^/]+)/$#', '$1', $sFilePath); 
  $sBackupFileName = "folder-$sLeafFolderName-dir.tar.gz";
  $sBackupFilePath = "$GARBAGEPATH/folder-$sLeafFolderName-dir.tar.gz";

  //$sCommandResult = 
  //   shell_exec('tar -zcvf '.$sBackupFilePath.' '.$sFilePath.' 2>&1');

  //$sCommandResult = $sCommandResult.
  //   shell_exec('rm -rf '.$sFilePath.' 2>&1');
  
  //$sCommandResult = htmlspecialchars($sCommandResult);

  $sPageContent = "
     =The server directory has been removed

   The directory: $sFilePath
   should have been removed and stored in the file
   $sBackupFilePath
  

  The commands returned the following messages 
  -->>
   $sCommandResult
  --<<
  ";

  $sPageContent = fnMessageToHtml($sPageContent);


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

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

    