<?php
session_start();

$filenm=$_GET['filename'];
if ($filenm=="")
 {
   echo "No file specified";
   header('Location: '.$_SERVER['HTTP_REFERER']);
   exit;
 }

$adminpath=$_SESSION['adminpath'];

if ($filenm!="ALL")
 {
   $file=$adminpath."/".$filenm;

   if ($_GET['delete']==1)
    {
      unlink($file);
      header('Location: '.$_SERVER['HTTP_REFERER']);
      exit;
    }
   if ($filenm=='DELSPLITS')
    {
      if ($dir = opendir($adminpath))
       {
         while (false !== ($file = readdir($dir)))
          {
            $ex = explode(".",$file);
	    $last = count($ex)-1;
	    $filenm=$adminpath."/".$file;
	    $isSplit=substr_count($file,".mysql_");

	    #echo "$file :: $filenm :: $isSplit<br/>\n";
 	    if (($ex[$last]=="sql") && ($file!=".") && ($file!="..") && ($isSplit>0)) {unlink($filenm);}
	  }
       }
      $_SESSION['delsrun']=1;
      header('Location: '.$_SERVER['HTTP_REFERER']);
      exit;
    }

   $timeout=$_GET['timeout'];
   if (($timeout<30) || ($timeout=="")) {$timeout=120;}

   if (file_exists($file))
    {
      $e=explode(".",$filenm);
      $last = count($e)-1;

      $f = fopen($file,"r");
      $cnt=0;
      while (!feof($f))
       {
         $line=fgets($f);
         if ($line=="") {continue;}
         $line=str_replace(", , );",",1,1);",$line); 
         $line=preg_replace("/([\xC2\xC3])([\x80-\xBF])/e","chr(ord('\\1')<<6&0xC0|ord('\\2')&0x3F)",$line);
         $line=str_replace(", ,",",'',",$line);
         $line=str_replace("#IND","0",$line);
         $queries[$cnt]=$line;
         $cnt++;
       }
      fclose($f);
    }
    else
    {
      echo "$file not found";
      exit;
    }

   ## Loop through queries, split to 5 files
   $nlines=ceil($cnt/5);
   $fcnt=1;
   for ($i=0;$i<$cnt;$i++)
    {
      if ((($i%$nlines)==0)||($i==0))
       {
         if ($i!=0) {fclose($f);}
         $newFileNm=$e[0];
         for ($j=1;$j<count($e);$j++)
          {
            $newFileNm.=".".$e[$j];
            if ($j==($last-1)) {$newFileNm.="_".$fcnt;}
          }
	 $newFile=$adminpath."/".$newFileNm;
   	 $fcnt++;

	 #echo $newFile."<br/>\n";
	 $f=fopen($newFile,"w");
       }
      fwrite($f,$queries[$i]);
    }
   fclose($f);
 }
 else
 {
   $maxsize=$_GET['filesize'];
   if (($dir = opendir($adminpath))&&($maxsize!=""))
    {
      while (false !== ($file = readdir($dir)))
       {
         $ex = explode(".",$file);
	 $last = count($ex)-1;
	 $filenm=$adminpath."/".$file;
         $fileTime=filemtime($filenm);
         $fileSize=filesize($filenm);
   
	 #echo "$file :: $fileTime : $loadTime<br/>\n";
	 if ($fileSize<$maxsize) {continue;}

	 $numSplits=ceil($fileSize/$maxsize)+1;
         unset($queries);
	 if (($ex[$last]=="sql") && ($file!=".") && ($file!=".."))
	  {
            $e=explode(".",$file);
	    $last = count($e)-1;

	    #echo "Splitting $filenm <br/>\n";

	    $f = fopen($filenm,"r");
      	    $cnt=0;
	    while (!feof($f))
     	     {
               $line=fgets($f);
	       if ($line=="") {continue;}
	       $line=str_replace(", , );",",1,1);",$line); 
	       $line=preg_replace("/([\xC2\xC3])([\x80-\xBF])/e","chr(ord('\\1')<<6&0xC0|ord('\\2')&0x3F)",$line);
      	       $line=str_replace(", ,",",'',",$line);
	       $line=str_replace("#IND","0",$line);
      	       $queries[$cnt]=$line;
	       $cnt++;
      	     }
	    fclose($f);

	    ## Loop through queries, split to 5 files
	    $nlines=ceil($cnt/$numSplits);
	    $fcnt=1;
	    for ($i=0;$i<$cnt;$i++)
	     {
	       if ((($i%$nlines)==0)||($i==0))
	        {
	      	  if ($i!=0) {fclose($f);}
		  $newFileNm=$e[0];
	      	  for ($j=1;$j<count($e);$j++)
		   {
		     $newFileNm.=".".$e[$j];
		     if ($j==($last-1)) {$newFileNm.="_".$fcnt;}
		   }
		  $newFile=$adminpath."/".$newFileNm;
		  $fcnt++;

		  #echo $newFile."<br/>\n";
		  $f=fopen($newFile,"w");
	        }
	       fwrite($f,$queries[$i]);
	     }
	    fclose($f);
	  }
       }
    }
 }

$_SESSION['fsrun']=1;
header('Location: '.$_SERVER['HTTP_REFERER']);
exit;
?>
