You are here

function spambot_sfs_request in Spambot 7

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

Invoke www.stopforumspam.com's api.

Parameters

array $query: A keyed array of url parameters ie. array('email' => 'blah@blah.com').

array $data: An array that will be filled with the data from www.stopforumspam.com.

Return value

bool TRUE on successful request (and $data will contain the data) FALSE otherwise.

3 calls to spambot_sfs_request()
spambot_account_is_spammer in ./spambot.module
Checks an account to see if it's a spammer.
spambot_user_register_form_validate in ./spambot.module
Validate callback for user_register form.
_spambot_user_spam_admin_form_submit_check in ./spambot.pages.inc
Do complex checking at this user account.

File

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

Code

function spambot_sfs_request(array $query, array &$data) {

  // An empty request results in no match.
  if (empty($query)) {
    return FALSE;
  }

  // Attempt to return a response from the cache bins if cache is enabled.
  $cache_enabled = variable_get('spambot_enable_cache', TRUE);
  if ($cache_enabled) {
    if (isset($query['email'])) {
      $query['email'] = is_array($query['email']) ? $query['email'] : array(
        $query['email'],
      );
      foreach ($query['email'] as $query_email) {
        $cache_email = cache_get("email:{$query_email}", 'cache_spambot');
        if ($cache_email !== FALSE) {
          $data['email'] = $cache_email->data;
          unset($query_email);
        }
      }
    }
    if (isset($query['username'])) {
      $cache_username = cache_get("username:{$query['username']}", 'cache_spambot');
      if ($cache_username !== FALSE) {
        $data['username'] = $cache_username->data;
        unset($query['username']);
      }
    }
    if (isset($query['ip'])) {
      $cache_ip = cache_get("ip:{$query['ip']}", 'cache_spambot');
      if ($cache_ip !== FALSE) {
        $data['ip'] = $cache_ip->data;
        unset($query['ip']);
      }
    }

    // Serve only a cached response if one exists.
    if (!empty($data)) {
      $data['success'] = TRUE;
      return TRUE;
    }
  }

  // Use php serialisation format.
  $query['f'] = 'serial';
  $url = 'http://www.stopforumspam.com/api?' . http_build_query($query, '', '&');
  $result = drupal_http_request($url);
  if (!empty($result->code) && $result->code == 200 && empty($result->error) && !empty($result->data)) {
    $data = unserialize($result->data);

    // Store responses to the cache for fast lookups.
    if ($cache_enabled) {
      $expire = variable_get('spambot_cache_expire', CACHE_PERMANENT);
      $expire = $expire != CACHE_PERMANENT ? time() + $expire : CACHE_PERMANENT;
      $expire_false = variable_get('spambot_cache_expire_false', 300);
      $expire_false = $expire_false != CACHE_PERMANENT ? time() + $expire_false : CACHE_PERMANENT;
      if (!empty($data['email'])) {
        foreach ($data['email'] as $data_email) {
          $expire_email = $data_email['appears'] ? $expire : $expire_false;
          cache_set("email:{$data_email['value']}", $data_email, 'cache_spambot', $expire_email);
        }
      }
      if (!empty($data['username'])) {
        foreach ($data['username'] as $data_username) {
          $expire_username = $data_username['appears'] ? $expire : $expire_false;
          cache_set("username:{$query['username']}", $data_username, 'cache_spambot', $expire_username);
        }
      }
      if (!empty($data['ip'])) {
        $expire_ip = $data['ip']['appears'] ? $expire : $expire_false;
        cache_set("ip:{$query['ip']}", $data['ip'], 'cache_spambot', $expire_ip);
      }
    }
    if (!empty($data['success'])) {
      return TRUE;
    }
    else {
      watchdog('spambot', "Request unsuccessful: %url <pre>\n@dump</pre>", array(
        '%url' => $url,
        '@dump' => print_r($data, TRUE),
      ));
    }
  }
  else {
    watchdog('spambot', "Error contacting service: %url <pre>\n@dump</pre>", array(
      '%url' => $url,
      '@dump' => print_r($result, TRUE),
    ));
  }
  return FALSE;
}