PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/_akismet/admin_akismet_comment.php

https://github.com/aravindc/pixelpost
PHP | 355 lines | 276 code | 49 blank | 30 comment | 60 complexity | afbb4ce517a8d2af77aeec5a4109e63c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // SVN file version:
  3. // $Id$
  4. /**
  5. * Akismet comment filter addon for Pixelpost 1.7
  6. * Version 1.3
  7. *
  8. * @copyright Aditya Mooley <adityamooley@sanisoft.com>, Dr. Tarique Sani <tarique@sanisoft.com>
  9. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License version 2 of the License, or
  10. * any later version.
  11. * Updated for Pixelpost 1.6 & 1.7 by Karin Uhlig <2@kg3.de>
  12. */
  13. $addon_name = "Pixelpost Akismet comment filter (Admin Side)";
  14. $addon_version = '1.4';
  15. // The workspace stuff
  16. $addon_workspace1 = "comments";
  17. $addon_menu1 = "comments";
  18. $addon_admin_submenu1 = "Akismet";
  19. $addon_function_name1 = "get_akismet_pages";
  20. // add the function
  21. add_admin_functions($addon_function_name1,$addon_workspace1,$addon_menu1,$addon_admin_submenu1);
  22. $addon_workspace2 = "additional_spam_measures";
  23. $addon_menu2 = "";
  24. $addon_admin_submenu2 = "";
  25. $addon_function_name2 = "akismet_settings";
  26. // add the function
  27. add_admin_functions($addon_function_name2,$addon_workspace2,$addon_menu2,$addon_admin_submenu2);
  28. add_admin_functions('get_akismet_links', 'show_commentbuttons_top');
  29. add_admin_functions('get_akismet_links2', 'show_commentbuttons_bottom');
  30. add_admin_functions('get_akismet_pages', 'pages_commentbuttons');
  31. add_admin_functions('get_akismet_pages', 'single_comment_list');
  32. add_admin_functions('get_akismet_style', 'admin_html_head');
  33. //global declaration
  34. GLOBAL $akismet_result_message;
  35. //Check whether ADMIN has submitted comment to mark as spam for Akismet
  36. if (isset($_GET['view']) && $_GET['view'] == 'comments' && isset($_GET['action']) && $_GET['action'] == 'akismetspam') {
  37. if (pp_submit_spam_comment()) {
  38. $GLOBALS['akismet_result_message'] = '<div class="jcaption confirm">Selected comments reported as spam to Akismet</div>';
  39. }
  40. }
  41. //Check whether ADMIN has submitted comment to mark as spam for Akismet
  42. if (isset($_GET['view']) && $_GET['view'] == 'comments' && isset($_GET['action']) && $_GET['action'] == 'akismetnotspam') {
  43. if (pp_submit_nonspam_comment()) {
  44. $GLOBALS['akismet_result_message'] = '<div class="jcaption confirm">Selected comments reported as not spam to Akismet</div>';
  45. }
  46. }
  47. function get_akismet_links()
  48. {
  49. global $pixelpost_db_prefix, $moderate_where, $cfgrow;
  50. // Echo the links
  51. if (isset($_GET['commentsview']) && $_GET['commentsview']=='akismet'){
  52. echo "<br /><br />";
  53. echo "<input class='cmnt-buttons' type='submit' name='akismetnotspam' value='Report as NOT Spam To Akismet' onclick=\"
  54. document.getElementById('managecomments').action='".PHP_SELF."?view=comments&amp;action=akismetnotspam'
  55. return confirm('Report all selected comments as Not Spam to Akismet?');\" />";
  56. echo "&nbsp;";
  57. }
  58. if (isset($_GET['show']) || !isset($_GET['commentsview'])){
  59. echo " <input class='cmnt-buttons' type='submit' name='akismetspam' value='Report as Spam To Akismet' onclick=\"
  60. document.getElementById('managecomments').action='".PHP_SELF."?view=comments&amp;action=akismetspam'
  61. return confirm('Report all selected comments as Spam to Akismet?');\" />";
  62. }
  63. if ((isset($GLOBALS['akismet_result_message'])) && ($GLOBALS['akismet_result_message']!="")){
  64. echo "<br /><br />".$GLOBALS['akismet_result_message'];
  65. }
  66. $query = mysql_query("select count(*) as count from ".$pixelpost_db_prefix."comments WHERE publish='spm' ");
  67. $akismet_count = mysql_fetch_array($query,MYSQL_ASSOC);
  68. if ($akismet_count['count'] < 1 && isset($_GET['commentsview']) && $_GET['commentsview']=='akismet'){
  69. echo "<div class=\"content\">Your quarantine is empty.</div> ";
  70. }
  71. }
  72. function get_akismet_links2()
  73. {
  74. global $pixelpost_db_prefix, $moderate_where, $cfgrow;
  75. // Echo the links
  76. if (isset($_GET['commentsview']) && $_GET['commentsview']=='akismet'){
  77. echo "<input class='cmnt-buttons' type='submit' name='akismetnotspam' value='Report as NOT Spam To Akismet' onclick=\"
  78. document.getElementById('managecomments').action='".PHP_SELF."?view=comments&amp;action=akismetnotspam'
  79. return confirm('Report all selected comments as Not Spam to Akismet?');\" />";
  80. echo "&nbsp;";
  81. }
  82. if (isset($_GET['show']) || !isset($_GET['commentsview'])){
  83. echo " <input class='cmnt-buttons' type='submit' name='akismetspam' value='Report as Spam To Akismet' onclick=\"
  84. document.getElementById('managecomments').action='".PHP_SELF."?view=comments&amp;action=akismetspam'
  85. return confirm('Report all selected comments as Spam to Akismet?');\" />";
  86. }
  87. // Delete comments older than X days and marked as SPAM by Akismet
  88. $query = "DELETE FROM {$pixelpost_db_prefix}comments WHERE (TO_DAYS(CURDATE()) - TO_DAYS(`datetime`)) > ".$cfgrow['akismet_keep']." AND publish='spm'";
  89. $result = mysql_query($query);
  90. }
  91. function get_akismet_pages() {
  92. global $moderate_where, $moderate_where2;
  93. global $comment_row_class;
  94. if (isset($_GET['commentsview']) && $_GET['commentsview']=='akismet'){
  95. $moderate_where = " and publish='spm' ";
  96. $moderate_where2 = " WHERE publish='spm' ";
  97. $comment_row_class = "akismet-spam-comment";
  98. }
  99. }
  100. function get_akismet_style() {
  101. global $pixelpost_db_prefix;
  102. if(isset($_GET['view']) AND $_GET['view'] == "comments") {
  103. $query = mysql_query("select count(*) as count from ".$pixelpost_db_prefix."comments WHERE publish='spm' ");
  104. $akismet_count = mysql_fetch_array($query,MYSQL_ASSOC);
  105. echo '<style type="text/css">
  106. .akismet-spam-comment {background-color:#eca189;color:#666;}
  107. </style><script type="text/javascript" src="../addons/_akismet/libraries/domFunction.js"></script>
  108. <script type="text/javascript" charset="utf-8">
  109. var ElementReady;
  110. var foobar = new domFunction(function()
  111. {
  112. // Script to make sure the function "getElementById()" will work on ALL browsers
  113. // Copied from: http://webbugtrack.blogspot.com/2007/08/bug-152-getelementbyid-returns.html
  114. if(ElementReady != true){
  115. //use browser sniffing to determine if IE or Opera (ugly, but required)
  116. var isOpera, isIE = false;
  117. if(typeof(window.opera) != \'undefined\'){isOpera = true;}
  118. if(!isOpera && navigator.userAgent.indexOf(\'Internet Explorer\')){isIE = true};
  119. //fix both IE and Opera (adjust when they implement this method properly)
  120. if(isOpera || isIE){
  121. document.nativeGetElementById = document.getElementById;
  122. //redefine it!
  123. document.getElementById = function(id){
  124. var elem = document.nativeGetElementById(id);
  125. if(elem){
  126. //verify it is a valid match!
  127. if(elem.id == id){
  128. //valid match!
  129. return elem;
  130. } else {
  131. //not a valid match!
  132. //start at one, because we know the first match, is wrong!
  133. for(var i=1;i<document.all[id].length;i++){
  134. if(document.all[id][i].id == id){
  135. return document.all[id][i];
  136. }
  137. }
  138. }
  139. }
  140. return null;
  141. };
  142. }
  143. ElementReady = true;
  144. }
  145. // The actual code the makes it work:
  146. var akismet = document.getElementById(\'commentsAkismet\');
  147. var akismet_total = \''.$akismet_count['count'].'\';
  148. if(akismet){
  149. akismet.innerHTML = akismet.innerHTML + \' (\' + akismet_total + \')\';
  150. }
  151. }); // End Dom Ready
  152. </script>
  153. ';
  154. }
  155. }
  156. // Checks whether specified field exists in current or specified table.
  157. $fieldname = "akismet_key";
  158. $table = $pixelpost_db_prefix ."config";
  159. $fieldexists = 0;
  160. $t = 0;
  161. $attention_call = "";
  162. global $pixelpost_db_pixelpost, $cfgrow;
  163. if ($table != "") {
  164. if (isset($table_name)) {
  165. $current_table = $table;
  166. }
  167. $result_id = mysql_list_fields( $pixelpost_db_pixelpost, $table );
  168. if ($result_id) {
  169. for ($t = 0; $t < mysql_num_fields($result_id); $t++) {
  170. if (strtolower( $fieldname) == strtolower(mysql_field_name($result_id, $t))) {
  171. $fieldexists = 1;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. // if the field does not exist: Create it!
  178. if ($fieldexists == 0) {
  179. $result = mysql_query("ALTER TABLE $table ADD `akismet_key` VARCHAR( 50 ) DEFAULT '' NOT NULL ");
  180. }
  181. // if the akismet_keep field does not exist, create it
  182. if(!mysql_query("SELECT akismet_keep from ".$pixelpost_db_prefix."config")) mysql_query("ALTER TABLE ".$pixelpost_db_prefix."config ADD `akismet_keep` INT DEFAULT '7' NOT NULL ") or die ('<span style="color:red"><b>Error: '. mysql_error());
  183. $key = $cfgrow['akismet_key'];
  184. global $pp_api_host, $pp_api_port, $pp_user_agent;
  185. $pp_api_host = $key.'.rest.akismet.com';
  186. $pp_api_port = 80;
  187. $pp_user_agent = "Pixelpost/".Get_Pixelpost_Version($pixelpost_db_prefix )." | Akismet/1.12";
  188. $addon_description = "<a name='akismet'></a>Pixelpost Add-On to filter spam using Akismet (<a href='http://akismet.com' target='_blank'>Info</a>).";
  189. function akismet_settings(){
  190. global $pixelpost_db_prefix,$cfgrow;
  191. $newakismet_key = $cfgrow['akismet_key'];
  192. $newakismet_keep = $cfgrow['akismet_keep'];
  193. if ((isset($_GET['optaction'])) && ($_GET['optaction'] == "updateantispam")) {
  194. $newakismet_key = mysql_real_escape_string($_POST['newakismet_key']);
  195. $newakismet_keep = $_POST['newakismet_keep'];
  196. if ('valid' == akismet_verify_key($newakismet_key)) {
  197. $query = "update ".$pixelpost_db_prefix."config set akismet_key='" .$newakismet_key ."'" ;
  198. $update = mysql_query($query );
  199. $addon_description .= "<font color='#006600'>API key validated succesfully.</font>";
  200. } else {
  201. $addon_description .= "<font color='#FF0000'>Unable to validate API Key</font>";
  202. }
  203. $query = "update ".$pixelpost_db_prefix."config set akismet_keep='" .$newakismet_keep ."'" ;
  204. if ($update = mysql_query($query )) $addon_description .= "<font color='#006600'>&quot;Days to keep&quot; succesfully saved.</font>";
  205. else $addon_description .= '<span style="color:red"><b>Saving &quot;Days to keep&quot; to database failed!</b></span><br />Error: '. mysql_error();
  206. }
  207. echo "<div class='jcaption'>Akismet settings</div>
  208. <div class='content'>
  209. <strong>You need an API key from Wordpress.com to activate this protection.</strong><br />
  210. If you have already signed up at Wordpress.com, copy your API key from your profile page.<br />
  211. Otherwise sign up for a free Wordpress account here: <a href='http://wordpress.com/signup/' target='_blank'>SIGN UP</a>.<br /><br />
  212. Enter your API key from Wordpress: <input type='text' name='newakismet_key' value='".$newakismet_key ."' style='width:100px'><br /><br />
  213. Days to keep comments marked as spam by Akismet: <input type='text' name='newakismet_keep' value='".$newakismet_keep ."' style='width:20px'><br /><br />
  214. </div>";
  215. }
  216. // Returns array with headers in $response[0] and entity in $response[1]
  217. function pp_http_post($request, $host, $path, $port = 80) {
  218. global $pp_user_agent;
  219. $http_request = "POST $path HTTP/1.0\r\n";
  220. $http_request .= "Host: $host\r\n";
  221. $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
  222. $http_request .= "Content-Length: " . strlen($request) . "\r\n";
  223. $http_request .= "User-Agent: {$pp_user_agent}\r\n";
  224. $http_request .= "\r\n";
  225. $http_request .= $request;
  226. $response = '';
  227. if( false !== ( $fs = fsockopen($host, $port, $errno, $errstr, 30) ) ) {
  228. fwrite($fs, $http_request);
  229. while ( !feof($fs) )
  230. $response .= fgets($fs, 1160); // One TCP-IP packet
  231. fclose($fs);
  232. $response = explode("\r\n\r\n", $response, 2);
  233. }
  234. return $response;
  235. }
  236. function akismet_verify_key( $key ) {
  237. global $pp_api_host, $pp_api_port, $cfgrow;
  238. $blog = urlencode( $cfgrow['siteurl'] );
  239. $response = pp_http_post("key=$key&blog=$blog", 'rest.akismet.com', '/1.1/verify-key', $pp_api_port);
  240. if ( 'valid' == $response[1] )
  241. return true;
  242. else
  243. return false;
  244. }
  245. //Function to report the comment is not spam
  246. function pp_submit_nonspam_comment () {
  247. global $pp_api_host, $pp_api_port, $cfgrow, $pixelpost_db_prefix;
  248. //Loop thru the $_POST['moderate_commnts_boxes'] and keep marking each comment as spam to Aksimet
  249. if (is_array($_POST['moderate_commnts_boxes'])) {
  250. foreach ($_POST['moderate_commnts_boxes'] as $cid)
  251. {
  252. $query = "SELECT * FROM {$pixelpost_db_prefix}comments WHERE id = '".(int)$cid."'";
  253. $result = mysql_query($query);
  254. if ( !mysql_num_rows($result) )
  255. {// it was deleted
  256. continue;
  257. }
  258. $row = mysql_fetch_assoc($result);
  259. $comment = array('comment_type' => 'comment', 'comment_author' => $row['name'], 'comment_author_email' => $row['email'], 'comment_author_url' => $row['url'], 'comment_content' => $row['message'], 'ip' => $row['ip'], 'user_agent' => $pp_user_agent, 'blog' => $cfgrow['siteurl']);
  260. $query_string = '';
  261. foreach ( $comment as $key => $data )
  262. {
  263. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  264. }
  265. $response = pp_http_post($query_string, $pp_api_host, "/1.1/submit-ham", $pp_api_port);
  266. //Since comment is not spam, let's mark it to publish
  267. $query = "UPDATE {$pixelpost_db_prefix}comments SET publish = 'yes' WHERE id = '".(int)$cid."'";
  268. mysql_query($query);
  269. }
  270. return true;
  271. } else {
  272. $GLOBALS['akismet_result_message'] = '<div class="jcaption confirm">You must select at least one comment.</div>';
  273. return false;;
  274. }
  275. }
  276. //Function to report the comment as spam which Akismet marked as not spam
  277. function pp_submit_spam_comment () {
  278. global $pp_api_host, $pp_api_port, $cfgrow, $pixelpost_db_prefix, $pp_user_agent;
  279. //Loop thru the $_POST['moderate_commnts_boxes'] and keep marking each comment as spam to Aksimet
  280. if (is_array($_POST['moderate_commnts_boxes'])) {
  281. foreach ($_POST['moderate_commnts_boxes'] as $cid) {
  282. $query = "SELECT * FROM {$pixelpost_db_prefix}comments WHERE id = '".(int)$cid."'";
  283. $result = mysql_query($query);
  284. if ( !mysql_num_rows($result) ) {// it was deleted
  285. continue;
  286. }
  287. $row = mysql_fetch_assoc($result);
  288. $comment = array('comment_type' => 'comment', 'comment_author' => $row['name'], 'comment_author_email' => $row['email'], 'comment_author_url' => $row['url'], 'comment_content' => $row['message'], 'ip' => $row['ip'], 'user_agent' => $pp_user_agent, 'blog' => $cfgrow['siteurl']);
  289. $query_string = '';
  290. foreach ( $comment as $key => $data ) {
  291. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  292. }
  293. $response = pp_http_post($query_string, $pp_api_host, "/1.1/submit-spam", $pp_api_port);
  294. //Since comment is spam, let's mark it not to publish
  295. $query = "UPDATE {$pixelpost_db_prefix}comments SET publish = 'spm' WHERE id = '".(int)$cid."'";
  296. mysql_query($query);
  297. }
  298. return true;
  299. } else {
  300. $GLOBALS['akismet_result_message'] = '<div class="jcaption confirm">You must select at least one comment.</div>';
  301. return false;
  302. }
  303. }
  304. ?>