<?php
/*********************************************************
程序名称：站长助手 WEBMASTER HELPER
作者：feixin
email:kusite@etang.com
版权：feixin (http://www.zc18.com)
//////////////////////////////////////////////////////////

最后编辑日期：2003-6-12
*********************************************************/

if (!defined('IN_WH')) {
  die("不要黑我啊");
}

include("./include/mysql.php");

$db = new WHmysql(WH_DB_HOST, WH_DB_NAME, WH_DB_USER, WH_DB_PW, 0);

if(empty($action) || $action != "backup"){

?>
<center>
<form method="post" action="index.php?op=mysql_backup" name="mysql_dump">
<input type="hidden" name="action" value="backup">
<select name="table_select[]" size="6" multiple="multiple">
    <?php
	$tables = $db->list_tables(WH_DB_NAME);
	$n = $db->num_rows($tables);
	if($n == 0){ $WH->message("暂时没有数据表！", "", 1);}

    $i = 0;
    while ($i < $n) {
        $table   = htmlspecialchars($db->tablename($tables,$i));
        echo '<option value="' . $table . '" selected>' . $table . '</option>' . "\n";
        $i++;
    } // end while
    ?>
</select>

<br />
<input type="radio" name="method" value="structure" />
只备份数据表结构<br />
<input type="radio" name="method" value="data"  checked="checked"/>
备份数据结构和内容<br />
<input type="radio" name="method" value="dataonly" />
只备份数据表内容<br />
备份到目录<input type="text" name="backup_dir" value="backup"><br />(相对于<?=$WHuds->root?>,目录要存在)
<br />
<input type="submit" value="确认" />
</form>

（技巧：你可以备份到目录后把它打包，然后下载）
</center>

<?php
}
else
{
	$tmp_file_url = WH_URL_PATH . "/". $backup_dir ;
	$backup_dir = $WHuds->safecheck($WHuds->root . WH_SLASH . $backup_dir);
	if(!$WHuds->exists($backup_dir))
	{
		$WH->message("不存在此目录{$backup_dir}，返回！", "index.php?op=mysql_backup", 1);
	}

	$n = count($table_select);
	if($n == 0){ $WH->message("暂时没有数据表！", "", 1);}

	$i = 0;
	
	$sql = "";
	while ($i < $n) {
        if($table_select[$i] != ""){ 
			$sql .= sqldumptable($table_select[$i]);
		}
        $i++;
    } // end while

	$tmpfile_name = WH_DB_NAME."_".date("Y-m-d")."_".time().".sql";

	$result = $WHuds->write($backup_dir . WH_SLASH . $tmpfile_name, $sql);

	if($result){ $WH->message("备份到文件<a href=\"$tmp_file_url/{$tmpfile_name}\"><b>{$tmpfile_name}</b></a>成功！", "", 1, 0);}
	else{ $WH->message("新建文件{$tmpfile_name}失败，可能不够权限执行此操作！", "", 1, 0);}


}

//函数参考discuz
function sqldumptable($table, $currsize = 0) {
	global $db, $multivol, $sizelimit, $startfrom, $method;

	if($method == "structure" || $method == "data") {
		$tabledump = "DROP TABLE IF EXISTS $table;\n";

		$createtable = $db->query("SHOW CREATE TABLE $table");
		$create = $db->fetch_row($createtable);

		$tabledump .= $create[1].";\n\n";

	}
	
	if($method == "dataonly" || $method == "data") {
		if(empty($startfrom)){ $startfrom = 0;}
		$tabledumped = 0;$offset = 64;
		$numrows = $offset;
		while(($multivol && $currsize + strlen($tabledump) < $sizelimit * 1000 && $numrows == $offset) || (!$multivol && !$tabledumped)) {
			$tabledumped = 1;
			if($multivol) {
				$limitadd = "LIMIT $startfrom, $offset";
				$startfrom += $offset;
			}

			$rows = $db->query("SELECT * FROM $table $limitadd");
			$numfields = $db->num_fields($rows);
			$numrows = $db->num_rows($rows);
			while ($row = $db->fetch_row($rows)) {
				$comma = "";
				$tabledump .= "INSERT INTO $table VALUES(";
				for($i = 0; $i < $numfields; $i++) {
					$tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
					$comma = ",";
				}
				$tabledump .= ");\n";
			}
		}

	}
	$tabledump .= "\n";
	return $tabledump;
}
?>