0.1.0
0.1.0.0An extensible simple spam detector library
MIT
The Requires
- php >=5.3.0
Wallogit.com
2017 © Pedro Peláez
An extensible simple spam detector library
Spam Filter is a simple library for detecting spam messages. It follows the open closed principle by introducing Spam Detectors which are just separate classes used to extend the spam filter detecting capabilities., (*1)
Spam Filter library can be loaded into your projects using Composer or by loading the inbuilt autoloader., (*3)
You can define the spam filter as a dependency in your project. Below is a minimal setup required, (*4)
{
"require" : {
"morrelinko/spam-detector": "0.2.0"
}
}
If you are not using composer for your dependency (which you should) there is a simple autoloader packaged with this library which you can just 'include()' into your project files, (*5)
require_once '/path/to/spam-detector/autoload.php';
This should be done once throughout your app, (*6)
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 }
Each time you call the check() method on a string, it returns a SpamResult
Object which holds the ... hmm ... spam check result., (*7)
You could provide more information about the entity trying to perform the action you are checking against the spam detector., (*8)
<?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
}
Some detectors will require these extra information to `perform`..., (*9)
The black list detector flags a string as a spam if it contains any of one or more words that has been added to the black list. Strings could be formed from Regular Expressions or a Character Sequence., (*10)
The link rifle detector checks if a text contains too many links based on the max links allowed and the percentage ratio of links to words.. You can also modify these values to your taste., (*11)
You create a detector simply by creating a class that implements the SpamDetectorInterface
which defines the following contract., (*12)
interface SpamDetectorInterface
{
public function detect($data);
}
The prepared data passed as the argument is made up of an array with these values., (*13)
If your detector returns true then the text is flagged as spam otherwise not spam if false is returned., (*14)
Below is an example of a "fantastic" spam detector that checks if a text is above 200 words and flags it as spam., (*15)
Its not usable, its just an example., (*16)
class LengthTooLong implements SpamDetectorInterface { public function detect($string) { if (str_word_count($string) > 200) { return true; } return false; } }
After creating your spam detector, you add it using the registerDetector() method in the SpamFilter, (*17)
... $lengthTooLong = new LengthTooLong(); $spamFilter->registerDetector($lengthTooLong);
The MIT License (MIT). Please see License File for more information., (*18)
Enjoy!!, (*19)
An extensible simple spam detector library
MIT