<?php
# Subroutine: adm_restore_submit.php called by Admin.php
# Xinfeng Chen, 2002.12.20
# Input: $file

require_once('bin/config.php');
require_once('admin/adm.inc');

#--- Validate dministrator ---
include('admin/adm_validate.php');
if(!$adm_validated){jumpTo('admin.php?cmd=logon'); exit;}

#--- Backup specified data ---
require_once('core/class_forum_base.php');
CForumBase::setFileBase("$filepath");

$backfile=CForumBase::getDirPath('BACKUP');
$backfile.="/$file";

if(!file_exists($backfile) || !is_file($backfile)){
 msgBox("备份文件错误，无法执行还原操作！",mCRITICAL);
 exit;
}

#--- Start restore ---
$fp=fopen($backfile,'rb');
$head=fread($fp,64);

list($tag,$bscope,$bid,$bts,$buser)=explode('|',$head);
$btime=strftime("%Y-%m-%d %H:%M:%S",$bts);
if($bscope != 'BOARD'){$bid='';}

switch($bscope){
 case 'ALL': $dirpath=CForumBase::getDirPath('BASE');break; 
 case 'BOARD':
  if(!$bid){
   msgBox("指定论坛错误，中止还原操作！",mCRITICAL);
   exit;
  }
  $dirpath=CForumBase::getDirPath('BOARD',$bid);break; 
 case 'USER': $dirpath=CForumBase::getDirPath('USER');break;
 default:
  msgBox("备份策略错误，中止还原操作！",mCRITICAL);
  exit;
}

_restoreDir(&$fp, $dirpath);

fclose($fp);

#--- Print result ---
$msg='还原操作成功.<br>';
$msg.="备份文件: $file<br>";
$msg.="文件大小: $fsize K<br>";
$msg.="备份时间: $btime<br>";
$msg.="执行用户: $buser<br>";
$msg.="备份策略: $bscope<br>";
$msg.="论坛ID: $bid<br><br>";

msgBox($msg, mINFORMATION, '还原操作成功',
 '?cmd=restore', '返回还原操作主页');
exit;

#--- Subroutines to restore files ---
function _restoreDir($a_fp, $a_rootpath){

 while($ftag=fread($a_fp,1)){
  $tmp=fread($a_fp,8);
  $tmparr=unpack('L*',$tmp);
  $frelpath=fread($a_fp,$tmparr[1]);
  $fsize=$tmparr[2];

  if($ftag=='F'){
   $flag=_restoreFile(&$a_fp, "$a_rootpath$frelpath", $fsize);
  }elseif($ftag=='D'){
   $dirpath="$a_rootpath$frelpath";
   if(!file_exists($dirpath)){
    mkdir($dirpath,0700);
   }
  }

 }

 return true;
}

function _restoreFile($a_fp, $a_fname, $a_size){

 if(!($tmpfp=fopen($a_fname, 'wb'))){return false;}
 $left=$a_size;
 while($left>0){
  $bufsize=($left<4096) ? $left : 4096;
  $buf=fread($a_fp, $bufsize);
  fwrite($tmpfp, $buf);
  $left-=strlen($buf);
 }
 fclose($tmpfp);
 
 return true;
}

?>