<?php

date_default_timezone_set('Europe/Belgrade');

if (isset($_SERVER['HTTP_REFERER']) && endsWith($_SERVER['HTTP_REFERER'], 'naruci-prolong.html')) {
    $from = $_POST['ime'] . " " . $_POST['prezime'];
    $message = "Narudzbina: " . $_POST['pakovanje'] . "\n" .
        "Ime: " . $_POST['ime'] . "\n" .
        "Prezime: " . $_POST['prezime'] . "\n" .
        "Email: " . $_POST['email'] . "\n" .
        "Telefon: " . $_POST['broj_telefona'] . "\n" .
        "Adresa: " . $_POST['adresa'] . "\n" .
        "Mesto: " . $_POST['grad'] . "\n" .
        "Vreme narudzbine: " . date('m/d/Y h:i:s a', time()) . "\n";

    //$to = "ivan.popov@live.com";
    $to = "order@prolong-tablete.com";

    if (mail($to, 'Prolong:New Order', $message)) {
        header("Location: " . $_SERVER['host'] . "/ok.html");
        exit;
    } else {
        header("Location: " . $_SERVER['host'] . "/naruci-prolong.html?error=true");
        exit;
    }
} else {
    header("Location: " . $_SERVER['host'] . "/naruci-prolong.html?error=true");
    exit;
}


function endsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}