You are here

public function RateBotDetector::checkIsBot in Rate 8.2

Check if the current user is blocked.

This function will first check if the user is already known to be a bot. If not, it will check if we have valid reasons to assume the user is a bot.

Return value

bool True if bot detected; false otherwise.

File

src/Plugin/RateBotDetector.php, line 198

Class

RateBotDetector
The rate.bot_detector service.

Namespace

Drupal\rate\Plugin

Code

public function checkIsBot() {
  if ($this
    ->isLocal()) {

    // The IP-address is a local IP-address. This is probably because of
    // misconfigured proxy servers. Do only the user agent check.
    return $this
      ->checkAgent();
  }
  if ($this
    ->checkIp()) {
    return TRUE;
  }
  if ($this
    ->checkAgent()) {

    // Identified as a bot by its user agent. Register this bot by IP-address
    // as well, in case this bots uses multiple agent strings.
    $this
      ->registerBot();
    return TRUE;
  }
  $threshold = $this->config
    ->get('bot_minute_threshold');
  if ($threshold && $this
    ->checkThreshold(60) > $threshold) {
    $this
      ->registerBot();
    return TRUE;
  }
  $threshold = $this->config
    ->get('bot_hour_threshold');

  // Always count, even if threshold is disabled. This is to determine if we
  // can skip the BotScout check.
  $count = $this
    ->checkThreshold(3600);
  if ($threshold && $count > $threshold) {
    $this
      ->registerBot();
    return TRUE;
  }
  if (!$count && $this
    ->checkBotscout()) {
    $this
      ->registerBot();
    return TRUE;
  }
  return FALSE;
}