2009-03-25 03:14:26 +01:00
|
|
|
<?php
|
2017-08-28 00:53:32 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\SpamProtection;
|
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Convert;
|
2017-11-02 02:02:01 +01:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
2017-08-28 00:53:32 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldGroup;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\Forms\FormField;
|
2021-08-12 03:35:02 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2017-08-28 00:53:32 +02:00
|
|
|
use SilverStripe\ORM\UnsavedRelationList;
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableNumericField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2017-08-28 07:41:04 +02:00
|
|
|
if (!class_exists(EditableFormField::class)) {
|
2017-08-28 03:44:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-03-25 03:14:26 +01:00
|
|
|
|
|
|
|
/**
|
2015-09-21 04:14:49 +02:00
|
|
|
* Editable Spam Protecter Field. Used with the User Defined Forms module (if
|
2009-03-25 03:14:26 +01:00
|
|
|
* installed) to allow the user to have captcha fields with their custom forms
|
2015-09-21 04:14:49 +02:00
|
|
|
*
|
2009-09-17 06:06:53 +02:00
|
|
|
* @package spamprotection
|
2009-03-25 03:14:26 +01:00
|
|
|
*/
|
2017-08-28 03:44:53 +02:00
|
|
|
class EditableSpamProtectionField extends EditableFormField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'Spam Protection Field';
|
|
|
|
|
|
|
|
private static $plural_name = 'Spam Protection Fields';
|
|
|
|
|
|
|
|
private static $table_name = 'EditableSpamProtectionField';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fields to include spam detection for
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $check_fields = array(
|
|
|
|
EditableEmailField::class,
|
|
|
|
EditableTextField::class,
|
|
|
|
EditableNumericField::class
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'SpamFieldSettings' => 'Text'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FormField
|
|
|
|
*/
|
|
|
|
protected $formField = null;
|
|
|
|
|
|
|
|
public function getFormField()
|
2015-11-21 07:15:15 +01:00
|
|
|
{
|
2017-08-28 03:44:53 +02:00
|
|
|
if ($this->formField) {
|
|
|
|
return $this->formField;
|
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
// Get protector
|
|
|
|
$protector = FormSpamProtectionExtension::get_protector();
|
|
|
|
if (!$protector) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
// Extract saved field mappings and update this field.
|
|
|
|
$fieldMapping = array();
|
|
|
|
foreach ($this->getCandidateFields() as $otherField) {
|
|
|
|
$mapSetting = "Map-{$otherField->Name}";
|
|
|
|
$spamField = $this->spamMapValue($mapSetting);
|
|
|
|
$fieldMapping[$otherField->Name] = $spamField;
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
2017-08-28 03:44:53 +02:00
|
|
|
$protector->setFieldMapping($fieldMapping);
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
// Generate field
|
2020-11-25 06:22:44 +01:00
|
|
|
$field = $protector->getFormField($this->Name, $this->Title, null);
|
|
|
|
|
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
|
|
|
|
return $field;
|
2017-08-28 03:44:53 +02:00
|
|
|
}
|
2016-01-08 04:35:46 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
/**
|
|
|
|
* @param FormField $field
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setFormField(FormField $field)
|
|
|
|
{
|
|
|
|
$this->formField = $field;
|
2016-01-08 04:35:46 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
/**
|
|
|
|
* Gets the list of all candidate spam detectable fields on this field's form
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function getCandidateFields()
|
|
|
|
{
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
// Get list of all configured classes available for spam detection
|
|
|
|
$types = $this->config()->get('check_fields');
|
|
|
|
$typesInherit = array();
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$subTypes = ClassInfo::subclassesFor($type);
|
|
|
|
$typesInherit = array_merge($typesInherit, $subTypes);
|
2017-07-07 06:09:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
// Get all candidates of the above types
|
2021-08-12 03:35:02 +02:00
|
|
|
$parent = $this->Parent();
|
|
|
|
if (!$parent) {
|
|
|
|
return DataList::create(EditableFormField::class);
|
|
|
|
}
|
|
|
|
return $parent
|
2017-08-28 03:44:53 +02:00
|
|
|
->Fields()
|
|
|
|
->filter('ClassName', $typesInherit)
|
|
|
|
->exclude('Title', ''); // Ignore this field and those without titles
|
|
|
|
}
|
2017-07-07 06:09:39 +02:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
/**
|
|
|
|
* Write the spam field mapping values to a serialised DB field
|
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
2022-04-13 03:47:43 +02:00
|
|
|
$fieldMap = json_decode($this->SpamFieldSettings ?? '', true);
|
2017-08-28 03:44:53 +02:00
|
|
|
if (empty($fieldMap)) {
|
|
|
|
$fieldMap = array();
|
2017-07-07 06:09:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
foreach ($this->record as $key => $value) {
|
2022-04-13 03:47:43 +02:00
|
|
|
if (substr($key ?? '', 0, 8) === 'spammap-') {
|
2017-08-28 03:44:53 +02:00
|
|
|
$fieldMap[substr($key, 8)] = $value;
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
2017-08-28 03:44:53 +02:00
|
|
|
}
|
2018-10-28 22:40:35 +01:00
|
|
|
$this->setField('SpamFieldSettings', json_encode($fieldMap));
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
return parent::onBeforeWrite();
|
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
/**
|
|
|
|
* Used in userforms 3.x and above
|
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
/** @var FieldList $fields */
|
|
|
|
$fields = parent::getCMSFields();
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
// Get protector
|
|
|
|
$protector = FormSpamProtectionExtension::get_protector();
|
|
|
|
if (!$protector) {
|
2015-11-21 07:15:15 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
|
|
|
|
return $fields;
|
|
|
|
}
|
2017-07-07 06:09:39 +02:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
// Each other text field in this group can be assigned a field mapping
|
|
|
|
$mapGroup = FieldGroup::create()
|
|
|
|
->setTitle(_t(__CLASS__.'.SPAMFIELDMAPPING', 'Spam Field Mapping'))
|
|
|
|
->setName('SpamFieldMapping')
|
|
|
|
->setDescription(_t(
|
|
|
|
__CLASS__.'.SPAMFIELDMAPPINGDESCRIPTION',
|
|
|
|
'Select the form fields that correspond to any relevant spam protection identifiers'
|
|
|
|
));
|
|
|
|
|
|
|
|
// Generate field specific settings
|
|
|
|
$mappableFields = FormSpamProtectionExtension::config()->get('mappable_fields');
|
2022-04-13 03:47:43 +02:00
|
|
|
$mappableFieldsMerged = array_combine($mappableFields ?? [], $mappableFields ?? []);
|
2017-08-28 03:44:53 +02:00
|
|
|
foreach ($this->getCandidateFields() as $otherField) {
|
|
|
|
$mapSetting = "Map-{$otherField->Name}";
|
|
|
|
$fieldOption = DropdownField::create(
|
|
|
|
'spammap-' . $mapSetting,
|
|
|
|
$otherField->Title,
|
|
|
|
$mappableFieldsMerged,
|
|
|
|
$this->spamMapValue($mapSetting)
|
|
|
|
)->setEmptyString('');
|
|
|
|
$mapGroup->push($fieldOption);
|
2017-07-07 06:09:39 +02:00
|
|
|
}
|
2017-08-28 03:44:53 +02:00
|
|
|
$fields->addFieldToTab('Root.Main', $mapGroup);
|
2017-07-07 06:09:39 +02:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to retrieve a value for the given spam field map name from the serialised data
|
|
|
|
*
|
|
|
|
* @param string $mapSetting
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function spamMapValue($mapSetting)
|
|
|
|
{
|
2022-04-13 03:47:43 +02:00
|
|
|
$map = json_decode($this->SpamFieldSettings ?? '', true);
|
2017-08-28 03:44:53 +02:00
|
|
|
if (empty($map)) {
|
|
|
|
$map = array();
|
|
|
|
}
|
2015-10-21 05:49:09 +02:00
|
|
|
|
2022-04-13 03:47:43 +02:00
|
|
|
if (array_key_exists($mapSetting, $map ?? [])) {
|
2017-08-28 03:44:53 +02:00
|
|
|
return $map[$mapSetting];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Using custom validateField method
|
|
|
|
* as Spam Protection Field implementations may have their own error messages
|
|
|
|
* and may not be based on the field being required, e.g. Honeypot Field
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function validateField($data, $form)
|
|
|
|
{
|
|
|
|
$formField = $this->getFormField();
|
|
|
|
$formField->setForm($form);
|
|
|
|
|
|
|
|
if (isset($data[$this->Name])) {
|
|
|
|
$formField->setValue($data[$this->Name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = $form->getValidator();
|
|
|
|
if (!$formField->validate($validator)) {
|
|
|
|
$errors = $validator->getErrors();
|
|
|
|
$foundError = false;
|
|
|
|
|
|
|
|
// field validate implementation may not add error to validator
|
2022-04-13 03:47:43 +02:00
|
|
|
if (count($errors ?? []) > 0) {
|
2017-08-28 03:44:53 +02:00
|
|
|
// check if error already added from fields' validate method
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
if ($error['fieldName'] == $this->Name) {
|
|
|
|
$foundError = $error;
|
|
|
|
break;
|
2015-10-21 05:49:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-28 03:44:53 +02:00
|
|
|
}
|
2015-10-21 05:49:09 +02:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
if ($foundError !== false) {
|
|
|
|
// use error messaging already set from validate method
|
|
|
|
$form->sessionMessage($foundError['message'], $foundError['messageType']);
|
|
|
|
} else {
|
|
|
|
// fallback to custom message set in CMS or default message if none set
|
|
|
|
$form->sessionError($this->getErrorMessage()->HTML());
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-28 03:44:53 +02:00
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
public function getFieldValidationOptions()
|
|
|
|
{
|
|
|
|
return FieldList::create();
|
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
public function getRequired()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
public function getIcon()
|
|
|
|
{
|
2017-11-02 02:02:01 +01:00
|
|
|
$resource = ModuleLoader::getModule('silverstripe/spamprotection')
|
2017-11-02 23:07:41 +01:00
|
|
|
->getResource('images/editablespamprotectionfield.png');
|
2017-11-02 02:02:01 +01:00
|
|
|
|
2017-11-02 23:07:41 +01:00
|
|
|
if (!$resource->exists()) {
|
|
|
|
return '';
|
2017-11-02 02:02:01 +01:00
|
|
|
}
|
2017-11-02 23:07:41 +01:00
|
|
|
|
|
|
|
return $resource->getURL();
|
2017-08-28 03:44:53 +02:00
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
public function showInReports()
|
|
|
|
{
|
|
|
|
return false;
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
}
|