<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[3]!=1)
{
  Header("Location: jump.php?id=25&jump=main.php");
  exit();
}
if(isset($_GET['action']) && $_GET['action']=="backup")
{
  $d=new db($core_mysql_host,$core_mysql_user,$core_mysql_pw,$core_mysql_dbname);
  if($_POST['weizhi']=="localpc" && $_POST['fenjuan']=='yes')
    Header("Location: jump.php?id=14&jump=backup.php");
  if(isset($_POST['fenjuan']) && $_POST['fenjuan']=="yes" && !$_POST['filesize'])
    Header("Location: jump.php?id=15&jump=backup.php");
  if($_POST['weizhi']=="server" && !writeable("./backup"))
    Header("Location: jump.php?id=16&jump=backup.php");
  if($_POST['bfzl']=="quanbubiao")
  {
    if(!$_POST['fenjuan'])
	{
      if(!$tables=$d->query("show table status from $core_mysql_dbname"))
	  {
        Header("Location: jump.php?id=17&jump=backup.php");
        exit();
	  }
      $sql="";
      while($d->nextrecord($tables))
      {
        $table=$d->f("Name");
        $sql.=make_header($table);
        $d->query("select * from $table");
        $num_fields=$d->nf();
        while($d->nextrecord())
	    {
          $sql.=make_record($table,$num_fields);
	    }
      }
      $filename=date("Ymd",time())."_all.sql";
      if($_POST['weizhi']=="localpc")
	    down_file($sql,$filename);
	  elseif($_POST['weizhi']=="server")
	  {
        if(write_file($sql,$filename))
	    {
          Header("Location: backup.php?action=recover");
          exit();
	    }
        else
	    {
          Header("Location: jump.php?id=17&jump=backup.php");
          exit();
	    }
      }
    }
    else
	{
      if(!$_POST['filesize'])
	  {
        Header("Location: jump.php?id=18&jump=backup.php");
        exit();
	  }
      if(!$tables=$d->query("show table status from $core_mysql_dbname"))
	  {
        Header("Location: jump.php?id=17&jump=backup.php");
        exit();
      }
      $sql="";
	  $p=1;
      $filename=date("Ymd",time())."_all";
      while($d->nextrecord($tables))
	  {
        $table=$d->f("Name");
        $sql.=make_header($table);
        $d->query("select * from $table");
        $num_fields=$d->nf();
        while($d->nextrecord())
		{
          $sql.=make_record($table,$num_fields);
          if(strlen($sql)>=$_POST['filesize']*1000)
		  {
            $filename.=("_v".$p.".sql");
            if(write_file($sql,$filename))
		      Header("Location: backup.php?action=recover");
			else
			{
              Header("Location: jump.php?id=17&jump=backup.php");
              exit();
            }
		    $p++;
            $filename=date("Ymd",time())."_all";
            $sql="";
          }
        }
      }
      if($sql!="")
	  {
        $filename.=("_v".$p.".sql");  
        if(write_file($sql,$filename))
		{
          Header("Location: backup.php?action=recover");
          exit();
        }
      }
    }
  }
  elseif($_POST['bfzl']=="danbiao")
  {
    if(!$_POST['fenjuan'])
    {
      $sql=make_header($_POST['tablename']);
      $d->query("select * from ".$_POST['tablename']);
      $num_fields=$d->nf();
      while($d->nextrecord())
	  {
        $sql.=make_record($_POST['tablename'],$num_fields);
	  }
      $filename=date("Ymd",time())."_".$_POST['tablename'].".sql";
      if($_POST['weizhi']=="localpc")
	    down_file($sql,$filename);
	  elseif($_POST['weizhi']=="server")
	  {
        if(write_file($sql,$filename))
        {
          Header("Location: backup.php?action=recover");
          exit();
        }
        else
        {
          Header("Location: jump.php?id=17&jump=backup.php");
          exit();
        }
      }
    }
    else
    {
      if(!$_POST['filesize'])
      {
	    Header("Location: jump.php?id=18&jump=backup.php");
        exit();
      }
      $sql=make_header($_POST['tablename']); $p=1; 
      $filename=date("Ymd",time())."_".$_POST['tablename'];
      $d->query("select * from ".$_POST['tablename']);
      $num_fields=$d->nf();
      while ($d->nextrecord())
      {  
        $sql.=make_record($_POST['tablename'],$num_fields);
        if(strlen($sql)>=$_POST['filesize']*1000)
        {
          $filename.=("_v".$p.".sql");
          if(write_file($sql,$filename))
          {
            Header("Location: backup.php?action=recover");
		  }
          else
          {
            Header("Location: jump.php?id=17&jump=backup.php");
            exit();
		  }
          $p++;
          $filename=date("Ymd",time())."_".$_POST['tablename'];
          $sql="";
        }
      }
      if($sql!="")
      {
	    $filename.=("_v".$p.".sql");  
        if(write_file($sql,$filename))
        {
          Header("Location: backup.php?action=recover");
          exit();
        }
      }
    }
  }
}
elseif(isset($_GET['action']) && isset($_GET['file']) && $_GET['action']=="recovers")
{
  $handler = opendir('backup');
  while( ($filename = readdir($handler)) !== false ) 
  {
    if($filename!="." && $filename!="..")
    {
	  if(substr($_GET['file'],0,13)==substr($filename,0,13))
        $file[]=$filename;
    }
  }
  closedir($handler);
  $count=count($file);
  $file_dir = dirname(__FILE__);
  for($j=0;$j<$count;$j++)
  {
    $file_name = "backup/".$file[$j];
    $get_sql_data = file_get_contents($file_name,$file_dir);
    $explode = explode("\n",$get_sql_data);
    $cnt = count($explode);
    for($i=0;$i<$cnt;$i++)
    {
      $sql = $explode[$i];
      $result = mysql_query($sql);
    }
  }
  Header("Location: jump.php?id=24&jump=main.php");
}
elseif(isset($_GET['action']) && $_GET['action']=="del" && isset($_GET['file']))
{
  $file="backup/".$_GET['file'];
  unlink($file);
  Header("Location: jump.php?id=24&jump=backup.php?action=recover");
}
elseif(isset($_GET['action']) && $_GET['action']=="recover")
{
  $handler = opendir('backup');
  while( ($filename = readdir($handler)) !== false ) 
  {
    if($filename!="." && $filename!="..")
      $file[]=$filename;
  }
  closedir($handler);
  $tpl -> assign("file", $file);
  $tpl -> assign("action", $_GET['action']);
  $tpl -> display("backup_r.htm");
}
else
{
  $rs = mysql_query("show table status from ".$core_mysql_dbname);
  while($rs1=mysql_fetch_array($rs))
  {
    $table[]=array($rs1['Name']);
  }
  $tpl -> assign("table", $table);
  $tpl -> display("backup.htm");
}
?>

