alexsabur/antispam-bundle

此包提供了一些基本功能,用于减少symfony表单中的垃圾邮件。

安装: 24

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

3.0.0 2024-05-23 13:10 UTC

This package is auto-updated.

Last update: 2024-09-23 13:52:32 UTC


README

NucleosAntiSpamBundle

此包提供了一些基本功能,用于减少Symfony中的垃圾邮件。它是core23/antispam-bundle的后继产品,但与isometriks/spam-bundle无关。

功能

  • 表单的蜜罐保护:将添加一个额外的“隐藏”字段到您的表单中(即通过CSS使其不可见)。填写此字段的人被认为是垃圾邮件机器人。

  • 表单的时间保护:测量表单的显示和提交之间的时间。任何在特定秒数内提交表单的人都被认为是垃圾邮件机器人。时间戳存储在会话中。

  • Twig的电子邮件地址混淆过滤器:为了防止垃圾邮件收集机器人检测到您的电子邮件地址,它们被混淆,例如用[AT]替换@。该过滤器会自动找到电子邮件地址,因此您可以将它应用于您的整个文本。

安装

打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

composer require alexsabur/antispam-bundle

启用包

在旧版本的Symfony中,您需要手动启用它

// config/bundles.php

return [
    // ...
    Nucleos\AntiSpamBundle\NucleosAntiSpamBundle::class => ['all' => true],
];

用法

基于表单的保护

在控制器中

$this->createForm(CustomFormType:class, null, [
    // Time protection
    'antispam_time'     => true,
    'antispam_time_min' => 10, // seconds
    'antispam_time_max' => 60,

    // Honeypot protection
    'antispam_honeypot'       => true,
    'antispam_honeypot_class' => 'hide-me',
    'antispam_honeypot_field' => 'email-repeat',
])

在表单类中

class MyType extends AbstractType
{
    // ...

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            // ...
            'antispam_time'     => true,
            'antispam_time_min' => 10,
            // same as above
        ]);
    }
}

Twig电子邮件地址混淆

Twig过滤器antispam@替换为例如[AT]

{# Replace plain text #}
{{ text|antispam }}

{# Replace rich text mails #}
{{ htmlText|antispam(true) }}

如果您想要对编码的电子邮件地址进行JavaScript解码,应使用AntiSpam.js

document.addEventListener('DOMContentLoaded', () => {
  new AntiSpam('.custom_class');
});

建议使用webpack / webpack-encore将JavaScript库包含在您的页面中。此文件位于assets文件夹中。

配置包

创建一个名为nucleos_antispam.yaml的配置文件

# config/packages/nucleos_antispam.yaml

nucleos_antispam:
    # Twig mail filter
    twig:
        mail:
            css_class: 'custom_class'
            at_text:   [ '[AT]', '(AT)', '[ÄT]' ]
            dot_text:  [ '[DOT]', '(DOT)', '[.]' ]

    # Time protection
    time:
        min: 5
        max: 3600
        global: true # This will add antispam to all forms

    # Honeypot protection
    honeypot:
        field: 'email_address'
        class: 'hidden'
        global: false
        provider: 'nucleos_antispam.provider.session'

许可证

此包受MIT许可证的约束。