<?php
require ('authentication_manager.php');
?>
<?php

//备份数据库
if (POWER != 5) {
  err('该命令必须以基本管理员身份登陆！请重登陆');
}


function filterbr($text) {
  return preg_replace('/[\r\n]+/', '\n', addslashes(stripslashes($text)));
  //return mysql_real_escape_string(stripslashes($text));
}
@ require ('inc/function/deldir.php');

if (!empty($_GET['table'])) {
  if (!in_array($_GET['table'], $sql['data'])) {
    err('数据表名出错！');
  }
  $table_pre = $_GET['table'];
  $array_tables = array($_GET['table']);
} else {
  $table_pre = 'all';
  unset($sql['data']['承载在线统计的表名']);
  $array_tables = $sql['data'];
}


$beifen_dir = ''.$table_pre.'_'.gmdate("YmdHis", time() + (floatval($web["time_pos"]) * 3600));
if (file_exists('inc/sql/'.$beifen_dir)) {
  deldir('inc/sql/'.$beifen_dir);
}
eval('@chmod(\'inc/sql\', 0'.$web['chmod'].');');
if (!@mkdir('inc/sql/'.$beifen_dir)) {
  err('备份目录inc/sql/'.$beifen_dir.'无法建立，可能是写权限不足');
}


if (!isset($sql['db_err'])) {
  db_conn();
}
if ($sql['db_err'] != '') {
  err($sql['db_err']);
}
$sql['sql_version'] = @mysql_get_server_info();


@ require('inc/function/write_file.php');

foreach ($array_tables as $table) {
  write_table_create($table);
}

foreach ($array_tables as $table) {
  write_table_insert($table);
}
@mysql_close();

alert('数据库备份成功！已储存为'.$beifen_dir.'', '?get=sql');

function write_table_insert($table) {
  global $sql, $db, $beifen_dir;
  $text = $line = '';
  $size = 0;
  $step = 0;
  $beifen_each_limit = 512 * 1024; //按此尺寸（KB）备份
  $result = @mysql_query('SELECT * FROM `'.$sql['pref'].''.$table.'`', $db);
  if (@mysql_num_rows($result) > 0) {
    while ($row = @mysql_fetch_assoc($result)) {
	  if (!isset($row_name)) {
	    $row_name = '<?php die(); ?> (`'.@implode("`, `", array_keys($row)).'`)';
      }
      //$row = array_map('filterbr', $row);
      $row = array_map('addslashes', $row);
      $line = "\n('".@implode("','", $row)."')";
	  $size += strlen($line).'  ';
		
	  $text .= $line.",";
	  if ($size >= $beifen_each_limit) {
	    $step++;
        write_file('inc/sql/'.$beifen_dir.'/insert_'.$table.'_'.$step.'.php', $row_name.' VALUES '.preg_replace('/,$/', ';', $text));
        $text = '';
	    $size = 0;
      } else {
      }
      unset($row);
    }
    $step++;
    write_file('inc/sql/'.$beifen_dir.'/insert_'.$table.'_'.$step.'.php', $row_name.' VALUES '.preg_replace('/,$/', ';', $text));
  }
  @mysql_free_result($result);
}

