rayvic

No Password fields

Nov 13th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.88 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Spam Free Wordpress
  4. Plugin URI: http://www.toddlahman.com/spam-free-wordpress/
  5. Description: Comment spam blocking plugin that uses anonymous password authentication to achieve 100% automated spam blocking with zero false positives, plus a few more features.
  6. Version: 1.5.1
  7. Author: Todd Lahman, LLC
  8. Author URI: http://www.toddlahman.com/
  9. */
  10.  
  11. // Plugin version
  12. $spam_free_wordpress_version = "1.5.1";
  13.  
  14. /*
  15. Copyright 2007 - 2011 by Todd Lahman, LLC.
  16.  
  17. This program is free software; you can redistribute it and/or modify
  18. it under the terms of the GNU General Public License, version 2, as
  19. published by the Free Software Foundation.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30.  
  31. // Add default database settings on plugin activation
  32. function sfw_add_default_data() {
  33. $sfw_options = array(
  34. 'blocklist_keys' => '',
  35. 'lbl_enable_disable' => 'disable',
  36. 'remote_blocked_list' => '',
  37. 'rbl_enable_disable' => 'disable',
  38. 'pw_field_size' => '30',
  39. 'tab_index' => '',
  40. 'affiliate_msg' => '',
  41. 'toggle_stats_update' => 'enable',
  42. 'toggle_html' => 'disable'
  43. // 'sfw_version' => '1.5.1'
  44. );
  45. add_option('spam_free_wordpress', $sfw_options);
  46. add_option('sfw_spam_hits', '1');
  47. }
  48.  
  49. // variable used as global to retrieve option array for functions
  50. $wp_sfw_options = get_option('spam_free_wordpress');
  51.  
  52. // Gets Spam Blocked Count
  53. $sfw_count = number_format_i18n(get_option('sfw_spam_hits'));
  54.  
  55. // Runs add_default_data function above when plugin activated
  56. register_activation_hook( __FILE__, 'sfw_add_default_data' );
  57.  
  58. // Delete the default options from database when plugin deactivated,
  59. // The post comment passwords can be deleted also using the following SQL statement.
  60. // DELETE from wp_postmeta WHERE meta_key = "sfw_comment_form_password" ;
  61.  
  62.  
  63. // Deletes all options listed in remove_default_data when plugin deactivated
  64. // Remove // to enable an option to be deleted
  65. function sfw_remove_default_data() {
  66. // delete_option('spam_free_wordpress');
  67. // delete_option('sfw_spam_hits');
  68. }
  69.  
  70. // Deletes all options listed in remove_default_data when plugin deactivated
  71. register_deactivation_hook( __FILE__, 'sfw_remove_default_data' );
  72.  
  73. // Checks to see if comment form password exists and if not creates one in custom fields
  74. function sfw_comment_pass_exist_check() {
  75. global $post;
  76. $new_post_comment_pwd = wp_generate_password(12, false);
  77. $sfw_pwd_exists_check = get_post_meta( $post->ID, 'sfw_comment_form_password', true );
  78.  
  79. if( empty($sfw_pwd_exists_check) || !$sfw_pwd_exists_check && comments_open() ) {
  80. update_post_meta($post->ID, 'sfw_comment_form_password', $new_post_comment_pwd);
  81. }
  82. }
  83. add_action('loop_start', 'sfw_comment_pass_exist_check', 1);
  84.  
  85. // Creates a new comment form password each time a comment is saved in the database
  86. function sfw_new_comment_pass() {
  87. global $post;
  88. $new_comment_pwd = wp_generate_password(12, false);
  89. $old_password = get_post_meta( $post->ID, 'sfw_comment_form_password', true );
  90.  
  91. update_post_meta($post->ID, 'sfw_comment_form_password', $new_comment_pwd, $old_password);
  92. }
  93.  
  94. // Call the function to change key 2 password to custom fields when after each new comment is saved in the database.
  95. add_action('comment_post', 'sfw_new_comment_pass', 1);
  96.  
  97. // Gets the remote IP address even if behind a proxy
  98. function get_remote_ip_address() {
  99. if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
  100. $ip_address = $_SERVER['HTTP_CLIENT_IP'];
  101. } else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  102. $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  103. } else if(!empty($_SERVER['REMOTE_ADDR'])) {
  104. $ip_address = $_SERVER['REMOTE_ADDR'];
  105. } else {
  106. $ip_address = '';
  107. }
  108. return $ip_address;
  109. }
  110.  
  111. // Returns Local Blocklist
  112. function sfw_local_blocklist_check() {
  113. global $wp_sfw_options;
  114.  
  115. // Gets IP address of commenter
  116. $comment_author_ip = get_remote_ip_address();
  117.  
  118. $local_blocklist_keys = trim( $wp_sfw_options['blocklist_keys'] );
  119. if ( '' == $local_blocklist_keys )
  120. return false; // If blocklist keys are empty
  121. $local_key = explode("\n", $local_blocklist_keys );
  122.  
  123. foreach ( (array) $local_key as $lkey ) {
  124. $lkey = trim($lkey);
  125.  
  126. // Skip empty lines
  127. if ( empty($lkey) ) { continue; }
  128.  
  129. // Can use '#' to comment out line in blocklist
  130. $lkey = preg_quote($lkey, '#');
  131.  
  132. $pattern = "#$lkey#i";
  133. if (
  134. preg_match($pattern, $comment_author_ip)
  135. )
  136. return true;
  137. }
  138. return false;
  139. }
  140.  
  141. // Returns Remote Blocklist
  142. function sfw_remote_blocklist_check() {
  143. global $wp_sfw_options;
  144.  
  145. // Gets IP address of commenter
  146. $comment_author_ip = get_remote_ip_address();
  147. // Retrieves remote blocklist url from database
  148. $rbl_url = $wp_sfw_options['remote_blocked_list'];
  149. // Uses a URL to retrieve a list of IP address in an array
  150. $get_remote_blocklist = wp_remote_get($rbl_url);
  151.  
  152. if ( '' == $rbl_url )
  153. return false; // If blocklist keys are empty or url is not in the database
  154. $remote_key = explode("\n", $get_remote_blocklist['body'] ); // Turns blocklist array into string and lists each IP address on new line
  155.  
  156. foreach ( (array) $remote_key as $rkey ) {
  157. $rkey = trim($rkey);
  158.  
  159. // Skip empty lines
  160. if ( empty($rkey) ) { continue; }
  161.  
  162. // Can use '#' to comment out line in blocklist
  163. $rkey = preg_quote($rkey, '#');
  164.  
  165. $pattern = "#$rkey#i";
  166. if (
  167. preg_match($pattern, $comment_author_ip)
  168. )
  169. return true;
  170. }
  171. return false;
  172. }
  173.  
  174. // Customizable Affiliate link
  175. function custom_affiliate_link() {
  176. $wp_sfw_options = get_option('spam_free_wordpress');
  177.  
  178. $aff_msg = $wp_sfw_options['affiliate_msg'];
  179.  
  180. if ($wp_sfw_options['affiliate_msg'] =='') {
  181. echo "<a href='http://www.toddlahman.com/spam-free-wordpress/'>Make Your Blog Spam Free</a>";
  182. } else {
  183. echo "<a href='http://www.toddlahman.com/spam-free-wordpress/'>".$aff_msg."</a>";
  184. }
  185. }
  186.  
  187. // Function for comments.php file
  188. function tl_spam_free_wordpress_comments_form() {
  189. global $wp_sfw_options, $post, $spam_free_wordpress_version, $wp_version, $sfw_count;
  190.  
  191. $sfw_comment_form_password_var = get_post_meta( $post->ID, 'sfw_comment_form_password', true );
  192.  
  193. $sfw_pw_field_size = $wp_sfw_options['pw_field_size'];
  194. $sfw_tab_index = $wp_sfw_options['tab_index'];
  195.  
  196. // If the reader is logged in don't require password for comments.php
  197. if ( !is_user_logged_in() ) {
  198. // Hidden credit
  199. echo '<!-- ' . $sfw_count . ' Spam Comments Blocked so far by Spam Free Wordpress version '.$spam_free_wordpress_version.' located at http://www.toddlahman.com/spam-free-wordpress/ -->';
  200. // Commenter IP address
  201. echo "<input type='hidden' name='comment_ip' id='comment_ip' value='".get_remote_ip_address()."' />";
  202. // Reader must enter this password manually on the comment form
  203. echo "<p>* Copy this password:
  204. <input type='text' value='".$sfw_comment_form_password_var."' onclick='this.select()' size='".$sfw_pw_field_size."' /></p>";
  205. echo "<p>* Type or paste password here:
  206. <input type='text' name='passthis' id='passthis' value='".$comment_passthis."' size='".$sfw_pw_field_size."' tabindex='".$sfw_tab_index."' /></p>";
  207. // Shows how many comment spam have been killed on the comment form
  208. if ($wp_sfw_options['toggle_stats_update'] == "enable") {
  209. // number_format will cause errors in other locales, so Wordpress created the undocumented number_format_i18n function that properly localizes the number
  210. // Examples:
  211. // http://cleverwp.com/date_i18n-reference-and-usage/
  212. // more here http://wpcodesnippets.info/blog/7-cool-undocumented-wordpress-functions.html
  213. // http://wpengineer.com/1918/24th-door-the-wpe-quit-smoking-widget/
  214. // http://hitchhackerguide.com/2011/02/12/number_format_i18n/
  215. // http://hitchhackerguide.com/2011/02/12/number_format_i18n-2/
  216. echo '<p>' . $sfw_count . ' Spam Comments Blocked so far by <a href="http://www.toddlahman.com/spam-free-wordpress/" target="_blank">Spam Free Wordpress</a></p>';
  217. } else {
  218. echo "";
  219. }
  220. }
  221. }
  222.  
  223. // Function for wp-comments-post.php file located in the root Wordpress directory. The same directory as the wp-config.php file.
  224. function tl_spam_free_wordpress_comments_post() {
  225. global $post, $wp_sfw_options;
  226.  
  227. $sfw_comment_script = get_post_meta( $post->ID, 'sfw_comment_form_password', true );
  228.  
  229. // If the reader is logged in don't require password for wp-comments-post.php
  230. if ( !is_user_logged_in() ) {
  231.  
  232. // Compares current comment form password with current password for post
  233. if ($_POST['passthis'] == '' || $_POST['passthis'] != $sfw_comment_script)
  234. wp_die( __('Error 1: Click back and type in the password.', spam_counter()) );
  235.  
  236. // Compares commenter IP address to local blocklist
  237. if ($wp_sfw_options['lbl_enable_disable'] == 'enable') {
  238. if ($_POST['comment_ip'] == '' || $_POST['comment_ip'] == sfw_local_blocklist_check() )
  239. wp_die( __('Spam Blocked by Spam Free Wordpress (local blocklist)', spam_counter()) );
  240. }
  241.  
  242. // Compares commenter IP address to remote blocklist
  243. if ($wp_sfw_options['rbl_enable_disable'] == 'enable') {
  244. if ($_POST['comment_ip'] == '' || $_POST['comment_ip'] == sfw_remote_blocklist_check() )
  245. wp_die( __('Spam Blocked by Spam Free Wordpress (remote blocklist)', spam_counter()) );
  246. }
  247.  
  248. }
  249. }
  250.  
  251. // Counts number of comment spam hits and stores in options database table
  252. function spam_counter() {
  253. $s_hits = get_option('sfw_spam_hits');
  254. update_option('sfw_spam_hits', $s_hits+1);
  255. }
  256.  
  257. // displays comment spam hits wherever it is called
  258. function display_spam_hits() {
  259. $s_hits = get_option('sfw_spam_hits');
  260. return $s_hits;
  261. }
  262.  
  263. // Register Admin Options Page
  264. function register_spam_free_wordpress_options_page() {
  265. add_options_page('Spam Free Wordpress Configuration', 'Spam Free Wordpress', 'manage_options', 'spam-free-wordpress-admin-page', 'spam_free_wordpress_options_page');
  266. }
  267.  
  268. // Admin Settings Options Page function
  269. function spam_free_wordpress_options_page() {
  270.  
  271. // Check to see if user has adequate permission to access this page
  272. if (!current_user_can('manage_options')){
  273. wp_die( __('You do not have sufficient permissions to access this page.') );
  274. }
  275.  
  276. global $spam_free_wordpress_version, $sfw_count;
  277.  
  278. ?>
  279. <div class="wrap">
  280. <h2>Spam Free Wordpress <?php echo $spam_free_wordpress_version; ?> Settings</h2>
  281.  
  282. <form method="post" action="">
  283. <?php
  284.  
  285. echo '<table class="form-table">';
  286. if ($_POST['options']) {
  287.  
  288. $new_wp_sfw_options = $_POST['wp_sfw_options'];
  289. update_option('spam_free_wordpress', $new_wp_sfw_options);
  290.  
  291. // Display saved message when options are updated.
  292. $msg_status = 'Spam Free Wordpress settings saved.';
  293. _e('<div id="message" class="updated fade"><p>' . $msg_status . '</p></div>');
  294. }
  295.  
  296. $wp_sfw_options = get_option('spam_free_wordpress');
  297.  
  298. ?>
  299.  
  300. <table class="form-table">
  301. <tr>
  302. <td valign="top">
  303. <h3>Local Comment Blocklist</h3>
  304. <p>The Local Blocklist is a list of blocked IP addresses stored in the blog database. When a comment comes from an IP address matching the Blocklist it will be blocked, which means you will never see it as waiting for approval or marked as spam. Blocked commenters will be able to view your blog, but any comments they submit will be blocked, which means not saved to the database, and they will see the message &#8220;Spam Blocked.&#8221;</p>
  305. <p>Enter one IP address (for example 192.168.1.1) per line. Wildcards like 192.168.1.* will not work.</p>
  306. <p><code>#</code> can be used to comment out an IP address.</p>
  307. <fieldset>
  308. <p>On <input type="radio" name="wp_sfw_options[lbl_enable_disable]" <?php echo (($wp_sfw_options['lbl_enable_disable'] == "enable") ? 'checked="checked"' : '') ; ?> value="enable" />&nbsp;&nbsp; Off <input type="radio" name="wp_sfw_options[lbl_enable_disable]" <?php echo (($wp_sfw_options['lbl_enable_disable'] == "disable") ? 'checked="checked"' : '') ; ?> value="disable" />
  309. </fieldset>
  310. <fieldset>
  311. <textarea name="wp_sfw_options[blocklist_keys]" cols='20' rows='12' ><?php echo $wp_sfw_options['blocklist_keys']; ?></textarea>
  312. </fieldset>
  313.  
  314. <h3>Remote Comment Blocklist</h3>
  315. <p>The Remote Comment Blocklist accesses a text file list of IP addresses on a remote server to block comment spam. This allows a global IP address blocklist to be shared with multiple blogs. It is also possible to use the Local Comment Blocklist for blog specific blocking, and the Remote Comment Blocklist for global blocking used by mutliple blogs at the same time. Remote Comment Blocklist works exactly the same way as the Local Comment Blocklist, except it is on a remote server. The URL to the remote text file could be for example: <code>http://www.example.com/mybl/bl.txt</code></p>
  316. <p><code>#</code> can be used to comment out an IP address.</p>
  317. <fieldset>
  318. <p>On <input type="radio" name="wp_sfw_options[rbl_enable_disable]" <?php echo (($wp_sfw_options['rbl_enable_disable'] == "enable") ? 'checked="checked"' : '') ; ?> value="enable" />&nbsp;&nbsp; Off <input type="radio" name="wp_sfw_options[rbl_enable_disable]" <?php echo (($wp_sfw_options['rbl_enable_disable'] == "disable") ? 'checked="checked"' : '') ; ?> value="disable" />
  319. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" size="60" name="wp_sfw_options[remote_blocked_list]" value="<?php echo $wp_sfw_options['remote_blocked_list']; ?>" />&nbsp;&nbsp; Enter URL to remote text file.</p>
  320. </fieldset>
  321.  
  322. <h3>Password Form Customization</h3>
  323. <fieldset>
  324. <p>
  325. <input type="text" name="wp_sfw_options[pw_field_size]" size="4" value="<?php echo $wp_sfw_options['pw_field_size']; ?>" />&nbsp;&nbsp; Password Field Size. Default is 30.
  326. &nbsp;&nbsp;&nbsp;<input type="text" name="wp_sfw_options[tab_index]" size="4" value="<?php echo $wp_sfw_options['tab_index']; ?>" />&nbsp;&nbsp; Tab Index
  327. </p>
  328. </fieldset>
  329.  
  330. <h3>Remove HTML from Comments</h3>
  331. <p>Strips the HTML from comments to render spam links as plain text. Also removes the allowed HTML tags message from below the comment box.</p>
  332. <fieldset>
  333. <p>On <input type="radio" name="wp_sfw_options[toggle_html]" <?php echo (($wp_sfw_options['toggle_html'] == "enable") ? 'checked="checked"' : '') ; ?> value="enable" />&nbsp;&nbsp; Off <input type="radio" name="wp_sfw_options[toggle_html]" <?php echo (($wp_sfw_options['toggle_html'] == "disable") ? 'checked="checked"' : '') ; ?> value="disable" /></p>
  334. </fieldset>
  335.  
  336. <h3>Pingbacks and Trackbacks</h3>
  337. <p>The plugin below will close pingbacks and trackbacks on all posts and pages on a blog.</p>
  338. <p>Download the Auto Close Pings and Trackbacks plugin from the <a href="http://www.toddlahman.com/spam-free-wordpress/" target="_blank">Spam Free Wordpress</a> homepage.</p>
  339. <p>To make sure pingbacks and trackbacks are closed on future posts and pages, go to <code>Settings -> Discussion</code> and uncheck the box next to <code>Allow link notifications from other blogs (pingbacks and trackbacks)</code>.</p>
  340.  
  341. <h3>Share Link Custom Message</h3>
  342. <p>Customize a URL link to the Spam Free Wordpress plugin page below if you want to share it with others somewhere else on your blog other than the comment form.</p>
  343. <fieldset>
  344. <p>Link Message &nbsp;&nbsp;<input type="text" size="60" name="wp_sfw_options[affiliate_msg]" value="<?php echo $wp_sfw_options['affiliate_msg']; ?>" /> &nbsp;&nbsp;<?php if(function_exists('custom_affiliate_link')) { custom_affiliate_link(); } ?></p>
  345. </fieldset>
  346. <p>Copy and paste the line of code below into a template file to display the custom share link.</p>
  347. <code>&lt;?php if(function_exists('custom_affiliate_link')) { custom_affiliate_link(); } ?&gt;</code>
  348.  
  349. <h3>Comment Form Spam Stats</h3>
  350. <p>Displays the number of spam comments blocked on the comment form, followed by a link to the Spam Free Wordpress homepage.</p>
  351. <p><b>Although PayPal donations are welcome, leaving this on is welcome compensation for this free plugin.</b></p>
  352. <fieldset>
  353. <p>On <input type="radio" name="wp_sfw_options[toggle_stats_update]" <?php echo (($wp_sfw_options['toggle_stats_update'] == "enable") ? 'checked="checked"' : '') ; ?> value="enable" />&nbsp;&nbsp; Off <input type="radio" name="wp_sfw_options[toggle_stats_update]" <?php echo (($wp_sfw_options['toggle_stats_update'] == "disable") ? 'checked="checked"' : '') ; ?> value="disable" /></p>
  354. </fieldset>
  355.  
  356. <p class="submit">
  357. <input type="submit" name="options" class="button-primary" value="<?php _e('Save Changes') ?>" />
  358. </p>
  359. </form>
  360. <h2><font style="BACKGROUND-COLOR: #ffffff">What Am I Worth?</font></h2>
  361. <p><font style="BACKGROUND-COLOR: #ffffff"><h2><tt>Is a reliable spam fighting plugin worth a dollar?</tt></h2></font></p>
  362. <p>
  363. <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  364. <input type="hidden" name="cmd" value="_s-xclick">
  365. <input type="hidden" name="hosted_button_id" value="SFVH6PCCC6TLG">
  366. <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  367. <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
  368. </form>
  369. </p>
  370. <br />
  371.  
  372. <h3>Installation Instructions</h3>
  373. <p>Complete installation instructions are available at <a href="http://www.toddlahman.com/spam-free-wordpress/" target="_blank">Spam Free Wordpress</a>.
  374.  
  375. <hr />
  376. <h4><a href="http://www.toddlahman.com/" target="_blank">Spam Free Wordpress</a> was built and created by Todd Lahman.</h4>
  377.  
  378. </td>
  379.  
  380. <td valign="top" bgcolor="#FFFFFF">
  381. <div align="center"><h3>Blocked Comment Spam</h3></div>
  382. <p align="center"><b><big><?php echo $sfw_count; ?></big></b></p>
  383. <br />
  384.  
  385. <div id="sideblock" style="float:right;width:275px;margin-left:10px;">
  386. <iframe width="275" height="1070" frameborder="0" src="http://www.toddlahman.com/plugin-news/sfw/spw-plugin-news.html?utm_source=sfw-plugin&utm_medium=sfw-plugin&utm_campaign=sfw-plugin"></iframe>
  387. </div>
  388. </td>
  389. </tr>
  390. </table>
  391.  
  392. <?php
  393.  
  394. }
  395.  
  396. // Remove note after comment box that says which HTML tags can be used in comment
  397. function sfw_remove_allowed_tags_field($no_allowed_tags) {
  398. unset($no_allowed_tags['comment_notes_after']);
  399. return $no_allowed_tags;
  400. }
  401.  
  402. // Strips out html from comment form when enabled
  403. if ($wp_sfw_options['toggle_html'] == "enable" && version_compare($wp_version, '3.0', '>=' )) {
  404. // Removes all HTML from comments and leaves it only as text
  405. add_filter('comment_text', 'wp_filter_nohtml_kses');
  406. add_filter('comment_text_rss', 'wp_filter_nohtml_kses');
  407. add_filter('comment_excerpt', 'wp_filter_nohtml_kses');
  408. // remove tags from below comment form
  409. add_filter('comment_form_defaults','sfw_remove_allowed_tags_field');
  410. }
  411.  
  412. // Adds password field to comment form is > Wordpress 3.0 and using comment_form function to generate comment form
  413. function do_spam_free_wordpress_automation() {
  414. global $wp_version;
  415.  
  416. // run the following code only if using Wordpress 3.x or greater
  417. if ( version_compare($wp_version, '3.0', '>=' ) ) {
  418.  
  419. // Calls the password form for comments.php if the comment_form function is outputting comment form fields
  420. add_filter('comment_form_after_fields', 'tl_spam_free_wordpress_comments_form', 1);
  421. }
  422. }
  423.  
  424. // Calls the Wordpress 3.x code for admin settings page
  425. add_action('init', 'do_spam_free_wordpress_automation', 1);
  426. // Calls the wp-comments-post.php authentication
  427. add_action('pre_comment_on_post', 'tl_spam_free_wordpress_comments_post', 1);
  428. // Add Admin Options Page
  429. add_action('admin_menu', 'register_spam_free_wordpress_options_page');
  430.  
  431. ?>
  432.  
Advertisement
Add Comment
Please, Sign In to add comment