function spambot_cron in Spambot 8
Same name and namespace in other branches
- 6.3 spambot.module \spambot_cron()
- 7 spambot.module \spambot_cron()
Implements hook_cron().
File
- ./
spambot.module, line 42 - Main module file.
Code
function spambot_cron() {
$config = \Drupal::config('spambot.settings');
// Checks the user limit added in the configuration.
if ($limit = $config
->get('spambot_cron_user_limit')) {
$last_uid = \Drupal::state()
->get('spambot_last_checked_uid', 0);
if ($last_uid < 1) {
// Skip scanning the anonymous and superadmin users.
$last_uid = 1;
}
$query = \Drupal::database()
->select('users')
->fields('users', [
'uid',
])
->condition('uid', $last_uid, '>')
->orderBy('uid')
->range(0, $limit);
// This checks the Users with the Blocked account for Spam also.
if (!$config
->get('spambot_check_blocked_accounts')) {
// @todo implement filter for non blocked accounts.
}
$uids = $query
->execute()
->fetchCol();
if ($uids) {
// Action to be done after the existing user is known as spam User.
$action = $config
->get('spambot_spam_account_action');
/** @var \Drupal\user\UserInterface[] $accounts */
$accounts = User::loadMultiple($uids);
foreach ($accounts as $account) {
$account_status = $account->status
->getValue()[0]['value'];
$result = spambot_account_is_spammer($account, $config);
if ($result > 0) {
switch ($account
->hasPermission('protected from spambot scans') ? SpambotSettingsForm::SPAMBOT_ACTION_NONE : $action) {
case SpambotSettingsForm::SPAMBOT_ACTION_BLOCK:
if ($account_status) {
$account
->block();
// Block spammer's account.
\Drupal::logger('spambot')
->notice('Blocked spam account: @name <@email> (uid @uid)', [
'@name' => $account
->getDisplayName(),
'@email' => $account
->getEmail(),
'@uid' => $account
->id(),
]);
}
else {
// Don't block an already blocked account.
\Drupal::logger('spambot')
->notice('Spam account already blocked: @name <@email> (uid @uid)', [
'@name' => $account
->getDisplayName(),
'@email' => $account
->getEmail(),
'@uid' => $account
->id(),
]);
}
break;
case SpambotSettingsForm::SPAMBOT_ACTION_DELETE:
$account
->delete();
\Drupal::logger('spambot')
->notice('Deleted spam account: @name <@email> (uid @uid)', [
'@name' => $account
->getDisplayName(),
'@email' => $account
->getEmail(),
'@uid' => $account
->id(),
]);
\Drupal::logger('spambot')
->notice('Deleted spam account: @name <@email> (uid @uid)', [
'@name' => $account
->getDisplayName(),
'@email' => $account
->getEmail(),
'@uid' => $account
->id(),
]);
break;
case SpambotSettingsForm::SPAMBOT_ACTION_NONE:
default:
\Drupal::logger('spambot')
->notice('Found spam account: @name <@email> (uid @uid)', [
'@name' => $account
->getDisplayName(),
'@email' => $account
->getEmail(),
'@uid' => $account
->id(),
]);
break;
}
// Mark this uid as successfully checked.
\Drupal::state()
->set('spambot_last_checked_uid', $account
->id());
}
elseif ($result == 0) {
// Mark this uid as successfully checked.
\Drupal::state()
->set('spambot_last_checked_uid', $account
->id());
}
elseif ($result < 0) {
// Error contacting service, so pause processing.
break;
}
}
}
}
}