You are here

function spambot_account_is_spammer in Spambot 8

Same name and namespace in other branches
  1. 6.3 spambot.module \spambot_account_is_spammer()
  2. 7 spambot.module \spambot_account_is_spammer()

Checks an account to see if it's a spammer.

This one uses configurable automated criteria checking of email and username only.

Parameters

object $account: User account.

Return value

int Positive if spammer, 0 if not spammer, negative if error.

1 call to spambot_account_is_spammer()
spambot_cron in ./spambot.module
Implements hook_cron().

File

./spambot.module, line 148
Main module file.

Code

function spambot_account_is_spammer($account, $config) {

  // Number of times email has been reported as spam in the forum.
  $email_threshold = $config
    ->get('spambot_criteria_email');
  $username_threshold = $config
    ->get('spambot_criteria_username');
  $ip_threshold = $config
    ->get('spambot_criteria_ip');

  // Build request parameters according to the criteria to use.
  $request = [];
  if (!empty($account
    ->getEmail()) && $email_threshold > 0 && !spambot_check_whitelist('email', $config, $account
    ->getEmail())) {
    $request['email'] = $account
      ->getEmail();
  }
  if (!empty($account
    ->getDisplayName()) && $username_threshold > 0 && !spambot_check_whitelist('username', $config, $account
    ->getDisplayName())) {
    $request['username'] = $account
      ->getDisplayName();
  }

  // Only do a remote API request if there is anything to check.
  if ($request) {
    $data = [];
    if (spambot_sfs_request($request, $data)) {
      if ($email_threshold > 0 && !empty($data['email']['appears']) && $data['email']['frequency'] >= $email_threshold || $username_threshold > 0 && !empty($data['username']['appears']) && $data['username']['frequency'] >= $username_threshold) {
        return 1;
      }
    }
    else {

      // Return error.
      return -1;
    }
  }

  // Now check IP's
  // If any IP matches the threshold, then flag as a spammer.
  if ($ip_threshold > 0) {
    $ips = spambot_account_ip_addresses($account);
    foreach ($ips as $ip) {

      // Skip the loopback interface.
      if ($ip == '127.0.0.1') {
        continue;
      }
      elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) === FALSE) {
        \Drupal::logger('spambot')
          ->notice('Invalid IP address: %ip (uid=%uid, name=%name, email=%email). Spambot will not rely on it', [
          '%ip' => $ip,
          '%name' => $account
            ->getDisplayName(),
          '%email' => $account
            ->getEmail(),
          '%uid' => $account
            ->id(),
        ]);
        continue;
      }
      $request = [
        'ip' => $ip,
      ];
      $data = [];
      if (spambot_sfs_request($request, $data)) {
        if (!empty($data['ip']['appears']) && $data['ip']['frequency'] >= $ip_threshold) {
          return 1;
        }
      }
      else {

        // Abort on error.
        return -1;
      }
    }
  }

  // Return no match.
  return 0;
}