<?php
    session_start();
    include '../prijava/connect.php';
    setlocale(LC_TIME,"hr_HR");
    date_default_timezone_set('Europe/Zagreb');

    $date = date('Y-m-d');
    $tables = array();
    $sql = "SHOW TABLES";
    $result = mysqli_query($dbc, $sql);

    while ($row = mysqli_fetch_row($result)) {
        $tables[] = $row[0];
    }

    $sqlScript = "";
    foreach ($tables as $table) {
        $query = "SHOW CREATE TABLE $table";
        $result = mysqli_query($dbc, $query);
        $row = mysqli_fetch_row($result);
        $sqlScript .= "\n\n" . $row[1] . ";\n\n";
        $query = "SELECT * FROM $table";
        $result = mysqli_query($dbc, $query);
        $columnCount = mysqli_num_fields($result);
        for ($i = 0; $i < $columnCount; $i ++) {
            while ($row = mysqli_fetch_row($result)) {
                $sqlScript .= "INSERT INTO $table VALUES(";
                for ($j = 0; $j < $columnCount; $j ++) {
                    $row[$j] = $row[$j];
                    if (isset($row[$j])) {
                        $sqlScript .= '"' . $row[$j] . '"';
                    } else {
                        $sqlScript .= '""';
                    }
                    if ($j < ($columnCount - 1)) {
                        $sqlScript .= ',';
                    }
                }
                $sqlScript .= ");\n";
            }
        }
        $sqlScript .= "\n"; 
    }
    if(!empty($sqlScript))
    {
        $backup_file_name = 'backup_'.$date.'.sql';
        $fileHandler = fopen($backup_file_name, 'w+');
        $number_of_lines = fwrite($fileHandler, $sqlScript);
        fclose($fileHandler); 
    }
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP;

    require '../mailer/Exception.php';
    require '../mailer/PHPMailer.php';
    require '../mailer/SMTP.php';

    $filename = "".$date.".sql";
    $day = date('d');
    $month = date('m');
    $year = date('Y');
    $filenameOriginal =  "backup_".$year."_".$month."_".$day.".sql";

    $smtpUsername = 'kriklunch@gmail.com';
    $smtpPassword = 'mryjvkxepolbfymp';

    $emailFrom = 'kriklunch@gmail.com';
    $emailFromName = 'Lunch Backup';
    
    $emailTo = 'kriklunch@gmail.com';
    
    $email_body = "
        <head>
            <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
        </head>
        <div>
            <div>
                <p>U prilogu je backup baze napravljen ".$day.".".$month.".".$year.".</p>
            </div>
            <div><br/></div>
        </div>
    ";
    
    $mail = new PHPMailer;
    $mail->CharSet = 'UTF-8';
    $mail->Encoding = 'base64';
    $mail->isSMTP(); 
    $mail->SMTPDebug = 0; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; // TLS only
    $mail->SMTPSecure = 'tls';
    $mail->SMTPKeepAlive = true;   
    $mail->Mailer = “smtp”;
    $mail->SMTPAuth = true;
    $mail->Username = $smtpUsername;
    $mail->Password = $smtpPassword;
    $mail->setFrom($emailFrom, $emailFromName);
    $mail->addAddress($emailTo);
    $mail->Subject = 'Baza';
    $mail->msgHTML($email_body);
    $mail->AddAttachment('./'.$backup_file_name.'', ''.$filenameOriginal.'');
    $mail->AltBody = 'HTML messaging not supported';
    
    if(!$mail->send()){
        echo "We are sorry, but the email did not go through.";
    } else {
        unlink($backup_file_name);
    }
?>
