<?php
    session_start();
    ini_set('display_errors', '0');
    date_default_timezone_set('Europe/Helsinki');
    //====================== EMAIL NOTIFICATION ====================
    $PRODUCTION_URL = 'https://kiiltavatpinnat.fi';
    $PRODUCTION_API = $PRODUCTION_URL . '/adminpanel/api/';
    defined('PRODUCTION_URL') ? NULL : define('PRODUCTION_URL', $PRODUCTION_URL);
    defined('PRODUCTION_API') ? NULL : define('PRODUCTION_API', $PRODUCTION_API);
    
    
    //==============================Curl Function==============================
    function curlFunction($url, $data, $headers = "", $userpwd = "", $type = "")
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($headers != "") {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if ($userpwd != "") {
            curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
        }
        if (in_array($type, ["GET", "PUT", "PATCH", "DELETE"])) {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else if ($type != "") {
            curl_setopt($ch, CURLOPT_POST, 0);
        } else {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch);
        if ($err) {
            return $err;
        } else {
            return $result;
        }
    }
    //==============================Curl Function==============================
    
    //====================== EMAIL NOTIFICATION ====================
    function systemNotification($payload)
    {
        extract($payload);
        switch ($notification) {
            case 'email':
                // include_once("library/phpmailer/class.phpmailer.php");
                // $mail = new PHPMailer;
                // $mail->IsSMTP();
                // $mail->Host = "mail.kiiltavatpinnat.fi";
                // $mail->Port = 465;
                // $mail->SMTPSecure = "ssl";
                // $mail->SMTPAuth = true;
                // $mail->Username = 'booking@kiiltavatpinnat.fi';
                // $mail->Password = 'Wajeehkiiltavat23!';
                // $mail->SetFrom("booking@kiiltavatpinnat.fi", "mail.kiiltavatpinnat.fi");
                // $mail->Timeout = 3600;
                // $mail->Subject = $subject;
                // $mail->AddAddress($email);
                // $mail->MsgHTML($message);
                // $mail->SMTPDebug = 2;
                // $return = $mail->Send();
                // print_r($return);die;
                // return $return;
                // break;
                $headers = "From: info@kiiltavatpinnat.fi\r\n";
                $headers .= "BCC: info@kiiltavatpinnat.fi\r\n";
                $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-Type: text/html; charset=\"UTF-8\"\r\n";
                mail($email, $subject, $message, $headers);
                return true;
        }
    }
    //====================== EMAIL NOTIFICATION ====================