Skip to content

Commit

Permalink
API Update to SpamProtector API changes
Browse files Browse the repository at this point in the history
API Fixed some references to old api. Some refactoring for readability.
BUG Fixed incorrect third party file paths
  • Loading branch information
tractorcow committed Feb 18, 2014
1 parent 062d41a commit d3ac217
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 24 deletions.
19 changes: 16 additions & 3 deletions code/MollomSpamProtector.php
@@ -1,6 +1,6 @@
<?php

require_once(MOLLOM_PATH . '/vendor/mollom/mollom/mollom.class.inc');
require_once(BASE_PATH . '/vendor/mollom/client/mollom.class.inc');

/**
* A customized version of {@link Mollom} to run on SilverStripe. See the
Expand All @@ -11,6 +11,11 @@
*/

class MollomSpamProtector extends Mollom implements SpamProtector {

/**
* @var array
*/
private $fieldMapping = array();

/**
* @var array
Expand Down Expand Up @@ -168,11 +173,19 @@ protected function request($method, $server, $path, $query = NULL, array $header
/**
* Return the Field that we will use in this protector.
*
* @param string $name
* @param string $title
* @param mixed $value
* @return MollomField
*/
public function getFormField($name = "MollomField", $title = "Captcha", $value = null) {
$field = new MollomField($name, $title, $value);

$field->setFieldMapping($this->fieldMapping);
return $field;
}
}

public function setFieldMapping($fieldMapping) {
$this->fieldMapping = $fieldMapping;
}

}
87 changes: 66 additions & 21 deletions code/formfields/MollomField.php
Expand Up @@ -10,6 +10,11 @@
*/

class MollomField extends FormField {

/**
* @var array
*/
private $fieldMapping = array();

/**
* @config
Expand Down Expand Up @@ -60,26 +65,44 @@ public function getCaptcha() {
return null;
}

/**
* Determines if the current user is exempt from spam detection
*
* @return boolean True if the user is exempt from spam detection
*/
protected function exemptUser() {

// Never show captcha for admins
if (Permission::check('ADMIN')) {
return true;
}

// Allow logged in members to bypass captcha if allowed
if (Member::currentUser() && !Config::inst()->get('MollomField', 'force_check_on_members')) {
return true;
}

return false;
}

/**
* Return if we should show the captcha to the user.
*
* @return boolean
*/
public function getShowCaptcha() {
if(Permission::check('ADMIN')) {
return false;
}

$requested = Session::get('mollom_captcha_requested');
$noMapping = ($this->getForm() && !$this->getForm()->hasSpamProtectionMapping());

// If this user is eligible to bypass spam detection, don't show them the recaptcha
if($this->exemptUser()) return false;
// Show captcha if always requested
if (Config::inst()->get('MollomField', 'always_show_captcha')) return true;

if($requested || !$noMapping) {
if(!Member::currentUser() || !Config::inst()->get('MollomField', 'force_check_on_members')) {
return true;
}
}
// If a captcha is requested then we need to redisplay it to the user
if (Session::get('mollom_captcha_requested')) return true;

return (bool) Config::inst()->get('MollomField', 'always_show_captcha');
// If there are no field mappings, then the captcha is mandatory
return empty($this->fieldMapping);
}

/**
Expand All @@ -98,6 +121,22 @@ public function getMollom() {

return $this->_mollom;
}

/**
* @return array
*/
public function getSpamMappedData() {
if(empty($this->fieldMapping)) return null;

$result = array();
$data = $this->form->getData();

foreach($this->fieldMapping as $fieldName => $mappedName) {
$result[$mappedName] = (isset($data[$fieldName])) ? $data[$fieldName] : null;
}

return $result;
}

/**
* Validate the captcha information
Expand All @@ -106,21 +145,16 @@ public function getMollom() {
*
* @return boolean
*/
public function validate($validator) {
if(Permission::check('ADMIN')) {
$this->clearMollomSession();

return true;
}
public function validate($validator) {

if(Member::currentUser() && !Config::inst()->get('MollomField', 'force_check_on_members')) {
// Bypass spam detection for eligible users
if($this->exemptUser()) {
$this->clearMollomSession();

return true;
}

$session_id = Session::get("mollom_session_id");
$mapped = $this->getForm()->getSpamMappedData();
$mapped = $this->getSpamMappedData();
$data = array();

// prepare submission
Expand Down Expand Up @@ -238,4 +272,15 @@ private function clearMollomSession() {
Session::clear('mollom_session_id');
Session::clear('mollom_captcha_requested');
}

/**
* Set the fields to map spam protection too
*
* @param array $fieldMapping array of Field Names, where the indexes of the array are
* the field names of the form and the values are the standard spamprotection
* fields used by the protector
*/
public function setFieldMapping($fieldMapping) {
$this->fieldMapping = $fieldMapping;
}
}

0 comments on commit d3ac217

Please sign in to comment.