Monday, August 9, 2010

reCAPTCHA and nms FormMail - part I

Another site I manage uses nms FormMail to provide visitors with a way to get in contact.

To help reduce spam encountered by this site a reCAPTCHA challenge has been added to the form used by FormMail.

This post, together with the second in the series, discuss the changes required to add reCAPTCHA to nmsFormMail.

To achieve this the following changes were made to the nms FormMail script…

  1. At the beginning of the script reference the light weight user agent LWP::UserAgent…
    ...
    
    use strict;
    use vars qw(
      $DEBUGGING $emulate_matts_code $secure %more_config
      $allow_empty_ref $max_recipients $mailprog @referers
      @allow_mail_to @recipients %recipient_alias
      @valid_ENV $date_fmt $style $send_confirmation_mail
      $confirmation_text $locale $charset $no_content
      $double_spacing $wrap_text $wrap_style $postmaster 
      $address_style
    );
    use LWP::UserAgent;
    ...
    
  2. In the handle_request() method add a call to check_captcha after checking for missing fields.
    ...
     my @missing = $self->get_missing_fields;
      if (scalar @missing) {
        $self->missing_fields_output(@missing);
        return;
      }
    
      $self->check_captcha    or return;
    
      my $date     = $self->date_string;
    ...
    
  3. After check_method_is_post() insert the sub routine check_captcha()…
    Not forgetting to replace ‘your_private_key’ with your private key.

    ...
    =item check_captcha ()
    
    Check the CAPTCHA response via the reCAPTCHA service.
    
    =cut
    
    sub check_captcha {
        
      my ($self) = @_;
      my ($ua) = LWP::UserAgent->new(); 
      my ($result) = ($ua)->post(
    	'http://api-verify.recaptcha.net/verify', 
    	{  
    		privatekey => 'your_private_key', 
    		remoteip   => $ENV{'REMOTE_ADDR'}, 
    		challenge => $self->{Form}{'recaptcha_challenge_field'}, 
    		response => $self->{Form}{'recaptcha_response_field'} 
    	});
      
    	if ( $result->is_success && $result->content =~ /^true/) {
    		return 1;
    	} else {
    
    		if ($self->{FormConfig}{'captcha_fail_redirect'}) {
    			print $self->cgi_object->redirect( $self->{FormConfig}{'captcha_fail_redirect'} );
    
    			return 0;
    		}
    		else {
    			my ($self) = @_;
    			$self->error_page( 'Error: Captcha Check Failed', <<END );
    <p>
      The Captcha response of the form you submitted did not match the challenge.
      Please check the form and make sure that your response matches the challenge in the captcha image.
      You can use the browser back button to return to the form.
    </p>
    END
    			return 0;
    		}
    	}
    }
    ...
    
  4. Add an additional form field captcha_fail_redirect to configuration_form_fields
    ...
    
       return_link_url
        print_blank_fields
        missing_fields_redirect
        captcha_fail_redirect
      );
    }
    ...
    
  5. And finally add captcha_fail_redirect to the list of valid names in parse_config_form_input
    ...
      my $val = $self->strip_nonprint($self->cgi_object->param($name));
      if ($name =~ /return_link_url|redirect|captcha_fail_redirect$/) {
        $val = $self->validate_url($val);
      }
      $self->{FormConfig}{$name} = $val;
    ...
    

A modified nms FormMail script is available for download.



Bookmark Site

Bookmark and Share

Tags

  C#     Contracting     Perl     For Sale     Availability     PHP     FlatPress     General     Technology  

Search

Admin area