<?php
/*
 * Public unsubscribe landing page.
 * Upload this file to http://mail.trgo-agencija.hr/newsletter/unsub.php.
 * It intentionally has no database access: it forwards only the opaque token
 * to the local mailbox, where unsubscribe_inbox_worker.php processes it.
 */
define('NEWSLETTER_UNSUB_RECIPIENT', 'unsub@trgo-agencija.hr');

function nl_remote_unsub_token()
{
    $token = isset($_REQUEST['u']) ? strtolower(trim((string)$_REQUEST['u'])) : '';
    return preg_match('/^[a-f0-9]{64}$/', $token) ? $token : '';
}

$token = nl_remote_unsub_token();
$sent = false;
$error = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($token === '') {
        $error = true;
    } else {
        /* The mail body deliberately contains only the opaque token. */
        $subject = 'Newsletter odjava';
        $body = $token . "\r\n";
        $headers = "Content-Type: text/plain; charset=UTF-8\r\n";
        $sent = @mail(NEWSLETTER_UNSUB_RECIPIENT, $subject, $body, $headers);
        if (!$sent) {
            $error = true;
        }
    }
}
?><!doctype html>
<html lang="hr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Odjava s newslettera</title>
<style>body{margin:0;background:#f3f6fb;color:#172033;font:16px/1.5 Arial,sans-serif}.box{max-width:620px;margin:56px auto;background:#fff;border:1px solid #dce3ef;border-radius:12px;padding:30px;box-shadow:0 3px 14px rgba(20,38,70,.06)}h1{margin-top:0}.btn{border:0;border-radius:7px;background:#1858c9;color:#fff;padding:11px 16px;font:inherit;font-weight:bold;cursor:pointer}.error{padding:11px 13px;background:#fff0ef;color:#94231c;border:1px solid #f4c7c3;border-radius:7px}.muted{color:#607087}</style></head>
<body><main class="box"><p style="margin:0 0 22px;text-align:center"><img src="http://mail.trgo-agencija.hr/newsletter/logo.jpg" alt="Trgo-Agencija d.o.o." style="max-width:240px;max-height:80px;height:auto;border:0"></p>
<?php if ($sent): ?>
<h1>Zahtjev za odjavu je zaprimljen</h1><p>Vaš će email biti uklonjen s newsletter liste pri sljedećoj obradi zahtjeva.</p>
<?php elseif ($error || $token === ''): ?>
<h1>Odjava nije moguća</h1><p class="error">Poveznica nije valjana ili zahtjev trenutačno nije moguće poslati. Pokušajte ponovno kasnije.</p>
<?php else: ?>
<h1>Odjava s newslettera</h1><p>Potvrdite da više ne želite primati ovaj newsletter na email adresu za koju je poveznica izdana.</p><p class="muted">Adresa se ovdje ne prikazuje niti sprema; šalje se samo sigurnosni identifikator zahtjeva.</p>
<form method="post"><input type="hidden" name="u" value="<?php echo htmlspecialchars($token, ENT_QUOTES, 'UTF-8'); ?>"><button class="btn" type="submit">Potvrdi odjavu</button></form>
<?php endif; ?>
</main></body></html>