<?php
date_default_timezone_set('Asia/Jakarta');

include("common.php");
include("classMySQL.php");
$db = new db_mysql($server_name, $userdb, $passdb, $databasename,"");
include("function.php");
require_once('class.phpmailer.php');
include("class.smtp.php");

mysql_connect($server_name, $userdb, $passdb) or die ("Could not connect to database");
mysql_select_db($databasename);
?>
<?php
$clrdt = date("H");
$cekdt = "00";
if($clrdt == $cekdt) {
$sql="DELETE FROM useronline2";
$result=mysql_query($sql);
}
?>
<?php
$tg = date("d");
$cektg = "01";
$cektg2 = "07";
$cektg3 = "15";
$cektg4 = "22";
if($tg == $cektg || $tg == $cektg2 || $tg == $cektg3 || $tg == $cektg4) {

$sqla1="SELECT userid, time FROM adminlastlog order by time DESC limit 1"; 
$dta1=mysql_query($sqla1);
if($dta1 === FALSE) {
    die(mysql_error()); // TODO: better error handling
}

while($rowa1 = mysql_fetch_array($dta1)){
  $usernameku = $rowa1['userid'];
  $ida = $rowa1['time'];

$sqla2="DELETE FROM adminlastlog WHERE userid='$usernameku' and time <> $ida";
$resulta2=mysql_query($sqla2);	   
}

$sqla3="SELECT userid, time FROM memberlog order by time DESC limit 1"; 
$dta3=mysql_query($sqla3);
if($dta3 === FALSE) {
    die(mysql_error()); // TODO: better error handling
}

while($rowa3 = mysql_fetch_array($dta3)){
  $usernameku2 = $rowa3['userid'];
  $ida2 = $rowa3['time'];

$sqla4="DELETE FROM memberlog WHERE userid='$usernameku2' and time <> $ida2";
$resulta4=mysql_query($sqla4);	   

}
}
?>
<?php
if($on_backup == 1) {
$dayne = $day_backup;
$datestamp = date("j-F-Y_H:i:s");

if($periode_backup == 1) {

$datene = date("Y-m-d H:i:s");
$masane = date('Y-m-d H:i:s', strtotime("$dayne +$tiap_periode hour") );   

if($masane <= $datene){
// Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
$dbhost = "localhost";
$dbuser = $userdb;            // Database username
$dbpwd = $passdb;            // Database password
$dbname = $databasename;            // Database name. Use --all-databases if you have more than one
$filename= "backup-".$databasename."_$datestamp.sql.gz";   // The name (and optionally path) of the dump file
$to = $mail_backup; 
$from = "".$bisnisname." Backup<no-reply@".$domain.">";      // Email address message will show as coming from.
$subjectv = "Backup Database Periode $datestamp";      // Subject of email

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

$attachmentname = array_pop(explode("/", $filename));   // If a path was included, strip it out for the attachment name

$message = "Haloo $pengelola,\n\n\nBerikut ini data backup database website anda :\n\nNama Domain : $domain.\nNama Database : $databasename.\nPeriode backup : $datestamp.\n\nSilahkan download file backup di attachment.\n\n\nIni adalah email autoresponder, jangan balas email ini.\n\n\n== Auto Backup System ==";
$mime_boundary = "< <<:" . md5(time());
$data = chunk_split(base64_encode(implode("", file($filename))));


$headersv = "From: $from\r\n";
$headersv .= "MIME-Version: 1.0\r\n";
$headersv .= "Content-type: multipart/mixed;\r\n";
$headersv .= " boundary=\"".$mime_boundary."\"\r\n";

$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= $message."\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
$content.= "--" . $mime_boundary . "\r\n";

mail($to, $subjectv, $content, $headersv);

unlink($filename);   //delete the backup file from the server


$datene = date("Y-m-d H:i:s");
$sqlxxx = "UPDATE autoback SET day='$datene'"; 
$result=mysql_query($sqlxxx); 
}
} else if ($periode_backup == 0) {

$datene = date("Y-m-d");
$masane = date('Y-m-d', strtotime("$dayne +$tiap_periode days") );   

if($masane <= $datene){    

// Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
$dbuser = $userdb;            // Database username
$dbpwd = $passdb;            // Database password
$dbname = $databasename;            // Database name. Use --all-databases if you have more than one
$filename= "backup-".$databasename."_$datestamp.sql.gz";   // The name (and optionally path) of the dump file
$to = $mail_backup; 
$from = "".$bisnisname." Backup<no-reply@".$domain.">";      // Email address message will show as coming from.
$subjectv = "Backup Database Periode $datestamp";      // Subject of email

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

$attachmentname = array_pop(explode("/", $filename));   // If a path was included, strip it out for the attachment name

$message = "Haloo $pengelola,\n\n\nBerikut ini data backup database website anda :\n\nNama Domain : $domain.\nNama Database : $databasename.\nPeriode backup : $datestamp.\n\nSilahkan download file backup di attachment.\n\n\nIni adalah email autoresponder, jangan balas email ini.\n\n\n== Auto Backup System ==";
$mime_boundary = "< <<:" . md5(time());
$data = chunk_split(base64_encode(implode("", file($filename))));

$headersv = "From: $from\r\n";
$headersv .= "MIME-Version: 1.0\r\n";
$headersv .= "Content-type: multipart/mixed;\r\n";
$headersv .= " boundary=\"".$mime_boundary."\"\r\n";

$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= $message."\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
$content.= "--" . $mime_boundary . "\r\n";

mail($to, $subjectv, $content, $headersv);

unlink($filename);   //delete the backup file from the server

$datene = date("Y-m-d H:i:s");
$sqlxxx = "UPDATE autoback SET day='$datene'"; 
$result=mysql_query($sqlxxx); 
}
}
}
?>
<?php
$datene = date("Y-m-d H:i:s");
$db->delete("unlockfield", "date<'$datene'");
?>
<?php
mysql_close();
?>
