<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  //Резервное копирование таблиц базы данных
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang."/".$lang.".php")) {include "../lang/".$lang."/".$lang.".php";}
   else {include "../lang/ru/ru.php";}

  $title = $TEXT['backup-tit'];

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}
?>
<table align="center" class="table" border="0" cellpadding="0" cellspacing="0" width="80%">
<tr>
 <td class="upd"><?php echo $TEXT['backup-help']; ?></td>
</tr>
</table>
<?php
  $exp_path = "../temp/manydhcp-".date("YMd")."-dump-db.sql";
  if(is_file($exp_path)) {unlink($exp_path);}
  $file = fopen($exp_path,"a");
  $tbl_count = 0;
  $tbl_str_count = 0;
  $search = array("\r\n","\n\r","\n");
  $replace = array("<br /> ","<br /> ","<br /> ");

  $program_ver = "## ManyDHCP version ".$TEXT['global-now-version']." ##\r\n";
  fwrite($file, $program_ver, strlen($program_ver));

  $used_db = "USE ".$dbname.";\r\n";
  fwrite($file, $used_db, strlen($used_db));

  $tbls = mysqli_query($dbcnx,"SELECT `TABLE_NAME` FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '".$dbname."'; ");
    while($all = mysqli_fetch_array($tbls))
      {
      $tbl_count = $tbl_count + 1;
      $i = $all['TABLE_NAME'];
      $cc = 0;

             $query_dump2 = "SELECT COUNT(*) AS cnt_tbl_cols FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = '".$dbname."' AND table_name = '".$i."'; ";
             $result_dump2 = mysqli_query($dbcnx,$query_dump2);
              if(!$result_dump2) {puterror($result2['skin'],$TEXT['backup-err']); require_once "../utils/bottom_adm.php"; exit();}
              $result_dump_tmp = mysqli_fetch_array($result_dump2);
              $cc = (int)$result_dump_tmp['cnt_tbl_cols'];
              $k = 0;
              $exp_head = "DELETE FROM ".$i.";\r\n";
              $exp_head2 = "INSERT INTO ".$i." VALUES ";
              fwrite($file, $exp_head, strlen($exp_head));

              $query_dump = "SELECT * FROM $i ";
               $result_dump = mysqli_query($dbcnx,$query_dump);
                if(!$result_dump) {puterror($result2['skin'],$TEXT['backup-err']); require_once "../utils/bottom_adm.php"; exit();}
              while($result_dump_osn = mysqli_fetch_array($result_dump,MYSQLI_NUM))
              {
               $str = "";
               for($k=0; $k < $cc; $k++)
               {
                $str .= "'".$result_dump_osn[$k]."', ";
               }
               $str = str_replace($search, $replace, $str);
               $str_tbl = $exp_head2."(".substr($str,0,-2).");\r\n";
               $tbl_str_count = $tbl_str_count + 1;
               fwrite($file, $str_tbl, strlen($str_tbl));
              }
       }
       fclose($file);
//*************************************
//Информация для лога
$ip = $_SERVER['REMOTE_ADDR'];
$doing = $TEXT['backup-tit'];
$what = $TEXT['backup-log'].$tbl_count.$TEXT['backup-log2'].$tbl_str_count;
$who = $result2['familiya']." ".$result2['name'];
//*************************************
$query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
 if(mysqli_query($dbcnx,$query2))
  {
   echo "<html><head>
         <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=transfer.php?trans=".$exp_path."' />
         </head></html>";
  } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>