APICHANGE: Removed getFieldName() and replaced it with getFormField(). APICHANGE: removed individual calls to update_form() on Spam Protectors to make the code simplier. APICHANGE: removed callback objects on the SpamProtectorFields as they were no longer used. APICHANGE: removed fieldMapping from individual formfields to the abstract class. MINOR: fixed misc documentation with package names

This commit is contained in:
Will Rossiter 2009-09-17 04:06:53 +00:00
parent 819732d910
commit 78038e14ca
4 changed files with 64 additions and 90 deletions

View File

@ -4,42 +4,25 @@
* Editable Spam Protecter Field. Used with the User Defined Forms module (if * Editable Spam Protecter Field. Used with the User Defined Forms module (if
* installed) to allow the user to have captcha fields with their custom forms * installed) to allow the user to have captcha fields with their custom forms
* *
* @package SpamProtection * @package spamprotection
*/ */
class EditableSpamProtectionField extends EditableFormField { class EditableSpamProtectionField extends EditableFormField {
static $singular_name = 'Spam Protection Field'; static $singular_name = 'Spam Protection Field';
static $plural_name = 'Spam Protection Fields'; static $plural_name = 'Spam Protection Fields';
function __construct( $record = null, $isSingleton = false ) {
parent::__construct( $record, $isSingleton );
}
function getFormField() { function getFormField() {
return $this->createField();
}
function getFilterField() {
return $this->createField(true);
}
function createField() {
if($protector = SpamProtectorManager::get_spam_protector()) { if($protector = SpamProtectorManager::get_spam_protector()) {
if($protector) { if($protector) {
$protector = new $protector(); $protector = new $protector();
if($class = $protector->getFieldName()) { return $protector->getFormField($this->Name, $this->Title, null);
return new $class($class, $this->Title);
}
} }
} }
return false; return false;
} }
/**
* @return string
*/
public function Icon() { public function Icon() {
return 'spamprotection/images/' . strtolower($this->class) . '.png'; return 'spamprotection/images/' . strtolower($this->class) . '.png';
} }

View File

@ -2,18 +2,17 @@
/** /**
* Spam Protector base interface. All Protectors should implement this interface * Spam Protector base interface. All Protectors should implement this interface
* to ensure that they contain all the correct methods and we do not get too many * to ensure that they contain all the correct methods.
* odd missing function errors
* *
* @package SpamProtection * @package spamprotection
*/ */
interface SpamProtector { interface SpamProtector {
/** /**
* Return the name of the Field Associated with this protector * Return the Field Associated with this protector
*/ */
public function getFieldName(); public function getFormField($name = null, $title = null, $value = null, $form = null, $rightTitle = null);
/** /**
* Function required to handle dynamic feedback of the system. * Function required to handle dynamic feedback of the system.
@ -21,14 +20,4 @@ interface SpamProtector {
*/ */
public function sendFeedback($object = null, $feedback = ""); public function sendFeedback($object = null, $feedback = "");
/**
* Updates the form with the given protection
*/
public function updateForm($form, $before = null, $fieldsToSpamServiceMapping = null);
/**
* Set which fields need to be mapped for protection
*/
public function setFieldMapping($fieldToPostTitle, $fieldsToPostBody = null, $fieldToAuthorName = null, $fieldToAuthorUrl = null, $fieldToAuthorEmail = null, $fieldToAuthorOpenId = null);
} }
?>

View File

@ -1,44 +1,34 @@
<?php <?php
/** /**
* This class acts as a template for spam protecting form field, for instance MollomField. * This class acts as a template for spam protecting form field, for instance MollomField.
* It provides a number of properties for mapping fields of the form which this object belongs to
* to spam checking service fields.
*
* In order to further process the form values or take any action according the status of spam checking,
* markAsSpam() and markAsHam should be called in validate() after the status of the spam checking has
* been obtained.
* *
* @package spamprotection * @package spamprotection
*/ */
class SpamProtectorField extends FormField { abstract class SpamProtectorField extends FormField {
protected $spanControlCallbackObj = null;
/** /**
* Set the Callback Object * Fields to map spam protection too.
*
* @var array
*/ */
function setCallbackObject($callbackObject) { private $spamFieldMapping = array();
$this->spanControlCallbackObj = $callbackObject;
/**
* Set the fields to map spam protection too
*
* @param Array array of Field Names
*/
public function setFieldMapping($array) {
$this->spamFieldMapping = $array;
} }
/** /**
* Tell the callback object the submission is spam * Get the fields that are mapped via spam protection
*
* @return Array
*/ */
protected function markAsSpam() { public function getFieldMapping() {
if ($this->spanControlCallbackObj && $this->spanControlCallbackObj instanceof Spamable) { return $this->spamFieldMapping;
$this->spanControlCallbackObj->markAsSpam($this->getForm());
} }
}
/**
* Tell the callback object the submission is ham
*/
protected function markAsHam() {
if ($this->spanControlCallbackObj && $this->spanControlCallbackObj instanceof Spamable) {
$this->spanControlCallbackObj->markAsHam($this->getForm());
}
}
} }
?>

View File

@ -1,6 +1,7 @@
<?php <?php
/** /**
* This class is responsible for setting an system-wide spam protecter field * This class is responsible for setting an system-wide spam protector field
* and add the protecter field to a form. * and add the protecter field to a form.
* *
* @package spamprotection * @package spamprotection
@ -8,21 +9,26 @@
class SpamProtectorManager { class SpamProtectorManager {
static $spam_protector = null; /**
* Current Spam Protector used on the site
*
* @var SpamProtector
*/
private static $spam_protector = null;
/** /**
* Set the name of the spam protecter class * Set the name of the spam protecter class
* *
* @param String the name of protecter field class * @param String the name of protecter field class
*/ */
static function set_spam_protector($protector) { public static function set_spam_protector($protector) {
self::$spam_protector = $protector; self::$spam_protector = $protector;
} }
/** /**
* Get the name of the spam protector class * Get the name of the spam protector class
*/ */
static function get_spam_protector() { public static function get_spam_protector() {
return self::$spam_protector; return self::$spam_protector;
} }
@ -32,32 +38,39 @@ class SpamProtectorManager {
* @param string the name of the field that the protecter field will be added in front of * @param string the name of the field that the protecter field will be added in front of
* @param array an associative array * @param array an associative array
* with the name of the spam web service's field, for example post_title, post_body, author_name * with the name of the spam web service's field, for example post_title, post_body, author_name
* and a string of field names (seperated by comma) as a value. * and a string of field names
* The naming of the fields is based on the implementation of the subclass of SpamProtectorField. * @param String Title for the captcha field
* *** Most of the web service doesn't require this. * @param String RightTitle for the captcha field
* @return SpamProtector object on success or null if the spamprotector class is not found * @return SpamProtector object on success or null if the spamprotector class is not found
* also null if spamprotectorfield creation fails. * also null if spamprotectorfield creation fails.
*/ */
static function update_form($form, $before=null, $fieldsToSpamServiceMapping=null) { static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) {
$check = null; $protectorClass = self::get_spam_protector();
$protectorClass = self::$spam_protector;
if(!class_exists($protectorClass)) { if(!class_exists($protectorClass)) {
user_error("Spam Protector class '$protectorClass' does not exist. Please define a valid Spam Protector", E_USER_WARNING); return user_error("Spam Protector class '$protectorClass' does not exist. Please define a valid Spam Protector", E_USER_WARNING);
} }
if(!$protectorClass) return null;
$protector = new $protectorClass();
try { try {
$check = $protector->updateForm($form, $before, $fieldsToSpamServiceMapping); $protector = new $protectorClass();
} catch (Exception $e) { $field = $protector->getFormField("Captcha", $title, null, $form, $rightTitle);
user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING);
if($field) {
// update the mapping
$field->setFieldMapping($fieldsToSpamServiceMapping);
// add the form field
if($before && $form->Fields()->fieldByName($before)) {
$form->Fields()->insertBefore($field, $before);
}
else {
$form->Fields()->push($field);
}
} }
if(!$check) return null; } catch (Exception $e) {
return user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING);
return $protector; }
} }
/** /**
@ -73,4 +86,3 @@ class SpamProtectorManager {
return $protector->sendFeedback($object, $feedback); return $protector->sendFeedback($object, $feedback);
} }
} }
?>