nucleos/antispam-bundle

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

此包的官方仓库似乎已消失,因此该包已被冻结。

3.1.0 2023-12-05 20:02 UTC

README

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

Continuous Integration Code Coverage Type Coverage

此包提供了一些基本功能,以减少 Symfony 中的垃圾邮件。

功能

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

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

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

安装

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

composer require nucleos/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'

when@test:
    nucleos_antispam:
        time:
            # This will allow you to submit forms in your tests without having to fake the wait
            min: 0

许可证

此包受 MIT 许可证 的约束。