<?php
/**********************************************************
 *               phpMySQLAutoBackup                       *
 *           Author:  http://www.DWalker.co.uk            *
 *            Now released under GPL License              *
 *                                                        *
 **********************************************************
 *     Version    Date              Comment               *
 *     0.2.0      7th July 2005     GPL release           *
 *     0.3.0      19th June 2006  Upgrade                 *
 *           - added ability to backup separate tables    *
 *     0.4.0      Dec 2006   removed bugs/improved code   *
 *     1.4.0      Dec 2007   improved faster version      *
 *     1.4.2    July 2008 modified to overcome issue with *
 *                       "double newline in headers"      *
 **********************************************************/
$phpMySQLAutoBackup_version="1.4.2";
// ---------------------------------------------------------
// For support and help please try the forum at: http://www.dwalker.co.uk/forum/

function has_data($value)
{
 if (is_array($value)) return (sizeof($value) > 0)? true : false;
 else return (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)) ? true : false;
}

function xmail ($to_emailaddress,$from_emailaddress, $subject, $content, $file_name, $backup_type)
{
 $mail_attached = "";
 $boundary = "----=_NextPart_000_01FB_010".md5($to_emailaddress);
 $mail_attached.="--".$boundary."\r\n"
                       ."Content-Type: application/octet-stream;\r\n name=\"$file_name\"\r\n"
                       ."Content-Transfer-Encoding: base64\r\n"
                       ."Content-Disposition: attachment; \r\n filename=\"$file_name\"\r\n\r\n"
                       .chunk_split(base64_encode($content))."\r\n";
 $mail_attached .= "--".$boundary."--\r\n";
 $add_header ="MIME-Version: 1.0\r\nContent-Type: multipart/mixed;\n        boundary=\"$boundary\" \n";
 $mail_content="--".$boundary."\r\n"."Content-Type: text/plain; \r\n charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\nBACKUP Successful...\r\n\r\nPlease see attached for your zipped Backup file; $backup_type \r\n\r\n".$mail_attached;
 return mail($to_emailaddress, $subject, $mail_content, "From: $from_emailaddress"."\r\n"."Reply-To:$from_emailaddress"."\r\n".$add_header);
}

function write_backup($gzdata, $backup_file_name)
{
 $fp = fopen(LOCATION."../backups/".$backup_file_name, "w");
 fwrite($fp, $gzdata);
 fclose($fp);
 //check folder is protected - stop HTTP access
 if (!file_exists(".htaccess"))
 {
  $fp = fopen(LOCATION."../backups/.htaccess", "w");
  fwrite($fp, "deny from all");
  fclose($fp);
 }
}
?>
