Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba

Spamprotection on Custom Forms


Go to End
Reply


7 Posts   3828 Views

Avatar
Bruce

Community Member, 29 Posts

29 August 2009 at 3:59pm

Edited: 29/08/2009 4:01pm

I have been struggling to understand how the spamprotection can be added to my forms.
This is not for protection on page-comments etc, but on a simple "Contact Us" form that I have added.
I cannot figure out from the documentation how to get either Recaptcha or Mollum to work.
Judging by the number of page hits I'm seeing in that part of the forums, I guess I'm not the only one puzzled.
The steps below basically build on the format indicated by the tutorials.

So I created a "ContactusPage.ss" file in themes/mytheme/templates/Layout which calls a ContactusForm function

 ...
 <div id="Content" class="typography">   
    <% include Breadcrumbs %>       
    $Content
    <div id="Contact">
       $ContactusForm
    </div>
 </div>
 ...

I then created this function in mysite/code as "ContactusPage.php"

 <?php
 class ContactusPage extends Page {
   static $db = array(
   );
   static $has_one = array(
   );
 }

 class ContactusPage_Controller extends Page_Controller {
   function ContactusForm() {
      // Create fields
      $fields = new FieldSet(
        new TextField('Name','My name is ...'),
        new EmailField('Email','My email address is ...'),
        new TextField('Subject','Message Subject:'),
        new TextareaField('MessageText', 'My message:', '5', '35')
      );
      // Create actions
      $actions = new FieldSet(
         new FormAction('doContactusMail', 'Send my message')
      );
      $requiredFields = new RequiredFields(
         "Email", "Name", "MessageText"
      );

//$protector = SpamProtectorManager::update_form($ContactusForm, 'Message');
//if($protector) $protector->setFieldMapping('Name', 'Email');
      
   return new Form($this, 'ContactusForm', $fields, $actions,$requiredFields);
   }

   function doContactusMail($data, $form) {
      $email = new Email();
      $email->from = 'contactusform@localhost';
      $email->to = 'webbuilder@localhost';
      $email->subject = 'Web Contact: '.$data['Subject'];
      $email->body = 'My name is '.$data['Name'].', ('.$data['Email'].') and my message is '.$data['MessageText'];
      $email->sendPlain();
      Director::redirectBack('sent/');
   }
 }
 
 ?>

All that works nicely, but I cannot add a captcha to it!
I have tried both Recaptcha & Mollom in what follows

Steps:
Added module spamprotection module (v0.2) to main directory
added Recaptcha FormField Module (v0.2) to main directory
added mollum (v0.2) to the main directory
(Also added userforms to main directory as a precaution)

Following the advise at: http://doc.silverstripe.com/doku.php?id=modules:recaptcha
I edited mysite/_config.php to add:

 
SpamProtectorManager::set_spam_protector("RecaptchaProtector");
 // get a key at http://recaptcha.net/api/getkey
 RecaptchaField::$public_api_key = '<mypublic_key>';
 RecaptchaField::$private_api_key = '<myprivate_key>';
 // field creation
 $recaptchaField = new RecaptchaField('MyCaptcha');

but that throws errors
"Fatal error: Class 'RecaptchaField' not found in /var/www/Silverstripe/mysite/_config.php on line 26"

So this is obviously not right and various hacks failed, so I abandoned Recaptcha and tried the alternative ... Mollom

in mysite/_config.php I added:

 Mollom::setPublicKey('<mypublic_key>');
 Mollom::setPrivateKey('<myprivate_key>');
 SpamProtectorManager::set_spam_protector('MollomSpamProtector');

(no errors ... so far so good)
The documentation
http://doc.silverstripe.com/doku.php?id=modules:spamprotection
suggests adding a protector ... but is vague as to where
"the following code needs appear after the form creation."

So inserting the following into ContactusPage.php was a failure ...

 $protector = SpamProtectorManager::update_form($ContactusForm, 'Message');
 if($protector) $protector->setFieldMapping('Name', 'Email');

Error:
[Notice] Undefined variable: ContactusForm

So now my lack of OOP-nous has caught me ...
Is there a smart cookie out there who is able to see how I can get some captchas going?
I would prefer Recatcha to mollom, but at the moment I have ... nothing!

Avatar
Willr

Forum Moderator, 5521 Posts

29 August 2009 at 7:12pm

