You are here

spambot.pages.inc in Spambot 6.3

Same filename and directory in other branches
  1. 7 spambot.pages.inc

File

spambot.pages.inc
View source
<?php

function spambot_user_spam_admin_form($form_state, $account) {
  $key = variable_get('spambot_sfs_api_key', FALSE);
  $node_count = db_result(db_query("SELECT COUNT(nid) FROM {node} WHERE uid = %d", $account->uid));
  if (module_exists('comment')) {
    $comment_count = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE uid = %d", $account->uid));
    $status = t('This account has @n nodes and @c comments.', array(
      '@n' => $node_count,
      '@c' => $comment_count,
    ));
  }
  else {
    $status = t('This account has @n nodes.', array(
      '@n' => $node_count,
    ));
  }
  $form = array();
  $form['check'] = array(
    '#type' => 'submit',
    '#value' => t('Check if this account matches a known spammer'),
  );
  $form['action'] = array(
    '#type' => 'fieldset',
    '#title' => t('Take action against this account'),
    '#collapsible' => TRUE,
    '#description' => $status,
  );
  $form['action']['unpublish_content'] = array(
    '#type' => 'checkbox',
    '#title' => t('Unpublish nodes and comments by this account'),
    '#default_value' => TRUE,
  );
  $form['action']['delete_content'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete nodes and comments by this account'),
    '#default_value' => FALSE,
  );
  $form['action']['report'] = array(
    '#type' => 'fieldset',
    '#title' => t('Report this account to www.stopforumspam.com'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
  );

  // Fetch a list of reportable nodes
  $form['action']['report']['nids'] = array();
  $result = db_query("SELECT nid, hostname FROM {node_spambot} WHERE uid = %d ORDER BY nid DESC LIMIT 20", $account->uid);
  $nid_hostnames = array();
  while ($object = db_fetch_object($result)) {
    $nid_hostnames[$object->nid] = $object->hostname;
  }
  foreach ($nid_hostnames as $nid => $hostname) {
    $node = node_load($nid);
    if (!empty($node->nid)) {
      $form['action']['report']['nids'][$nid] = array(
        '#type' => 'checkbox',
        '#title' => l(mb_strimwidth($node->title, 0, 128, '...'), 'node/' . $nid, array(
          'attributes' => array(
            'title' => $node->teaser,
          ),
        )) . ' ' . t('(node, ip=@ip)', array(
          '@ip' => $hostname,
        )),
        '#disabled' => empty($key),
      );
    }
  }

  // Fetch a list of reportable comments
  if (module_exists('comment')) {
    $form['action']['report']['cids'] = array();
    $result = db_query("SELECT cid FROM {comments} WHERE uid = %d ORDER BY cid DESC LIMIT 20", $account->uid);
    $cids = array();
    while ($object = db_fetch_object($result)) {
      $cids[$object->cid] = $object->cid;
    }
    foreach ($cids as $cid) {
      $comment = _comment_load($cid);
      if (!empty($comment->cid)) {
        $form['action']['report']['cids'][$cid] = array(
          '#type' => 'checkbox',
          '#title' => l(mb_strimwidth($comment->subject, 0, 128, '...'), 'node/' . $comment->nid, array(
            'fragment' => 'comment-' . $comment->cid,
            'attributes' => array(
              'title' => mb_strimwidth($comment->comment, 0, 256, '...'),
            ),
          )) . ' ' . t('(comment, ip=@ip)', array(
            '@ip' => $comment->hostname,
          )),
          '#disabled' => empty($key),
        );
      }
    }
  }
  if ($key) {
    $evidence_count = count($form['action']['report']['nids']) + count($form['action']['report']['cids']);
    $form['action']['report']['#description'] = $evidence_count ? t('Select one or more posts below to report them to www.stopforumspam.com.') : t('This account cannot be reported because no evidence or IP address is available.');
  }
  else {
    $form['action']['report']['#description'] = t('An API key from <a href="http://www.stopforumspam.com">www.stopforumspam.com</a> must <a href="!admin-url">be configured</a> to report spammers.', array(
      '!admin-url' => url('admin/settings/spambot'),
    ));
  }
  $form['action']['block_user'] = array(
    '#type' => 'checkbox',
    '#title' => t('Block this account'),
    '#default_value' => TRUE,
  );
  $form['action']['delete_user'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete this account'),
    '#default_value' => FALSE,
  );
  $form['action']['action'] = array(
    '#type' => 'submit',
    '#value' => t('Take action'),
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $account->uid,
  );
  return $form;
}
function spambot_user_spam_admin_form_validate($form, &$form_state) {
  $key_required = !empty($form_state['values']['report']['nids']) && count(array_filter($form_state['values']['report']['nids'])) ? TRUE : FALSE;
  if (module_exists('comment')) {
    $key_required = !empty($form_state['values']['report']['cids']) && count(array_filter($form_state['values']['report']['cids'])) || $key_required;
  }
  if ($key_required && !variable_get('spambot_sfs_api_key', FALSE)) {
    form_set_error('', t('To report spammers to www.stopforumspam.com, you need to register for an API key at <a href="http://www.stopforumspam.com">www.stopforumspam.com</a> and enter it into the !page.', array(
      '!page' => l('spambot settings', 'admin/settings/spambot'),
    )));
  }
}
function spambot_user_spam_admin_form_submit($form, &$form_state) {
  $account = user_load($form_state['values']['uid']);
  if ($form_state['values']['op'] == $form_state['values']['check']) {

    // This is a more comprehensive check than the automated criteria checks.
    // This tests everything.
    $messages = array();
    $service_down = FALSE;

    // Check email and username
    $request = array(
      'email' => $account->mail,
      'username' => $account->name,
    );
    $data = array();
    if (spambot_sfs_request($request, $data)) {
      if (!empty($data['email']['appears'])) {
        $messages[] = t('This account\'s email address matches @num times: !link', array(
          '!link' => l($request['email'], 'http://www.stopforumspam.com/search?q=' . $request['email']),
          '@num' => $data['email']['frequency'],
        ));
      }
      if (!empty($data['username']['appears'])) {
        $messages[] = t('This account\'s username matches @num times: !link', array(
          '!link' => l($request['username'], 'http://www.stopforumspam.com/search?q=' . $request['username']),
          '@num' => $data['username']['frequency'],
        ));
      }
    }
    else {
      drupal_set_message(t('Error contacting service.'), 'warning');
      $service_down = TRUE;
    }

    // Check IP addresses
    if (!$service_down) {
      $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) === FALSE) {
          $messages[] = t('Invalid IP address: @ip. Spambot will not rely on it.', array(
            '@ip' => $ip,
          ));
          continue;
        }
        $request = array(
          'ip' => $ip,
        );
        $data = array();
        if (spambot_sfs_request($request, $data)) {
          if (!empty($data['ip']['appears'])) {
            $messages[] = t('An IP address !ip used by this account matches @num times.', array(
              '!ip' => l($ip, 'http://www.stopforumspam.com/search?q=' . $ip),
              '@num' => $data['ip']['frequency'],
            ));
          }
        }
        else {
          drupal_set_message(t('Error contacting service.'), 'warning');
          $service_down = TRUE;
          break;
        }
      }
    }
    if (count($messages)) {
      foreach ($messages as $message) {
        drupal_set_message($message);
      }
    }
    else {
      drupal_set_message(t('No matches against known spammers found.'));
    }
  }
  else {
    if ($form_state['values']['op'] == $form_state['values']['action']) {
      if ($account->uid == 1) {
        drupal_set_message(t('Sorry, taking action against uid 1 is not allowed.'));
        return;
      }

      // Block account
      if (!empty($form_state['values']['block_user'])) {
        if ($account->status) {
          user_save($account, array(
            'status' => 0,
          ));
          drupal_set_message(t('Account blocked.'));
        }
        else {
          drupal_set_message(t('This account is already blocked.'));
        }
      }

      // Prepare some data
      $nodes = array();
      $result = db_query("SELECT nid FROM {node} WHERE uid = %d ORDER BY nid", $account->uid);
      while ($object = db_fetch_object($result)) {
        $nodes[$object->nid] = $object->nid;
      }
      $node_hostnames = array();
      $result = db_query("SELECT nid, hostname FROM {node_spambot} WHERE uid = %d ORDER BY nid", $account->uid);
      while ($object = db_fetch_object($result)) {
        $node_hostnames[$object->nid] = $object->hostname;
      }
      $comments = array();
      if (module_exists('comment')) {
        $result = db_query("SELECT cid FROM {comments} WHERE uid = %d ORDER BY cid", $account->uid);
        while ($object = db_fetch_object($result)) {
          $comments[$object->cid] = $object->cid;
        }
      }

      // Report posts to www.stopforumspam.com
      if (!empty($form_state['values']['report']['nids'])) {
        foreach (array_filter($form_state['values']['report']['nids']) as $nid => $unused) {
          $node = node_load($nid);
          if (!empty($node->nid)) {
            if (spambot_report_account($account, $node_hostnames[$nid], $node->title . "\n\n" . $node->body)) {
              drupal_set_message(t('Node %title has been reported.', array(
                '%title' => $node->title,
              )));
            }
            else {
              drupal_set_message(t('There was a problem reporting node %title.', array(
                '%title' => $node->title,
              )));
            }
          }
        }
      }
      if (module_exists('comment') && !empty($form_state['values']['report']['cids'])) {
        foreach (array_filter($form_state['values']['report']['cids']) as $cid => $unused) {
          $comment = _comment_load($cid);
          if (!empty($comment->cid)) {
            if (spambot_report_account($account, $comment->hostname, $comment->subject . "\n\n" . $comment->comment)) {
              drupal_set_message(t('Comment %title has been reported.', array(
                '%title' => $comment->subject,
              )));
            }
            else {
              drupal_set_message(t('There was a problem reporting comment %title.', array(
                '%title' => $comment->subject,
              )));
            }
          }
        }
      }

      // Delete nodes and content
      if (!empty($form_state['values']['delete_content'])) {
        if (count($nodes)) {
          foreach ($nodes as $nid) {
            node_delete($nid);
          }
        }
        if (count($comments)) {
          foreach ($comments as $cid) {
            $comment = _comment_load($cid);
            module_load_include('inc', 'comment', 'comment.admin');
            _comment_delete_thread($comment);
            _comment_update_node_statistics($comment->nid);
          }
          cache_clear_all();
        }
        drupal_set_message(t('Nodes and comments have been deleted.'));
      }
      else {
        if (!empty($form_state['values']['unpublish_content'])) {

          // Unpublish nodes and content
          if (count($nodes)) {
            module_load_include('inc', 'node', 'node.admin');
            node_mass_update($nodes, array(
              'status' => 0,
            ));
          }
          if (count($comments)) {
            db_query("UPDATE {comments} SET status = %d WHERE uid = %d", COMMENT_NOT_PUBLISHED, $account->uid);
            cache_clear_all();
          }
          drupal_set_message(t('Nodes and comments have been unpublished.'));
        }
      }

      // Delete user
      if (!empty($form_state['values']['delete_user'])) {

        // Redirect to user delete form
        $form_state['redirect'] = 'user/' . $account->uid . '/delete';
      }
    }
  }
}