📄 database.php
字号:
<?php
/*
[DISCUZ!] admin/database.php - dump, import, optimize, maintance database
This is NOT a freeware, use is subject to license terms
Version: 2.1.0
Author: Crossday (info@discuz.net)
Copyright: Crossday Studio (www.crossday.com)
Last Modified: 2002/12/25 10:00
*/
if(!defined("IN_DISCUZ")) {
exit("Access Denied");
}
include $discuz_root.'./include/attachment.php';
if($action == "export" && $exportsubmit && $type) {
$db->query("SET SQL_QUOTE_SHOW_CREATE = 0");
$sqldump = "";
$time = gmdate("$dateformat $timeformat", $timestamp + $timeoffset * 3600);
if($type == "all") {
$tables = array('announcements', 'attachments', 'banned', 'buddys', 'favorites', 'forumlinks', 'forums',
'karmalog', 'members', 'posts', 'searchindex', 'sessions', 'settings', 'smilies',
'stats', 'styles', 'stylevars', 'subscriptions', 'templates', 'threads', 'pm', 'usergroups',
'words');
} elseif($type == "standard") {
$tables = array('announcements', 'attachments', 'banned', 'buddys', 'forumlinks', 'forums', 'karmalog',
'members', 'posts', 'settings', 'smilies', 'stats', 'styles', 'stylevars',
'templates', 'threads', 'usergroups', 'words');
} elseif($type == "majority") {
$tables = array('attachments', 'forumlinks', 'forums', 'members', 'posts', 'settings', 'smilies', 'stats',
'styles', 'stylevars', 'templates', 'threads', 'usergroups');
} elseif($type == "mini") {
$tables = array('announcements', 'banned', 'forumlinks', 'forums', 'members', 'settings', 'smilies', 'stats',
'styles', 'stylevars', 'templates', 'usergroups', 'words');
}
$sqldump = '';
if($multivol) {
if($saveto == 'server') {
$volume = intval($volume) + 1;
$tableid = $tableid ? $tableid - 1 : 0;
$startfrom = intval($startfrom);
for($i = $tableid; $i < count($tables) && strlen($sqldump) < $sizelimit * 1000; $i++) {
$sqldump .= sqldumptable($tablepre.$tables[$i], $startfrom, strlen($sqldump));
$startfrom = 0;
}
$tableid = $i;
} else {
cpheader();
cpmsg("只有备份到服务器才能使用分卷备份功能。");
}
} else {
foreach($tables as $table) {
$sqldump .= sqldumptable($tablepre.$table);
}
}
$dumpfile = substr($filename, 0, strrpos($filename, "."))."-%s".strrchr($filename, ".");
if(trim($sqldump)) {
$dumpversion = strip_tags($version);
$sqldump = "# Identify: ".base64_encode("$timestamp,$dumpversion,$type,$multivol,$volume")."\n".
"#\n".
"# Discuz! Data Dump".($multivol ? " Volume $volume" : NULL)."\n".
"# Version: Discuz! $dumpversion\n".
"# Time: $time\n".
"# Type: $type\n".
"# Tablepre: $tablepre\n".
"#\n".
"# Discuz! Community: http://www.Discuz.net\n".
"# Please visit our website for newest infomation about Discuz!\n".
"# --------------------------------------------------------\n\n\n".
$sqldump;
if($saveto == "local") {
ob_end_clean();
header('Content-Encoding: none');
header('Content-Type: '.(strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE') ? 'application/octetstream' : 'application/octet-stream'));
header('Content-Disposition: '.(strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE') ? 'inline; ' : 'attachment; ').'filename="dz_'.date('ymd').'.sql"');
header('Content-Length: '.strlen($export));
header('Pragma: no-cache');
header('Expires: 0');
echo $sqldump;
discuz_exit();
} elseif($saveto == "server") {
cpheader();
if($filename != "") {
@$fp = fopen(($multivol ? sprintf($dumpfile, $volume) : $filename), "w");
@flock($fp, 3);
if(@!fwrite($fp, $sqldump)) {
@fclose($fp);
cpmsg("数据文件无法保存到服务器,请检查目录属性。");
} elseif($multivol) {
cpmsg("分卷备份:数据文件 #$volume 成功创建,程序将自动继续。", "admincp.php?action=export&type=$type&saveto=server&filename=$filename&multivol=1&sizelimit=$sizelimit&volume=$volume&tableid=$tableid&startfrom=$startrow&exportsubmit=yes");
} else {
cpmsg("数据成功备份至服务器 <a href=\"$filename\">$filename</a> 中。");
}
} else {
cpmsg("您没有输入备份文件名,请返回修改。");
}
}
} else {
if($multivol) {
$volume--;
$filelist = "<ul>";
for($i = 1; $i <= $volume; $i++) {
$filename = sprintf($dumpfile, $i);
$filelist .= "<li><a href=\"$filename\">$filename\n";
}
cpheader();
cpmsg("恭喜您,全部 $volume 个备份文件成功创建,备份完成。\n<br>$filelist");
} else {
cpheader();
cpmsg("备份出错,数据表没有内容。");
}
}
}
cpheader();
if($action == "export") {
if(!$exportsubmit) {
?>
<table cellspacing="0" cellpadding="0" border="0" width="90%" align="center">
<tr><td bgcolor="<?=BORDERCOLOR?>">
<table border="0" cellspacing="<?=BORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="100%">
<tr class="header"><td>特别提示</td></tr>
<tr bgcolor="<?=ALTBG1?>"><td>
<br><ul><li>数据备份功能根据您的选择备份全部论坛贴子和设置数据,导出的数据文件可用“数据恢复”功能或 phpMyAdmin 导入。</ul>
<ul><li>附件的备份只需手工转移 attachments 目录和文件即可,Discuz! 不提供单独备份。</ul>
<ul><li>强烈建议:备份到服务器请使用 .sql 作为扩展名,这将给日后的维护带来很大方便。</ul>
</td></tr></table></td></tr></table>
<br><br><form name="backup" method="post" action="admincp.php?action=export">
<table cellspacing="0" cellpadding="0" border="0" width="90%" align="center">
<tr><td bgcolor="<?=BORDERCOLOR?>">
<table border="0" cellspacing="<?=BORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="100%">
<tr class="header"><td colspan="2">数据备份方式</td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>" width="40%"><input type="radio" value="all" name="type"> 全部备份</td>
<td bgcolor="<?=ALTBG2?>" width="60%">包括论坛全部数据表数据(模板和附件文件除外)</td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="standard" checked name="type"> 标准备份(推荐)</td>
<td bgcolor="<?=ALTBG2?>">包括常用的全部数据表数据</td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="majority" name="type"> 精简备份</td>
<td bgcolor="<?=ALTBG2?>">仅包括用户、板块设置及贴子数据</td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="mini" name="type" > 最小备份</td>
<td bgcolor="<?=ALTBG2?>">仅包括用户、板块设置及系统设置数据</td></tr>
<tr bgcolor="<?=ALTBG2?>" class="header"><td colspan="2">选择目标位置</td></tr>
<tr bgcolor="<?=ALTBG2?>">
<td colspan="2"><input type="radio" value="local" name="saveto" onclick="this.form.filename.disabled=this.checked;if(this.form.multivol.checked) {alert('注意:\n\n备份到本地无法使用分卷备份功能。');this.form.multivol.checked=false;this.form.sizelimit.disabled=true;}"> 备份到本地</td></tr>
<tr bgcolor="<?=ALTBG2?>"><td><input type="radio" value="server" checked name="saveto" onclick="this.form.filename.disabled=!this.checked"> 备份到服务器</td>
<td><input type="text" size="40" name="filename" value="./forumdata/dz_<?=date('md').'_'.random(5)?>.sql" onclick="alert('注意:\n\n数据文件保存在服务器的可见目录下,其他人有 \n可能下载得到这些文件,这是不安全的。因此请 \n在使用随机文件名的同时,及时删除备份文件。');"></td>
</tr>
<tr class="header"><td colspan="2">使用分卷备份</td></tr>
<tr bgcolor="<?=ALTBG2?>">
<td><input type="checkbox" name="multivol" value="1" onclick="this.form.sizelimit.disabled=!this.checked;if(this.checked && this.form.saveto[1].checked!=true) {alert('注意:\n\n只有选择备份到服务器才能使用分卷备份功能。');this.form.saveto[1].checked=true;this.form.filename.disabled=false;}"> 文件长度限制(KB)</td>
<td><input type="text" size="40" name="sizelimit" value="1024" disabled></td>
</tr></table></td></tr></table><br><center>
<input type="submit" name="exportsubmit" value="备份数据"></center></form>
<?
}
} elseif($action == 'import') {
if(!$importsubmit && !$deletesubmit) {
$exportlog = array();
if(is_dir($discuz_root.'./forumdata')) {
$dir = dir($discuz_root.'./forumdata');
while($entry = $dir->read()) {
$entry = "./forumdata/$entry";
if (is_file($entry) && strtolower(strrchr($entry, ".")) == ".sql") {
$filesize = filesize($entry);
$fp = fopen($entry, "r");
$identify = explode(",", base64_decode(preg_replace("/^# Identify:\s*(\w+).*/s", "\\1", fgets($fp, 256))));
fclose ($fp);
$exportlog[$identify[0]] = array( "version" => $identify[1],
"type" => $identify[2],
"multivol" => $identify[3],
"volume" => $identify[4],
"filename" => $entry,
"size" => $filesize);
}
}
$dir->close();
} else {
cpmsg("目录不存在或无法访问,请检查 ./forumdata/ 目录。");
}
krsort($exportlog);
reset($exportlog);
$exportinfo = "";
foreach($exportlog as $dateline => $info) {
$info[dateline] = is_int($dateline) ? gmdate("$dateformat $timeformat", $dateline + $timeoffset * 3600) : "未知";
switch($info[type]) {
case all: $info[type] = "全部"; break;
case standard: $info[type] = "标准"; break;
case majority: $info[type] = "精简"; break;
case mini: $info[type] = "最小"; break;
}
$info[size] = sizecount($info[size]);
$info[multivol] = $info[multivol] ? "是" : "否";
$info[volume] = $info[multivol] ? $info[volume] : "";
$exportinfo .= "<tr align=\"center\"><td bgcolor=\"".ALTBG1."\"><input type=\"checkbox\" name=\"delete[]\" value=\"$info[filename]\"></td>\n".
"<td bgcolor=\"".ALTBG2."\"><a href=\"$info[filename]\">".substr(strrchr($info[filename], "/"), 1)."</a></td>\n".
"<td bgcolor=\"".ALTBG1."\">$info[version]</td>\n".
"<td bgcolor=\"".ALTBG2."\">$info[dateline]</td>\n".
"<td bgcolor=\"".ALTBG1."\">$info[type]</td>\n".
"<td bgcolor=\"".ALTBG2."\">$info[size]</td>\n".
"<td bgcolor=\"".ALTBG1."\">$info[multivol]</td>\n".
"<td bgcolor=\"".ALTBG2."\">$info[volume]</td>\n".
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -