<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[3]!=1)
  jump("jump.php?id=25&jump=main.php");
if(isset($_GET['action']) && $_GET['action']=="backup")
{
  $d=new db(c_sqlhost,c_sqluser,c_sqlpw,c_sqldb);
  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 ".c_sqldb))
        jump("jump.php?id=17&jump=backup.php");
      $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))
          jump("backup.php?action=recover");
        jump("jump.php?id=17&jump=backup.php");
      }
    }
    else
	{
      if(!$_POST['filesize'])
        jump("jump.php?id=18&jump=backup.php");
      if(!$tables=$d->query("show table status from ".c_sqldb))
        jump("jump.php?id=17&jump=backup.php");
      $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
              jump("jump.php?id=17&jump=backup.php");
		    $p++;
            $filename=date("Ymd",time())."_all";
            $sql="";
          }
        }
      }
      if($sql!="")
	  {
        $filename.=("_v".$p.".sql");  
        if(write_file($sql,$filename))
          jump("backup.php?action=recover");
      }
    }
  }
  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))
          jump("backup.php?action=recover");
        jump("jump.php?id=17&jump=backup.php");
      }
    }
    else
    {
      if(!$_POST['filesize'])
	    jump("jump.php?id=18&jump=backup.php");
      $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
            jump("jump.php?id=17&jump=backup.php");
          $p++;
          $filename=date("Ymd",time())."_".$_POST['tablename'];
          $sql="";
        }
      }
      if($sql!="")
      {
	    $filename.=("_v".$p.".sql");  
        if(write_file($sql,$filename))
          jump("backup.php?action=recover");
      }
    }
  }
}
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))
      {
	    $files[]=$filename;
	  }
    }
  }
  closedir($handler);
  $count=count($files);
  $file_dir = dirname(__FILE__);
  for($j=0;$j<$count;$j++)
  {
    $file_name = "backup/".$files[$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);
    }
  }
  jump("jump.php?id=24&jump=main.php");
}
elseif(isset($_GET['action']) && $_GET['action']=="del" && isset($_GET['file']))
{
  $file="backup/".$_GET['file'];
  unlink($file);
  jump("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 ".c_sqldb);
  while($rs1=mysql_fetch_array($rs))
  {
    $table[]=array($rs1['Name']);
  }
  $tpl -> assign("table", $table);
  $tpl -> display("backup.htm");
}
?>