$protector = SpamProtectorManager::update_form($form, 'Message');
if($protector) $protector->setFieldMapping('Title', 'Content', 'Name', 'Website', 'Email');

You have to pass a valid Form object as $form. So you would need to change your code to

....
// Create actions 
...

$form = new Form($this, 'ContactusForm', $fields, $actions,$requiredFields); 

$protector = SpamProtectorManager::update_form($form, 'Captcha'); 
if($protector) $protector->setFieldMapping('Name', 'Email');

return $form;
}

As for the key errors - RecaptchaField::$public_api_key, etc should work fine as long as you have the recaptcha folder in the root and a _config file in the module. Maybe check if you copied the code straight off the wiki you didn't get the incorrectly encoded ' .

Avatar
Bruce

Community Member, 29 Posts

29 August 2009 at 9:25pm

Edited: 29/08/2009 9:32pm

Great ...
I see now that I needed to replace the

return new Form($this, 'ContactusForm', $fields, $actions,$requiredFields); 

statement with a declaration

 
$form = new Form($this, 'ContactusForm', $fields, $actions,$requiredFields);

which allowed the following lines to use $form.
Now when the two "protector" lines of code are active, I get a blank page returned
... meaning I now just need to sort the recaptcha stuff.

So: I know the recaptcha module is on the document root

recaptcha
               - tags
                       - - 0.2
                          - - - CHANGELOG  
                              - - - code  
                              - - - _config.php  
                              - - - lang  
                              - - - LICENSE  
                              - - - README

(the _config.php file has only a PHP tag)
Should I be putting the code from the wiki page in there (and not in mysite/_config.php?)

And I am confused by what you mean by "you didn't get the incorrectly encoded ' ."
I cannot see anything amiss, has the wiki page got an error in it?

Avatar
Willr

Forum Moderator, 5521 Posts

29 August 2009 at 10:34pm

Looks like the module structure isn't correct. Tags and 0.2 etc should not be there. It should be the same structure as the rest of the modules

recaptcha
 - code
 - _config
 - README
 ...

Avatar
Bruce

Community Member, 29 Posts

6 September 2009 at 12:43am

Thanks Will,
moving the files to the root of the recaptcha directory certainly helped!
(to the uninitiated, the default layout of the module *should* be ready to go, & not have to be reconstructed)

Anyway, adding the recaptcha field to the field definitions, was the last building block.

As an aid to following forum members: this is the final definition:

 <?php
 class ContactusPage extends Page {
   static $db = array(
   );
   static $has_one = array(
   );
 }

 class ContactusPage_Controller extends Page_Controller {
   function ContactusForm() {
      // Create fields
      $fields = new FieldSet(
        new TextField('Name','My name is ...'),
        new EmailField('Email','My email address is ...'),
        new TextField('Subject','Message Subject:'),
        new TextareaField('MessageText', 'My message:', '5', '35'),
        new RecaptchaField('MyCaptcha')
      );
      // Create actions
      $actions = new FieldSet(
         new FormAction('doContactusMail', 'Send my message')
      );
      $requiredFields = new RequiredFields(
         "Email", "Name", "MessageText"
      );

      $form = new Form($this, 'ContactusForm', $fields, $actions, $requiredFields);
      $protector = SpamProtectorManager::update_form($form, 'MyCaptcha');
      if($protector) $protector->setFieldMapping('Name', 'Email');
      return $form;
   }

   function doContactusMail($data, $form) {
      $email = new Email();
      $email->from = 'contactusform@localhost';
      $email->to = 'webbuilder@localhost';
      $email->subject = 'Web Contact: '.$data['Subject'];
      $email->body = 'My name is '.$data['Name'].', ('.$data['Email'].') and my message is '.$data['MessageText'];
      $email->sendPlain();
      Director::redirectBack('sent/');
   }
 }
 
 ?>

In order to get the Captcha to actually show on the page, the strict www3 declaration at the top of my default page (themes/mytheme/templates/Page.ss) needed to be changed from:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

to XHTML-transitional

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

not the best, but it gets the captcha going!

Avatar
affilimate

Community Member, 9 Posts

8 October 2009 at 12:26am

Hi Mike,

the error message on the recaptcha seems to be too long for this theme - can you have a look at the screenshot attached and let meknow how to fix this.
Thanks

Attached Files
Avatar
Willr

Forum Moderator, 5521 Posts

8 October 2009 at 8:45am

thats really a theme author issue. I think you need to add a display: block to the error message