{% extends "freeform/_layouts/settings" %}

{% import "_includes/forms" as forms %}

{% set crumbs = [
    { label: craft.freeform.name, url: url("freeform") },
    { label: "Settings"|t("freeform"), url: url("freeform/settings") },
] %}

{% set title = "Spam Protection"|t("freeform") %}

{% block content %}

    <form action="" method="post" accept-charset="UTF-8">
        <input type="hidden" name="action" value="freeform/settings/save-settings">
        {{ redirectInput('freeform/settings/spam') }}
        {{ csrfInput() }}

        <p class="first">{{ "Spam protection features are configured in the <b>Integrations</b> settings area:"|t('freeform')|raw }}</p>
        <ul class="freeform-settings-list">
            <li>
                <a href='{{ cpUrl("freeform/integrations/single/Honeypot") }}'>
                    {{ "Freeform Honeypot"|t('freeform') }}
                </a>
            </li>
            <li>
                <a href='{{ cpUrl("freeform/integrations/single/JavascriptTest") }}'>
                    {{ "Freeform Javascript Test"|t('freeform') }}
                </a>
            </li>
            <li>
                <a href='{{ cpUrl("freeform/integrations/captchas/ReCaptcha") }}'>
                    {{ "Captchas"|t('freeform') }}
                </a>
            </li>
            <li>
                <a href='{{ cpUrl("freeform/integrations/spam-blocking/BlockKeywords") }}'>
                    {{ "Keyword Blocking"|t('freeform') }}
                </a>
            </li>
            <li>
                <a href='{{ cpUrl("freeform/integrations/spam-blocking/BlockEmailAddresses") }}'>
                    {{ "Email Address Blocking"|t('freeform') }}
                </a>
            </li>
            <li>
                <a href='{{ cpUrl("freeform/integrations/spam-blocking/BlockIpAddresses") }}'>
                    {{ "IP Address Blocking"|t('freeform') }}
                </a>
            </li>
        </ul>

        <hr />

        <h2>{{ "Spam Behavior"|t('freeform') }}</h2>

        {{ forms.selectField({
            id: "spam-protection-behavior",
            label: "Spam Protection Behavior"|t('freeform'),
            instructions: "Select the behavior you'd like Freeform to take when it detects a submission as being spam."|t('freeform'),
            name: "settings[spamProtectionBehavior]",
            value: settings.spamProtectionBehavior,
            options: {
                "simulate_success": "Simulate Success (recommended)"|t('freeform'),
                "display_errors": "Display Errors (for debugging)"|t('freeform'),
            }
        }) }}

        {{ forms.lightswitchField({
            label: "Bypass All Spam Checks for Logged in Users"|t('freeform'),
            instructions: "When enabled, Freeform will not run any spam protection measures for logged in users."|t('freeform'),
            name: "settings[bypassSpamCheckOnLoggedInUsers]",
            on: settings.bypassSpamCheckOnLoggedInUsers,
            errors: settings.errors("bypassSpamCheckOnLoggedInUsers"),
        }) }}

        <hr>

        <h2>{{ "Spam Folder"|t('freeform') }}</h2>

        {{ forms.lightswitchField({
            label: "Use Spam Folder"|t('freeform'),
            instructions: "When enabled, all submissions caught by the honeypot or blocked email addresses, keywords and IP addresses will be flagged as spam and stored in the database, but available to manage in a separate menu inside Freeform."|t('freeform'),
            name: "settings[spamFolderEnabled]",
            on: settings.spamFolderEnabled,
            toggle: "spam-features",
            errors: settings.errors("spamFolderEnabled"),
        }) }}

        <div id="spam-features" class="field {{ not settings.spamFolderEnabled ? 'hidden' }}">
            {{ forms.lightswitchField({
                label: "Automatically Purge Spam Submissions"|t('freeform'),
                instructions: "If you wish to have Freeform automatically purge submissions flagged as spam after a specified number of days, enable this setting and specify the number of days after submission date it should purge spammy submissions."|t('freeform'),
                warning: "Enabling this and saving this settings page will result in the purging of submissions flagged as spam feature beginning, and cannot be undone (it may not happen immediately, and may take a couple hours before the next process runs)."|t('freeform'),
                name: "purge-toggle",
                on: settings.purgableSpamAgeInDays,
                toggle: "purge",
                errors: settings.errors("purgableSpamAgeInDays"),
            }) }}

            <div id="purge" class="field {{ not settings.purgableSpamAgeInDays ? 'hidden' }}">
                {{ forms.selectField({
                    label: "Days After Submission Date to Purge"|t('freeform'),
                    instructions: "Select the number of days after spammy submissions have been submitted for Freeform to begin automatically purging. This process will only run every hour, and only when Freeform is accessed on the front end in templates or in the control panel."|t('freeform'),
                    name: "settings[purgableSpamAgeInDays]",
                    value: settings.purgableSpamAgeInDays,
                    errors: settings.errors("purgableSpamAgeInDays"),
                    id: "purge-value",
                    options: {
                        0: "Please select"|t("freeform"),
                        1: "1 day"|t("freeform"),
                        2: "2 days"|t("freeform"),
                        3: "3 days"|t("freeform"),
                        4: "4 days"|t("freeform"),
                        5: "5 days"|t("freeform"),
                        6: "6 days"|t("freeform"),
                        7: "7 days"|t("freeform"),
                        14: "14 days"|t("freeform"),
                        30: "30 days"|t("freeform"),
                        60: "60 days"|t("freeform"),
                    }
                }) }}
            </div>
        </div>

        <hr>

        <h2>{{ "Form Throttling"|t('freeform') }}</h2>

        {% set input %}
            {{ forms.text({
                id: "throttling-count",
                name: "settings[submissionThrottlingCount]",
                value: settings.submissionThrottlingCount,
                placeholder: "unlimited"|t('freeform'),
            }) }}

            {{ forms.select({
                id: "throttling-time-frame",
                name: "settings[submissionThrottlingTimeFrame]",
                value: settings.submissionThrottlingTimeFrame,
                options: {
                    m: "per minute"|t("freeform"),
                    s: "per second"|t("freeform"),
                }
            }) }}
        {% endset %}

        {{ forms.field({
            label: "Form Submission Throttling"|t("freeform"),
            instructions: "Globally (affecting all users) prevent spam or attacks by limiting the number of times all forms can be submitted within a given timeframe."|t("freeform"),
            errors: settings.errors("submissionThrottlingCount"),
            warning: "This feature is intended for extreme conditions, such as preventing your site from going down if attacked by a spammer. It should NOT be used as a 'fine-tuning' spam measure, as it applies to ALL users. For example, if you set it to '1 per minute', once one user submits any form, any other user will not be able to submit a form within that timeframe. A more realistic value for smaller websites is something like 50 per minute. Use extreme caution for larger and more active sites."|t('freeform'),
        }, input) }}

        {{ forms.textField({
            label: "Minimum Submit Time"|t('freeform'),
            instructions: "The minimum amount of time (in seconds) that has to go by since loading the form for the user to be able to submit the form successfully. Otherwise the submission will be flagged as spam and the Spam Protection Behavior setting will take effect."|t('freeform'),
            name: "settings[minimumSubmitTime]",
            value: settings.minimumSubmitTime,
            errors: settings.errors("minimumSubmitTime"),
        }) }}

        {{ forms.textField({
            label: "Form Submit Expiration"|t('freeform'),
            instructions: "The maximum amount of time (in minutes) a user has to submit the form before the form expires and the Spam Protection Behavior setting will take effect. This still has to be less than the Craft CSRF token expiry and PHP Session limit set for your server."|t('freeform'),
            name: "settings[formSubmitExpiration]",
            value: settings.formSubmitExpiration,
            errors: settings.errors("formSubmitExpiration"),
        }) }}

    </form>

{% endblock %}
