2017 © Pedro Peláez
 

library spam-detector

An extensible simple spam detector library

image

morrelinko/spam-detector

An extensible simple spam detector library

  • Friday, August 19, 2016
  • by morrelinko
  • Repository
  • 3 Watchers
  • 21 Stars
  • 6,518 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 3 Versions
  • 1 % Grown

The README.md

Spam Detector

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)

Build Status, (*2)

Installation

Spam Filter library can be loaded into your projects using Composer or by loading the inbuilt autoloader., (*3)

Composer Installation

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"
    }
}
Using autoload.php

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';

Setup

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);

Usage


// 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)

Currently Supported Spam Detectors

1. BlackList Detector:

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)

2. LinkRife Detector:

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)

Creating your own custom Detector

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)

  • 'name' => Optional name of the user. Could be username or full name [This is provided by you].
  • 'email' => Optional e-mail address of the user [This is provided by you]
  • 'text' => The content of the message [This is provided by you]
  • 'ip' => The IP address of the user
  • 'user_agent': The browser user-agent of the user

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);

Licence

The MIT License (MIT). Please see License File for more information., (*18)

Enjoy!!, (*19)

The Versions

19/08 2016

dev-master

9999999-dev

An extensible simple spam detector library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Morrison Laju

17/08 2016

0.2.0

0.2.0.0

An extensible simple spam detector library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Morrison Laju

16/01 2014

0.1.0

0.1.0.0

An extensible simple spam detector library

  Sources   Download

MIT

The Requires

  • php >=5.3.0