diff --git a/mollom.module b/mollom.module index 2960ab3..5e30885 100644 --- a/mollom.module +++ b/mollom.module @@ -666,7 +666,7 @@ function mollom_data_delete_form_submit($form, &$form_state) { $id = $data['postId']; if (!empty($form_state['values']['mollom']['feedback'])) { - if (mollom_data_report($entity, $id, $form_state['values']['mollom']['feedback'])) { + if (mollom_data_report($entity, $id, $form_state['values']['mollom']['feedback'], 'moderate', 'mollom_data_delete_form_submit')) { drupal_set_message(t('The content was successfully reported as inappropriate.')); } } @@ -682,9 +682,17 @@ function mollom_data_delete_form_submit($form, &$form_state) { * The entity type to send feedback for. * @param $id * The entity id to send feedback for. + * @param $feedback + * The feedback reason for reporting content. + * @param $type + * The type of feedback, one of 'moderate' or 'flag'. + * @param $source + * An optional single word string identifier for the user interface source. + * This is tracked along with the feedback to provide a more complete picture + * of how feedback is used and submitted on the site. */ -function mollom_data_report($entity, $id, $feedback) { - return mollom_data_report_multiple($entity, array($id), $feedback); +function mollom_data_report($entity, $id, $feedback, $type = 'moderate', $source = 'mollom_data_report') { + return mollom_data_report_multiple($entity, array($id), $feedback, $type, $source); } /** @@ -694,15 +702,23 @@ function mollom_data_report($entity, $id, $feedback) { * The entity type to send feedback for. * @param $ids * An array of entity ids to send feedback for. + * @param $feedback + * The feedback reason for reporting content. + * @param $type + * The type of feedback, one of 'moderate' or 'flag'. + * @param $source + * An optional single word string identifier for the user interface source. + * This is tracked along with the feedback to provide a more complete picture + * of how feedback is used and submitted on the site. */ -function mollom_data_report_multiple($entity, array $ids, $feedback) { +function mollom_data_report_multiple($entity, array $ids, $feedback, $type = 'moderate', $source = 'mollom_data_report_multiple') { $return = TRUE; foreach ($ids as $id) { // Load the Mollom session data. $data = mollom_data_load($entity, $id); // Send feedback, if we have session data. if (!empty($data->contentId) || !empty($data->captchaId)) { - $result = _mollom_send_feedback($data, $feedback, 'moderate', 'mollom_data_report_multiple'); + $result = _mollom_send_feedback($data, $feedback, $type, $source); $return = $return && $result; } } @@ -3951,3 +3967,171 @@ function profile_mollom_form_info_alter(&$form_info, $form_id) { /** * @} End of "name mollom_profile". */ + +/** + * @name mollom_action Actions module integration for Mollom. + * @{ + */ + +/** + * Implements hook_action_info(). + */ +function mollom_action_info() { + return array( + // Unpublish Action + 'mollom_unpublish_comment' => array( + 'label' => t('Report to Mollom as spam and unpublish'), + 'type' => 'comment', + 'configurable' => FALSE, + 'triggers' => array( + 'comment_insert', + 'comment_update', + ), + 'aggregate' => TRUE, + ), + // Delete action + 'mollom_delete_comment' => array( + 'label' => t('Report to Mollom as spam and delete'), + 'type' => 'comment', + 'configurable' => FALSE, + 'triggers' => array( + 'comment_insert', + 'comment_update', + ), + 'aggregate' => TRUE, + ), + ); +} + +/** + * Action callback to report to mollom and unpublish. + */ +function mollom_unpublish_comment($comments, $context = array()) { + mollom_action_comment($comments, 'unpublish'); +} + +/** + * Action callback to report to mollom and delete. + */ +function mollom_delete_comment($comments, $context = array()) { + mollom_action_comment($comments, 'delete'); +} + +/** + * Perform action and send to Mollom as Spam + * + * @param $comments + * An array of comments to perform the action upon. + * @param $op + * The type of action to perform; one of 'delete', 'unpublish'. + */ +function mollom_action_comment($comments, $op) { + $cids = array(); + $nids = array(); + + // Make sure this is a supported operation. + if (!in_array($op, array('unpublish', 'delete'))) { + watchdog('Mollom', 'Called comment action for an unsupported operation: @operation', array('@operation' => $op), WATCHDOG_ERROR); + return; + } + + // Retrieve any relevant callback for comments + $report_access_callbacks = array(); + $access_permissions = array(); + $entity_access_callbacks = array(); + $delete_callbacks = array(); + + foreach (mollom_form_list() as $form_id => $info) { + if (!isset($info['entity']) || $info['entity'] != 'comment') { + continue; + } + // If there is a 'report access callback' add it to the list. + if (isset($info['report access callback']) + && function_exists($info['report access callback']) + && !in_array($info['report access callback'], $report_access_callbacks)) { + $report_access_callbacks[] = $info['report access callback']; + } + // Otherwise add any access permissions. + else if (isset($info['report access']) && !in_array($info['report access'], $access_permissions)) { + $access_permissions[] = $info['report access']; + } + // Check for entity report access callbacks. + if (isset($info['entity report access callback']) + && function_exists($info['entity report access callback']) + && !in_array($info['entity report access callback'], $entity_access_callbacks)) { + $entity_access_callbacks[] = $info['entity report access callback']; + } + // Check entity delete callbacks. + if (!empty($info['entity delete multiple callback']) + && !in_array($info['entity delete multiple callback'], $delete_callbacks) + && function_exists($info['entity delete multiple callback'])) { + $delete_callbacks[] = $info['entity delete multiple callback']; + } + } + + foreach ($comments as $comment) { + // Check access for this comment. + $include = TRUE; + + // Check reporting callbacks. + foreach($report_access_callbacks as $callback) { + if (!$include = $callback('comment', $comment->cid)) { + break; + } + } + if (!$include) { + break; + } + + // Check reporting user permissions. + foreach($access_permissions as $permission) { + if (!$include = user_access($permission)) { + break; + } + } + if (!$include) { + break; + } + + // Check entity reporting callbacks. + foreach($report_access_callbacks as $callback) { + if (!$include = $callback($comment)) { + break; + } + } + if (!$include) { + break; + } + + // If still here, then user has access to report this comment. + $cids[] = $comment->cid; + $nids[$comment->nid] = TRUE; + } + + // Send feedback to Mollom. + mollom_data_report_multiple('comment', $cids, 'spam', 'moderate', "mollom_action_comment_$op"); + + if ($op == "unpublish") { + // Unpublish the comment. + db_update("comment") + ->fields(array("status" => COMMENT_NOT_PUBLISHED)) + ->condition("cid",$cids) + ->execute(); + + foreach ($nids as $nid => $val) { + _comment_update_node_statistics($nid); + } + } + else if ($op == 'delete') { + // Delete the Mollom data about the comment. + mollom_data_delete_multiple('comment', $cids); + // Delete the actual comment content. + foreach($delete_callbacks as $callback) { + $callback($cids); + } + } +} + +/** + * @} End of "name mollom_action". + */