linko/spamfilter

此软件包已废弃,不再维护。未建议替代包。

一个可扩展的简单垃圾邮件检测库

0.2.0 2016-08-17 09:08 UTC

This package is not auto-updated.

Last update: 2024-07-29 13:57:55 UTC


README

Spam Filter是一个简单的垃圾邮件检测库。它通过引入Spam Detectors(用于扩展垃圾邮件检测能力的独立类)遵循开闭原则。

Build Status

安装

您可以使用Composer或内置的自动加载器将Spam Filter库加载到项目中。

Composer安装

您可以在项目中将垃圾邮件过滤器定义为依赖项。以下是最小配置要求

{
	"require" : {
		"morrelinko/spam-detector": "0.2.0"
	}
}
使用autoload.php

如果您没有使用composer作为依赖项(您应该使用),这个库附带了一个简单的自动加载器,您可以直接将其包含到项目文件中

	require_once '/path/to/spam-detector/autoload.php';

设置

这应该在整个应用程序中只执行一次

use SpamDetector\SpamDetector;

// Create a black list spam detector
$blackListDetector = new BlackList();

// add some text string to the black list detector
$blackListDetector->add('example.com');
$blackListDetector->add('127.0.0.1');

// Create the spam filter
$spamDetector = new SpamDetector();

// Register the spam detector
$spamDetector->registerDetector($blackListDetector);

用法

// Run the check
$spamCheckResult = $spamDetector->check("
	Hello, this is some text containing example.com
	and should fail as it has a word that is black-listed
");

if($spamCheckResult->passed()) {
	// Do stuff
}

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

您可以为尝试执行您检查的垃圾邮件检测器的实体提供更多信息。

<?php

$check = $spamDetector->check(array(
    'name' => 'johndoe',
    'email' => 'johndoe@gmail.com',
    'text' => 'Hello, this is some clean comment John Doe is trying to post'
));

if ($check->passed()) {
    // Post comment
}

一些检测器将需要这些额外信息来“执行”...

当前支持的垃圾邮件检测器

1. 黑名单检测器

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

2. LinkRife检测器

LinkRife检测器检查文本是否包含过多链接,基于允许的最大链接数和链接与单词的比例。您也可以根据自己的喜好修改这些值。

创建自己的自定义检测器

您可以通过创建一个实现SpamDetectorInterface接口的类来创建检测器,该接口定义了以下合同。

interface SpamDetectorInterface
{
    public function detect($data);
}

作为参数传递的准备好的数据由包含这些值的数组组成。

  • 'name' => 可选的用户名。可以是用户名或全名 [这是由您提供的]。
  • 'email' => 可选的用户电子邮件地址 [这是由您提供的]
  • 'text' => 消息的内容 [这是由您提供的]
  • 'ip' => 用户的IP地址
  • 'user_agent': 用户的浏览器用户代理

如果您的检测器返回true,则文本将被标记为垃圾邮件,如果返回false,则不会被标记为垃圾邮件。

以下是一个“极好的”垃圾邮件检测器的示例,它会检查文本是否超过200个单词,并将其标记为垃圾邮件。

它不可用,只是个例子。

class LengthTooLong implements SpamDetectorInterface
{
	public function detect($string)
	{
		if (str_word_count($string) > 200) {
			return true;
		}

		return false;
	}
}

创建垃圾邮件检测器后,您可以使用SpamFilter中的registerDetector()方法添加它

...

$lengthTooLong = new LengthTooLong();

$spamFilter->registerDetector($lengthTooLong);

许可

MIT许可证(MIT)。请参阅许可文件以获取更多信息。

享受吧!!