function write_table_create($table) {
  global $sql, $db, $beifen_dir;
  $result = @mysql_query('SELECT MAX(id) AS in_mark FROM `'.$sql['pref'].''.$table.'`', $db);
  $row = @mysql_fetch_assoc($result);
  $li_count = $row['in_mark'] + 1;
  @mysql_free_result($result);
  unset($row);
  $text = '';
  switch ($table) {
    case $sql['data']['承载版区分类的表名'] :
    $text .= '
(
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `area_id` varchar(255) NOT NULL DEFAULT \'\',
  `area_name` varchar(255) NOT NULL DEFAULT \'\',
  `area_admin` varchar(255) NOT NULL DEFAULT \'\',
  `area_power_write` int(10) NOT NULL DEFAULT \'0\',
  `area_power_read` int(10) NOT NULL DEFAULT \'0\',
  `area_power_reply` int(10) NOT NULL DEFAULT \'0\',
  `area_expires` int(10) NOT NULL DEFAULT \'0\',
  `area_check` int(2) NOT NULL DEFAULT \'0\',
  `area_check_reply` int(2) NOT NULL DEFAULT \'0\',
  `area_index` varchar(255) NOT NULL DEFAULT \'\',
  `area_mark` text NOT NULL,
  `area_mode` int(2) NOT NULL DEFAULT \'0\',
  `area_total` int(10) NOT NULL DEFAULT \'0\',
  `area_today` varchar(12) NOT NULL DEFAULT \'\',
  `area_style` varchar(255) NOT NULL DEFAULT \'\',
  `area_keywords` varchar(255) NOT NULL DEFAULT \'\',
  `area_description` varchar(255) NOT NULL DEFAULT \'\',
  PRIMARY KEY (id),
  UNIQUE area_id (area_id)
) '.($sql['sql_version'] > '4.1' ? 'ENGINE=MyISAM AUTO_INCREMENT='.$li_count.' DEFAULT CHARSET='.$sql['char'].'' : 'TYPE=MYISAM').';
';

    break;

    case $sql['data']['承载主题文章的表名'] :
    $text .= '
(
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT \'\',
  `title_mark` varchar(255) NOT NULL DEFAULT \'\',
  `title_style` varchar(64) NOT NULL DEFAULT \'\',
  `text` text NOT NULL,
  `date` timestamp NOT NULL DEFAULT \'0000-00-00 00:00:00\',
  `reply` int(10) NOT NULL DEFAULT \'0\',
  `views` int(10) NOT NULL DEFAULT \'0\',
  `area_id` varchar(32) NOT NULL DEFAULT \'\',
  `author_ip` varchar(64) NOT NULL DEFAULT \'\',
  `topdate` timestamp NOT NULL DEFAULT \'0000-00-00 00:00:00\',
  `area_check` int(2) NOT NULL default \'0\' COMMENT \'0 正常 1 审核中 2 审核未通过 3 已下架\',
  `reward` varchar(20) NOT NULL DEFAULT \'\',
  `other` varchar(255) NOT NULL DEFAULT \'\',
  `zanfan` varchar(16) NOT NULL DEFAULT \'0|0\',
  `city` varchar(255) NOT NULL DEFAULT \'\',
  PRIMARY KEY (id,area_check),
  KEY area_id (area_id),
  KEY topdate (topdate),
  KEY author_ip (author_ip),
  KEY title_mark (title_mark(5))
) '.($sql['sql_version'] > '4.1' ? 'ENGINE=MyISAM AUTO_INCREMENT='.$li_count.' DEFAULT CHARSET='.$sql['char'].'' : 'TYPE=MYISAM').'
/*!50100 PARTITION BY LIST (area_check)
(PARTITION p0 VALUES IN (0),
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2),
PARTITION p3 VALUES IN (3)) */;
';

    break;

    case $sql['data']['承载关键词的表名'] :
    $text .= '
(
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `title` text NOT NULL,
  PRIMARY KEY (id),
  FULLTEXT title (title)
) '.($sql['sql_version'] > '4.1' ? 'ENGINE=MyISAM AUTO_INCREMENT='.$li_count.' DEFAULT CHARSET='.$sql['char'].'' : 'TYPE=MYISAM').';
';

    break;

    case $sql['data']['承载成员档案的表名'] :
    $text .= '
(
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(64) NOT NULL DEFAULT \'\',
  `password` varchar(32) NOT NULL DEFAULT \'\',
  `email` varchar(255) NOT NULL DEFAULT \'\',
  `password_question` int(2) NOT NULL DEFAULT \'0\',
  `password_answer` varchar(32) NOT NULL DEFAULT \'\',
  `power` varchar(255) NOT NULL DEFAULT \'\',
  `point` int(10) NOT NULL DEFAULT \'0\',
  `writecount` int(10) NOT NULL DEFAULT \'0\',
  `replycount` int(10) NOT NULL DEFAULT \'0\',
  `thisdate` timestamp NOT NULL DEFAULT \'0000-00-00 00:00:00\',
  `regdate` timestamp NOT NULL DEFAULT \'0000-00-00 00:00:00\',
  `realname` varchar(64) NOT NULL DEFAULT \'\',
  `sex` varchar(8) NOT NULL DEFAULT \'\',
  `birthday` varchar(16) NOT NULL DEFAULT \'\',
  `handtel` varchar(12) NOT NULL DEFAULT \'\',
  `hometel` varchar(20) NOT NULL DEFAULT \'\',
  `company` varchar(255) NOT NULL DEFAULT \'\',
  `address` varchar(255) NOT NULL DEFAULT \'\',
  `zip` varchar(8) NOT NULL DEFAULT \'\',
  `qq` varchar(16) NOT NULL DEFAULT \'\',
  `sign` varchar(255) NOT NULL DEFAULT \'\',
  `company_description` text NOT NULL,
  `mess` text NOT NULL,
  `mess_num` varchar(16) NOT NULL DEFAULT \'0\',
  `check_reg` int(2) NOT NULL DEFAULT \'0\' COMMENT \'0 正常 1 审核中 2 黑名单\',
  `other` varchar(255) NOT NULL default \'\',
  `login_key_qq` varchar(255) NOT NULL DEFAULT \'\',
  `login_key_weibo` varchar(255) NOT NULL DEFAULT \'\',
  `login_key_baidu` varchar(255) NOT NULL DEFAULT \'\',
  `login_key_162100` varchar(255) NOT NULL DEFAULT \'\',
  `session_key` varchar(64) NOT NULL DEFAULT \'\',
  `stop_login` int(2) NOT NULL DEFAULT \'0\',
  PRIMARY KEY (id,check_reg),
  UNIQUE username_check_reg (username,check_reg),
  UNIQUE email_check_reg (email,check_reg)
) '.($sql['sql_version'] > '4.1' ? 'ENGINE=MyISAM AUTO_INCREMENT='.$li_count.' DEFAULT CHARSET='.$sql['char'].'' : 'TYPE=MYISAM').'
/*!50100 PARTITION BY LIST (check_reg)
(PARTITION p0 VALUES IN (0),
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2)) */;
';

    break;

    case $sql['data']['承载回复评论的表名'] :
    $text .= '
(
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `r_id` int(10) NOT NULL DEFAULT \'0\' COMMENT \'相对于主题ID\',
  `text` text NOT NULL,
  `date` timestamp NOT NULL DEFAULT \'0000-00-00 00:00:00\',
  `author_ip` varchar(64) NOT NULL DEFAULT \'\',
  `area_check_reply` int(2) NOT NULL DEFAULT \'0\' COMMENT \'0 正常 1 审核中 2 审核未通过\',
  `reward` decimal(9,2) NOT NULL DEFAULT \'0\' COMMENT \'获得悬赏\',
  `floor` int(10) NOT NULL DEFAULT \'0\' COMMENT \'楼层\',
  `quote` int(10) NOT NULL DEFAULT \'0\' COMMENT \'引用\',
  PRIMARY KEY (id,area_check_reply),
  KEY r_id (r_id),
  KEY author_ip (author_ip)
) '.($sql['sql_version'] > '4.1' ? 'ENGINE=MyISAM AUTO_INCREMENT='.$li_count.' DEFAULT CHARSET='.$sql['char'].'' : 'TYPE=MYISAM').'
/*!50100 PARTITION BY LIST (area_check_reply)
(PARTITION p0 VALUES IN (0),
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2)) */;
';

    break;

    case $sql['data']['承载财务数据的表名'] :
    $text .= '
(
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(64) NOT NULL DEFAULT \'\',
  `type` int(2) NOT NULL DEFAULT \'0\' COMMENT \'0 funds 1 credit 2 collect\',
  `description` text NOT NULL,
  `date` timestamp NOT NULL DEFAULT \'0000-00-00 00:00:00\',
  `money` decimal(9,2) NOT NULL DEFAULT \'0\',
  `check_credit` int(2) NOT NULL DEFAULT \'0\' COMMENT \'0 正常 1 信誉审核中 2 审核未通过\',
  `other` varchar(255) NOT NULL DEFAULT \'\',
  PRIMARY KEY (id,type),
  KEY username (username)
) '.($sql['sql_version'] > '4.1' ? 'ENGINE=MyISAM AUTO_INCREMENT='.$li_count.' DEFAULT CHARSET='.$sql['char'].'' : 'TYPE=MYISAM').'
/*!50100 PARTITION BY LIST (type)
(PARTITION p0 VALUES IN (0),
PARTITION p1 VALUES IN (1),
PARTITION p2 VALUES IN (2)) */;
';

    break;

    case $sql['data']['承载上传数据的表名'] :
    $text .= '
(
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT \'\',
  `text` longblob NOT NULL,
  `type` varchar(64) NOT NULL DEFAULT \'\',
  `username` varchar(64) NOT NULL DEFAULT \'\',
  `date` timestamp NOT NULL DEFAULT \'0000-00-00 00:00:00\',
  PRIMARY KEY (id),
  UNIQUE title (title)
) '.($sql['sql_version'] > '4.1' ? 'ENGINE=MyISAM AUTO_INCREMENT='.$li_count.' DEFAULT CHARSET='.$sql['char'].'' : 'TYPE=MYISAM').';
';

    break;
  }
  write_file('inc/sql/'.$beifen_dir.'/create_'.$table.'.php', '<?php die(); ?> '.$text);
}


?>