dmFloodControlPlugin provides a way to limit the number of actions a client (identified with an IP) can do.
You can assign a number of credits for an action. When the client uses this action, a credit is used.
When all credits are used, a dmFloodControlOutOfCreditException is thrown.
This plugin can be used to prevent bots to send tons of mails from your website, for example.
Note that in this case, it is not as efficient as a captcha.
git clone git://github.com/knplabs/dmFloodControlPlugin.git plugins/dmFloodControlPlugin
class ProjectConfiguration extends dmProjectConfiguration { public function setup() { parent::setup(); $this->enablePlugins(array( // your enabled plugins 'dmFloodControlPlugin' ));
php symfony doctrine:generate-migrations-diff
php symfony doctrine:migrate
php symfony dm:setup
Suppose you have a website that allows you user to send emails to their friend (to share a page, for example).
You don't want a bot to use this feature to spam tons of mail boxes with your server. So you decide to restrict to 20 mails per IP.
Your current code, without flood control, looks like:
// send a mail to the $emails array
Let's add flood control to prevent the same IP to send more than 20 mails.
In your app.yml config file, choose a code for the send_mail action. Internally, dmFloodControlPlugin uses only integer as action_codes, to improve SQL performances.
/config/app.yml or /apps/front/config/app.yml
all:
flood_control:
send_mail_code: 1
send_mail_limit: 20
You can use whatever configuration system you want, as dmFloodControl won't use it directly.
Now let's add flood control before sending the mail to the $emails array.
$actionCode = sfConfig::get('app_flood_control_send_mail_code'); $limit = sfConfig::get('app_flood_control_send_mail_limit'); $nbCreditsUsed = count($emails); $this->getService('flood_control')->consume($actionCode, $limit, $nbCreditsUsed); // send a mail to the $emails array
by calling ->consume() you registers the fact that the current user IP is consuming $nbCreditsUsed for this $actionCode. If the number of credits used exceeds the $limit, a dmFloodControlOutOfCreditException is thrown.
You could catch this exception to handle it as you want:
try { $actionCode = sfConfig::get('app_flood_control_send_mail_code'); $limit = sfConfig::get('app_flood_control_send_mail_limit'); $nbCreditsUsed = count($emails); $this->getService('flood_control')->consume($actionCode, $limit, $nbCreditsUsed); // send a mail to the $emails array } catch(dmFloodControlOutOfCreditException $e) { // do something when the credits limit is exceeded }
To reset the credits used for an action and the current IP, you can use
$this->getService('flood_control')->resetActionForCurrentIp($actionCode);
To reset the credits used for an action and a given IP, you can use
$this->getService('flood_control')->resetActionForIp($actionCode, '127.0.0.1');
flood_control is a service defined in dmFloodControlPlugin/config/dm/services.yml.
You can replace the implementation class name and the default options in your own services.yml config file.
Open issues
Closed issues
dmFloodControlPlugin, created on April 23, 2010 by Thibault D, used by 22 projects