<?
$C_126cmsadmin_tname = 'dbsql_config';

include(dirname(__FILE__)."/global.php");




		$PW = 'w6_';
		$dir=ROOT126CMS . '/data/mysql_bak';
		$cachefile=ROOT126CMS . '/cache/cache_bakin';



@$job = $submitted_vars["job"];

if($job == "del"){
   
   $delfile = $_POST['delfile'];
   
   		foreach($delfile as $key => $value){
			if(eregi("\.sql$",$value)){
				unlink($dir."/$value");
				
				@$showmsg .= "<br>成功删除 ".$value." 文件！";
				$formurl = "bakin.php";
			}
		}

  include('showmsg.php');
  exit;
  
}

if($job == "bakin"){
   
	$count = $submitted_vars['count'];
	$set = $submitted_vars['set'];
	
	
	//$stop = 0;//0为结束
	if(!$set){
	  $handle=opendir($dir);
		
		$i = 0;
		while($file = readdir($handle)){
			if(( eregi("^w6_",$file) || eregi("^$PW",$file)) && eregi("\.sql$",$file)){
				$filedb[$i] = $file;//以文件名作为数组
				$count++;//取得文件总数
			}
		$i++;
		}
		asort($filedb);//对数组进行排序并保持索引关系

		$filedb=implode("|",$filedb);
		
		updatetofile($cachefile,$filedb);//缓存要备份的数据文件名
		
		$filedb=explode("|",$filedb);
	}else{
		$filedb=read_file($cachefile);
		$filedb=explode("|",$filedb);
	}

	!$set && $set = 0;
	bakindata($dir.'/'.$filedb[$set]);
	$showmsg = "<p>&nbsp;</p><p>&nbsp;</p><p align=\"center\"> 成功导入 ".$filedb[$set]." 文件的内容 </p>";
	
	$count--;
	$set++;
	
	if($count == 0){
		$showmsg .= "数据库已全部成功导入！";
		$formurl = "bakup.php";
		include('showmsg.php');
	}else{
		echo $showmsg;
		echo "<meta http-equiv=\"refresh\" content=\"1;URL=?job=bakin&set=$set&count=$count\">";
	}
	
	exit();	
	/*
	if($pre=='w6_table_')$pre='w6_data0_';

		if(!$count){
			$count=0;
			$handle=opendir($dir);
			while($file = readdir($handle)){
				if(eregi("^$PW",$file) && eregi("\.sql$",$file)){
					$count++;
				}
			}
			bakindata($dir.'/w6_table_1.sql');
		}
		!$step && $step=1;
		/*
		$sql=readover(D_P.'data/'.$pre.$step.'.sql');
		bakindata($sql);
		*/
		/*
		if( $count > 1 )bakindata($dir.'/'.$pre.$step.'.sql');
		$i=$step;
		$step++;
		
		$showmsg = "<p align=\"center\"> 成功导入 ".$pre.($step-1).'.sql'." 文件的内容 </p>";
		if($count > 1 && $step <= $count){
			
			//$showmsg = "<p align=\"center\"> 成功导入 ".$pre.$step.'.sql'." 文件的内容 </p>";
			$formurl = "?job=bakin&step=$step&count=$count&pre=$pre";
			
			echo "<meta http-equiv=\"refresh\" content=\"1;URL=?job=bakin&step=$step&count=$count&pre=$pre\">";
		
		}else{
			$showmsg .= "数据库已全部成功导入！";
			$formurl = "bakup.php";
		}	*/
		
		//exit("$showmsg");
	//$showmsg = "数据库已成功导入！";
	//$formurl = "bakup.php";
    //include('showmsg.php');

    //exit;
  
}

function bakindata($filename) {
	global $cfg_dbcharset;
	$sql=file($filename);
	$query='';
	$num=0;
	foreach($sql as $key => $value){
		$value=trim($value);
		if(!$value || $value[0]=='#') continue;
		if(eregi("\;$",$value)){
			$query.=$value;
			if(eregi("^CREATE",$query)){
				$extra = substr(strrchr($query,')'),1);
				$query = str_replace($extra,'',$query);
				if(mysql_get_server_info() > '4.1'){
					$extra = $cfg_dbcharset ? "ENGINE=MyISAM DEFAULT CHARSET=$cfg_dbcharset;" : "ENGINE=MyISAM;";
				}else{
					$extra = "TYPE=MyISAM;";
				}
				$query .= $extra;
			}elseif(eregi("^INSERT",$query)){
				$query='REPLACE '.substr($query,6);
			}
			mysql_query($query);
			$query='';
		} else{
			$query.=$value;
		}

	}
}

