<?
  include "../../mainfile.php";
?>
<?
  ini_set("max_execution_time",0);
  extract($_POST);
?>
<?
  $tday = getdate();
  $tday = date("dmy");

  $starttime=time();
//  flush();

  $x=$_SERVER[SERVER_SOFTWARE];
  if (strpos($x,"Win32")!=0) {
    $path = $path . "bkpfile\\";
  } else {
    $path = $path . "bkpfile/";
  }

  $fileName = "data";
  $filetype = "txt";
  
  $myfile = $path.$fileName.$tday.".$filetype";

  fopen ($myfile ,"w");
  
//----------------------------------------------------------------------------

  function getTblField($tblname){
  
    $query = "SHOW COLUMNS FROM ".$tblname;
    $result = mysql_query($query);
    $num = mysql_num_rows($result);
    $i=0;
    while($i<$num){
  
      $tfld = mysql_result($result,$i,"Field");
      $arrOutput[$i] = $tfld;
  
      $i++;
    }
	
	return $arrOutput;
	
  }

///---------------
  function get_content($pfleName, $ptable , $pbkpsql, $ptrc) {
    global $filetype, $path, $filen, $tday, $myfile;
	
    if($ptrc=='1'){
	   	$fp = fopen ($myfile,"a");
		fwrite ($fp,"TRUNCATE TABLE " . $ptable . ";\r\n" );
		fclose ($fp);
    }

    $ttblfld = getTblField($ptable);

    $content="";
	$cnt=0;
	$cnt2=0;
	
    $result = mysql_query($pbkpsql);
	$num = mysql_num_rows($result);
	$i = 0;
	while($i<$num){

      $insert = "INSERT INTO $ptable VALUES (";
	  $icol=1;
	  foreach($ttblfld as $fldname){
	    if( $icol<count($ttblfld) ){
	      $coma = ",";
	    }else{
	      $coma = "";
	    }
	  
	    $tresult = str_replace("'","\"",mysql_result($result,$i,$fldname));
	    $insert = $insert . "'$tresult'".$coma;
	  
        $icol++;
	  }

	  $insert = $insert . ");\r\n";
	  $content = $content . $insert;
	  
      $cnt++;
	  $cnt2++;

      if ($cnt==100 || $cnt2==($num)) {

    	$fp = fopen ($myfile,"a");
		if(	fwrite ($fp,$content) ){
          $cnt = 0;
	      $content= '';
		}else{
		}
		fclose ($fp);

      }//if
	  
	  $i++;
	}

  }



//----------------------------------------------------------------------------
  $arrMyTbl = array("xoops_adm_teacher",
                    "xoops_pc_holiday",
					"xoops_pc_punchrecord",
					"xoops_pc_remarkevent",
					"xoops_pc_remarkrecord",
					"xoops_pc_request",
					"xoops_pc_section",
					"xoops_pc_setting",
					"xoops_pc_tempreport",
					"xoops_pc_timetable");



  foreach($arrMyTbl as $ttable){
    get_content("data",$ttable,"SELECT * FROM ".$ttable,"1");
  }
?>
<?
  $_SESSION["dwn"]=1;
  header("Location: main.php?pg=sys&ac=bkp&file=$fileName$tday.$filetype");
?>