1. Go to this page and download the library: Download tobento/app-spam library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tobento / app-spam example snippets
use Tobento\App\AppFactory;
use Tobento\App\Spam\DetectorsInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'public', 'public')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots:
$app->boot(\Tobento\App\Spam\Boot\Spam::class);
$app->booting();
// Implemented interfaces:
$detectors = $app->get(DetectorsInterface::class);
// Run the app
$app->run();
<form>
<!-- Using the default -->
<?= $view->spamDetector()->render($view)
use Tobento\App\Spam\Factory;
<form>
<?= $view->spamDetector(new Factory\Honeypot(inputName: 'name'))->render($view)
use Tobento\App\AppFactory;
use Tobento\App\Spam\Middleware\ProtectAgainstSpam;
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'public', 'public')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots:
$app->boot(\Tobento\App\View\Boot\View::class);
$app->boot(\Tobento\App\View\Boot\Form::class);
$app->boot(\Tobento\App\Spam\Boot\Spam::class);
$app->booting();
// Routes:
$app->route('POST', 'register', function() {
// being spam protected!
return 'response';
})->middleware([
ProtectAgainstSpam::class,
// you may specify a specific detector other than the default:
'detector' => 'register',
// or you may specify a detector factory:
'detector' => new Factory\Composite(
new Factory\Named('default'),
new Factory\WithoutUrl(inputNames: ['message']),
),
]);
// Run the app:
$app->run();
use Tobento\App\Spam\Detector;
$detector = new Detector\Composite(
'default',
new Detector\Honeypot(name: 'default', inputName: 'hp'),
);
use Tobento\App\Spam\Detector;
$detector = new Detector\EmailDomain(
name: 'default',
inputName: 'email',
blacklist: ['mail.ru'], // Email domains considered as spam.
whitelist: ['gmail.com'], // Email domains not considered as spam, exludes from blacklist.
);
use Tobento\App\Spam\Detector;
$detector = new Detector\EmailRemote(
name: 'default',
inputName: 'email',
checkDNS: true,
checkSMTP: true,
checkMX: true,
timeoutInSeconds: 5,
);
use Tobento\App\Spam\Detector;
$detector = new Detector\Honeypot(
name: 'default',
inputName: 'hp',
);
use Psr\Clock\ClockInterface;
use Tobento\App\Spam\Detector;
use Tobento\Service\Encryption\EncrypterInterface;
$detector = new Detector\MinTimePassed(
encrypter: $encrypter, // EncrypterInterface
clock: $clock, // ClockInterface
name: 'default',
inputName: 'mtp',
milliseconds: 1000,
);
use Tobento\App\Spam\Detector;
$detector = new Detector\NullDetector(name: 'null');
use Tobento\App\Spam\Detector;
$detector = new Detector\WithoutUrl(
name: 'default',
inputNames: ['message'],
);
use Tobento\App\Spam\Factory;
$factory = new Factory\Composite(
new Factory\Honeypot(),
);
use Tobento\App\Spam\Factory;
$factory = new Factory\EmailRemote(
inputName: 'email',
checkDNS: true,
checkSMTP: true,
checkMX: true,
timeoutInSeconds: 5,
);
use Tobento\App\Spam\Factory;
$factory = new Factory\Honeypot(
// you may change the default input name:
inputName: 'hp',
);
use Tobento\App\Spam\Factory;
$factory = new Factory\MinTimePassed(
// you may change the default input name:
inputName: 'mtp',
// you may change the default input name:
milliseconds: 1000,
// you may change the enrypter to be used, otherwise the default is used:
encrypterName: 'spam',
);
use Tobento\App\Spam\Factory;
$factory = new Factory\Named(
detector: 'default',
);
use Tobento\App\Spam\Factory;
$factory = new Factory\WithoutUrl(
inputNames: ['message'],
);
use Tobento\App\Spam\Detector;
use Tobento\App\Spam\DetectorInterface;
use Tobento\App\Spam\Factory;
return [
// ...
'detectors' => [
// using a factory:
'default' => new Factory\Composite(
new Factory\Honeypot(inputName: 'hp'),
new Factory\MinTime(milliseconds: 1000),
),
// using a closure:
'secondary' => static function (string $name): DetectorInterface {
return new Detector\Composite(
new Detector\Honeypot(name: $name, inputName: 'hp'),
);
},
// using a class instance:
'null' => new Detector\NullDetector(name: 'null'),
],
];
use Tobento\App\Boot;
use Tobento\App\Spam\Boot\Spam;
use Tobento\App\Spam\Detector;
use Tobento\App\Spam\DetectorFactoryInterface;
use Tobento\App\Spam\DetectorsInterface;
use Tobento\App\Spam\Factory;
class SpamDetectorsBoot extends Boot
{
public const BOOT = [
// you may ensure the spam boot.
Spam::class,
];
public function boot()
{
// you may use the app on method to add only if requested:
$app->on(
DetectorsInterface::class,
static function(DetectorsInterface $detectors) {
$detectors->add(
name: 'null',
detector: new Detector\NullDetector(), // DetectorInterface|DetectorFactoryInterface
);
}
);
}
}
use Psr\Http\Message\ServerRequestInterface;
use Tobento\App\Spam\DetectorsInterface;
use Tobento\App\Spam\Exception\SpamDetectedException;
class SpamService
{
public function isSpam(ServerRequestInterface $request, DetectorsInterface $detectors): bool
{
try {
$detectors->get('name')->detect($request);
} catch (SpamDetectedException $e) {
return true;
}
return false;
}
}
use Psr\Http\Message\ServerRequestInterface;
use Tobento\App\Spam\DetectorsInterface;
use Tobento\App\Spam\Exception\SpamDetectedException;
class SpamService
{
public function isSpam(mixed $value, DetectorsInterface $detectors): bool
{
try {
$detectors->get('name')->detectFromValue($value);
} catch (SpamDetectedException $e) {
return true;
}
return false;
}
}
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Adding boots
$app->boot(\Tobento\App\Spam\Boot\ValidationSpamRule::class);
$app->boot(\Tobento\App\Spam\Boot\Spam::class);
// Run the app
$app->run();
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Adding boots
$app->boot(\Tobento\App\Validation\Boot\Validator::class);
$app->boot(\Tobento\App\Spam\Boot\Spam::class);
// Run the app
$app->run();
use Tobento\App\Spam\Factory;
use Tobento\App\Spam\Validation\SpamRule;
$validation = $validator->validating(
value: '[email protected]',
rules: [
// using a detector name:
new SpamRule(
detector: 'email',
// you may specify a custom error message:
errorMessage: 'Custom error message',
),
// using a detector factory:
new SpamRule(detector: new Factory\Named('email')),
// or if booted the ValidationSpamRule::class:
'spam:email',
// or with multiple detector names:
'spam:emailRemote:emailDomain',
],
);
$validation = $validator->validating(
value: 'foo',
rules: [
// skips validation:
new SpamRule(detector: 'email', skipValidation: true),
// does not skip validation:
new SpamRule(detector: 'email', skipValidation: false),
// skips validation:
new SpamRule(detector: 'email', skipValidation: fn (mixed $value): bool => $value === 'foo'),
],
);
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Adding boots
// $app->boot(\Tobento\App\Spam\Boot\HttpSpamErrorHandler::class); // not needed to boot!
$app->boot(\Tobento\App\Spam\Boot\Spam::class);
// Run the app
$app->run();
app/config/spam.php
app/config/spam.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.