                                                                                                                                                    

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
//--Description:
//--  The purpose of this script is to allow a file on the 
//--  server to be restored from a previous version using the 
//--  files stored in the backup file.
//--
//--  This functionality is very important considering that it is
//--  possible to render the entire system unusable by introducing
//--  a syntax error into one of the common library functions, some
//--  of which are included into nearly all of the user interface 
//--  scripts.



  $WEBROOT = "/home/waggacwc/public_html";
  $BACKUPFOLDER = "$WEBROOT/refurb/manage/old-data";


  $sErrorTemplateContents = "";
  $sFilePath = '';

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

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

  //-- This is the base-name for the backup files
  $sBackupFileBaseName = preg_replace("/\..*$/", "-old", $sFileName);
  $sBackupFilePath = $BACKUPFOLDER.'/'.$sBackupFileBaseName."1.txt";
  $sBackupFilePathEncoded = htmlspecialchars($sBackupFilePath);

  $sHelpMessage = "
     
       HOW TO GET THIS SCRIPT TO WORK

   You can enter the filename which you wish to restore directly in
   the query string of the URL for this script. Type something like

      http://server.org/script/path/undo-file-change.php?file=filepath


  

  if ($sFilePath == '')
  {
     
    $sPageContent = "
          = No File Names Specified

      You did not specify any file to be restored


      ";

    echo $sPageContent;

    exit; 
  } //-- if no file specified
  


  if (!file_exists($sFilePath))
  {
    if (file_exists($WEBROOT.$sFilePath))
    {
      $sFilePath = $WEBROOT.$sFilePath;
    }
    elseif (file_exists($WEBROOT."/".$sFilePath))
    {
      $sFilePath = $WEBROOT."/".$sFilePath;
    }
    else
    {
      $sPageContent = "
            = The File Doesnt Exist

        The file-name [$sFilePathEncoded] which you just specified does
        not exist and therefore cannot be restored

        ";

      echo $sPageContent;

      exit; 
    }	  
  }

  if (is_dir($sFilePath))
  {
    if (substr($sFilePath, -1) != "/")
    {
      $sFilePath = "$sFilePath/";
    }
    
    $sPageContent = "
           = The file is a directory

      The file-name [$sFilePathEncoded] which you just specified is a 
      directory (folder). 

       ";

    echo $sPageContent;

    exit;
  } //-- if file is a directory

  $iBackups = 4;

  rename($BACKUPFOLDER.'/'.$sBackupFileBaseName."1.txt", $sFilePath);

  for ($ii = 1; $ii <= $iBackups; $ii++)
  {
       $sNewName = "$BACKUPFILEDIRECTORY/$sBackupFileBaseName$ii.txt";
       $sOldName = "$BACKUPFILEDIRECTORY/$sBackupFileBaseName".($ii+1).".txt";
       if (file_exists($sOldName))
       {
          if (file_exists($sNewName))
          {
             unlink($sNewName);
          }
          rename($sOldName, $sNewName);
       } //-- if
  } //-- for


  //-- Display a message indicating success.

  $sPageContent = "
        = Server File Restored

    The file: $sFilePathEncoded
    was restored from: $sBackupFilePathEncoded


      ";

  echo $sPageContent;

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


                                                                                                        