You are here

function comment_spamapi in Spam 6

Same name and namespace in other branches
  1. 5.3 modules/spam_comment.inc \comment_spamapi()

Spam module _spamapi() hook.

File

content/spam_content_comment.inc, line 67
Include file for integration with comments.

Code

function comment_spamapi($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL) {
  switch ($op) {
    case 'content_module':

      // Register with the spam api as a content type module.
      return 'comment';
    case 'content_id':

      // Tell the spam module the id of a given comment.
      if (is_object($arg1)) {

        // The delete hook uses an object instead of an array.
        $arg1 = (array) $arg1;
      }
      return _spam_content_comment_cid($arg1['cid']);
    case 'content_types':

      // Register the "comment" content type with the spam module.
      return array(
        array(
          'name' => 'comments',
          'module' => 'comment',
          'title' => t('Comments'),
          'description' => t('Check this box to filter comments for spam.'),
          'default_value' => 1,
        ),
      );
    case 'filter_content_type':
      return variable_get('spam_filter_comments', 1);
    case 'filter_fields':

      // Tell spam module which fields it should scan for spam.
      $fields['main'] = array(
        'subject',
        'comment',
      );
      if (is_object($arg1)) {

        // The delete hook uses an object instead of an array.
        $arg1 = (array) $arg1;
      }
      if (isset($arg1['author'])) {
        $fields['other'][] = 'author';
      }
      if (isset($arg1['name'])) {
        $fields['other'][] = 'name';
      }
      if (isset($arg1['mail'])) {
        $fields['other'][] = 'mail';
      }
      if (isset($arg1['homepage'])) {
        $fields['other'][] = 'homepage';
      }
      return $fields;
    case 'feedback_form':
      $form = array();
      if (is_numeric($form['cid'])) {
        $form['cid'] = array(
          '#type' => 'textfield',
          '#title' => t('Comment ID'),
          '#value' => $arg1['cid'],
          '#disabled' => TRUE,
        );
      }

    // fall through...
    case 'error_form':
      if (!is_array($form)) {
        $form = array();
      }
      $form['comment'] = array(
        '#type' => 'fieldset',
        '#title' => 'Comment',
      );
      $form['comment']['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#value' => $arg1['subject'],
        '#disabled' => TRUE,
      );
      $form['comment']['body'] = array(
        '#type' => 'textarea',
        '#title' => t('Comment'),
        '#value' => $arg1['comment'],
        '#disabled' => TRUE,
      );
      $form['comment']['author'] = array(
        '#type' => 'markup',
        '#prefix' => '<div><strong>' . t('Author') . ':</strong></div>',
        '#value' => theme('username', user_load(array(
          'uid' => $arg1['uid'],
        ))),
      );
      return $form;
    case 'load':
      return db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $arg1));
    case 'title':
      return db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $arg1));
    case 'edit_link':
      return "comment/edit/{$arg1}";
    case 'status':
      $status = db_result(db_query('SELECT status FROM {comments} WHERE cid = %d', $arg1));
      if ($status == COMMENT_PUBLISHED) {
        return SPAM_PUBLISHED;
      }
      else {
        return SPAM_NOT_PUBLISHED;
      }
    case 'hostname':
      return db_result(db_query('SELECT hostname FROM {comments} WHERE cid = %d', $arg1));
    case 'link':
      if (is_object($arg1) && isset($arg1->cid)) {
        return spam_links('comment', $arg1->cid, $arg1);
      }
      break;
    case 'redirect':
      $nid = db_result(db_query('SELECT nid FROM {comments} WHERE cid = %d', $arg1));
      return drupal_goto("node/{$nid}", NULL, "comment-{$arg1}");
    case 'overview_filter_join':
      return 'INNER JOIN {comments} c ON t.content_id = CAST(c.cid AS CHAR(32))';
    case 'overview_filter_where':
      switch ($arg1) {
        case 'title':
          return "c.subject LIKE '%%%s%%'";
        case 'status':
          return 'c.status = %d';
      }
    case 'menu':

      // Create page for listing all spam comments in comment admin section.
      $items = array();
      $items['admin/content/comment/list/spam'] = array(
        'title' => t('Spam'),
        'page callback' => 'spam_content_comment_admin',
        'access arguments' => array(
          'administer spam',
        ),
        //TODO: remove dependencies on comment.admin.inc?
        'file' => 'comment.admin.inc',
        'file path' => drupal_get_path('module', 'comment'),
        'type' => MENU_LOCAL_TASK,
      );
      return $items;
      break;
    case 'publish':

      // Only update comments that exist and need to be published.
      $nid = db_result(db_query('SELECT nid FROM {comments} WHERE cid = %d AND status = %d OR status = %d', $arg1, COMMENT_NOT_PUBLISHED, SPAM_COMMENT));
      if ($nid) {
        db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_PUBLISHED, $arg1);
        _comment_update_node_statistics($nid);
        $comment = (array) _comment_load($arg1);
        comment_invoke_comment($comment, 'publish');
      }
      break;
    case 'unpublish':

      // Only update comments that exist and need to be unpublished.
      // When 'unpublishing' comments, we actually mark them as spam.
      // But non-spam comments in the approval queue are also 'unpublished'.
      $nid = db_result(db_query('SELECT nid FROM {comments} WHERE cid = %d AND status <> %d', $arg1, SPAM_COMMENT));
      if ($nid) {
        db_query('UPDATE {comments} SET status = %d WHERE cid = %d', SPAM_COMMENT, $arg1);
        _comment_update_node_statistics($nid);
      }
      break;
    case 'theme_forms':

      // Add spam comments admin form to spam.module's hook_theme.
      return array(
        'spam_content_comment_admin_overview' => array(
          'file' => 'content/spam_content_comment.inc',
          'arguments' => array(
            'form' => NULL,
          ),
        ),
      );
  }
}