<?php
    # Autoload
    require 'vendor/autoload.php';
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    include('./config.php');
    
    function getUserIP()
    {
        // Get real visitor IP behind CloudFlare network
        if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
                  $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
                  $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
        }
        $client  = @$_SERVER['HTTP_CLIENT_IP'];
        $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
        $remote  = $_SERVER['REMOTE_ADDR'];
    
        if(filter_var($client, FILTER_VALIDATE_IP))
        {
            $ip = $client;
        }
        elseif(filter_var($forward, FILTER_VALIDATE_IP))
        {
            $ip = $forward;
        }
        else
        {
            $ip = $remote;
        }
    
        return $ip;
    }

    $bodytemplate = file_get_contents("mailtemplate_off.html");
    $daten = json_decode(file_get_contents('php://input'), true);

    $name = $daten["name"]; // vorname/nachname
    $daten["vorname"]               = $name["vorname"];
    $daten["nachname"]              = $name["nachname"];
    $daten["ip"]                    = getUserIP();
    $daten["date"]                  = date("d.m.Y");
    $daten["time"]                  = date("H:i");
    $daten["infotext"]              = nl2br($daten["infos"]);

    $body = $bodytemplate;
    $forjson = [];
    $groups = "";
    foreach($daten as $key => $value) 
    {
        echo $key . " -> " .  gettype($value);
        if ( in_array(gettype($value), ["string","integer", "double"]) )
        {
            $body = str_replace("%".$key."%", $value, $body );
            $forjson[$key] = $value;
        }
    }
   
    // Daten lokal ablegen:
    $filename = date("YmdHis") . "_" . $daten["nachname"];
    file_put_contents("auftraege/off_".$filename.'.json', json_encode($forjson, JSON_UNESCAPED_UNICODE), FILE_APPEND | LOCK_EX);

    // erstmal per mail
    try {
        // Versuch, eine neue Instanz der Klasse PHPMailer zu erstellen
        $mail = new PHPMailer (true);
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;
        $mail->isSMTP();
        $mail->Host = "postfix";
        $mail->Port = "25";
        $mail->SMTPAuth = false;
        $mail->CharSet = 'UTF-8';
        $mail->Encoding = 'base64';
        $mail->setFrom($config["mailfrom"], 'dialog-it Xtend');
        $mail->addAddress($config["mailto"], 'Support');
        $mail->isHTML(true);
        $mail->Subject = (($daten["anrede"]=="Frau") ? "Mitarbeiterin Offboarding: " : "Mitarbeiter Offboarding: " ).  $daten["vorname"] . " " . $daten["nachname"];
        $mail->Body = $body;
        // Anhang hinzufügen
        $mail->addAttachment("auftraege/off_".$filename.".json", $filename.".json");

        $mail->send();
        echo "
        Auftrag für Offboarding " . (($daten["anrede"]=="Frau") ? "Mitarbeiterin" : "Mitarbeiter" ). " " . $daten["vorname"] . " " . $daten["nachname"] . " erteilt.<br/>
        Die Auftragsbestätigung erhalten sie über unser Ticketsystem per eMail.
        ";
        return true;
    } catch (Exception $e) {
        echo "<h2>Fehler beim Senden...</h2>";
        return false;
    }

?>
