<?php 
	require("session_check.php");	
	date_default_timezone_set("Asia/shanghai");

	//语言包引入
	$d_langpackage=new datalp;
	
	$operation	= get_argg('operation');
	
	
	if($operation=='savebackup'){
		$dbo = new dbex;
		dbtarget('w',$dbServs);
	
		$tabledb	= get_argp('tabledb');
		$start		= (int)get_argg('start');
		$tableid	= (int)get_argg('tableid');
		
		$bak="/* iweb_sns Backup SQL File \n Version: v0.9 \n Time: ".date('Y-m-d H:i:s')."\n iweb_sns: http://www.jooyea.net*/\n\n\n\n";
		
		$bakupdata=bakupdata($dbo,$tabledb,$start);
	
		if(!$tabledb){
			echo "<script language='javascript'> alert('$d_langpackage->d_boject_no'); history.go(-1);</script>";
		}
		$step=1;
		$aaa=num_rand(10);
		$start=0;
		$bakuptable=bakuptable($dbo,$tabledb);
		
		$f_num=ceil($step/2);
		$filename='isns_'.date('m-d').'_'.$aaa.'_'.$f_num.'.sql';
		$step++;
		$writedata=$bakuptable ? $bakuptable.$bakupdata : $bakupdata;
	
		$t_name	= $tabledb[$tableid-1];
		$c_n	= $startfrom;
		trim($writedata) && writefile('../doc/'.$filename,$bak.$writedata,true,'ab');		
		if($step>1){
			for($i=1;$i<=$f_num;$i++){
				$temp=substr($filename,0,19).$i.".sql";
				if(file_exists("../doc/$temp")){
					$bakfile.='<a href="'."../doc/$temp".'">'.$temp.'</a><br>';
				}
			}
		}
		echo "<script language='javascript'> alert('$d_langpackage->d_backup_suc'); location.href='database.recover.php'; </script>";
	}
	else{
		$dbo = new dbex;
		dbtarget('r',$dbServs);
		$tabledb=mysql_query("show table status from $db;");
		while($row[]=mysql_fetch_array($tabledb));   
	}
?>
<!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=utf-8" />
<link rel="stylesheet" type="text/css" href="css/right.css">
<title></title>
</head>
<script language="JavaScript" type="text/JavaScript"> 
function checkAll(form, name) {
	for(var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];
		if(e.name.match(name)) {
			e.checked = form.elements['chkall'].checked;
		}
	}
}
</script>
<body>
  <form action="?operation=savebackup" method="post">
  <div class="container">
	<div class="rs_head"><?php echo $d_langpackage->d_db_backup?></div>
</div>
    <TABLE class="main main_left">
      <tr>
        <th width="60"><?php echo $d_langpackage->d_choice?></th>
        <th width="60"><?php echo $d_langpackage->d_db_id?></th>
        <th width="780"><?php echo $d_langpackage->d_db_table?></th>
      </tr>
      <?php 
      foreach($row as $n=>$table){
      	if($table[0]!=""){
      ?>
      <tr>
        <td><input type="checkbox" class="checkbox" name="tabledb[]" value="<?php echo $table[0];?>" /></td>
        <td><?php echo $n+1;?></td>
        <td><?php echo $table[0];?></td>
      </tr>
      <?php 
      	}
      }
      ?>
      <tr class="nobg">
        <td class="td25"><input type="checkbox" name="chkall" id="chkall" class="checkbox" onclick="checkAll(this.form, 'tabledb')" />
          <label for="chkall"><?php echo $d_langpackage->d_check_all?></label></td>
        <td colspan="2">
          <div class="fixsel"> <input type="submit" class="btn" name="forumlinksubmit" value="<?php echo $d_langpackage->d_refer?>"  /> </div></td>
      </tr>
    </table>
  </form>
</body>
</html>
<?php 
function writefile($filename,$data,$check=1,$method="rb+",$iflock=1,$chmod=1){
	$check && strpos($filename,'..')!==false;
	touch($filename);
	$handle=fopen($filename,$method);
	if($iflock){
		flock($handle,LOCK_EX);
	}
	fwrite($handle,$data);
	if($method=="rb+") ftruncate($handle,strlen($data));
	fclose($handle);
	$chmod && @chmod($filename,0777);
}
function num_rand($lenth){
	mt_srand((double)microtime() * 1000000);
	for($i=0;$i<$lenth;$i++){
		$randval.= mt_rand(0,9);
	}
	$randval=substr(md5($randval),mt_rand(0,32-$lenth),$lenth);
	return $randval;
}
function bakupdata($dbo,$tabledb,$start=0){
	global $sizelimit,$tableid,$startfrom,$stop,$rows;
	$tableid=$tableid?$tableid-1:0;
	$stop=0;
	$t_count=count($tabledb);
	for($i=$tableid;$i<$t_count;$i++){
		$sql="SELECT * FROM $tabledb[$i]";
		$rs = $dbo->getRs($sql);
		foreach($rs as $key=>$val){
			$num_F = count($val)/2;
			$start++;
			$table=$tabledb[$i];
			$bakupdata .= "INSERT INTO $table VALUES("."'".addslashes($val[0])."'";
			$tempdb='';
			for($j=1;$j<$num_F;$j++){
				$tempdb.=",'".addslashes($val[$j])."'";
			}
			$bakupdata .=$tempdb. ");\n";
			if($sizelimit && strlen($bakupdata)>$sizelimit*1000){
				break;
			}
		}

		if($start>=$rows){
			$start=0;
			$rows=0;
		}

		$bakupdata .="\n";
		if($sizelimit && strlen($bakupdata)>$sizelimit*1000){
			$start==0 && $i++;
			$stop=1;
			break;
		}
		$start=0;
	}

	if($stop==1){
		$i++;
		$tableid=$i;
		$startfrom=$start;
		$start=0;
	}
	return $bakupdata;
}
function bakuptable($dbo,$tabledb){
	global $tableid;
	$tableid=$tableid?$tableid-1:0;
	$t_count=count($tabledb);
	for($i=$tableid;$i<$t_count;$i++){
		$creattable.= "DROP TABLE IF EXISTS $tabledb[$i];\n";
		$CreatTable = $dbo->create("SHOW CREATE TABLE $tabledb[$i]");
		$creattable.=$CreatTable['Create Table'].";\n\n";
	}
	return $creattable;
}
?>