version() > '4.1' ? 'Engine' : 'Type';
$tablepre = $_G['config']['db'][1]['tablepre'];
$dbcharset = $_G['config']['db'][1]['dbcharset'];
require_once libfile('function/attachment');
cpheader();
if(!isfounder()) cpmsg('noaccess_isfounder', '', 'error');
$excepttables = array($tablepre.'common_admincp_session', $tablepre.'common_syscache', $tablepre.'common_failedlogin', $tablepre.'forum_rsscache', $tablepre.'common_searchindex', $tablepre.'forum_spacecache', $tablepre.'common_session');
$backupdir = C::t('common_setting')->fetch('backupdir');
if(!$backupdir) {
$backupdir = random(6);
@mkdir('./data/backup_'.$backupdir, 0777);
C::t('common_setting')->update('backupdir',$backupdir);
}
$backupdir = 'backup_'.$backupdir;
if(!is_dir('./data/'.$backupdir)) {
mkdir('./data/'.$backupdir, 0777);
}
if($operation == 'export') {
$_SERVER['REQUEST_METHOD'] = 'POST';
if(!submitcheck('exportsubmit')) {
$shelldisabled = function_exists('shell_exec') ? '' : 'disabled';
$tables = '';
$dztables = array();
$tables = C::t('common_setting')->fetch('custombackup', true);
$discuz_tables = fetchtablelist($tablepre);
foreach($discuz_tables as $table) {
$dztables[$table['Name']] = $table['Name'];
}
$defaultfilename = date('ymd').'_'.random(8);
include DISCUZ_ROOT.'./config/config_ucenter.php';
$uc_tablepre = explode('.', UC_DBTABLEPRE);
$uc_tablepre = $uc_tablepre[1] ? $uc_tablepre[1] : $uc_tablepre[0];
$uc_tablepre = substr($uc_tablepre, '0', '-8');
if(UC_CONNECT == 'mysql' && UC_DBHOST == $_G['config']['db'][1]['dbhost'] && UC_DBNAME == $_G['config']['db'][1]['dbname'] && $uc_tablepre == $tablepre) {
$db_export = 'db_export_discuz_uc';
$db_export_key = 'discuz_uc';
$db_export_tips = cplang('db_export_tips_uc', array('uc_backup_url' => $uc_backup_url)).cplang('db_export_tips');
$db_export_discuz_table = cplang('db_export_discuz_table_uc');
} else {
$db_export = 'db_export_discuz';
$db_export_key = 'discuz';
$uc_backup_url = UC_API.'/admin.php?m=db&a=ls&iframe=1';
$db_export_tips = cplang('db_export_tips_nouc', array('uc_backup_url' => $uc_backup_url)).cplang('db_export_tips');
$db_export_discuz_table = cplang('db_export_discuz_table');
}
shownav('founder', 'nav_db', 'nav_db_export');
showsubmenu('nav_db', array(
array('nav_db_export', 'db&operation=export', 1),
array('nav_db_import', 'db&operation=import', 0),
array('nav_db_runquery', 'db&operation=runquery', 0),
array('nav_db_optimize', 'db&operation=optimize', 0),
array('nav_db_dbcheck', 'db&operation=dbcheck', 0)
));
showtips($db_export_tips);
showformheader('db&operation=export&setup=1');
showtableheader();
showsetting('db_export_type', array('type', array(
array($db_export_key, $lang[$db_export], array('showtables' => 'none')),
array('custom', $lang['db_export_custom'], array('showtables' => ''))
)), $db_export_key, 'mradio');
showtagheader('tbody', 'showtables');
showtablerow('', '', '';
showtablerow('', 'colspan="2"', mcheckbox('customtables', $dztables));
showtagfooter('tbody');
showtagheader('tbody', 'advanceoption');
showsetting('db_export_method', '', '', '
');
showtitle('db_export_options');
showsetting('db_export_options_extended_insert', 'extendins', 0, 'radio');
showsetting('db_export_options_sql_compatible', array('sqlcompat', array(
array('', $lang['default']),
array('MYSQL40', 'MySQL 3.23/4.0.x'),
array('MYSQL41', 'MySQL 4.1.x/5.x')
)), '', 'mradio');
showsetting('db_export_options_charset', array('sqlcharset', array(
array('', cplang('default')),
$dbcharset ? array($dbcharset, strtoupper($dbcharset)) : array(),
$db->version() > '4.1' && $dbcharset != 'utf8' ? array('utf8', 'UTF-8') : array()
), TRUE), 0, 'mradio');
showsetting('db_export_usehex', 'usehex', 1, 'radio');
if(function_exists('gzcompress')) {
showsetting('db_export_usezip', array('usezip', array(
array('1', $lang['db_export_zip_1']),
array('2', $lang['db_export_zip_2']),
array('0', $lang['db_export_zip_3'])
)), 0, 'mradio');
}
showsetting('db_export_filename', '', '', '.sql');
showtagfooter('tbody');
showsubmit('exportsubmit', 'submit', '', 'more_options');
showtablefooter();
showformfooter();
} else {
DB::query('SET SQL_QUOTE_SHOW_CREATE=0', 'SILENT');
if(!$_GET['filename'] || !preg_match('/^[\w\_]+$/', $_GET['filename'])) {
cpmsg('database_export_filename_invalid', '', 'error');
}
$time = dgmdate(TIMESTAMP);
if($_GET['type'] == 'discuz' || $_GET['type'] == 'discuz_uc') {
$tables = arraykeys2(fetchtablelist($tablepre), 'Name');
} elseif($_GET['type'] == 'custom') {
$tables = array();
if(empty($_GET['setup'])) {
$tables = C::t('common_setting')->fetch('custombackup', true);
} else {
C::t('common_setting')->update('custombackup', empty($_GET['customtables'])? '' : $_GET['customtables']);
$tables = & $_GET['customtables'];
}
if( !is_array($tables) || empty($tables)) {
cpmsg('database_export_custom_invalid', '', 'error');
}
}
$memberexist = array_search(DB::table('common_member'), $tables);
if($memberexist !== FALSE) {
unset($tables[$memberexist]);
array_unshift($tables, DB::table('common_member'));
}
$volume = intval($_GET['volume']) + 1;
$idstring = '# Identify: '.base64_encode("$_G[timestamp],".$_G['setting']['version'].",{$_GET['type']},{$_GET['method']},{$volume},{$tablepre},{$dbcharset}")."\n";
$dumpcharset = $_GET['sqlcharset'] ? $_GET['sqlcharset'] : str_replace('-', '', $_G['charset']);
$setnames = ($_GET['sqlcharset'] && $db->version() > '4.1' && (!$_GET['sqlcompat'] || $_GET['sqlcompat'] == 'MYSQL41')) ? "SET NAMES '$dumpcharset';\n\n" : '';
if($db->version() > '4.1') {
if($_GET['sqlcharset']) {
DB::query('SET NAMES %s', array($_GET['sqlcharset']));
}
if($_GET['sqlcompat'] == 'MYSQL40') {
DB::query("SET SQL_MODE='MYSQL40'");
} elseif($_GET['sqlcompat'] == 'MYSQL41') {
DB::query("SET SQL_MODE=''");
}
}
$backupfilename = './data/'.$backupdir.'/'.str_replace(array('/', '\\', '.', "'"), '', $_GET['filename']);
if($_GET['usezip']) {
require_once './source/class/class_zip.php';
}
if($_GET['method'] == 'multivol') {
$sqldump = '';
$tableid = intval($_GET['tableid']);
$startfrom = intval($_GET['startfrom']);
if(!$tableid && $volume == 1) {
foreach($tables as $table) {
$sqldump .= sqldumptablestruct($table);
}
}
$complete = TRUE;
for(; $complete && $tableid < count($tables) && strlen($sqldump) + 500 < $_GET['sizelimit'] * 1000; $tableid++) {
$sqldump .= sqldumptable($tables[$tableid], $startfrom, strlen($sqldump));
if($complete) {
$startfrom = 0;
}
}
$dumpfile = $backupfilename."-%s".'.sql';
!$complete && $tableid--;
if(trim($sqldump)) {
$sqldump = "$idstring".
"# \n".
"# Discuz! Multi-Volume Data Dump Vol.$volume\n".
"# Version: Discuz! {$_G[setting][version]}\n".
"# Time: $time\n".
"# Type: {$_GET['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".
"$setnames".
$sqldump;
$dumpfilename = sprintf($dumpfile, $volume);
@$fp = fopen($dumpfilename, 'wb');
@flock($fp, 2);
if(@!fwrite($fp, $sqldump)) {
@fclose($fp);
cpmsg('database_export_file_invalid', '', 'error');
} else {
fclose($fp);
if($_GET['usezip'] == 2) {
$fp = fopen($dumpfilename, "r");
$content = @fread($fp, filesize($dumpfilename));
fclose($fp);
$zip = new zipfile();
$zip->addFile($content, basename($dumpfilename));
$fp = fopen(sprintf($backupfilename."-%s".'.zip', $volume), 'w');
if(@fwrite($fp, $zip->file()) !== FALSE) {
@unlink($dumpfilename);
}
fclose($fp);
}
unset($sqldump, $zip, $content);
cpmsg('database_export_multivol_redirect', "action=db&operation=export&formhash=".formhash()."&type=".rawurlencode($_GET['type'])."&saveto=server&filename=".rawurlencode($_GET['filename'])."&method=multivol&sizelimit=".rawurlencode($_GET['sizelimit'])."&volume=".rawurlencode($volume)."&tableid=".rawurlencode($tableid)."&startfrom=".rawurlencode($startrow)."&extendins=".rawurlencode($_GET['extendins'])."&sqlcharset=".rawurlencode($_GET['sqlcharset'])."&sqlcompat=".rawurlencode($_GET['sqlcompat'])."&exportsubmit=yes&usehex={$_GET['usehex']}&usezip={$_GET['usezip']}", 'loading', array('volume' => $volume));
}
} else {
$volume--;
$filelist = '';
cpheader();
if($_GET['usezip'] == 1) {
$zip = new zipfile();
$zipfilename = $backupfilename.'.zip';
$unlinks = array();
for($i = 1; $i <= $volume; $i++) {
$filename = sprintf($dumpfile, $i);
$fp = fopen($filename, "r");
$content = @fread($fp, filesize($filename));
fclose($fp);
$zip->addFile($content, basename($filename));
$unlinks[] = $filename;
$filelist .= "- $filename
\n";
}
$fp = fopen($zipfilename, 'w');
if(@fwrite($fp, $zip->file()) !== FALSE) {
foreach($unlinks as $link) {
@unlink($link);
}
} else {
C::t('common_cache')->insert(array(
'cachekey' => 'db_export',
'cachevalue' => serialize(array('dateline' => $_G['timestamp'])),
'dateline' => $_G['timestamp'],
), false, true);
cpmsg('database_export_multivol_succeed', '', 'succeed', array('volume' => $volume, 'filelist' => $filelist));
}
unset($sqldump, $zip, $content);
fclose($fp);
@touch('./data/'.$backupdir.'/index.htm');
$filename = $zipfilename;
C::t('common_cache')->insert(array(
'cachekey' => 'db_export',
'cachevalue' => serialize(array('dateline' => $_G['timestamp'])),
'dateline' => $_G['timestamp'],
), false, true);
cpmsg('database_export_zip_succeed', '', 'succeed', array('filename' => $filename));
} else {
@touch('./data/'.$backupdir.'/index.htm');
for($i = 1; $i <= $volume; $i++) {
$filename = sprintf($_GET['usezip'] == 2 ? $backupfilename."-%s".'.zip' : $dumpfile, $i);
$filelist .= "- $filename
\n";
}
C::t('common_cache')->insert(array(
'cachekey' => 'db_export',
'cachevalue' => serialize(array('dateline' => $_G['timestamp'])),
'dateline' => $_G['timestamp'],
), false, true);
cpmsg('database_export_multivol_succeed', '', 'succeed', array('volume' => $volume, 'filelist' => $filelist));
}
}
} else {
$tablesstr = '';
foreach($tables as $table) {
$tablesstr .= '"'.$table.'" ';
}
require DISCUZ_ROOT . './config/config_global.php';
list($dbhost, $dbport) = explode(':', $dbhost);
$query = DB::query("SHOW VARIABLES LIKE 'basedir'");
list(, $mysql_base) = DB::fetch($query, DB::$drivertype == 'mysqli' ? MYSQLI_NUM : MYSQL_NUM);
$dumpfile = addslashes(dirname(dirname(__FILE__))).'/'.$backupfilename.'.sql';
@unlink($dumpfile);
$mysqlbin = $mysql_base == '/' ? '' : addslashes($mysql_base).'bin/';
@shell_exec($mysqlbin.'mysqldump --force --quick '.($db->version() > '4.1' ? '--skip-opt --create-options' : '-all').' --add-drop-table'.($_GET['extendins'] == 1 ? ' --extended-insert' : '').''.($db->version() > '4.1' && $_GET['sqlcompat'] == 'MYSQL40' ? ' --compatible=mysql40' : '').' --host="'.$dbhost.($dbport ? (is_numeric($dbport) ? ' --port='.$dbport : ' --socket="'.$dbport.'"') : '').'" --user="'.$dbuser.'" --password="'.$dbpw.'" "'.$dbname.'" '.$tablesstr.' > '.$dumpfile);
if(@file_exists($dumpfile)) {
if($_GET['usezip']) {
require_once libfile('class/zip');
$zip = new zipfile();
$zipfilename = $backupfilename.'.zip';
$fp = fopen($dumpfile, "r");
$content = @fread($fp, filesize($dumpfile));
fclose($fp);
$zip->addFile($idstring."# \n ".$setnames."\n #".$content, basename($dumpfile));
$fp = fopen($zipfilename, 'w');
@fwrite($fp, $zip->file());
fclose($fp);
@unlink($dumpfile);
@touch('./data/'.$backupdir.'/index.htm');
$filename = $backupfilename.'.zip';
unset($sqldump, $zip, $content);
C::t('common_cache')->insert(array(
'cachekey' => 'db_export',
'cachevalue' => serialize(array('dateline' => $_G['timestamp'])),
'dateline' => $_G['timestamp'],
), false, true);
cpmsg('database_export_zip_succeed', '', 'succeed', array('filename' => $filename));
} else {
if(@is_writeable($dumpfile)) {
$fp = fopen($dumpfile, 'rb+');
@fwrite($fp, $idstring."# \n ".$setnames."\n #");
fclose($fp);
}
@touch('./data/'.$backupdir.'/index.htm');
$filename = $backupfilename.'.sql';
C::t('common_cache')->insert(array(
'cachekey' => 'db_export',
'cachevalue' => serialize(array('dateline' => $_G['timestamp'])),
'dateline' => $_G['timestamp'],
), false, true);
cpmsg('database_export_succeed', '', 'succeed', array('filename' => $filename));
}
} else {
cpmsg('database_shell_fail', '', 'error');
}
}
}
} elseif($operation == 'import') {
checkpermission('dbimport');
if(!submitcheck('deletesubmit')) {
$exportlog = $exportsize = $exportziplog = array();
if(is_dir(DISCUZ_ROOT.'./data/'.$backupdir)) {
$dir = dir(DISCUZ_ROOT.'./data/'.$backupdir);
while($entry = $dir->read()) {
$entry = './data/'.$backupdir.'/'.$entry;
if(is_file($entry)) {
if(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);
$key = preg_replace('/^(.+?)(\-\d+)\.sql$/i', '\\1', basename($entry));
$exportlog[$key][$identify[4]] = array(
'version' => $identify[1],
'type' => $identify[2],
'method' => $identify[3],
'volume' => $identify[4],
'filename' => $entry,
'dateline' => filemtime($entry),
'size' => $filesize
);
$exportsize[$key] += $filesize;
} elseif(preg_match("/\.zip$/i", $entry)) {
$filesize = filesize($entry);
$exportziplog[] = array(
'type' => 'zip',
'filename' => $entry,
'size' => filesize($entry),
'dateline' => filemtime($entry)
);
}
}
}
$dir->close();
} else {
cpmsg('database_export_dest_invalid', '', 'error');
}
$restore_url = $_G['siteurl'].'data/restore.php';
shownav('founder', 'nav_db', 'nav_db_import');
showsubmenu('nav_db', array(
array('nav_db_export', 'db&operation=export', 0),
array('nav_db_import', 'db&operation=import', 1),
array('nav_db_runquery', 'db&operation=runquery', 0),
array('nav_db_optimize', 'db&operation=optimize', 0),
array('nav_db_dbcheck', 'db&operation=dbcheck', 0)
));
showtips('db_import_tips');
showtableheader('db_import');
showtablerow('', array('colspan="9" class="tipsblock"'), array(cplang('do_import_option', array('restore_url' => $restore_url))));
showformheader('db&operation=import');
showtitle('db_export_file');
showsubtitle(array('', 'filename', 'version', 'time', 'type', 'size', 'db_method', 'db_volume', ''));
$datasiteurl = $_G['siteurl'].'data/';
foreach($exportlog as $key => $val) {
$info = $val[1];
$info['dateline'] = is_int($info['dateline']) ? dgmdate($info['dateline']) : $lang['unknown'];
$info['size'] = sizecount($exportsize[$key]);
$info['volume'] = count($val);
$info['method'] = $info['type'] != 'zip' ? ($info['method'] == 'multivol' ? $lang['db_multivol'] : $lang['db_shell']) : '';
$datafile_server = '.'.$info['filename'];
showtablerow('', '', array(
"",
"".$key."",
$info['version'],
$info['dateline'],
$lang['db_export_'.$info['type']],
$info['size'],
$info['method'],
$info['volume'],
$info['type'] == 'zip' ? "$lang[db_import_unzip]" : "$lang[import]"
));
echo '';
foreach($val as $info) {
$info['dateline'] = is_int($info['dateline']) ? dgmdate($info['dateline']) : $lang['unknown'];
$info['size'] = sizecount($info['size']);
showtablerow('', '', array(
'',
"".substr(strrchr($info['filename'], "/"), 1)."",
$info['version'],
$info['dateline'],
'',
$info['size'],
'',
$info['volume'],
''
));
}
echo '';
}
foreach($exportziplog as $info) {
$info['dateline'] = is_int($info['dateline']) ? dgmdate($info['dateline']) : $lang['unknown'];
$info['size'] = sizecount($info['size']);
$info['method'] = $info['method'] == 'multivol' ? $lang['db_multivol'] : $lang['db_zip'];
$datafile_server = '.'.$info['filename'];
showtablerow('', '', array(
"",
"".substr(strrchr($info['filename'], "/"), 1)."",
'',
$info['dateline'],
$lang['db_export_'.$info['type']],
$info['size'],
$info['method'],
'',
"$lang[db_import_unzip]"
));
}
showsubmit('deletesubmit', 'submit', 'del');
showformfooter();
showtablefooter();
} else {
if(is_array($_GET['delete'])) {
foreach($_GET['delete'] as $filename) {
$file_path = './data/'.$backupdir.'/'.str_replace(array('/', '\\'), '', $filename);
if(is_file($file_path)) {
@unlink($file_path);
} else {
$i = 1;
while(1) {
$file_path = './data/'.$backupdir.'/'.str_replace(array('/', '\\'), '', $filename.'-'.$i.'.sql');
if(is_file($file_path)) {
@unlink($file_path);
$i++;
} else {
break;
}
}
}
}
cpmsg('database_file_delete_succeed', '', 'succeed');
} else {
cpmsg('database_file_delete_invalid', '', 'error');
}
}
} elseif($operation == 'runquery') {
$checkperm = checkpermission('runquery', 0);
$runquerys = array();
include_once(DISCUZ_ROOT.'source/admincp/admincp_quickquery.php');
if(!submitcheck('sqlsubmit')) {
$runqueryselect = '';
foreach($simplequeries as $key => $query) {
if(empty($query['sql'])) {
$runqueryselect .= "