diff --git a/b2-include/b2functions.php b/b2-include/b2functions.php index af353d7faf..eda53efa49 100644 --- a/b2-include/b2functions.php +++ b/b2-include/b2functions.php @@ -546,10 +546,14 @@ function get_postdata2($postid=0) { // less flexible, but saves DB queries return $postdata; } -function get_commentdata($comment_ID,$no_cache=0) { // less flexible, but saves DB queries +function get_commentdata($comment_ID,$no_cache=0,$include_unapproved=false) { // less flexible, but saves DB queries global $postc,$id,$commentdata,$tablecomments,$querycount, $wpdb; if ($no_cache) { - $myrow = $wpdb->get_row("SELECT * FROM $tablecomments WHERE comment_ID = $comment_ID", ARRAY_A); + $query = "SELECT * FROM $tablecomments WHERE comment_ID = $comment_ID"; + if (false == $include_unapproved) { + $query .= " AND comment_approved = '1'"; + } + $myrow = $wpdb->get_row($query, ARRAY_A); ++$querycount; } else { $myrow['comment_ID']=$postc->comment_ID; @@ -1310,6 +1314,147 @@ function pingGeoURL($blog_ID) { getRemoteFile($host,$path); } +/* wp_set_comment_status: + part of otaku42's comment moderation hack + changes the status of a comment according to $comment_status. + allowed values: + hold : set comment_approve field to 0 + approve: set comment_approve field to 1 + delete : remove comment out of database + + returns true if change could be applied + returns false on database error or invalid value for $comment_status + */ +function wp_set_comment_status($comment_id, $comment_status) { + global $wpdb, $tablecomments; + + switch($comment_status) { + case 'hold': + $query = "UPDATE $tablecomments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1"; + break; + case 'approve': + $query = "UPDATE $tablecomments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1"; + break; + case 'delete': + $query = "DELETE FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1"; + break; + default: + return false; + } + + if ($wpdb->query($query)) { + return true; + } else { + return false; + } +} + + +/* wp_get_comment_status + part of otaku42's comment moderation hack + gets the current status of a comment + + returned values: + "approved" : comment has been approved + "unapproved": comment has not been approved + "deleted ": comment not found in database + + a (boolean) false signals an error + */ +function wp_get_comment_status($comment_id) { + global $wpdb, $tablecomments; + + $result = $wpdb->get_var("SELECT comment_approved FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1"); + if ($result == NULL) { + return "deleted"; + } else if ($result == "1") { + return "approved"; + } else if ($result == "0") { + return "unapproved"; + } else { + return false; + } +} + + +/* wp_notify_postauthor + notifies the author of a post about a new comment + needs the id of the new comment + always returns true + */ +function wp_notify_postauthor($comment_id) { + global $wpdb, $tablecomments, $tableposts, $tableusers; + global $querystring_start, $querystring_equal, $querystring_separator; + global $blogfilename, $blogname, $siteurl; + + $comment = $wpdb->get_row("SELECT * FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1"); + $post = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID='$comment->comment_post_ID' LIMIT 1"); + $user = $wpdb->get_row("SELECT * FROM $tableusers WHERE ID='$post->post_author' LIMIT 1"); + + if ("" != $user->user_email) { + $comment_author_domain = gethostbyaddr($comment->comment_author_IP); + + $notify_message = "New comment on your post #$comment->comment_post_ID \"".stripslashes($post->post_title)."\"\r\n\r\n"; + $notify_message .= "Author : $comment->comment_author (IP: $comment->comment_author_IP , $comment_author_domain)\r\n"; + $notify_message .= "E-mail : $comment->comment_author_email\r\n"; + $notify_message .= "URL : $comment->comment_author_url\r\n"; + $notify_message .= "Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=$comment->comment_author_IP\r\n"; + $notify_message .= "Comment:\r\n".stripslashes($comment->comment_content)."\r\n\r\n"; + $notify_message .= "You can see all comments on this post here: \r\n"; + $notify_message .= $siteurl.'/'.$blogfilename.'?p='.$comment_post_ID.'&c=1#comments'; + + $subject = '[' . stripslashes($blogname) . '] Comment: "' .stripslashes($post->post_title).'"'; + if ('' != $comment->comment_author_email) { + $from = "From: \"$comment->comment_author\" <$comment->comment_author_email>"; + } else { + $from = 'From: "' . stripslashes($comment->comment_author) . "\" <$user->user_email>"; + } + $from .= "\nX-Mailer: WordPress $b2_version with PHP/" . phpversion(); + + @mail($user->user_email, $subject, $notify_message, $from); + } + + return true; +} + +/* wp_notify_moderator + notifies the moderator of the blog (usually the admin) + about a new comment that waits for approval + always returns true + */ +function wp_notify_moderator($comment_id) { + global $wpdb, $tablecomments, $tableposts, $tableusers; + global $querystring_start, $querystring_equal, $querystring_separator; + global $blogfilename, $blogname, $siteurl; + + $comment = $wpdb->get_row("SELECT * FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1"); + $post = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID='$comment->comment_post_ID' LIMIT 1"); + $user = $wpdb->get_row("SELECT * FROM $tableusers WHERE ID='$post->post_author' LIMIT 1"); + + $comment_author_domain = gethostbyaddr($comment->comment_author_IP); + $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $tablecomments WHERE comment_approved = '0'"); + + $notify_message = "A new comment on the post #$comment->comment_post_ID \"".stripslashes($post->post_title)."\" is waiting for your approval\r\n\r\n"; + $notify_message .= "Author : $comment->comment_author (IP: $comment->comment_author_IP , $comment_author_domain)\r\n"; + $notify_message .= "E-mail : $comment->comment_author_email\r\n"; + $notify_message .= "URL : $comment->comment_author_url\r\n"; + $notify_message .= "Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=$comment->comment_author_IP\r\n"; + $notify_message .= "Comment:\r\n".stripslashes($comment->comment_content)."\r\n\r\n"; + $notify_message .= "To approve this comment, visit: $siteurl/wp-admin/wp-post.php?action=mailapprovecomment&p=".$comment->comment_post_ID."&comment=$comment_id\r\n"; + $notify_message .= "To delete this comment, visit: $siteurl/wp-admin/wp-post.php?action=confirmdeletecomment&p=".$comment->comment_post_ID."&comment=$comment_id\r\n"; + $notify_message .= "Currently $comments_waiting comments are waiting for approval. Please visit the moderation panel:\r\n"; + $notify_message .= "$siteurl/wp-admin/wp-moderation.php\r\n"; + + $subject = '[' . stripslashes($blogname) . '] Please approve: "' .stripslashes($post->post_title).'"'; + $admin_email = get_settings("admin_email"); + $from = "From: $admin_email"; + $from .= "\nX-Mailer: WordPress $b2_version with PHP/" . phpversion(); + + @mail($admin_email, $subject, $notify_message, $from); + + return true; +} + // implementation of in_array that also should work on PHP3 if (!function_exists('in_array')) { @@ -1431,4 +1576,4 @@ function add_filter($tag, $function_to_add) { return true; } -?> \ No newline at end of file +?> diff --git a/b2-include/b2template.functions.php b/b2-include/b2template.functions.php index 056eec7879..cf39ee9037 100644 --- a/b2-include/b2template.functions.php +++ b/b2-include/b2template.functions.php @@ -1403,9 +1403,13 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde // generic comments/trackbacks/pingbacks numbering -function comments_number($zero='No Comments', $one='1 Comment', $more='% Comments') { +function comments_number($zero='No Comments', $one='1 Comment', $more='% Comments', $include_unapproved = false) { global $id, $comment, $tablecomments, $querycount, $wpdb; - $number = $wpdb->get_var("SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = $id"); + $query = "SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = '$id'"; + if (false == $include_unapproved) { + $query .= " AND comment_approved = '1'"; + } + $number = $wpdb->get_var($query); if ($number == 0) { $blah = $zero; } elseif ($number == 1) { @@ -1436,7 +1440,7 @@ function comments_popup_script($width=400, $height=400, $file='b2commentspopup.p function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') { global $id, $b2commentspopupfile, $b2commentsjavascript, $post, $wpdb, $tablecomments, $HTTP_COOKIE_VARS, $cookiehash; global $querystring_start, $querystring_equal, $querystring_separator, $siteurl; - $number = $wpdb->get_var("SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = $id"); + $number = $wpdb->get_var("SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = $id AND comment_approved = '1'"); if (0 == $number && 'closed' == $post->comment_status) { echo $none; return; @@ -1707,4 +1711,4 @@ function permalink_single_rss($file = '') { /***** // Permalink tags *****/ -?> \ No newline at end of file +?> diff --git a/b2comments.php b/b2comments.php index 91a3caa4d4..bc26f98949 100644 --- a/b2comments.php +++ b/b2comments.php @@ -14,7 +14,7 @@ $comment_author_email = trim($HTTP_COOKIE_VARS["comment_author_email_".$cookiehash]); $comment_author_url = trim($HTTP_COOKIE_VARS["comment_author_url_".$cookiehash]); - $comments = $wpdb->get_results("SELECT * FROM $tablecomments WHERE comment_post_ID = $id ORDER BY comment_date"); + $comments = $wpdb->get_results("SELECT * FROM $tablecomments WHERE comment_post_ID = $id AND comment_approved = '1' ORDER BY comment_date"); ?> @@ -73,6 +73,19 @@ if ($comments) {
+ +
+ Please note:
+ This blog uses comment moderation. In other words: your comment will need approval
+ by the administrator before it will appear in the blog. Approval usually happens
+ within the next 24 hours. Please send your comment only once. Thank you.
+
@@ -85,4 +98,4 @@ if ($comments) { \ No newline at end of file +?> diff --git a/b2comments.post.php b/b2comments.post.php index 397caf09c2..e7b1f1071e 100644 --- a/b2comments.post.php +++ b/b2comments.post.php @@ -82,37 +82,40 @@ if (!empty($lasttime)) { if ($ok) { // if there was no comment from this IP in the last 10 seconds + $comment_moderation = get_settings("comment_moderation"); + $moderation_notify = get_settings("moderation_notify"); + + // o42: this place could be the hook for further comment spam checking + // $approved should be set according the final approval status + // of the new comment + if ('manual' == $comment_moderation) { + $approved = 0; + } else if ('auto' == $comment_moderation) { + $approved = 0; + } else { // none + $approved = 1; + } + $wpdb->query("INSERT INTO $tablecomments (comment_ID,comment_post_ID,comment_author,comment_author_email,comment_author_url,comment_author_IP,comment_date,comment_content,comment_karma,comment_approved) VALUES ('0', '$comment_post_ID', '$author', '$email', '$url', '$user_ip', '$now', '$comment', '0', '$approved')"); - $wpdb->query("INSERT INTO $tablecomments VALUES ('0', '$comment_post_ID', '$author', '$email', '$url', '$user_ip', '$now', '$comment', '0')"); + // o42: this should be changed as soon as other sql dbs are supported + // as it's proprietary to mysql $comment_ID = $wpdb->get_var("SELECT last_insert_id()"); - if ($comments_notify) { - $postdata = get_postdata($comment_post_ID); - $authordata = get_userdata($postdata['Author_ID']); - - if('' != $authordata->user_email) { - $notify_message = "New comment on your post #$comment_post_ID \"".stripslashes($postdata['Title'])."\"\r\n\r\n"; - $notify_message .= "Author : $comment_author (IP: $user_ip , $user_domain)\r\n"; - $notify_message .= "E-mail : $comment_author_email\r\n"; - $notify_message .= "URL : $comment_author_url\r\n"; - $notify_message .= "Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=$user_ip\r\n"; - $notify_message .= "Delete : $siteurl/wp-admin/wp-post.php?action=deletecomment&p=$comment_post_ID&comment=$comment_ID \r\n"; - $notify_message .= "Comment:\r\n".stripslashes($original_comment)."\r\n\r\n"; - $notify_message .= "You can see all comments on this post here: \r\n"; - $notify_message .= $siteurl.'/'.$blogfilename.$querystring_start.'p'.$querystring_equal.$comment_post_ID.$querystring_separator.'c'.$querystring_equal.'1#comments'; - - $subject = '[' . stripslashes($blogname) . '] Comment: "' .stripslashes($postdata['Title']).'"'; - - if ('' != $comment_author_email) { - $from = "From: \"$comment_author\" <$comment_author_email>\r\n"; - } else { - $from = 'From: "' . stripslashes($comment_author) . "\" <$authordata->user_email>\r\n"; - } - $from .= "X-Mailer: WordPress $b2_version with PHP/" . phpversion(); - - @mail($authordata->user_email, $subject, $notify_message, $from); - } + $fp = fopen("/tmp/wpdebug.txt", "w+"); + fwrite($fp, "comment_moderation: $comment_moderation\n"); + fwrite($fp, "moderation_notify : $moderation_notify\n"); + + if (($moderation_notify) && (!$approved)) { + wp_notify_moderator($comment_ID); + fwrite($fp, "notify moderator -> $comment_ID\n"); } + + if (($comment_notify) && ($approved)) { + wp_notify_postauthor($comment_ID); + fwrite($fp, "notify postauthor -> $comment_ID\n"); + } + + fclose($fp); if ($email == '') $email = ' '; // this to make sure a cookie is set for 'no email' @@ -124,8 +127,8 @@ if ($ok) { // if there was no comment from this IP in the last 10 seconds setcookie('comment_author_email_'.$cookiehash, $email, time()+30000000); setcookie('comment_author_url_'.$cookiehash, $url, time()+30000000); - header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); - header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); $location = (!empty($HTTP_POST_VARS['redirect_to'])) ? $HTTP_POST_VARS['redirect_to'] : $HTTP_SERVER_VARS["HTTP_REFERER"]; @@ -138,4 +141,4 @@ if ($ok) { // if there was no comment from this IP in the last 10 seconds die('Sorry, you can only post a new comment once every 10 seconds. Slow down cowboy.'); } -?> \ No newline at end of file +?> diff --git a/b2commentspopup.php b/b2commentspopup.php index 6a1c79537a..bde4d55031 100644 --- a/b2commentspopup.php +++ b/b2commentspopup.php @@ -30,7 +30,7 @@ foreach ($posts as $post) { start_b2();