View source
<?php
define('SPAM_COMMENT', 9);
function spam_comment(&$comment, $op) {
switch ($op) {
case 'update':
if (!spam_bypass_filters()) {
spam_content_update($comment, 'comment');
$spam = spam_content_is_spam($comment, 'comment', $comment->cid);
if (isset($spam['is_spam']) && $spam['is_spam']) {
$comment['is_spam'] = TRUE;
}
}
break;
case 'insert':
if (!spam_bypass_filters()) {
spam_content_insert($comment, 'comment');
$spam = spam_content_is_spam($comment, 'comment', $comment->cid);
if (isset($spam['is_spam']) && $spam['is_spam']) {
$comment['is_spam'] = TRUE;
}
}
break;
case 'delete':
spam_content_delete($comment, 'comment');
break;
case 'form':
if (is_array($comment['cid'])) {
_spam_content_comment_cid($comment['cid']['#value']);
}
break;
case 'view':
$comment->is_spam = FALSE;
if (spam_score_is_spam(_spam_content_comment_score($comment->cid))) {
drupal_add_css(drupal_get_path('module', 'spam') . '/content/spam-comment.css');
$comment->is_spam = TRUE;
}
break;
}
}
function _spam_content_comment_cid($id = NULL) {
static $cid = 0;
if (isset($id) && is_numeric($id)) {
$cid = $id;
}
return $cid;
}
function comment_spamapi($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL) {
switch ($op) {
case 'content_module':
return 'comment';
case 'content_id':
if (is_object($arg1)) {
$arg1 = (array) $arg1;
}
return _spam_content_comment_cid($arg1['cid']);
case 'content_types':
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':
$fields['main'] = array(
'subject',
'comment',
);
if (is_object($arg1)) {
$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,
);
}
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':
$items = array();
$items['admin/content/comment/list/spam'] = array(
'title' => t('Spam'),
'page callback' => 'spam_content_comment_admin',
'access arguments' => array(
'administer spam',
),
'file' => 'comment.admin.inc',
'file path' => drupal_get_path('module', 'comment'),
'type' => MENU_LOCAL_TASK,
);
return $items;
break;
case 'publish':
$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':
$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':
return array(
'spam_content_comment_admin_overview' => array(
'file' => 'content/spam_content_comment.inc',
'arguments' => array(
'form' => NULL,
),
),
);
}
}
function comment_spamapi_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'comment_form' && is_array($form) && in_array(variable_get('spam_visitor_action', SPAM_ACTION_PREVENT), array(
SPAM_ACTION_PREVENT_SILENT,
SPAM_ACTION_PREVENT,
))) {
$form['#validate'][] = 'comment_spam_scan';
}
else {
if ($form_id == 'comment_admin_overview' || $form_id == 'spam_content_comment_admin_overview') {
$form['#spam_submit_valuetype'] = 'comments';
$form['#spam_submit_itemtype'] = 'comment';
$form['#submit'] = array_merge(array(
'spam_admin_overview_submit',
), $form['#submit']);
$parameters = $form['#parameters'];
if (is_array($parameters)) {
if (!in_array('new', $parameters)) {
$form['options']['operation']['#options']['markasnotspam'] = t('Mark the selected comments as not spam');
}
if (!in_array('spam', $parameters)) {
$form['options']['operation']['#options']['markasspam'] = t('Mark the selected comments as spam');
}
if (in_array('new', $parameters)) {
$form['options']['operation']['#options']['teachnotspam'] = t('Teach filters selected comments are not spam');
}
}
if (isset($form_state['post']['comments']) && is_array($form_state['post']['comments'])) {
foreach ($form_state['post']['comments'] as $cid) {
switch ($form_state['post']['operation']) {
case 'markasspam':
$score = _spam_content_comment_score($cid);
if (!spam_score_is_spam($score)) {
spam_mark_as_spam('comment', $cid);
}
break;
case 'markasnotspam':
$score = _spam_content_comment_score($cid);
if (spam_score_is_spam($score)) {
spam_mark_as_not_spam('comment', $cid);
}
break;
case 'teachnotspam':
spam_mark_as_not_spam('comment', $cid);
break;
}
}
}
}
}
}
function _spam_content_comment_score($cid) {
static $scores = array();
if (!isset($scores[$cid])) {
$scores[$cid] = db_result(db_query("SELECT score FROM {spam_tracker} WHERE content_type = 'comment' AND content_id = '%s'", $cid));
}
return $scores[$cid];
}
function spam_content_comment_admin() {
$edit = $_POST;
if (isset($edit['operation']) && $edit['operation'] == 'delete' && $edit['comments']) {
return drupal_get_form('comment_multiple_delete_confirm');
}
else {
return drupal_get_form('spam_content_comment_admin_overview', arg(4));
}
}
function spam_content_comment_admin_overview(&$form_state, $arg) {
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Update options'),
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
);
$options = array(
'markasnotspam' => t('Mark the selected comments as not spam'),
'delete' => t('Delete the selected comments'),
);
$form['options']['operation'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => 'markasnotspam',
);
$form['options']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
$form['header'] = array(
'#type' => 'value',
'#value' => array(
theme('table_select_header_cell'),
array(
'data' => t('Subject'),
'field' => 'subject',
),
array(
'data' => t('Author'),
'field' => 'name',
),
array(
'data' => t('Posted in'),
'field' => 'node_title',
),
array(
'data' => t('Time'),
'field' => 'timestamp',
'sort' => 'desc',
),
array(
'data' => t('Operations'),
),
),
);
$result = pager_query('SELECT c.subject, c.nid, c.cid, c.comment, c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name, u.uid, n.title AS node_title FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid INNER JOIN {node} n ON n.nid = c.nid WHERE c.status = %d' . tablesort_sql($form['header']['#value']), 50, 0, NULL, SPAM_COMMENT);
$destination = drupal_get_destination();
while ($comment = db_fetch_object($result)) {
$comments[$comment->cid] = '';
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$form['subject'][$comment->cid] = array(
'#value' => l($comment->subject, 'node/' . $comment->nid, array(
'attributes' => array(
'title' => truncate_utf8($comment->comment, 128),
),
'fragment' => 'comment-' . $comment->cid,
)),
);
$form['username'][$comment->cid] = array(
'#value' => theme('username', $comment),
);
$form['node_title'][$comment->cid] = array(
'#value' => l($comment->node_title, 'node/' . $comment->nid),
);
$form['timestamp'][$comment->cid] = array(
'#value' => format_date($comment->timestamp, 'small'),
);
$form['operations'][$comment->cid] = array(
'#value' => l(t('edit'), 'comment/edit/' . $comment->cid, array(
'query' => $destination,
)),
);
}
$form['comments'] = array(
'#type' => 'checkboxes',
'#options' => $comments,
);
$form['pager'] = array(
'#value' => theme('pager', NULL, 50, 0),
);
return $form;
}
function spam_content_comment_admin_overview_validate($form, &$form_state) {
$form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(
0,
));
if (count($form_state['values']['comments']) == 0) {
form_set_error('', t('Please select one or more comments to perform the update on.'));
drupal_goto('admin/content/comment/list/spam');
}
}
function spam_content_comment_admin_overview_submit($form, &$form_state) {
comment_admin_overview_submit($form, $form_state);
}
function theme_spam_content_comment_admin_overview($form) {
return theme('comment_admin_overview', $form);
}
function spam_preprocess_comment(&$vars) {
$comment = $vars['comment'];
if ($comment->is_spam) {
$vars['title'] = '<div class="comment-spam-title">' . $vars['title'] . '</div>';
$vars['content'] = '<div class="comment-spam-body">' . $vars['content'] . '</div>';
}
}
function comment_spam_scan($form, &$form_state) {
if ($form_state['clicked_button']['#value'] == t('Save') && in_array(variable_get('spam_visitor_action', SPAM_ACTION_PREVENT), array(
SPAM_ACTION_PREVENT_SILENT,
SPAM_ACTION_PREVENT,
))) {
$comment = $form['#post'];
if (!empty($comment->comment)) {
$_SESSION['spam_form'] = $form;
spam_scan($comment, 'comment');
}
}
if (isset($_SESSION['spam_form'])) {
unset($_SESSION['spam_form']);
}
}
function spam_mark_comment_as_spam_action(&$object, $context = array()) {
if (isset($object->cid)) {
$cid = $object->cid;
}
elseif (isset($context['cid'])) {
$cid = $context['cid'];
}
if ($cid) {
spam_mark_as_spam('comment', $cid);
watchdog('action', 'Marked comment %cid as spam.', array(
'%cid' => $cid,
));
}
}
function spam_mark_comment_as_not_spam_action(&$object, $context = array()) {
if (isset($object->cid)) {
$cid = $object->cid;
}
elseif (isset($context['cid'])) {
$cid = $context['cid'];
}
if ($cid) {
spam_mark_as_not_spam('comment', $cid);
watchdog('action', 'Marked comment %cid as not spam.', array(
'%cid' => $cid,
));
}
}