Converted to PSR-2

This commit is contained in:
helpfulrobot 2015-11-21 19:15:15 +13:00
parent 8e4f03a409
commit 7ee2dcea0f
6 changed files with 393 additions and 367 deletions

View File

@ -6,125 +6,136 @@
*
* @package spamprotection
*/
if(class_exists('EditableFormField')) {
if (class_exists('EditableFormField')) {
class EditableSpamProtectionField extends EditableFormField
{
private static $singular_name = 'Spam Protection Field';
class EditableSpamProtectionField extends EditableFormField {
private static $plural_name = 'Spam Protection Fields';
/**
* Fields to include spam detection for
*
* @var array
* @config
*/
private static $check_fields = array(
'EditableEmailField',
'EditableTextField',
'EditableNumericField'
);
private static $singular_name = 'Spam Protection Field';
public function getFormField()
{
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if (!$protector) {
return false;
}
private static $plural_name = 'Spam Protection Fields';
/**
* Fields to include spam detection for
*
* @var array
* @config
*/
private static $check_fields = array(
'EditableEmailField',
'EditableTextField',
'EditableNumericField'
);
// Extract saved field mappings and update this field.
$fieldMapping = array();
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$spamField = $this->getSetting($mapSetting);
$fieldMapping[$otherField->Name] = $spamField;
}
$protector->setFieldMapping($fieldMapping);
public function getFormField() {
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if(!$protector) return false;
// Generate field
return $protector->getFormField($this->Name, $this->Title, null);
}
// Extract saved field mappings and update this field.
$fieldMapping = array();
foreach($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$spamField = $this->getSetting($mapSetting);
$fieldMapping[$otherField->Name] = $spamField;
}
$protector->setFieldMapping($fieldMapping);
/**
* Gets the list of all candidate spam detectable fields on this field's form
*
* @return DataList
*/
protected function getCandidateFields()
{
// Generate field
return $protector->getFormField($this->Name, $this->Title, null);
}
// Get list of all configured classes available for spam detection
$types = self::config()->check_fields;
$typesInherit = array();
foreach ($types as $type) {
$subTypes = ClassInfo::subclassesFor($type);
$typesInherit = array_merge($typesInherit, $subTypes);
}
/**
* Gets the list of all candidate spam detectable fields on this field's form
*
* @return DataList
*/
protected function getCandidateFields() {
// Get all candidates of the above types
return $this
->Parent()
->Fields()
->filter('ClassName', $typesInherit)
->exclude('Title', ''); // Ignore this field and those without titles
}
// Get list of all configured classes available for spam detection
$types = self::config()->check_fields;
$typesInherit = array();
foreach ($types as $type) {
$subTypes = ClassInfo::subclassesFor($type);
$typesInherit = array_merge($typesInherit, $subTypes);
}
public function getFieldConfiguration()
{
$fields = parent::getFieldConfiguration();
// Get all candidates of the above types
return $this
->Parent()
->Fields()
->filter('ClassName', $typesInherit)
->exclude('Title', ''); // Ignore this field and those without titles
}
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if (!$protector) {
return $fields;
}
public function getFieldConfiguration() {
$fields = parent::getFieldConfiguration();
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
return $fields;
}
// Get protector
$protector = FormSpamProtectionExtension::get_protector();
if (!$protector) return $fields;
// Each other text field in this group can be assigned a field mapping
$mapGroup = FieldGroup::create(_t(
'EditableSpamProtectionField.SPAMFIELDMAPPING',
'Spam Field Mapping'
))->setDescription(_t(
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
'Select the form fields that correspond to any relevant spam protection identifiers'
));
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
return $fields;
}
// Generate field specific settings
$mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields');
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$fieldOption = DropdownField::create(
$this->getSettingName($mapSetting),
$otherField->Title,
$mappableFieldsMerged,
$this->getSetting($mapSetting)
)->setEmptyString('');
$mapGroup->push($fieldOption);
}
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
// Each other text field in this group can be assigned a field mapping
$mapGroup = FieldGroup::create(_t(
'EditableSpamProtectionField.SPAMFIELDMAPPING',
'Spam Field Mapping'
))->setDescription(_t(
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
'Select the form fields that correspond to any relevant spam protection identifiers'
));
return $fields;
}
// Generate field specific settings
$mappableFields = Config::inst()->get('FormSpamProtectionExtension', 'mappable_fields');
$mappableFieldsMerged = array_combine($mappableFields, $mappableFields);
foreach ($this->getCandidateFields() as $otherField) {
$mapSetting = "Map-{$otherField->Name}";
$fieldOption = DropdownField::create(
$this->getSettingName($mapSetting),
$otherField->Title,
$mappableFieldsMerged,
$this->getSetting($mapSetting)
)->setEmptyString('');
$mapGroup->push($fieldOption);
}
$fields->insertBefore($mapGroup, $this->getSettingName('ExtraClass'));
public function validateField($data, $form)
{
$formField = $this->getFormField();
if (!$formField->validate($form->getValidator())) {
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
}
}
return $fields;
}
public function getFieldValidationOptions()
{
return new FieldList();
}
public function validateField($data, $form) {
$formField = $this->getFormField();
if (!$formField->validate($form->getValidator())) {
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
}
}
public function getRequired()
{
return false;
}
public function getFieldValidationOptions() {
return new FieldList();
}
public function getIcon()
{
return 'spamprotection/images/' . strtolower($this->class) . '.png';
}
public function getRequired() {
return false;
}
public function getIcon() {
return 'spamprotection/images/' . strtolower($this->class) . '.png';
}
public function showInReports() {
return false;
}
}
public function showInReports()
{
return false;
}
}
}

