<?php
include('include/config.inc.php');
 
// Mail Function With Attachment Sql From Php MyAdmi :Start
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) 
{
    $handle = fopen($filename, "r");
  $content = fread($handle,filesize($filename));
  fclose($handle);
  $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
 
    $nmessage = "--".$uid."\r\n";
    $nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $nmessage .= $message."\r\n\r\n";
    $nmessage .= "--".$uid."\r\n";
    $nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; 
    $nmessage .= "Content-Transfer-Encoding: base64\r\n";
    $nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $nmessage .= $content."\r\n\r\n";
    $nmessage .= "--".$uid."--";
error_reporting(E_ALL);
    if (mail($mailto, $subject, $nmessage, $header)) {
        echo "Mail Sent Successfully to "; // or use booleans here
    } else {
        echo "Mail NOT Sent to " .$mailto ."<br/>";
    }

}

set_time_limit(0);
 

$backupDir = './backup/'; // full path, must be 777 and passwd-protected
$filePrefix = "New Software";
$backup_file = $filePrefix.'_'.date('Y_m_d_H_i_s').'.sql';
$fp = fopen($backupDir . $backup_file, 'w');
$schema = '# Database Backup' .
          '#' . "\n" .
          '# Backup Date: ' . date('Y-m-d H:i:s') . "\n\n";
fputs($fp, $schema);

$tables_query = mysql_query('show tables');
while ($tables = mysql_fetch_array($tables_query))
{
  list(,$table) = each($tables);

  $schema = 'drop table if exists ' . $table . ';' . "\n" .
            'create table ' . $table . ' (' . "\n";

  $table_list = array();
  $fields_query = mysql_query("show fields from " . $table);
  while ($fields = mysql_fetch_array($fields_query))
  {
    $table_list[] = $fields['Field'];

    $schema .= '  ' . $fields['Field'] . ' ' . $fields['Type'];
    if (strlen($fields['Default']) > 0) $schema .= ' default \'' . $fields['Default'] . '\'';

    if ($fields['Null'] != 'YES') $schema .= ' not null';

    if (isset($fields['Extra'])) $schema .= ' ' . $fields['Extra'];

    $schema .= ',' . "\n";
  }
  $schema = ereg_replace(",\n$", '', $schema);

// add the keys
  $index = array();
  $keys_query = mysql_query("show keys from " . $table);
  while ($keys = mysql_fetch_array($keys_query))
  {
    $kname = $keys['Key_name'];

    if (!isset($index[$kname]))
    {
      $index[$kname] = array('unique' => !$keys['Non_unique'],
                             'fulltext' => ($keys['Index_type'] == 'FULLTEXT' ? '1' : '0'),
                             'columns' => array());
    }

    $index[$kname]['columns'][] = $keys['Column_name'];
  }

  while (list($kname, $info) = each($index))
  {
    $schema .= ',' . "\n";

    $columns = implode($info['columns'], ', ');

    if ($kname == 'PRIMARY')
      $schema .= '  PRIMARY KEY (' . $columns . ')';
    elseif ( $info['fulltext'] == '1' )
      $schema .= '  FULLTEXT ' . $kname . ' (' . $columns . ')';
    elseif ($info['unique'])
    {
      $schema .= '  UNIQUE ' . $kname . ' (' . $columns . ')';
      $schema .= '  KEY ' . $kname . ' (' . $columns . ')';
    }
  }

  $schema .= "\n" . ');' . "\n\n";
  fputs($fp, $schema);
// dump the data
  $rows_query = mysql_query("select " . implode(',', $table_list) . " from " . $table);
  while ($rows = mysql_fetch_array($rows_query))
  {
    $schema = 'insert into ' . $table . ' (' . implode(', ', $table_list) . ') values (';

    reset($table_list);
    while (list(,$i) = each($table_list))
    {
      if (!isset($rows[$i]))
        $schema .= 'NULL, ';
      elseif ( trim($rows[$i]) != '' )
      {
        $row = addslashes($rows[$i]);
        $row = ereg_replace("\n#", "\n".'\#', $row);
        $schema .= '\'' . $row . '\', ';
      }
      else
        $schema .= '\'\', ';
    }
    $schema = ereg_replace(', $', '', $schema) . ');' . "\n";
    fputs($fp, $schema);
  }
}
fclose($fp);

$to = "bhatt6652@gmail.com";
$from = "bhatt6652@gmail.com";
$my_file = "./backup/".$backup_file;
$my_path = $backup_file;
$my_name = "Nat";
$my_replyto = "";
$my_subject = "Welcome To Om Techno Plus.";
$my_message = "Sql Backup Created";
mail_attachment($my_file, $my_path, $to, $from, $my_name, $my_replyto, $my_subject, $my_message);
 



$captchaFolder  = 'backup/';
$fileTypes      = '*.sql';
$newTime = 0;
$newfilename = "";
$nextfileName="";
$nextTime=0;
foreach (glob($captchaFolder . $fileTypes) as $Filename) 
{
 
  // Read file creation time 
  $FileCreationTime = filectime($Filename);
  // Is the file older than the given time span?
  if ($newTime < $FileCreationTime)
  {
    $newTime=$FileCreationTime;
    $newfilename=$Filename;
  }
}
print "LATEST  file is $newfilename <br>";
foreach (glob($captchaFolder . $fileTypes) as $Filename)
 {

 // echo $newfilename;
  // compare New file with Old file
  if ($Filename != $newfilename)
  {
    $FileCreationTime = filectime($Filename);
    if ($nextTime < $FileCreationTime)
  {
    $nextTime=$FileCreationTime;
    $nextfileName=$Filename;
  }
  }
}
print " NEXT LATEST  file is $nextfileName";
 foreach (glob($captchaFolder . $fileTypes) as $Filename)
 {
  if ($Filename != $nextfileName && $Filename !=$newfilename)
 {
   unlink($Filename);
 }
}
?>