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

View File

@ -2,18 +2,17 @@
/**
* 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
* odd missing function errors
* to ensure that they contain all the correct methods.
*
* @package SpamProtection
* @package spamprotection
*/
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.
@ -21,14 +20,4 @@ interface SpamProtector {
*/
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
/**
* 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
*/
class SpamProtectorField extends FormField {
protected $spanControlCallbackObj = null;
abstract class SpamProtectorField extends FormField {
/**
* Set the Callback Object
* Fields to map spam protection too.
*
* @var array
*/
function setCallbackObject($callbackObject) {
$this->spanControlCallbackObj = $callbackObject;
}
/**
* Tell the callback object the submission is spam
*/
protected function markAsSpam() {
if ($this->spanControlCallbackObj && $this->spanControlCallbackObj instanceof Spamable) {
$this->spanControlCallbackObj->markAsSpam($this->getForm());
}
}
private $spamFieldMapping = array();
/**
* Tell the callback object the submission is ham
* Set the fields to map spam protection too
*
* @param Array array of Field Names
*/
protected function markAsHam() {
if ($this->spanControlCallbackObj && $this->spanControlCallbackObj instanceof Spamable) {
$this->spanControlCallbackObj->markAsHam($this->getForm());
}
public function setFieldMapping($array) {
$this->spamFieldMapping = $array;
}
}
?>
/**
* Get the fields that are mapped via spam protection
*
* @return Array
*/
public function getFieldMapping() {
return $this->spamFieldMapping;
}
}

View File

@ -1,6 +1,7 @@
<?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.
*
* @package spamprotection
@ -8,21 +9,26 @@
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
*
* @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;
}
/**
* Get the name of the spam protector class
*/
static function get_spam_protector() {
public static function get_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 array an associative array
* 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.
* The naming of the fields is based on the implementation of the subclass of SpamProtectorField.
* *** Most of the web service doesn't require this.
* and a string of field names
* @param String Title for the captcha field
* @param String RightTitle for the captcha field
* @return SpamProtector object on success or null if the spamprotector class is not found
* also null if spamprotectorfield creation fails.
*/
static function update_form($form, $before=null, $fieldsToSpamServiceMapping=null) {
$check = null;
$protectorClass = self::$spam_protector;
static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) {
$protectorClass = self::get_spam_protector();
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 {
$check = $protector->updateForm($form, $before, $fieldsToSpamServiceMapping);
$protector = new $protectorClass();
$field = $protector->getFormField("Captcha", $title, null, $form, $rightTitle);
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);
}
}
} catch (Exception $e) {
user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING);
return user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING);
}
if(!$check) return null;
return $protector;
}
/**
@ -72,5 +85,4 @@ class SpamProtectorManager {
$protector = new self::$spam_protector();
return $protector->sendFeedback($object, $feedback);
}
}
?>
}