View File

@ -6,33 +6,36 @@
* @deprecated 1.0
*/
class SpamProtectorManager {
private static $spam_protector = null;
class SpamProtectorManager
{
private static $spam_protector = null;
public static function set_spam_protector($protector) {
Deprecation::notice('1.1',
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
public static function set_spam_protector($protector)
{
Deprecation::notice('1.1',
'SpamProtectorManager::set_spam_protector() is deprecated. '.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'
);
self::$spam_protector = $protector;
}
self::$spam_protector = $protector;
}
public static function get_spam_protector() {
Deprecation::notice('1.1',
'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');
public static function get_spam_protector()
{
Deprecation::notice('1.1',
'SpamProtectorManager::get_spam_protector() is deprecated'.
'Use the new config system. FormSpamProtectorExtension.default_spam_protector');
return self::$spam_protector;
}
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) {
Deprecation::notice('1.1',
'SpamProtectorManager::update_form is deprecated'.
'Please use $form->enableSpamProtection() for adding spamprotection'
);
return self::$spam_protector;
}
public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null)
{
Deprecation::notice('1.1',
'SpamProtectorManager::update_form is deprecated'.
'Please use $form->enableSpamProtection() for adding spamprotection'
);
return $form->enableSpamProtection();
}
}
return $form->enableSpamProtection();
}
}

View File

