Jump to:

10993 Posts in 2727 Topics by 1818 members

All other Modules

SilverStripe Forums » All other Modules » Spamprotection on Custom Forms

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 2741 Views
  • Bruce
    Avatar
    Community Member
    29 Posts

    Spamprotection on Custom Forms Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Spamprotection on Custom Forms Link to this post

    $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 ' .

  • Bruce
    Avatar
    Community Member
    29 Posts

    Re: Spamprotection on Custom Forms Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Spamprotection on Custom Forms Link to this post

    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
    ...

  • Bruce
    Avatar
    Community Member
    29 Posts

    Re: Spamprotection on Custom Forms Link to this post

    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!

  • affilimate
    Avatar
    Community Member
    9 Posts

    Re: Spamprotection on Custom Forms Link to this post

    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
  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Spamprotection on Custom Forms Link to this post

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

    2741 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.