<?php
//@set_time_limit(600);
$admin = 1;
$restore_inport = 1;
                                                                 
if(!strlen($pathtoroot))
{
  $path=dirname(realpath(__FILE__));
  if(strlen($path))
  {
    /* determine the OS type for path parsing */
    $pos = strpos($path,":");
    if ($pos === false)
    {
      $gOS_TYPE="unix";
      $pathchar = "/";
    }
    else
    {
      $gOS_TYPE="win";
      $pathchar="\\";
    }
    $p = $path.$pathchar;
    /*Start looking for the root flag file */
    while(!strlen($pathtoroot) && strlen($p))
    {
      $sub = substr($p,strlen($pathchar)*-1);
      if($sub==$pathchar)
      {
        $filename = $p."root.flg";
      }
      else
        $filename = $p.$pathchar."root.flg";
      if(file_exists($filename))
      {
        $pathtoroot = $p;
      }
      else
      {
        $parent = realpath($p.$pathchar."..".$pathchar);
	if($parent!=$p)
	{
	  $p = $parent;
	}
	else
	  $p = "";
      }
    }
    if(!strlen($pathtoroot))
      $pathtoroot = ".".$pathchar;
  }
  else
  {
    $pathtoroot = ".".$pathchar;
  }
}
$sub = substr($pathtoroot,strlen($pathchar)*-1);
if($sub!=$pathchar)
{
  $pathtoroot = $pathtoroot.$pathchar;
}

require_once($pathtoroot."kernel/startup.php");
$rootURL="http://".ThisDomain().$objConfig->Get("Site_Path");
$admin = $objConfig->Get("AdminDirectory");
if(!strlen($admin))
    $admin = "admin";    
$adminURL = $rootURL.$admin;
$localURL=$rootURL."kernel/";
$imagesURL = $rootURL."admin/images";
//admin only util
$pathtolocal = $pathtoroot."kernel/";
require_once ($pathtoroot."admin/include/elements.php");
require_once ($pathtoroot."kernel/admin/include/navmenu.php");
require_once ($pathtolocal."admin/include/navmenu.php");
require_once($pathtoroot."admin/toolbar.php");
require_once($pathtoroot.$admin."/install/install_lib.php");

$conn = GetAdodbConnection();
$section = "in-portal:restore";
int_header(NULL);
//echo "FileOffset: $FileOffset <br>\n";
if(count($_POST))
{
	$filename = "dump".$_POST["backupdate"]."txt";
	$filename = $objConfig->Get("Backup_Path")."/".$filename;
}
else 
	$filename = $_GET["Filename"];

$FileOffset = (int)$_GET["Offset"];
//echo "FileOffset: $FileOffset <br>\n";
if(!file_exists($filename))
{
	echo prompt_language("la_restore_file_not_found")." : $filename";
	exit();
}
if(!is_readable($filename))
{
	echo prompt_language("la_restore_access_denied");
	exit();
}

$TotalSize = filesize($filename);

$MaxLines = 200;

$PageTitle = admin_language("la_text_Restore_in_progress");
$CancelURL =   $rootURL ."admin/backup/restore1.php?env=".BuildEnv();

stats($PageTitle,$FileOffset,$TotalSize);
//echo "FileOffset: $FileOffset <br>\n";
if($FileOffset < $TotalSize)
{
  $FileOffset = RunRestoreFile($conn,$filename,$FileOffset,$MaxLines);
  if($FileOffset>-1)
  {
  	if($FileOffset ==0)
  	  $FileOffset = $TotalSize;
    $url = $_SERVER['PHP_SELF']."?env=".BuildEnv()."&Offset=$FileOffset&Filename=$filename";
  }
  else 
  {  	
  	switch($FileOffset)
  	{
  		case -1:
  			$error =  prompt_language("la_restore_file_error");
  		break;
  		case -2:
  			$error = prompt_language("la_restore_read_error");
  		break;
  		default:
  			$error = "(".$FileOffset.") ".prompt_language("la_restore_unknown_error");
  		break;
  	}
  	echo $error;
  	die();  		
  }  
}
else
  $url = $adminURL."/backup/restore4.php?env=".BuildEnv();

reload($url);
echo "</BODY>";
echo "</HTML>";

function stats($caption,$myprogress,$totalnum)
{
    global $rootURL, $CancelURL;
    
    if($totalnum>0)
    {
      $pct=round(($myprogress/ $totalnum)*100);
    }
    else
    $pct = 100;
    $o .="<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"4\" class=\"tableborder\">";
    
    echo "\n";   
    $o .= int_subsection_title_ret($caption."-".$pct."%");
    $o .= "<TR><TD  align=\"middle\"><br />"; 
    $o .= "  <TABLE CLASS=\"tableborder_full\" width=\"75%\">";
    $o .="  <TR border=1><TD width=\"".$pct."%\" STYLE=\"background:url('".$rootURL."admin/images/progress_bar_segment.gif');\">&nbsp;</TD>";
    $comp_pct = 100-$pct;
    $o .= "  <TD bgcolor=#FFFFFF width=\"".$comp_pct."%\"></TD></TR>";
    $o .= "  </TABLE>";
    $o .= "  <BR /><input type=button VALUE=\"".admin_language("la_Cancel")."\" CLASS=\"button\" ONCLICK=\"document.location='".$CancelURL."';\">";
    echo $o."\n";
    echo "</TD></TR></TABLE>";
}

function reload($url)
{
	print "<script language=\"javascript\">" ;
	print "setTimeout(\"document.location='$url';\",40);";
	print " </script>";
	echo "<A HREF=\"$url\">$url </A>";
}
?>