@ -6,22 +6,23 @@
* @package spamprotection
*/
class CommentSpamProtection extends Extension {
public function alterCommentForm(&$form) {
$form->enableSpamProtection(array(
'name' => 'IsSpam',
'mapping' => array(
'Name' => 'authorName',
'Email' => 'authorEmail',
'URL' => 'authorUrl',
'Comment' => 'body',
'ReturnURL' => 'contextUrl'
),
'checks' => array(
'spam',
'profanity'
)
));
}
class CommentSpamProtection extends Extension
{
public function alterCommentForm(&$form)
{
$form->enableSpamProtection(array(
'name' => 'IsSpam',
'mapping' => array(
'Name' => 'authorName',
'Email' => 'authorEmail',
'URL' => 'authorUrl',
'Comment' => 'body',
'ReturnURL' => 'contextUrl'
),
'checks' => array(
'spam',
'profanity'
)
));
}
}

View File

@ -7,106 +7,108 @@
* @package spamprotection
*/
class FormSpamProtectionExtension extends Extension {
class FormSpamProtectionExtension extends Extension
{
/**
* @config
*
* The default spam protector class name to use. Class should implement the
* {@link SpamProtector} interface.
*
* @var string $spam_protector
*/
private static $default_spam_protector;
/**
* @config
*
* The default spam protector class name to use. Class should implement the
* {@link SpamProtector} interface.
*
* @var string $spam_protector
*/
private static $default_spam_protector;
/**
* @config
*
* The {@link enableSpamProtection} method will define which of the form
* values correlates to this form mapped fields list. Totally custom forms
* and subclassed SpamProtector instances are define their own mapping
*
* @var array $mappable_fields
*/
private static $mappable_fields = array(
'id',
'title',
'body',
'contextUrl',
'contextTitle',
'authorName',
'authorMail',
'authorUrl',
'authorIp',
'authorId'
);
/**
* Instantiate a SpamProtector instance
*
* @param array $options Configuration options
* @return SpamProtector
*/
public static function get_protector($options = null)
{
// generate the spam protector
if (isset($options['protector'])) {
$protector = $options['protector'];
} else {
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
}
/**
* @config
*
* The {@link enableSpamProtection} method will define which of the form
* values correlates to this form mapped fields list. Totally custom forms
* and subclassed SpamProtector instances are define their own mapping
*
* @var array $mappable_fields
*/
private static $mappable_fields = array(
'id',
'title',
'body',
'contextUrl',
'contextTitle',
'authorName',
'authorMail',
'authorUrl',
'authorIp',
'authorId'
);
/**
* Instantiate a SpamProtector instance
*
* @param array $options Configuration options
* @return SpamProtector
*/
public static function get_protector($options = null) {
// generate the spam protector
if(isset($options['protector'])) {
$protector = $options['protector'];
} else {
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
}
if ($protector && class_exists($protector)) {
return Injector::inst()->create($protector);
} else {
return null;
}
}
if($protector && class_exists($protector)) {
return Injector::inst()->create($protector);
} else {
return null;
}
}
/**
* Activates the spam protection module.
*
* @param array $options
*/
public function enableSpamProtection($options = array())
{
// captcha form field name (must be unique)
if (isset($options['name'])) {
$name = $options['name'];
} else {
$name = 'Captcha';
}
/**
* Activates the spam protection module.
*
* @param array $options
*/
public function enableSpamProtection($options = array()) {
// captcha form field name (must be unique)
if(isset($options['name'])) {
$name = $options['name'];
} else {
$name = 'Captcha';
}
// captcha field title
if (isset($options['title'])) {
$title = $options['title'];
} else {
$title = '';
}
// captcha field title
if(isset($options['title'])) {
$title = $options['title'];
} else {
$title = '';
}
// set custom mapping on this form
$protector = self::get_protector($options);
// set custom mapping on this form
$protector = self::get_protector($options);
if (isset($options['mapping'])) {
$protector->setFieldMapping($options['mapping']);
}
if(isset($options['mapping'])) {
$protector->setFieldMapping($options['mapping']);
}
if($protector) {
// add the form field
if($field = $protector->getFormField($name, $title)) {
$field->setForm($this->owner);
// Add before field specified by insertBefore
$inserted = false;
if(!empty($options['insertBefore'])) {
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']);
}
if(!$inserted) {
// Add field to end if not added already
$this->owner->Fields()->push($field);
}
}
}
return $this->owner;
}
if ($protector) {
// add the form field
if ($field = $protector->getFormField($name, $title)) {
$field->setForm($this->owner);
// Add before field specified by insertBefore
$inserted = false;
if (!empty($options['insertBefore'])) {
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']);
}
if (!$inserted) {
// Add field to end if not added already
$this->owner->Fields()->push($field);
}
}
}
return $this->owner;
}
}

View File

@ -12,29 +12,29 @@
* @package spamprotection
*/
interface SpamProtector {
/**
* Return the {@link FormField} associated with this protector.
*
* Most spam methods will simply return a piece of HTML to be injected at
* the end of the form. If a spam method needs to inject more than one
* form field (i.e a hidden field and a text field) then return a
* {@link FieldGroup} from this method to include both.
*
* @param string $name
* @param string $title
* @param mixed $value
* @return FormField The resulting field
*/
public function getFormField($name = null, $title = null, $value = null);
interface SpamProtector
{
/**
* Return the {@link FormField} associated with this protector.
*
* Most spam methods will simply return a piece of HTML to be injected at
* the end of the form. If a spam method needs to inject more than one
* form field (i.e a hidden field and a text field) then return a
* {@link FieldGroup} from this method to include both.
*
* @param string $name
* @param string $title
* @param mixed $value
* @return FormField The resulting field
*/
public function getFormField($name = null, $title = null, $value = null);
/**
* 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);
/**
* 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);
}

View File

@ -3,133 +3,142 @@
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest extends SapphireTest {
protected $usesDatabase = false;
class FormSpamProtectionExtensionTest extends SapphireTest
{
protected $usesDatabase = false;
/**
* @var Form
*/
protected $form = null;
public function setUp() {
parent::setUp();
/**
* @var Form
*/
protected $form = null;
public function setUp()
{
parent::setUp();
$this->form = new Form($this, 'Form', new FieldList(
new TextField('Title'),
new TextField('Comment'),
new TextField('URL')
), new FieldList()
);
$this->form->disableSecurityToken();
}
$this->form = new Form($this, 'Form', new FieldList(
new TextField('Title'),
new TextField('Comment'),
new TextField('URL')
), new FieldList()
);
$this->form->disableSecurityToken();
}
public function testEnableSpamProtection() {
Config::inst()->update(
'FormSpamProtectionExtension', 'default_spam_protector',
'FormSpamProtectionExtensionTest_FooProtector'
);
public function testEnableSpamProtection()
{
Config::inst()->update(
'FormSpamProtectionExtension', 'default_spam_protector',
'FormSpamProtectionExtensionTest_FooProtector'
);
$form = $this->form->enableSpamProtection();
$form = $this->form->enableSpamProtection();
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
}
}
public function testEnableSpamProtectionCustomProtector()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector'
));
public function testEnableSpamProtectionCustomProtector() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector'
));
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
}
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testEnableSpamProtectionCustomTitle()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
'title' => 'Baz',
));
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testEnableSpamProtectionCustomTitle() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BarProtector',
'title' => 'Baz',
));
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
}
public function testCustomOptions()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
'title' => 'Qux',
'name' => 'Borris'
));
public function testCustomOptions() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_BazProtector',
'title' => 'Qux',
'name' => 'Borris'
));
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
}
public function testInsertBefore() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'URL'
));
$fields = $form->Fields();
$this->assertEquals('Title', $fields[0]->Title());
$this->assertEquals('Comment', $fields[1]->Title());
$this->assertEquals('Foo', $fields[2]->Title());
$this->assertEquals('URL', $fields[3]->Title());
}
public function testInsertBeforeMissing() {
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'NotAField'
));
// field should default to the end instead
$fields = $form->Fields();
$this->assertEquals('Title', $fields[0]->Title());
$this->assertEquals('Comment', $fields[1]->Title());
$this->assertEquals('URL', $fields[2]->Title());
$this->assertEquals('Foo', $fields[3]->Title());
}
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
}
public function testInsertBefore()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'URL'
));
$fields = $form->Fields();
$this->assertEquals('Title', $fields[0]->Title());
$this->assertEquals('Comment', $fields[1]->Title());
$this->assertEquals('Foo', $fields[2]->Title());
$this->assertEquals('URL', $fields[3]->Title());
}
public function testInsertBeforeMissing()
{
$form = $this->form->enableSpamProtection(array(
'protector' => 'FormSpamProtectionExtensionTest_FooProtector',
'insertBefore' => 'NotAField'
));
// field should default to the end instead
$fields = $form->Fields();
$this->assertEquals('Title', $fields[0]->Title());
$this->assertEquals('Comment', $fields[1]->Title());
$this->assertEquals('URL', $fields[2]->Title());
$this->assertEquals('Foo', $fields[3]->Title());
}
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {}
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping)
{
}
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly {
public function getFormField($name = null, $title = null, $value = null) {
$title = $title ?: 'Bar';
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping) {}
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
$title = $title ?: 'Bar';
return new TextField($name, $title, $value);
}
public function setFieldMapping($fieldMapping)
{
}
}
/**
* @package spamprotection
*/
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly {
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly
{
public function getFormField($name = null, $title = null, $value = null)
{
return new TextField($name, 'Foo', $value);
}
public function getFormField($name = null, $title = null, $value = null) {
return new TextField($name, 'Foo', $value);
}
public function setFieldMapping($fieldMapping) {}
}
public function setFieldMapping($fieldMapping)
{
}
}