gorankrgovic/spam-marker

一个可扩展、简单但强大的垃圾邮件过滤器库

1.1.0 2017-05-26 13:52 UTC

This package is auto-updated.

Last update: 2024-09-12 09:16:37 UTC


README

SpamMarker是一个简单的库,用于检测提供的字符串中的垃圾邮件。它通过引入Spam Filters(用于扩展SpamMarker检测能力的独立类)遵循开闭原则。

最初的想法,实际上是一个略有不同的库,来自一个名为Spam Filter的lib,由Laju Morrison创建,您可以在这里找到。

我只是厌倦了我项目中的所有各种垃圾邮件,所以我最终创建了它。

安装

您可以使用Composer将此库加载到项目中,或者通过重新发明轮子并创建自己的自动加载器。

通过Composer安装

 composer require gorankrgovic/spam-marker

设置和基本用法

这应该在应用程序中仅执行一次。

use SpamMarker\SpamMarker;

// 1.
// Use some filters
use SpamMarker\Filter\BlackListed;

// 2.
// Initalize the blackisted filter
$blackListed = new BlackListed();

// Add some spammy keywords
$blackListed->add('hacker');
$blackListed->add('attacker');

// 3.
// Initialize the spam marker
$spamMarker = new SpamMarker();

// 4.
// Register the filters
$spamMarker->registerFilter($blackListed);

// 5.
// Check your string against filters
$spamMarkerResult = $spamMarker->check("
	Hello, this is some text containing hacker and attacker 
	and should fail as it has a word that is black-listed
");

// var_dump ( $spamMarkerResult );
// It gives you two objects - is_spam (bool) and messages array

if ( $spamMarkerResult->passed() )
{
    // Do some stuff, because the spam marker is passed
    echo 'Your string doesn\'t contain the spam';
}
else
{
    echo 'String containts the spam!';
}

// If you only want to see if it's failed then just...
if ( $spamMarkerResult->failed() ) {
    // Do stuff
} 

每次您在字符串上调用check()方法时,它都会返回一个包含垃圾邮件检查结果和消息的SpamOutput对象。

您可以通过两种方法更改其中的各种内容,例如每个过滤器的消息:

调用setOption函数

$loadedFilter->setOption('message', 'My own returning error message');

或者通过不同的配置初始化

$config = array(
    'message' => 'My own custom error message'
);

$blackListed = new BlackListed($config);

当前提供的过滤器

1. 黑名单过滤器

黑名单检测器会将包含黑名单中任何一词或多个词的字符串标记为垃圾邮件。字符串可以由正则表达式或字符序列形成。

2. 黑名单文件过滤器

这个与上面类似,但它从提供的文件夹中获取正则表达式和字符串。目前文件夹位于src/Filter/blacklists。如果您想提供自己的文件,在初始化时必须声明文件夹路径。

$config = array(
    'dir' => '/path/to/your/folder'
);

$blackListedFile = new BlackListedFile($config);

请务必不要忘记添加包含提供目录中文件名的index文件。

3. 邮件过多过滤器

名字已经说明了问题。它匹配字符串中的链接数量。它可以像这样轻松配置

$config = array(
    'emailsAllowed' => 2
);
4. 链接过多过滤器

字符串中可以允许多少个链接

$config = array(
    'linksAllowed' => 2,
    'linksRatio' => 20 // Percentage of links to words ratio within the text
);
5. 电话号码过多过滤器

可以允许多少个电话号码。

$config = array(
    'phonesAllowed' => 2
);
6. 大写字母过多过滤器

字符串中允许多少个大写单词。当有人输入FREE FREE BLAH BLAH DISCOUNT OFF时很有用。

$config = array(
    'wordsRatio' => 20 // Percentage of allowed uppercase words in a string
);

创建您自己的自定义过滤器

您可以通过创建一个实现定义以下合同的类的检测器来创建一个检测器。

interface FilterInterface
    {
        public function filter($data);
    }

您的过滤器必须返回一个数组

array(
    'founded' => true, // or false if spam not found
    'message' => 'message to return if spam found'
);

创建您自己的过滤器后,您可以使用SpamMarker中的registerFilter()方法添加它。

许可证

MIT许可证(MIT)。有关更多信息,请参阅LICENSE。

太棒了!