📄 database.inc.php
字号:
<?php
/*
[DISCUZ!] admin/database.inc.php - dump, import, optimize, maintance database
This is NOT a freeware, use is subject to license terms
Version: 4.0.0
Web: http://www.comsenz.com
Copyright: 2001-2005 Comsenz Technology Ltd.
Last Modified: 2005-3-8 12:31
*/
if(!defined('IN_DISCUZ') || !isset($PHP_SELF) || !preg_match("/[\/\\\\]admincp\.php$/", $PHP_SELF)) {
exit('Access Denied');
}
require_once DISCUZ_ROOT.'./include/attachment.func.php';
cpheader();
if($action == 'export') {
if(!submitcheck('exportsubmit', 1)) {
$shelldisabled = function_exists('shell_exec') ? '' : 'disabled';
?>
<table cellspacing="<?=INNERBORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="85%" align="center" class="tableborder">
<tr class="header"><td><?=$lang['tips']?></td></tr>
<tr bgcolor="<?=ALTBG1?>"><td>
<br><?=$lang['database_export_tips']?>
</td></tr></table>
<br><br><form name="backup" method="post" action="admincp.php?action=export">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table cellspacing="<?=INNERBORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="85%" align="center" class="tableborder">
<tr class="header"><td colspan="2"><?=$lang['database_export_type']?></td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>" width="40%"><input type="radio" value="full" name="type"> <?=$lang['database_export_full']?></td>
<td bgcolor="<?=ALTBG2?>" width="60%"><?=$lang['database_export_full_comment']?></td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="standard" checked name="type"> <?=$lang['database_export_standard']?></td>
<td bgcolor="<?=ALTBG2?>"><?=$lang['database_export_standard_comment']?></td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="mini" name="type" > <?=$lang['database_export_mini']?></td>
<td bgcolor="<?=ALTBG2?>"><?=$lang['database_export_mini_comment']?></td></tr>
<tr class="header"><td colspan="2"><?=$lang['database_export_method']?></td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" name="method" value="shell" $shelldisabled> <?=$lang['database_export_shell']?></td>
<td bgcolor="<?=ALTBG2?>"> </td>
</tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" name="method" value="multivol" checked> <?=$lang['database_export_multivol']?></td>
<td bgcolor="<?=ALTBG2?>"><input type="text" size="40" name="sizelimit" value="2048"></td>
</tr>
<tr bgcolor="<?=ALTBG2?>">
<td colspan="2" class="singleborder"> </td>
</tr>
<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" checked> <?=$lang['database_export_filename']?></td>
<td bgcolor="<?=ALTBG2?>"><input type="text" size="40" name="filename" value="./forumdata/<?=date('md').'_'.random(8)?>.sql" onclick="alert('<?=$lang['database_export_filename_confirm']?>');"></td>
</tr>
</table><br><center>
<input type="submit" name="exportsubmit" value="<?=$lang['submit']?>"></center></form>
<?
} else {
if(!$filename || preg_match("/(\.)(exe|jsp|asp|aspx|cgi|fcgi|pl)(\.|$)/i", $filename)) {
cpmsg('database_export_filename_invalid');
}
$time = gmdate("$dateformat $timeformat", $timestamp + $timeoffset * 3600);
if($type == 'full') {
$tables = array('advertisements', 'orders', 'access', 'adminactions', 'admingroups', 'adminnotes', 'adminsessions', 'announcements',
'attachments', 'attachtypes', 'banned', 'bbcodes', 'blogcaches', 'buddys', 'creditslog', 'failedlogins',
'favorites', 'forumfields', 'forumlinks', 'forums', 'medals', 'memberfields', 'members', 'moderators',
'onlinelist', 'onlinetime', 'paymentlog', 'plugins', 'pluginvars', 'pms', 'polls', 'posts', 'profilefields',
'ranks', 'ratelog', 'regips', 'rsscaches', 'searchindex', 'sessions', 'settings', 'smilies', 'stats',
'statvars', 'styles', 'stylevars', 'subscriptions', 'templates', 'threads', 'threadsmod', 'threadtypes',
'usergroups', 'validating', 'words');
} elseif($type == 'standard') {
$tables = array('advertisements', 'orders', 'access', 'adminactions', 'admingroups', 'adminnotes', 'adminsessions', 'announcements', 'attachments',
'attachtypes', 'banned', 'bbcodes', 'buddys', 'creditslog', 'favorites', 'forumfields', 'forumlinks', 'forums',
'medals', 'memberfields', 'members', 'moderators', 'onlinelist', 'onlinetime', 'paymentlog', 'plugins',
'pluginvars', 'polls', 'posts', 'profilefields', 'ranks', 'ratelog', 'settings', 'smilies', 'stats', 'styles',
'stylevars', 'templates', 'threads', 'threadsmod', 'threadtypes', 'usergroups', 'validating', 'words');
} elseif($type == 'mini') {
$tables = array('access', 'adminactions', 'admingroups', 'adminsessions', 'announcements', 'attachtypes', 'bbcodes',
'buddys', 'forumfields', 'forumlinks', 'forums', 'medals', 'memberfields', 'members', 'moderators', 'onlinelist',
'onlinetime', 'plugins', 'pluginvars', 'profilefields', 'ranks', 'settings', 'smilies', 'stats', 'styles',
'stylevars', 'templates', 'threadtypes', 'usergroups', 'words');
}
if($type == 'full' || $type == 'standard') {
$query = $db->query("SELECT datatables FROM {$tablepre}plugins WHERE datatables<>''");
while($plugin = $db->fetch_array($query)) {
foreach(explode(',', $plugin['tables']) as $table) {
if($table = trim($table)) {
$tables[] = $table;
}
}
}
}
$volume = intval($volume) + 1;
$idstring = '# Identify: '.base64_encode("$timestamp,$version,$type,$method,$volume")."\n";
if($method == 'multivol') {
$sqldump = '';
$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;
$dumpfile = substr($filename, 0, strrpos($filename, '.'))."-%s".strrchr($filename, '.');
if(trim($sqldump)) {
$sqldump = "$idstring".
"# <?exit();?>\n".
"# Discuz! Multi-Volume Data Dump Vol.$volume\n".
"# Version: Discuz! $version\n".
"# Time: $time\n".
"# Type: $type\n".
"# Table Prefix: $tablepre\n".
"#\n".
"# Discuz! Home: http://www.discuz.com\n".
"# Please visit our website for newest infomation about Discuz!\n".
"# --------------------------------------------------------\n\n\n".
$sqldump;
@$fp = fopen(($method == 'multivol' ? sprintf($dumpfile, $volume) : $filename), 'wb');
@flock($fp, 2);
if(@!fwrite($fp, $sqldump)) {
@fclose($fp);
cpmsg('database_export_file_invalid');
} else {
cpmsg('database_export_multivol_redirect', "admincp.php?action=export&type=$type&saveto=server&filename=$filename&method=multivol&sizelimit=$sizelimit&volume=$volume&tableid=$tableid&startfrom=$startrow&exportsubmit=yes");
}
} else {
$volume--;
$filelist = '<ul>';
for($i = 1; $i <= $volume; $i++) {
$filename = sprintf($dumpfile, $i);
$filelist .= "<li><a href=\"$filename\">$filename\n";
}
cpheader();
cpmsg('database_export_multivol_succeed');
}
} else {
$tablesstr = '';
foreach($tables as $table) {
$tablesstr .= '"'.$tablepre.$table.'" ';
}
require './config.inc.php';
list($dbhost, $dbport) = explode(':', $dbhost);
$query = $db->query("SHOW VARIABLES LIKE 'basedir'");
list(, $mysql_base) = $db->fetch_array($query, MYSQL_NUM);
$dumpfile = addslashes(dirname(dirname(__FILE__))).'/'.$filename;
@unlink($dumpfile);
$mysqlbin = $mysql_base == '/' ? '' : addslashes($mysql_base).'bin/';
@shell_exec($mysqlbin.'mysqldump --force --add-drop-table --extended-insert'.($db->version() > '4.1' ? ' --compatible=mysql40' : '').
' -h"'.$dbhost.($dbport ? (is_numeric($dbport) ? ' -P'.$dbport : ' -S"'.$dbport.'"') : '').'" -u"'.$dbuser.'" -p"'.$dbpw.'" "'.$dbname.'" '.$tablesstr.' > '.$dumpfile);
if(@file_exists($dumpfile)) {
if(@is_writeable($dumpfile)) {
$fp = fopen($dumpfile, 'rb+');
fwrite($fp, $idstring.'# <?exit();?>');
fclose($fp);
}
cpmsg('database_export_succeed');
} else {
cpmsg('database_shell_fail');
}
}
}
} elseif($action == 'import') {
if(!submitcheck('importsubmit', 1) && !submitcheck('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) && preg_match("/\.sql/i", $entry)) {
$filesize = filesize($entry);
$fp = fopen($entry, 'rb');
$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],
'method' => $identify[3],
'volume' => $identify[4],
'filename' => $entry,
'size' => $filesize);
}
}
$dir->close();
} else {
cpmsg('database_export_dest_invalid');
}
krsort($exportlog);
reset($exportlog);
$exportinfo = '';
foreach($exportlog as $dateline => $info) {
$info['dateline'] = is_int($dateline) ? gmdate("$dateformat $timeformat", $dateline + $timeoffset * 3600) : $lang['unknown'];
switch($info['type']) {
case 'full':
$info['type'] = $lang['database_export_full'];
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -