<?php

	session_start();
	require_once "../init.php";
	require_once "../authority.inc.php";
	
	if(!$bakname) die("没有需要备份的文件");
	//die($bakname);
	header('Content-Type: text/xml; charset=gb2312');
	if($action=="bak"){
		if(xCopy($bakname,$bakname."_bak",1)){
			die("备份成功");
		}else{
			die("备份失败");
		}
	} elseif ($action=="back"){
		if(file_exists($bakname."_bak")){
			if(xCopy($bakname."_bak",$bakname,1)) {
				die("恢复成功");
			} else {
				die("恢复失败");
			}
		} else {
			die("不存在该备份文件,恢复失败。");
		}
	}

	function xCopy($source, $destination, $child){
		if (!file_exists($destination))
		{
			if (!mkdir(rtrim($destination, '/')))
			{
			return false;
			}
			@chmod($destination, 0777);
		 }
		if(!is_dir($source)){  
		return 0;
		}
		if(!is_dir($destination)){
		mkdir($destination);   
		}
		$handle=dir($source);
		while($entry=$handle->read()){
		if(($entry!=".")&&($entry!="..")){
		if(is_dir($source."/".$entry)){ 
		if($child)
		xCopy($source."/".$entry,$destination."/".$entry,$child);
		}
		else{
		copy($source."/".$entry,$destination."/".$entry);
		}
		}    
		}    
		return 1;
	}
?>