You are here

node_age.module in Spam 5.3

File

filters/node_age/node_age.module
View source
<?php

/**
 * Node-age comment filter plug in for the spam module.
 * Copyright(c) 2008
 *  Jeremy Andrews <jeremy@tag1consulting.com>.  All rights reserved.
 *
 */

/**
 * Drupal _menu() hook.
 */
function node_age_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/spam/filters/node-age',
      'title' => t('Node age'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'node_age_admin_settings',
      ),
      'description' => t('Configure the node age filter.'),
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}
function node_age_spamapi($op, $type = NULL, $content = array(), $fields = array(), $extra = NULL) {
  switch ($op) {
    case 'filter':
      if (!module_invoke('spam', 'filter_enabled', 'node_age', $type, $content, $fields, $extra)) {
        return;
      }
      return node_age_spam_filter($content, $type, $fields, $extra);
    case 'filter_module':
      return 'node_age';
    case 'filter_info':
      return array(
        'name' => t('Node age'),
        'module' => t('node_age'),
        'description' => t('A node-age comment spam filter.'),
        'help' => t('The node-age filter assigns a higher spam probability to comments made against older nodes.'),
      );
      break;
    case 'filter_install':
      return array(
        'status' => SPAM_FILTER_ENABLED,
        'gain' => 150,
        'weight' => -2,
      );
  }
}

/**
 * Determine if a comment is being posted against too old of a node.
 */
function node_age_spam_filter($content, $type, $fields, $extra = array(), $filter_test = FALSE) {
  $action = array();
  $id = spam_invoke_module($type, 'content_id', $content, $extra);
  if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
    $nid = arg(2);
    spam_log(SPAM_DEBUG, 'node_age_spam_filter', t('retrieved nid (@nid) from url', array(
      '@nid' => $nid,
    )), $type, $id);
  }
  else {
    $nid = db_result(db_query('SELECT nid FROM {comments} WHERE cid = %d', $id));
    spam_log(SPAM_DEBUG, 'node_age_spam_filter', t('retrieved nid (@nid) from database', array(
      '@nid' => $nid,
    )), $type, $id);
  }
  if ($type == 'comment' && $nid) {
    $node = spam_invoke_module('node', 'load', $nid);
    if (is_object($node)) {
      $timestamp_field = variable_get('node_age_filter_on', 'created');
      if ($node->{$timestamp_field} < time() - variable_get('node_age_limit_long', 4838400)) {
        $action['total'] = variable_get('node_age_weight_long', 99);
        spam_log(SPAM_DEBUG, 'node_age_spam_filter', t('node (@nid) older than long limit, spam probability(@weight)', array(
          '@nid' => $nid,
          '@weight' => $action['total'],
        )), $type, $id);
      }
      else {
        if ($node->{$timestamp_field} < time() - variable_get('node_age_limit_short', 2419200)) {
          $action['total'] = variable_get('node_age_weight_short', 85);
          spam_log(SPAM_DEBUG, 'node_age_spam_filter', t('node (@nid) older than short limit, spam probability(@weight)', array(
            '@nid' => $nid,
            '@weight' => $action['total'],
          )), $type, $id);
        }
        else {
          $action['total'] = 0;
          spam_log(SPAM_DEBUG, 'node_age_spam_filter', t('node (@nid) is recent.', array(
            '@nid' => $nid,
          )), $type, $id);
        }
      }
    }
  }
  else {
    spam_log(SPAM_DEBUG, 'node_age_spam_filter', t('content type is not comment, skipping'), $type, $id);
    $action['total'] = 0;
  }
  return $action;
}

/**
 * Module administrative configuration options.
 */
function node_age_admin_settings() {
  $form = array();
  $form['short'] = array(
    '#type' => 'fieldset',
    '#title' => 'Old content',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $limits = drupal_map_assoc(spam_range(604800, 14515200, 604800), 'format_interval');
  $weights = drupal_map_assoc(array(
    60,
    65,
    70,
    75,
    80,
    81,
    82,
    83,
    84,
    85,
    86,
    87,
    88,
    89,
    90,
    91,
    92,
    93,
    94,
    95,
    96,
    97,
    98,
    99,
  ));
  $form['short']['node_age_limit_short'] = array(
    '#type' => 'select',
    '#title' => t('Old content is content not published within the past'),
    '#options' => $limits,
    '#required' => TRUE,
    '#default_value' => variable_get('node_age_limit_short', 2419200),
  );
  $form['short']['node_age_weight_short'] = array(
    '#type' => 'select',
    '#title' => t('Probability that comments posted to old content is spam'),
    '#options' => $weights,
    '#required' => TRUE,
    '#description' => t('Probability that comments posted to old content are spam, as a percentage.'),
    '#default_value' => variable_get('node_age_weight_short', 85),
  );
  $form['long'] = array(
    '#type' => 'fieldset',
    '#title' => 'Really old content',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['long']['node_age_limit_long'] = array(
    '#type' => 'select',
    '#title' => t('Really old content is content not published within the past'),
    '#options' => $limits,
    '#required' => TRUE,
    '#default_value' => variable_get('node_age_limit_long', 4838400),
  );
  $form['long']['node_age_weight_long'] = array(
    '#type' => 'select',
    '#title' => t('Probability that comments posted to really old content is spam'),
    '#options' => $weights,
    '#required' => TRUE,
    '#description' => t('Probability that comments posted to really old content are spam, as a percentage.'),
    '#default_value' => variable_get('node_age_weight_long', 99),
  );
  return system_settings_form($form);
}

/**
 * Validate the configuration.
 */
function node_age_admin_settings_validate($form_id, $form_values) {
  $limit_short = $form_values['node_age_limit_short'];
  $limit_long = $form_values['node_age_limit_long'];
  if ($limit_short >= $limit_long) {
    form_set_error('node_age_limit_long', t('Really old content has to be older than old content.'));
  }
}

/**
 * Save the configuration.
 */
function node_age_admin_settings_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Reset to defaults')) {
    variable_del('node_age_limit_short');
    variable_del('node_age_weight_short');
    variable_del('node_age_limit_long');
    variable_del('node_age_weight_long');
    drupal_set_message('Configuration reset to defaults.');
  }
  else {
    variable_set('node_age_limit_short', $form_values['node_age_limit_short']);
    variable_set('node_age_weight_short', $form_values['node_age_weight_short']);
    variable_set('node_age_limit_long', $form_values['node_age_limit_long']);
    variable_set('node_age_weight_long', $form_values['node_age_weight_long']);
    drupal_set_message('Configuration saved.');
  }
}

Functions

Namesort descending Description
node_age_admin_settings Module administrative configuration options.
node_age_admin_settings_submit Save the configuration.
node_age_admin_settings_validate Validate the configuration.
node_age_menu Drupal _menu() hook.
node_age_spamapi
node_age_spam_filter Determine if a comment is being posted against too old of a node.