function writetofile_w($filename,$book)
{
$fd=fopen("$filename","w");
fwrite($fd,$book);
fclose($fd);
return true;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>后台管理</title>
<link href="css.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
	text-decoration: none;
	color: #000000;
}
a:visited {
	text-decoration: none;
	color: #000000;
}
a:hover {
	text-decoration: underline;
	color: #FF0000;
}
a:active {
	text-decoration: none;
	color: #000000;
}
-->
</style>
<script language=JavaScript>
function CheckAll(form){
	for (var i=0;i<form.elements.length;i++){
		var e = form.elements[i];
		e.checked == true ? e.checked = false : e.checked = true;
	}
}
</script>
</head>

<body>
<div align="center" class="font12">
  <table width="100%"  border="0" cellpadding="3" cellspacing="1" bgcolor="#A4B6D7">
    <tr>
      <td width="100%" height="22" align="center" background="images/titlebg.jpg">&nbsp;</td>
    </tr>
  </table>
  <br />
  <table width="100%" height="25" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
      <td align="left" bgcolor="#FFFFFF"> [<a 
      href="bakup.php" class="a_bt">备份数据</a>] 
      [<a 
      href="bakin.php" class="a_bt">还原数据</a>] </td>
    </tr>
  </table>
  <br />
  <table width="100%" border="0" cellpadding="0" cellspacing="0" class="table">
    <tr>
      <td height="22" align="left" class="head">数据库表</td>
    </tr>
    <tr>
      <td height="24" align="left">
	  <br />
      <UL>
      <LI>本功能在恢复备份数据的同时,将覆盖原有数据,请确定是否需要恢复,以免造成数据损失。<br />
        <br />
        数据恢复功能只能恢复由当前版本导出的数据文件,其他软件导出格式可能无法识别。<br />
        <br />
        如果一个备份文件有多个分卷，您只需任选一个备份文件导入，程序会自动导入其他分卷 <br />
        <!---->
      </LI>
      </UL>
</td>
    </tr>
    <tr>
      <td align="center">
	  <FORM name="formlist" action="" method="post">
  <table width="99%" border="0" cellspacing="1" cellpadding="0">
        <tr>
          <td width="7%" height="30" align="center" background="images/titlebg.jpg">ID</td>
          <td width="30%" align="center" background="images/titlebg.jpg">文件名</td>
          <td width="22%" align="center" background="images/titlebg.jpg">备份时间</td>
          <td width="16%" align="center" background="images/titlebg.jpg">卷号</td>
          <td width="15%" align="center" background="images/titlebg.jpg">导入</td>
          <td width="10%" align="center" background="images/titlebg.jpg">删除</td>
        </tr>
		<?
		$filedb=array();
		$handle=opendir($dir);
		while($file = readdir($handle)){
			if(( eregi("^w6_",$file) || eregi("^$PW",$file)) && eregi("\.sql$",$file)){
				$strlen=eregi("^$PW",$file) ? 19 + strlen($PW) : 22;//目的是取 $bk['num']
				$fp=fopen($dir."/$file",'rb');
				$bakinfo=fread($fp,200);
				fclose($fp);
				$detail=explode("\n",$bakinfo);
				$bk['name']=$file;
				//$bk['version']=substr($detail[2],10);
				$bk['time']=substr($detail[3],8);
				$bk['pre']=substr($file,0,$strlen);
				$bk['num']=substr($file,$strlen,strrpos($file,'.')-$strlen);
				$filedb[]=$bk;
			}
		}
		
		asort($filedb);
		
		//print_r($filedb);

?>
<!--
<?
EOT;
foreach($filedb as $id=>$bakfile){
$id++;
print <<<EOT
-->
        <tr class=trA onMouseOver="this.className='trB'" 
        onmouseout="this.className='trA'">
          <td align="center" class="td">$id</td>
          <td align="center" class="td"><a href="../../data/mysql_bak/$bakfile[name]" target="_blank">$bakfile[name]</a></td>
          <td align="center" class="td">$bakfile[time]</td>
          <td align="center" class="td">$bakfile[num]</td>
          <td align="center" class="td"><a href="?job=bakin&pre=$bakfile[pre]">导入</a></td>
          <td align="center" class="td"><input name="delfile[]" type=checkbox value="$bakfile[name]"></td>
        </tr>
<!--
EOT;
}
?>
-->
        <tr>
          <td height="30" colspan="6" align="center"><input type="button" name="chkall" value="全 选" onClick="CheckAll(this.form)">
<input type=submit value="删除备份">
            <input name="job" type="hidden" id="job" value="del" /></td>
        </tr>
      </table>
	  </form></td>
    </tr>
  </table>
</div>
</body>
</html>
