morrelinko/spam-detector

此包已被弃用,不再维护。未建议替代包。

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

0.2.0 2016-08-17 09:08 UTC

This package is not auto-updated.

Last update: 2024-07-26 21:07:20 UTC


README

垃圾邮件过滤器是一个用于检测垃圾邮件的简单库。它通过引入用于扩展垃圾邮件检测功能的独立类(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)。请参阅 许可证文件 了解更多信息。

祝您享受!