You are here

function spambot_cron in Spambot 6.3

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

Implementation of hook_cron

File

./spambot.module, line 142

Code

function spambot_cron() {
  $limit = variable_get('spambot_cron_user_limit', 0);
  if ($limit) {
    $last_uid = variable_get('spambot_last_checked_uid', 0);
    if ($last_uid < 1) {

      // Skip scanning the first account
      $last_uid = 1;
    }
    $result = db_query("SELECT uid FROM {users} WHERE uid > %d ORDER BY uid LIMIT %d", $last_uid, $limit);
    $uids = array();
    while ($object = db_fetch_object($result)) {
      $uids[$object->uid] = $object->uid;
    }
    $action = variable_get('spambot_spam_account_action', SPAMBOT_ACTION_NONE);
    foreach ($uids as $uid) {
      $account = user_load($uid);
      if ($account->status || variable_get('spambot_check_blocked_accounts', FALSE)) {
        $result = spambot_account_is_spammer($account);
        if ($result > 0) {
          $link = l(t('spammer'), 'user/' . $account->uid);
          switch (user_access('protected from spambot scans', $account) ? SPAMBOT_ACTION_NONE : $action) {
            case SPAMBOT_ACTION_BLOCK:
              if ($account->status) {
                user_save($account, array(
                  'status' => 0,
                ));
                watchdog('spambot', t('Blocked spam account: @name &lt;@email&gt; (uid @uid)', array(
                  '@name' => $account->name,
                  '@email' => $account->mail,
                  '@uid' => $account->uid,
                )), array(), WATCHDOG_NOTICE, $link);
              }
              else {

                // Don't block an already blocked account
                watchdog('spambot', t('Spam account already blocked: @name &lt;@email&gt; (uid @uid)', array(
                  '@name' => $account->name,
                  '@email' => $account->mail,
                  '@uid' => $account->uid,
                )), array(), WATCHDOG_NOTICE, $link);
              }
              break;
            case SPAMBOT_ACTION_DELETE:
              user_delete(array(), $account->uid);
              watchdog('spambot', t('Deleted spam account: @name &lt;@email&gt; (uid @uid)', array(
                '@name' => $account->name,
                '@email' => $account->mail,
                '@uid' => $account->uid,
              )), array(), WATCHDOG_NOTICE, $link);
              break;
            default:
              watchdog('spambot', t('Found spam account: @name &lt;@email&gt; (uid @uid)', array(
                '@name' => $account->name,
                '@email' => $account->mail,
                '@uid' => $account->uid,
              )), array(), WATCHDOG_NOTICE, $link);
              break;
          }

          // Mark this uid as successfully checked
          variable_set('spambot_last_checked_uid', $uid);
        }
        else {
          if ($result == 0) {

            // Mark this uid as successfully checked
            variable_set('spambot_last_checked_uid', $uid);
          }
          else {
            if ($result < 0) {

              // Error contacting service, so pause processing
              break;
            }
          }
        }
      }
    }
  }
}