                    <?php

//-- Description: 
//--   The idea of this function is to walk through a directory tree
//--   of files and modify the path to the globals file in each script
//--   
//--   
//-- Notes:
//--   
//-- See Also:
//--   fnDirectoryTreeHtmlLinkList
//--


function fnRecursiveFileModifier($sDirectoryPath)
{
    //echo "trying to open $sDirectoryPath<br>";
    $CONFIGFILENAME = "RETRO-GLOBALS.php";

    if ($dir = opendir($sDirectoryPath))
    {
      //$sDirectoryPathEncoded =
      //   htmlspecialchars($sDirectoryPath);

      while (false !== ($sFileName = readdir($dir)))
      {
        if ($sFileName == ".") continue;
        if ($sFileName == "..") continue;

        $sFilePath = $sDirectoryPath."/".$sFileName;
        $sFilePathEncoded = htmlspecialchars($sFilePath);

        if (is_dir($sFilePath))
        {

          fnRecursiveFileModifier($sFilePath);
        } 
        else
        {
           echo "examining file $sFilePathEncoded <br>";
          //-- Get the contents of the file
          $aaTextLines = file($sFilePath);
          $sFileContents = implode('', $aaTextLines);

          $sFileContents = preg_replace(
            "#include.*RETRO-GLOBALS\.php#i", 
            "include_once \"/usr50/home/retrovox/public_html/test-sys/RETRO-GLOBALS.php", $sFileContents);

          $sFileContents = preg_replace(
            "#^([ \t]*)include[ \t]+(['\"])#im", 
            "$1include_once $2", $sFileContents);

          if ($sFileName == "RETRO-GLOBALS.php")
          {
             echo "FOUND CONFIG FILE <br>";
             $sFileContents = preg_replace(
               '#\$LIBRARYPATH[ \t]*=.*#i', 
               '$LIBRARYPATH="/usr50/home/retrovox/public_html/test-sys/product-data/lib";', $sFileContents);
          }          
           
          $fileHandler = fopen($sFilePath, "w");
          fwrite($fileHandler, $sFileContents);
          fclose($fileHandler);

        }

      } //-- while more files in directory
      closedir($dir);
    }
    //return $sReturn;

} //-- function fnRecursiveFileModifier


?>
                                                                                                                                                

<?php
//-->-->-->-->-->-->-->-->-->-->-->-->-->-->
//--Description:
//--  The purpose of this script is to install the browser webedit
//--  system from a zip file which is accessible from the internet
//--
//--Notes:
//--  On the 'retrovox server' http://www.retrovox.com.au it does not
//--  appear to be possible to use any kind of Internet retrieval 
//--  programs from within a php script. This makes it impossible to 
//--  retrieve the script from across the internet.


   $sLibraryPath = '/usr50/home/retrovox/public_html/manage/php-lib';
   include "$sLibraryPath/RETROVOX-CONSTANT-VARIABLES.php";
   include "$sLibraryPath/fnRetrovoxStartHtml.php";
   include "$sLibraryPath/fnRetrovoxEndHtml.php";
   include "$sLibraryPath/fnRetrovoxDirectoryListing.php";

 
  //$sZipSystemUrl = 
   // "http://www.geocities.com/matth3wbishop/eg/php/folder-refurb-dir.tar.gz";
 
  $sZipFileName = "folder-refurb-dir.tar.gz";
  $sZipFilePath = 
    "/usr50/home/retrovox/public_html/manage/$sZipFileName";

  $sInstallRoot = '/usr50/home/retrovox/public_html';
  $sInstallFolderName = 'test-sys';

 $sParentDirectory = $sInstallRoot;
 $sDirectoryName = $sInstallFolderName;

 if (!file_exists($sParentDirectory))
  {
     echo fnRetrovoxStartHtml("parent directory doesnt exist");
     echo "
       <br>The parent directory <em>$sParentDirectoryEncoded</em> doesn't exist
           ";
     echo fnRetrovoxEndHtml();
     exit;
  }
  
  if (!is_dir($sParentDirectory))
  {
     echo fnRetrovoxStartHtml("parent directory is not a directory");
     echo "
       <br>The parent directory <em>$sParentDirectoryEncoded</em>
           is not actually a directory. You cannot add sub-directories to 
           files";
     echo fnRetrovoxEndHtml();
     exit;
  }

  if (substr($sParentDirectory, -1) == '/')
  {
     $sParentDirectory = preg_replace('#/$#', '', $sParentDirectory);
  }

  if (substr($sDirectoryName, 0, 1) == '/')
  {
     $sDirectoryName = preg_replace('#^/#', '', $sDirectoryName);
  }

  $sNewDirectoryPath = $sParentDirectory.'/'.$sDirectoryName;
  $sNewDirectoryPathEncoded = htmlspecialchars($sNewDirectoryPath);

  $bAllowOverWrite = true;
  if (is_dir($sNewDirectoryPath) && ($bAllowOverWrite == false))
  {
     echo fnRetrovoxStartHtml("The new directory already exists");
     echo "
       <br>The  directory <em>$sNewDirectoryPathEncoded</em> already
           exists.
           ";
     echo fnRetrovoxEndHtml();
     exit;
  }

  /*
  if (!mkdir($sNewDirectoryPath, 0755) && !file_exists($sNewDirectoryPath))
  {
     echo fnRetrovoxStartHtml("The directory could not be created");
     echo "
       <br>The  directory <em>$sNewDirectoryPathEncoded</em> was not able
           to be created. This is probably a problem of permissions.
           You probably need to make the parent directory
           $sParentDirectoryEncoded writable by the web-server user
           This means that you need to be able to change the permissions
           on the parent directory by some kind of shell or telnet 
           system and by typing 
              chmod a+w parent-directory

           hope that helps
           ";
     echo fnRetrovoxEndHtml();
     exit;
  }
  */



  

  //$sCommandResult = 
  //   shell_exec('wget '.$sZipSystemUrl.' ');

  //$sCommandResult = 
  //   shell_exec('cp '.$sZipFilePath.' '.$sNewDirectoryPath.' 2>&1');

  $sCommandResult = $sCommandResult.
     shell_exec('tar -xzvf '.$sZipFilePath.' 2>&1');

  $sCommandResult = $sCommandResult.
     shell_exec('chmod -R a+w *  2>&1');

  $sStripPath = "home/waggacwc/public_html/refurb";
  $sNewFolderName = "$sInstallRoot/".basename($sStripPath);
  $sInstallPath = "$sInstallRoot/$sInstallFolderName";

  $sCommandResult = $sCommandResult.
     shell_exec('mv '.$sStripPath.' '.$sInstallPath.' 2>&1');


  echo 'mv '.$sStripPath.' '.$sInstallPath.' 2>&1';


  //$sCommandResult = $sCommandResult.
  //   shell_exec('mv '.$sNewFolderName.' '.$sInstallPath.' 2>&1');

  //-- These commands remove previously untarred files
  //$sCommandResult = $sCommandResult.
  //   shell_exec('rm -rf /usr50/home/retrovox/public_html/manage/home 2>&1');

  fnRecursiveFileModifier($sInstallPath);


  $sPageContent = 
   "<html><body><pre>
    INSTALLING SYSTEM FROM A ZIP FILE

    The commands returned the following results

     $sCommandResult

    </pre></body></html>
       ";


  echo $sPageContent;

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

                                    