query($sql, 'SILENT');
}
}
$file_next = basename(preg_replace("/_($identify[2])(\..+)$/", "_".($identify[2] + 1)."\\2", $file));
if($identify[2] == 1) {
redirect('分卷数据成功导入数据库,程序将自动导入本次其他的备份.','index.php?job=database&action=resume&sqlfile='.rawurlencode($file_next).'&autoimport=yes');
} elseif($autoimport) {
redirect('数据文件卷号 '.$identify[2].' 成功导入,程序将自动继续。', 'index.php?job=database&action=resume&sqlfile='.rawurlencode($file_next).'&autoimport=yes');
} else {
restats();
redirect('数据成功导入','index.php?job=database&action=filelist');
}
} else {
redirect('数据文件非 btlog 格式或与程序当前版本信息不符,无法导入.','index.php?job=database&action=filelist',5);
}
}
// 备份操作
if ($action == 'dobackup') {
$volume = intval($volume) + 1;
$sqlfilename = $backupdir.'/'.$filename.'_'.$volume.'.sql';
if(!$sqlfilename || preg_match("/(\.)(exe|jsp|asp|asa|htr|stm|shtml|php3|aspx|cgi|fcgi|pl|php|bat)(\.|$)/i", $sqlfilename)) {
redirect('您没有输入备份文件名或文件名中使用了敏感的扩展名.', 'index.php?job=database&action=backup',5);
}
$idstring = '# Identify: '.base64_encode("$timestamp,$IXNA_VERSION,$volume")."\n";
//清除表内临时的数据
//$db->query("TRUNCATE TABLE {$tablepre}searchindex");
$sqlcompat = in_array($sqlcompat, array('MYSQL40', 'MYSQL41')) ? $sqlcompat : '';
$dumpcharset = str_replace('-', '', $charset);
$setnames = intval($addsetnames) || ($db->version() > '4.1' && (!$sqlcompat || $sqlcompat == 'MYSQL41')) ? "SET character_set_connection=".$dumpcharset.", character_set_results=".$dumpcharset.", character_set_client=binary;\n\n" : '';
if($db->version() > '4.1') {
$db->query("SET character_set_connection=$dumpcharset, character_set_results=$dumpcharset, character_set_client=binary;");
if($sqlcompat == 'MYSQL40') {
$db->query("SET SQL_MODE='MYSQL40'");
}
}
$sqldump = '';
$tableid = $tableid ? $tableid - 1 : 0;
$startfrom = intval($startfrom);
for($i = $tableid; $i < count($tables) && strlen($sqldump) < $sizelimit * 1000; $i++) {
$sqldump .= sqldumptable($tables[$i], $startfrom, strlen($sqldump));
$startfrom = 0;
}
$tableid = $i;
if(trim($sqldump)) {
$sqldump = "$idstring".
"# \n".
"# Time: ".sadate('Y-m-d H:i',$timestamp)."\n".
"# --------------------------------------------------------\n\n\n".$setnames.$sqldump;
@$fp = fopen($sqlfilename, 'wb');
@flock($fp, 2);
if(@!fwrite($fp, $sqldump)) {
@fclose($fp);
redirect('数据文件无法备份到服务器, 请检查目录属性.', 'index.php?job=database&action=backup',5);
} else {
redirect('分卷备份:数据文件 '.$volume.' 成功创建,程序将自动继续.', "index.php?job=database&action=dobackup&filename=".rawurlencode($filename)."&sizelimit=".rawurlencode($sizelimit)."&volume=".rawurlencode($volume)."&tableid=".rawurlencode($tableid)."&startfrom=".rawurlencode($startrow)."&extendins=".rawurlencode($extendins)."&sqlcompat=".rawurlencode($sqlcompat));
}
} else {
redirect('数据成功备份至服务器指定文件中', 'index.php?job=database&action=filelist');
}
}// 备份操作结束
function sqldumptable($table, $startfrom = 0, $currsize = 0) {
global $db, $sizelimit, $startrow, $extendins, $sqlcompat, $dumpcharset;
$offset = 300;
$tabledump = '';
if(!$startfrom) {
$tabledump = "DROP TABLE IF EXISTS $table;\n";
$createtable = $db->query("SHOW CREATE TABLE $table");
$create = $db->fetch_row($createtable);
$tabledump .= $create[1];
if($sqlcompat == 'MYSQL41' && $db->version() < '4.1') {
$tabledump = preg_replace("/TYPE\=(.+)/", "ENGINE=\\1 DEFAULT CHARSET=".$dumpcharset, $tabledump);
}
if($db->version() > '4.1' && $dumpcharset) {
$tabledump = preg_replace("/(DEFAULT)*\s*CHARSET=.+/", "DEFAULT CHARSET=".$dumpcharset, $tabledump);
}
$query = $db->query("SHOW TABLE STATUS LIKE '$table'");
$tablestatus = $db->row($query);
$tabledump .= ($tablestatus['Auto_increment'] ? " AUTO_INCREMENT=$tablestatus[Auto_increment]" : '').";\n\n";
if($sqlcompat == 'MYSQL40' && $db->version() >= '4.1') {
if($tablestatus['Auto_increment'] <> '') {
$temppos = strpos($tabledump, ',');
$tabledump = substr($tabledump, 0, $temppos).' auto_increment'.substr($tabledump, $temppos);
}
}
}
$tabledumped = 0;
$numrows = $offset;
if($extendins == '0') {
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);
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";
}
$startfrom += $offset;
}
} else {
while($currsize + strlen($tabledump) < $sizelimit * 1000 && $numrows == $offset) {
$tabledumped = 1;
$rows = $db->query("SELECT * FROM $table LIMIT $startfrom, $offset");
$numfields = $db->num_fields($rows);
if($numrows = $db->num($rows)) {
$tabledump .= "INSERT INTO $table VALUES";
$commas = '';
while($row = $db->fetch_row($rows)) {
$comma = '';
$tabledump .= $commas." (";
for($i = 0; $i < $numfields; $i++) {
$tabledump .= $comma.'\''.mysql_escape_string($row[$i]).'\'';
$comma = ',';
}
$tabledump .= ')';
$commas = ',';
}
$tabledump .= ";\n";
}
$startfrom += $offset;
}
}
$startrow = $startfrom;
$tabledump .= "\n";
return $tabledump;
}
//批量删除备份文件
if($action == 'deldbfile') {
if (!$sqlfile || !is_array($sqlfile)) {
redirect('未选择任何文件');
}
$selected = count($sqlfile);
$succ = $fail = 0;
foreach ($sqlfile AS $file=>$value) {
if (file_exists($file)) {
@chmod($file, 0777);
if (@unlink($file)) {
$succ++;
} else {
$fail++;
}
} else {
redirect(basename($file).' 文件已不存在', 'index.php?job=database&action=filelist');
}
}
redirect('删除数据文件操作完毕,删除'.$selected.'个,成功'.$succ.'个,失败'.$fail.'个.', 'index.php?job=database&action=filelist',5);
}
// 数据库维护操作
if($action == 'dotools') {
$doname = array(
'check' => '检查',
'repair' => '修复',
'analyze' => '分析',
'optimize' => '优化'
);
$dodb = $tabledb = array();
foreach ($do AS $value) {
$dodb[] = array('do'=>$value,'name'=>$doname[$value]);
foreach ($tables AS $table) {
if ($db->query($value.' TABLE '.$table)) {
$result = '成功';
} else {
$result = '失败';
}
$tabledb[] = array('do'=>$value,'table'=>$table,'result'=>$result);
}
}
$subnav = '数据库维护';
}// 数据库维护操作结束
// 获取文件大小
function sizecount($filesize) {
if($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' G';
} elseif($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' M';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' K';
} else {
$filesize = $filesize . ' bytes';
}
return $filesize;
}
function splitsql($sql) {
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";\n", trim($sql));
unset($sql);
foreach($queriesarray as $query) {
$queries = explode("\n", trim($query));
foreach($queries as $query) {
$ret[$num] .= $query[0] == "#" ? NULL : $query;
}
$num++;
}
return($ret);
}
function random($length) {
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
if(!$action) {
$action = 'mysqlinfo';
}
if (in_array($action, array('backup', 'tools'))) {
if ($action == 'backup') {
$backuppath = sadate('Y-m-d',$timestamp).'_'.random(8);
$tdtitle = '备份数据库';
$act = 'dobackup';
} else {
$tdtitle = '数据库维护';
$act = 'dotools';
}
$subnav = ''.$tdtitle;
}//backup
// 数据库信息
if ($action == 'mysqlinfo') {
$mysql_version = mysql_get_server_info();
$mysql_runtime = '';
$query = $db->query("SHOW STATUS");
while ($r = $db->row($query)) {
if (eregi("^uptime", $r['Variable_name'])){
$mysql_runtime = $r['Value'];
}
}
$mysql_runtime = format_timespan($mysql_runtime);
$query = $db->query("SHOW TABLE STATUS");
$ixna_table_num = $ixna_table_rows = $ixna_data_size = $ixna_index_size = $ixna_free_size = 0;
$other_table_num = $other_table_rows = $other_data_size = $other_index_size = $other_free_size = 0;
$ixna_table = $other_table = array();
while($table = $db->row($query)) {
if(in_array($table['Name'],$tables)) {
$ixna_data_size = $ixna_data_size + $table['Data_length'];
$ixna_index_size = $ixna_index_size + $table['Index_length'];
$ixna_table_rows = $ixna_table_rows + $table['Rows'];
$ixna_free_size = $ixna_free_size + $table['Data_free'];
$table['Data_length'] = get_real_size($table['Data_length']);
$table['Index_length'] = get_real_size($table['Index_length']);
$table['Data_free'] = get_real_size($table['Data_free']);
$ixna_table_num++;
$ixna_table[] = $table;
} else {
$other_data_size = $other_data_size + $table['Data_length'];
$other_index_size = $other_index_size + $table['Index_length'];
$other_table_rows = $other_table_rows + $table['Rows'];
$other_free_size = $other_free_size + $table['Data_free'];
$table['Data_length'] = get_real_size($table['Data_length']);
$table['Index_length'] = get_real_size($table['Index_length']);
$table['Data_free'] = get_real_size($table['Data_free']);
$other_table_num++;
$other_table[] = $table;
}
}
$ixna_data_size = get_real_size($ixna_data_size);
$ixna_index_size = get_real_size($ixna_index_size);
$ixna_free_size = get_real_size($ixna_free_size);
$other_data_size = get_real_size($other_data_size);
$other_index_size = get_real_size($other_index_size);
$other_free_size = get_real_size($other_free_size);
unset($table);
$subnav = '数据库信息';
}
// 管理数据文件
if ($action == 'filelist') {
$file_i = 0;
if(is_dir($backupdir)) {
$dirs = dir($backupdir);
$dbfiles = array();
$today = @sadate('Y-m-d',$timestamp);
while ($file = $dirs->read()) {
$filepath = $backupdir.'/'.$file;
$pathinfo = pathinfo($file);
if(is_file($filepath) && $pathinfo['extension'] == 'sql') {
$fp = fopen($filepath, 'rb');
$identify = explode(',', base64_decode(preg_replace("/^# Identify:\s*(\w+).*/s", "\\1", fgets($fp, 200))));
fclose($fp);
$moday = @sadate('Y-m-d',@filemtime($filepath));
$mtime = @sadate('Y-m-d H:i',@filemtime($filepath));
$dbfile = array(
'filesize' => sizecount(filesize($filepath)),
'mtime' => ($moday == $today) ? ''.$mtime.'' : $mtime,
'bktime' => $identify[0] ? @sadate('Y-m-d H:i',$identify[0]) : '未知',
'version' => $identify[1] ? $identify[1] : '未知',
'volume' => $identify[2] ? $identify[2] : '未知',
'filepath' => urlencode($file),
'filename' => htmlspecialchars($file),
);
$file_i++;
$dbfiles[] = $dbfile;
}
}
unset($dbfile);
$dirs->close();
$noexists = 0;
} else {
$noexists = 1;
}
$subnav = '数据文件管理';
} // end filelist
if ($action == 'checkresume') {
$subnav = '导入备份数据';
$sqlfile = htmlspecialchars($sqlfile);
$fp = fopen($backupdir.'/'.$sqlfile, 'rb');
$identify = explode(',', base64_decode(preg_replace("/^# Identify:\s*(\w+).*/s", "\\1", fgets($fp, 200))));
fclose($fp);
if (count($identify) != 3) {
redirect('SQL文件有可能是当前程序的老版本备份的,为了程序程序正常运作,不允许导入.确实要导入,请通过其他MYSQL管理程序导入.', 'index.php?job=database&action=filelist',5);
}
if ($identify[2] != 1) {
redirect('为了程序正常运作,只允许导入卷号为1的SQL文件.', 'index.php?job=database&action=filelist',5);
}
if ($identify[1] != $IXNA_VERSION) {
redirect('SQL文件版本信息和当前程序版本不匹配,为了程序程序正常运作,不允许导入.确实要导入,请通过其他MYSQL管理程序导入.', 'index.php?job=database&action=filelist',5);
}
}//backup
$navlink_L = ' » 数据管理'.($subnav ? ' » '.$subnav : '');
cpheader();
include template("database", _ROOT . "/template/admin/");
?>