<?
function sql_dumptable($table, $startfrom = 0, $currsize = 0) {
	global $db, $sizelimit, $startrow, $sqlcompat, $sqlcharset, $dumpcharset;
	if(!isset($tabledump)) $tabledump = '';
	$offset = 100;
	if(!$startfrom) {
		$tabledump = "DROP TABLE IF EXISTS `$table`;\n";
		$createtable = $db->query("SHOW CREATE TABLE `$table`");
		$create = $db->fetch_array($createtable);
		$tabledump .= $create[1].";\n\n";
		if($sqlcompat == 'MYSQL41') $tabledump = preg_replace("/TYPE\=([a-zA-Z0-9]+)/", "ENGINE=MyISAM DEFAULT CHARSET=".$dumpcharset, $tabledump);//ENGINE=\\1
		if($sqlcharset) $tabledump = preg_replace("/(DEFAULT)*\s*CHARSET=[a-zA-Z0-9]+/", "DEFAULT CHARSET=".$sqlcharset, $tabledump);
	}
	$tabledump = str_replace("ENGINE=InnoDB", "", $tabledump);
	$tabledumped = 0;
	$numrows = $offset;
	while($currsize + strlen($tabledump) < $sizelimit * 1000 && $numrows == $offset) {
		$tabledumped = 1;
		$rows = $db->query("SELECT * FROM `$table` LIMIT $startfrom, $offset");
		$numfields = $db->num_fields($rows);
		$numrows = $db->num_rows($rows);
		while($row = $db->fetch_array($rows)) {
			$comma = "";
			$tabledump .= "INSERT INTO `$table` VALUES(";
			for($i = 0; $i < $numfields; $i++) {
				$tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
				$comma = ",";
			}
			$tabledump .= ");\n";
		}
		$startfrom += $offset;
	}
	$startrow = $startfrom;
	$tabledump .= "\n";
	return $tabledump;
}
if(isset($backup)) {
			$fileid = isset($fileid) ? intval($fileid) : 1;
			$sizelimit = $sizelimit ? intval($sizelimit) : 2048;
			if($fileid == 1 && $tables) {
				if(!isset($tables) || !is_array($tables)) msg('请选择需要备份的表','phpleft.php?file='.$file);
				$random = date('Y-m-d H.i.s').' '.strtolower(rand(10000,99999));
				$tsize = 0;
				foreach($tables as $k=>$v) {
					$tsize += $sizes[$v];
				}
				$tid = ceil($tsize*1024/$sizelimit);
				cache_write($info['username'].'_backup.php', $tables,'file/cache/');
			} else {
				if(!$tables = cache_read($info['username'].'_backup.php','file/cache/')) msg('请选择需要备份的表','phpleft.php?file='.$file);
			}
			$dumpcharset = $sqlcharset ? $sqlcharset : $CFG['db_charset'];
			$setnames = "SET NAMES '$dumpcharset';\n\n";
		
				if($sqlcharset) $db->query("SET NAMES '".$sqlcharset."';\n\n");
				if($sqlcompat == 'MYSQL40')	{
					$db->query("SET SQL_MODE='MYSQL40'");
				} else if($sqlcompat == 'MYSQL41') {
					$db->query("SET SQL_MODE=''");
				}
			$sqldump = '';
			$tableid = isset($tableid) ? $tableid - 1 : 0;
			$startfrom = isset($startfrom) ? intval($startfrom) : 0;
			$tablenumber = count($tables);
			
			for($i = $tableid; $i < $tablenumber && strlen($sqldump) < $sizelimit * 1000; $i++) {
				$sqldump .= sql_dumptable($tables[$i], $startfrom, strlen($sqldump));
				$startfrom = 0;
			}
			if(trim($sqldump)) {
				$sqldump = "# \n# --------------------------------------------------------\n\n\n".$sqldump;
				$tableid = $i;
				$filename = $random.'/'.$fileid.'.sql';
				MakeHtmlFile('file/backup/'.$filename, $sqldump, 1);
				$fid = $fileid;
				msg('分卷 <strong>#'.$fileid++.'</strong> 备份成功.. 程序将自动继续...', 'phpleft.php?file='.$file.'&sizelimit='.$sizelimit.'&sqlcompat='.$sqlcompat.'&sqlcharset='.$sqlcharset.'&tableid='.$tableid.'&fileid='.$fileid.'&fileid='.$fileid.'&tid='.$tid.'&startfrom='.$startrow.'&random='.$random.'&backup=1');
			} else {
			   delhtmls('file/cache/'.$info['username'].'_backup.php');
			   msg('数据库备份成功', 'phpleft.php?file='.$file.'&action=import');
			}
}else{
$dtables = $tables = $C = array();
$i = $j = $dtotalsize = $totalsize = 0;
$result = $db->query("SHOW TABLES FROM `".$PL_CFG['db_name']."`");
			while($rr = $db->fetch_array($result)) {
				if(!$rr[0]) continue;
				$tablelist[] = $rr[0];
}
foreach($tablelist as $i){
	            $rm = $db->query("SHOW TABLE STATUS FROM `".$PL_CFG['db_name']."` LIKE '".$i."'");
                $r = $db->fetch_array($rm);	
				if(preg_match('/^'.$PL_CFG['tb_pre'].'/', $i)) {
					$dtables[$i]['name'] = $r['Name'];
					$dtables[$i]['rows'] = $r['Rows'];
					$dtables[$i]['size'] = round($r['Data_length']/1024/1024, 2);
					$dtables[$i]['index'] = round($r['Index_length']/1024/1024, 2);
					$dtables[$i]['tsize'] = $dtables[$i]['size']+$dtables[$i]['index'];
					$dtables[$i]['auto'] = $r['Auto_increment'];
					$dtables[$i]['updatetime'] = $r['Update_time'];
					$dtables[$i]['note'] = $r['Comment'];
					$dtables[$i]['chip'] = $r['Data_free'];
					$dtotalsize += $r['Data_length']+$r['Index_length'];
					$C[str_replace($DT_PRE, '', $r['Name'])] = $r['Comment'];
					$i++;
				} else {
					$tables[$j]['name'] = $r['Name'];
					$tables[$j]['rows'] = $r['Rows'];
					$tables[$j]['size'] = round($r['Data_length']/1024/1024, 2);
					$tables[$j]['index'] = round($r['Index_length']/1024/1024, 2);
					$tables[$j]['tsize'] = $tables[$j]['size']+$tables[$j]['index'];
					$tables[$j]['auto'] = $r['Auto_increment'];
					$tables[$j]['updatetime'] = $r['Update_time'];
					$tables[$j]['note'] = $r['Comment'];
					$tables[$j]['chip'] = $r['Data_free'];
					$totalsize += $r['Data_length']+$r['Index_length'];
					$j++;
				}
	
	
	
}
$dtotalsize = round($dtotalsize/1024/1024, 2);
$totalsize = round($totalsize/1024/1024, 2);
define('ThisFile', ThisFile());
include FTemplate(ThisFile);
}
?>