[phpmix-cvs] drupal/modules/akismet akismet.module,1.2,1.3
Status: Pre-Alpha
Brought to you by:
markus_petrux
From: <php...@li...> - 2006-06-06 03:13:08
|
Update of /cvsroot/phpmix/drupal/modules/akismet In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8895 Modified Files: akismet.module Log Message: - Fixed a few bugs here and there. - Implemented spam check for content (aka nodes). Spam check for comments was already coded. - A flag (AKISMET_MODE_TEST) has been implemented for not sending real requests to Akismet while testing. Index: akismet.module =================================================================== RCS file: /cvsroot/phpmix/drupal/modules/akismet/akismet.module,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** akismet.module 4 Jun 2006 20:11:40 -0000 1.2 --- akismet.module 5 Jun 2006 02:02:11 -0000 1.3 *************** *** 7,10 **** --- 7,21 ---- /** + * Operate in test mode? + * + * Uncomment one of the following lines, only for developement and testing purposes. + * When the constant is defined, then Akismet requests are NOT really sent, and + * Spam checks always return the value defined here. + */ + define('AKISMET_MODE_TEST', TRUE); + //define('AKISMET_MODE_TEST', FALSE); + + + /** * Implementation of hook_help(). */ *************** *** 191,197 **** $op = arg(3); if ($op == 'publish' || $op == 'unpublish') { ! $callback = 'akismet_switch_content_status'; else if ($op == 'submit-spam' || $op == 'submit-ham') { ! $callback = 'akismet_switch_spam_status'; } if (isset($callback) && akismet_content_exists($content_type, arg(2))) { --- 202,209 ---- $op = arg(3); if ($op == 'publish' || $op == 'unpublish') { ! $callback = 'akismet_callback_set_published_status'; ! } else if ($op == 'submit-spam' || $op == 'submit-ham') { ! $callback = 'akismet_callback_set_spam_status'; } if (isset($callback) && akismet_content_exists($content_type, arg(2))) { *************** *** 254,273 **** * @param string Operation; it can be 'publish' or 'unpublish'. */ ! function akismet_switch_content_status($content_type, $op, $content_id) { ! if ($content_type == 'comment') { ! // Retrieve the comment (existence has been checked in hook_menu). ! $comment = akismet_content_load($content_type, $content_id); ! if ($op == 'publish' && $comment->status == COMMENT_NOT_PUBLISHED) { ! akismet_content_publish_operation($content_type, $comment, 'publish'); ! } ! else if ($op == 'unpublish' && $comment->status == COMMENT_PUBLISHED) { ! akismet_content_publish_operation($content_type, $comment, 'unpublish'); ! } ! drupal_goto("node/$comment->nid", NULL, "comment-$comment->cid"); } ! else if ($content_type == 'node') { ! // @TODO: } - drupal_not_found(); } --- 266,293 ---- * @param string Operation; it can be 'publish' or 'unpublish'. */ ! function akismet_callback_set_published_status($content_type, $content_id, $op) { ! // Load the content (existence has been checked in hook_menu). ! $content = akismet_content_load($content_type, $content_id); ! ! if ($content_type == 'node') { ! $is_published = ($content->status ? TRUE : FALSE); } ! else { // comment ! $is_published = ($content->status == COMMENT_PUBLISHED ? TRUE : FALSE); ! } ! ! if ($op == 'publish' && !$is_published) { ! akismet_content_publish_operation($content_type, $content, 'publish'); ! } ! else if ($op == 'unpublish' && $is_published) { ! akismet_content_publish_operation($content_type, $content, 'unpublish'); ! } ! ! if ($content_type == 'node') { ! drupal_goto("node/$content->nid"); ! } ! else { // comment ! drupal_goto("node/$content->nid", NULL, "comment-$content->cid"); } } *************** *** 281,312 **** * @param string Operation; it can be 'submit-spam' or 'submit-ham'. */ ! function akismet_switch_spam_status($content_type, $op, $content_id) { $is_spam = akismet_content_is_spam($content_type, $content_id); ! if ($content_type == 'comment') { ! // Retrieve the comment (existence has been checked in hook_menu). ! $comment = akismet_content_load($content_type, $content_id); ! // insert or remove the spam marker (publishing/unpublishing if necessary). ! if ($op == 'submit-spam') { ! if (!$is_spam) { ! akismet_content_spam_operation($content_type, $content_id, 'submit-spam'); ! } ! if ($comment->status == COMMENT_PUBLISHED) { ! akismet_content_publish_operation($content_type, $comment, 'unpublish'); ! } } ! else if ($op == 'submit-ham') { ! if ($is_spam) { ! akismet_content_spam_operation($content_type, $content_id, 'submit-ham'); ! } ! if ($comment->status == COMMENT_NOT_PUBLISHED) { ! akismet_content_publish_operation($content_type, $comment, 'publish'); ! } } - drupal_goto("node/$comment->nid", NULL, "comment-$comment->cid"); } ! else if ($content_type == 'node') { ! // @TODO: } - drupal_not_found(); } --- 301,341 ---- * @param string Operation; it can be 'submit-spam' or 'submit-ham'. */ ! function akismet_callback_set_spam_status($content_type, $content_id, $op) { $is_spam = akismet_content_is_spam($content_type, $content_id); ! ! // Load the content (existence has been checked in hook_menu). ! $content = akismet_content_load($content_type, $content_id); ! ! if ($content_type == 'node') { ! $is_published = ($content->status ? TRUE : FALSE); ! } ! else { // comment ! $is_published = ($content->status == COMMENT_PUBLISHED ? TRUE : FALSE); ! } ! ! // insert or remove the spam marker (publishing/unpublishing if necessary). ! if ($op == 'submit-spam') { ! if (!$is_spam) { ! akismet_content_spam_operation($content_type, $content, 'submit-spam'); } ! if ($is_published) { ! akismet_content_publish_operation($content_type, $content, 'unpublish'); } } ! else if ($op == 'submit-ham') { ! if ($is_spam) { ! akismet_content_spam_operation($content_type, $content, 'submit-ham'); ! } ! if (!$is_published) { ! akismet_content_publish_operation($content_type, $content, 'publish'); ! } ! } ! ! if ($content_type == 'node') { ! drupal_goto("node/$content->nid"); ! } ! else { // comment ! drupal_goto("node/$content->nid", NULL, "comment-$content->cid"); } } *************** *** 319,324 **** case 'update': // If Akismet connections are not enabled, we have nothing else to do here. // Also quit asap, if current user has administration permission. ! if (!variable_get('akismet_connection_enabled', 1) || user_access('administer nodes')) { break; } --- 348,357 ---- case 'update': // If Akismet connections are not enabled, we have nothing else to do here. + if (!variable_get('akismet_connection_enabled', 1)) { + break; + } + // Also quit asap, if current user has administration permission. ! if (user_access('administer nodes') && !defined('AKISMET_MODE_TEST')) { break; } *************** *** 326,337 **** // Now, check if it's about a node type that we have not been explicitly requested to check. $check_nodetypes = variable_get('akismet_check_nodetypes', NULL); ! if (is_array($check_nodetypes) && isset($check_nodetypes[$node->type]) && $check_nodetypes[$node->type]) { break; } // Ok, let's send a query to Akismet. ! if (akismet_api_cmd_comment_check(akismet_build_akismet_comment_data('node', $node))) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. ! akismet_content_spam_operation('node', $node->nid, 'submit-spam'); // Unpublish the node, if necessary. if ($node->status) { --- 359,370 ---- // Now, check if it's about a node type that we have not been explicitly requested to check. $check_nodetypes = variable_get('akismet_check_nodetypes', NULL); ! if (!is_array($check_nodetypes) || !isset($check_nodetypes[$node->type]) || !$check_nodetypes[$node->type]) { break; } // Ok, let's send a query to Akismet. ! if (akismet_api_cmd_comment_check(akismet_prepare_comment_data('node', $node))) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. ! akismet_content_spam_operation('node', $node, 'submit-spam'); // Unpublish the node, if necessary. if ($node->status) { *************** *** 356,360 **** function akismet_comment(&$comment, $op) { if ($op == 'delete') { ! db_query("DELETE FROM {akismet_spam_marks} WHERE content_type = 'comment' AND content_id = %d", $content->cid); } } --- 389,393 ---- function akismet_comment(&$comment, $op) { if ($op == 'delete') { ! db_query("DELETE FROM {akismet_spam_marks} WHERE content_type = 'comment' AND content_id = %d", $comment->cid); } } *************** *** 370,382 **** // Hook into comment edit/reply form. ! if ($form_id == 'comment_form' && variable_get('akismet_check_comments', 1) && !user_access('administer comments')) { ! if (isset($form['cid']) && isset($form['cid']['#value']) && is_numeric($form['cid']['#value'])) { ! // This is the simple hook method, we already have the $cid. ! $form['#submit']['_akismet_comment_form_submit'] = array(); ! } ! else { ! // This is a bit more complex, because the user is creating a new comment, so ! // how do we get the $cid? See comments below, within our own submit callback. ! $form['#submit'] = array('_akismet_comment_form_submit' => array($form['#submit'])); } } --- 403,417 ---- // Hook into comment edit/reply form. ! if ($form_id == 'comment_form' && variable_get('akismet_check_comments', 1)) { ! if (!user_access('administer comments') || defined('AKISMET_MODE_TEST')) { ! if (isset($form['cid']) && isset($form['cid']['#value']) && is_numeric($form['cid']['#value'])) { ! // This is the simple hook method, we already have the $cid. ! $form['#submit']['_akismet_comment_form_submit'] = array(); ! } ! else { ! // This is a bit more complex, because the user is creating a new comment, so ! // how can we get the $cid? See comments below, within our own submit callback. ! $form['#submit'] = array('_akismet_comment_form_submit' => array($form['#submit'])); ! } } } *************** *** 421,429 **** // information that we need to make the Akismet request to check for spam. if ($cid) { ! $comment = akismet_content_load('comment', $cid, TRUE); // If we got a comment, send query to Akismet. ! if ($comment && akismet_api_cmd_comment_check(akismet_build_akismet_comment_data('comment', $comment))) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. ! akismet_content_spam_operation('comment', $cid, 'submit-spam'); // Unpublish the comment, if necessary. if ($comment->status == COMMENT_PUBLISHED) { --- 456,464 ---- // information that we need to make the Akismet request to check for spam. if ($cid) { ! $comment = akismet_content_load('comment', $cid); // If we got a comment, send query to Akismet. ! if ($comment && akismet_api_cmd_comment_check(akismet_prepare_comment_data('comment', $comment))) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. ! akismet_content_spam_operation('comment', $comment, 'submit-spam'); // Unpublish the comment, if necessary. if ($comment->status == COMMENT_PUBLISHED) { *************** *** 474,501 **** * @param string Content type; can be either 'node' or 'comment'. * @param integer Content ID; can be either a nid or a cid. ! * @param boolen Whether to load all possible comment data or only the basic record (optional, defaults to FALSE). ! * @return mixed An object with requested content or FALSE on failure. */ ! function akismet_content_load($content_type, $content_id, $alldata = FALSE) { if ($content_type == 'node') { ! if ($alldata) { ! $content = node_load($content_id); ! if (empty($content->nid)) { ! $content = FALSE; ! } ! } ! else { ! $content = db_fetch_object(db_query('SELECT * FROM {node} WHERE nid = %d', $content_id)); } } else if ($content_type == 'comment') { ! if ($alldata) { ! $content = db_fetch_object(db_query(db_rewrite_sql('SELECT c.*, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', 'c'), $content_id)); ! if ($content->uid) { ! $content->name = $content->registered_name; ! } ! } ! else { ! $content = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $content_id)); } } --- 509,525 ---- * @param string Content type; can be either 'node' or 'comment'. * @param integer Content ID; can be either a nid or a cid. ! * @return mixed An object with requested content; FALSE on failure. */ ! function akismet_content_load($content_type, $content_id) { if ($content_type == 'node') { ! $content = node_load($content_id); ! if (empty($content->nid)) { ! $content = FALSE; } } else if ($content_type == 'comment') { ! $content = db_fetch_object(db_query(db_rewrite_sql('SELECT c.*, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', 'c'), $content_id)); ! if ($content->uid) { ! $content->name = $content->registered_name; } } *************** *** 509,531 **** * Mark content as spam or remove the mark. * - * A previous check needs to be made in order to see if content is already marked as spam or not. - * * @param string Content type; can be either 'node' or 'comment'. ! * @param integer Content ID; can be either a nid or a cid. * @param string Operation; it can be 'submit-spam' or 'submit-ham'. */ ! function akismet_content_spam_operation($content_type, $content_id, $op) { ! if ($op == 'spam') { db_query("INSERT INTO {akismet_spam_marks} (content_type, content_id, spam_created) VALUES ('%s', %d, %d)", $content_type, $content_id, time()); if (variable_get('akismet_connection_enabled', 1)) { ! // @TODO: Submit spam!? } } ! else if ($op == 'ham') { db_query("DELETE FROM {akismet_spam_marks} WHERE content_type = '%s' AND content_id = %d", $content_type, $content_id); if (variable_get('akismet_connection_enabled', 1)) { ! // @TODO: Submit ham!? } } } --- 533,577 ---- * Mark content as spam or remove the mark. * * @param string Content type; can be either 'node' or 'comment'. ! * @param object Content object. * @param string Operation; it can be 'submit-spam' or 'submit-ham'. */ ! function akismet_content_spam_operation($content_type, $content, $op) { ! if ($content_type == 'node') { ! $content_id = $content->nid; ! $content_title = $content->title; ! $content_link = l(t('view'), "node/$content->nid"); ! } ! else { // comment ! $content_id = $content->cid; ! $content_title = $content->subject; ! $content_link = l(t('view'), "node/$content->nid", NULL, NULL, "comment-$content->cid"); ! } ! ! if ($op == 'submit-spam') { db_query("INSERT INTO {akismet_spam_marks} (content_type, content_id, spam_created) VALUES ('%s', %d, %d)", $content_type, $content_id, time()); + if (variable_get('akismet_connection_enabled', 1)) { ! akismet_api_cmd_submit_spam(akismet_prepare_comment_data($content_type, $content)); ! $action = ($content_type == 'node' ? t('Content submitted as spam') : t('Comment submitted as spam')); ! } ! else { ! $action = ($content_type == 'node' ? t('Content marked as spam') : t('Comment marked as spam')); } } ! else { // submit-ham db_query("DELETE FROM {akismet_spam_marks} WHERE content_type = '%s' AND content_id = %d", $content_type, $content_id); + if (variable_get('akismet_connection_enabled', 1)) { ! akismet_api_cmd_submit_ham(akismet_prepare_comment_data($content_type, $content)); ! $action = ($content_type == 'node' ? t('Content submitted as ham') : t('Comment submitted as ham')); ! } ! else { ! $action = ($content_type == 'node' ? t('Content marked as ham') : t('Comment marked as ham')); } } + + // Add an entry to the watchdog log. + watchdog('content', t('%action: %title', array('%action' => $action, '%title' => theme('placeholder', $content_title))), WATCHDOG_NOTICE, $content_link); } *************** *** 542,559 **** // Only the node record is updated, no other hooks are invoked. - // Cache operations in case we need to come here more than once per request. - static $node_operations = FALSE; - if (!$node_operations) { - $node_operations = node_operations(); - } - - // extract the appropriate database query operation - $query = $node_operations[$op][1]; // perform the update action, then refresh node statistics ! db_query($query, $content->nid); // Add an entry to the watchdog log. ! $action = ($op == 'unpublish' ? t('unpublished') : t('published')); ! watchdog('content', t('Content %action: %title', array('%action' => $action, '%subject' => theme('placeholder', $content->title))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $comment->nid)); } else if ($content_type == 'comment') { --- 588,597 ---- // Only the node record is updated, no other hooks are invoked. // perform the update action, then refresh node statistics ! db_query('UPDATE {node} SET status = %d WHERE nid = %d', ($op == 'publish' ? 1 : 0), $content->nid); // Add an entry to the watchdog log. ! $action = ($op == 'publish' ? t('Content published') : t('Content unpublished')); ! watchdog('content', t('%action: %title', array('%action' => $action, '%title' => theme('placeholder', $content->title))), WATCHDOG_NOTICE, l(t('view'), "node/$content->nid")); } else if ($content_type == 'comment') { *************** *** 577,582 **** // Add an entry to the watchdog log. ! $action = ($op == 'unpublish' ? t('unpublished') : t('published')); ! watchdog('content', t('Comment %action: %subject', array('%action' => $action, '%subject' => theme('placeholder', $content->subject))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $content->nid, NULL, NULL, 'comment-'. $content->cid)); } } --- 615,620 ---- // Add an entry to the watchdog log. ! $action = ($op == 'publish' ? t('Comment published') : t('Comment unpublished')); ! watchdog('content', t('%action: %subject', array('%action' => $action, '%subject' => theme('placeholder', $content->subject))), WATCHDOG_NOTICE, l(t('view'), "node/$content->nid", NULL, NULL, "comment-$content->cid")); } } *************** *** 589,593 **** * @return array */ ! function akismet_build_akismet_comment_data($content_type, $content) { // Prepare data that is common to nodes/comments. $comment_data = array( --- 627,631 ---- * @return array */ ! function akismet_prepare_comment_data($content_type, $content) { // Prepare data that is common to nodes/comments. $comment_data = array( *************** *** 637,640 **** --- 675,681 ---- */ function akismet_api_cmd_verify_key($key) { + if (defined('AKISMET_MODE_TEST')) { + return TRUE; + } global $base_url; $request = "key=$key&blog=$base_url"; *************** *** 646,655 **** * Akismet API: Comment Check api-key.rest.akismet.com/1.1/comment-check * ! * @param array Comment data; see function akismet_build_akismet_comment_data(). * @return boolean TRUE if the comment IS spam; FALSE otherwise. */ function akismet_api_cmd_comment_check($comment_data) { ! $comment_data = array_merge(_akismet_api_build_server_data(), $comment_data); ! $query_string = _akismet_api_get_query_string($comment_data); $host = variable_get('akismet_wpapikey', '') . AKISMET_API_HOST; $response = _akismet_api_http_post($query_string, $host, '/'. AKISMET_API_VERSION .'/comment-check'); --- 687,699 ---- * Akismet API: Comment Check api-key.rest.akismet.com/1.1/comment-check * ! * @param array Comment data; see function akismet_prepare_comment_data(). * @return boolean TRUE if the comment IS spam; FALSE otherwise. */ function akismet_api_cmd_comment_check($comment_data) { ! if (defined('AKISMET_MODE_TEST')) { ! return AKISMET_MODE_TEST; ! } ! $comment_data = array_merge(_akismet_api_prepare_request_data(), $comment_data); ! $query_string = _akismet_api_build_query_string($comment_data); $host = variable_get('akismet_wpapikey', '') . AKISMET_API_HOST; $response = _akismet_api_http_post($query_string, $host, '/'. AKISMET_API_VERSION .'/comment-check'); *************** *** 660,667 **** * Akismet API: Submit Spam api-key.rest.akismet.com/1.1/submit-spam * ! * @param array Comment data; see function akismet_build_akismet_comment_data(). */ function akismet_api_cmd_submit_spam($comment_data) { ! $query_string = _akismet_api_get_query_string($comment_data); $host = variable_get('akismet_wpapikey', '') . AKISMET_API_HOST; $response = _akismet_api_http_post($query_string, $host, '/'. AKISMET_API_VERSION .'/submit-spam'); --- 704,714 ---- * Akismet API: Submit Spam api-key.rest.akismet.com/1.1/submit-spam * ! * @param array Comment data; see function akismet_prepare_comment_data(). */ function akismet_api_cmd_submit_spam($comment_data) { ! if (defined('AKISMET_MODE_TEST')) { ! return; ! } ! $query_string = _akismet_api_build_query_string($comment_data); $host = variable_get('akismet_wpapikey', '') . AKISMET_API_HOST; $response = _akismet_api_http_post($query_string, $host, '/'. AKISMET_API_VERSION .'/submit-spam'); *************** *** 671,678 **** * Akismet API: Submit Ham api-key.rest.akismet.com/1.1/submit-ham * ! * @param array Comment data; see function akismet_build_akismet_comment_data(). */ function akismet_api_cmd_submit_ham($comment_data) { ! $query_string = _akismet_api_get_query_string($comment_data); $host = variable_get('akismet_wpapikey', '') . AKISMET_API_HOST; $response = _akismet_api_http_post($query_string, $host, '/'. AKISMET_API_VERSION .'/submit-ham'); --- 718,728 ---- * Akismet API: Submit Ham api-key.rest.akismet.com/1.1/submit-ham * ! * @param array Comment data; see function akismet_prepare_comment_data(). */ function akismet_api_cmd_submit_ham($comment_data) { ! if (defined('AKISMET_MODE_TEST')) { ! return; ! } ! $query_string = _akismet_api_build_query_string($comment_data); $host = variable_get('akismet_wpapikey', '') . AKISMET_API_HOST; $response = _akismet_api_http_post($query_string, $host, '/'. AKISMET_API_VERSION .'/submit-ham'); *************** *** 680,688 **** /** ! * Prepare $_SERVER data that is to be sent along akismet requests. * ! * @return array */ ! function _akismet_api_build_server_data() { // You may add more elements here, but they are often related to internal server // data that makes little sense to check whether a comment is spam or not. --- 730,738 ---- /** ! * Prepare user request data for Akismet requests. * ! * @return array Relevant information extracted from $_SERVER superglobal. */ ! function _akismet_api_prepare_request_data() { // You may add more elements here, but they are often related to internal server // data that makes little sense to check whether a comment is spam or not. *************** *** 723,732 **** /** ! * Convert an associative array into a query string formatted according to akismet rules. * * @param array * @return string */ ! function _akismet_api_get_query_string($array) { global $base_url; $string = 'blog='. $base_url . base_path(); --- 773,782 ---- /** ! * Build query string for Akismet request. * * @param array * @return string */ ! function _akismet_api_build_query_string($array) { global $base_url; $string = 'blog='. $base_url . base_path(); |