<?php

require_once '../includes/visitor.inc.php';

use Mnv\Core\Config;
use Mnv\Modules\Newsletter;


if (request()->ajax()) {

    $rewrite     = request()->get('rewrite');
    $signupEmail = request()->get('email');

    if ($rewrite != Config::getValue('rewrite')) {
        header("HTTP/1.0 301 Moved Permanently");
        header("Location: " . SITE_URL);

        /* saving visit */
        writeErrorVisit(301);
        exit;
    }

    $subscription = new Newsletter();

    if (!empty($signupEmail)) {
        $subscription->register($signupEmail);
    }

    return response()->json($subscription->response, \Mnv\Http\Response::HTTP_OK, ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)->send();

} else {
    die('Hacking attempt!');
}