2009-03-25 03:14:26 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2015-11-21 07:15:15 +01:00
|
|
|
if (class_exists('EditableFormField')) {
|
|
|
|
class EditableSpamProtectionField extends EditableFormField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'Spam Protection Field';
|
|
|
|
|
|
|
|
private static $plural_name = 'Spam Protection Fields';
|
|
|
|
/**
|
|
|
|
* Fields to include spam detection for
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $check_fields = array(
|
|
|
|
'EditableEmailField',
|
|
|
|
'EditableTextField',
|
|
|
|
'EditableNumericField'
|
|
|
|
);
|
|
|
|
|
2017-07-07 06:09:39 +02:00
|
|
|
private static $db = array(
|
|
|
|
'SpamFieldSettings' => 'Text'
|
|
|
|
);
|
|
|
|
|
2016-01-08 04:35:46 +01:00
|
|
|
/**
|
|
|
|
* @var FormField
|
|
|
|
*/
|
|
|
|
protected $formField = null;
|
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
public function getFormField()
|
|
|
|
{
|
2016-01-08 04:35:46 +01:00
|
|
|
if ($this->formField) {
|
|
|
|
return $this->formField;
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
// Get protector
|
|
|
|
$protector = FormSpamProtectionExtension::get_protector();
|
|
|
|
if (!$protector) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract saved field mappings and update this field.
|
|
|
|
$fieldMapping = array();
|
|
|
|
foreach ($this->getCandidateFields() as $otherField) {
|
|
|
|
$mapSetting = "Map-{$otherField->Name}";
|
2017-07-07 06:09:39 +02:00
|
|
|
$spamField = $this->spamMapValue($mapSetting);
|
2015-11-21 07:15:15 +01:00
|
|
|
$fieldMapping[$otherField->Name] = $spamField;
|
|
|
|
}
|
|
|
|
$protector->setFieldMapping($fieldMapping);
|
|
|
|
|
|
|
|
// Generate field
|
|
|
|
return $protector->getFormField($this->Name, $this->Title, null);
|
|
|
|
}
|
|
|
|
|
2016-01-08 04:35:46 +01:00
|
|
|
/**
|
|
|
|
* @param FormField $field
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setFormField(FormField $field)
|
|
|
|
{
|
|
|
|
$this->formField = $field;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
/**
|
|
|
|
* Gets the list of all candidate spam detectable fields on this field's form
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function getCandidateFields()
|
|
|
|
{
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all candidates of the above types
|
|
|
|
return $this
|
|
|
|
->Parent()
|
|
|
|
->Fields()
|
|
|
|
->filter('ClassName', $typesInherit)
|
|
|
|
->exclude('Title', ''); // Ignore this field and those without titles
|
|
|
|
}
|
|
|
|
|
2017-07-07 06:09:39 +02:00
|
|
|
/**
|
|
|
|
* This method is in place for userforms 2.x
|
|
|
|
*
|
|
|
|
* @deprecated 3.0 Please use {@link getCMSFields()} instead
|
|
|
|
*/
|
2015-11-21 07:15:15 +01:00
|
|
|
public function getFieldConfiguration()
|
|
|
|
{
|
2017-07-07 06:09:39 +02:00
|
|
|
return $this->getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the spam field mapping values to a serialised DB field
|
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
$fieldMap = Convert::json2array($this->SpamFieldSettings);
|
|
|
|
if (empty($fieldMap)) {
|
|
|
|
$fieldMap = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->record as $key => $value) {
|
|
|
|
if (substr($key, 0, 8) === 'spammap-') {
|
|
|
|
$fieldMap[substr($key, 8)] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->setField('SpamFieldSettings', Convert::raw2json($fieldMap));
|
|
|
|
|
|
|
|
return parent::onBeforeWrite();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
// Get protector
|
|
|
|
$protector = FormSpamProtectionExtension::get_protector();
|
|
|
|
if (!$protector) {
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->Parent()->Fields() instanceof UnsavedRelationList) {
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each other text field in this group can be assigned a field mapping
|
2017-07-07 06:09:39 +02:00
|
|
|
$mapGroup = FieldGroup::create()
|
|
|
|
->setTitle(_t('EditableSpamProtectionField.SPAMFIELDMAPPING', 'Spam Field Mapping'))
|
|
|
|
->setName('SpamFieldMapping')
|
|
|
|
->setDescription(_t(
|
|
|
|
'EditableSpamProtectionField.SPAMFIELDMAPPINGDESCRIPTION',
|
|
|
|
'Select the form fields that correspond to any relevant spam protection identifiers'
|
|
|
|
));
|
2015-11-21 07:15:15 +01:00
|
|
|
|
|
|
|
// 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(
|
2017-07-07 06:09:39 +02:00
|
|
|
'spammap-' . $mapSetting,
|
2015-11-21 07:15:15 +01:00
|
|
|
$otherField->Title,
|
|
|
|
$mappableFieldsMerged,
|
2017-07-07 06:09:39 +02:00
|
|
|
$this->spamMapValue($mapSetting)
|
2015-11-21 07:15:15 +01:00
|
|
|
)->setEmptyString('');
|
|
|
|
$mapGroup->push($fieldOption);
|
|
|
|
}
|
2017-07-07 06:09:39 +02:00
|
|
|
$fields->addFieldToTab('Root.Main', $mapGroup);
|
2015-11-21 07:15:15 +01:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2017-07-07 06:09:39 +02:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
$map = Convert::json2array($this->SpamFieldSettings);
|
|
|
|
if (empty($map)) {
|
|
|
|
$map = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($mapSetting, $map)) {
|
|
|
|
return $map[$mapSetting];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:49:09 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2017-07-07 04:23:01 +02:00
|
|
|
*
|
2015-10-21 05:49:09 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-07 04:23:01 +02:00
|
|
|
public function validateField($data, $form)
|
2015-11-21 07:15:15 +01:00
|
|
|
{
|
|
|
|
$formField = $this->getFormField();
|
2016-03-02 17:51:13 +01:00
|
|
|
$formField->setForm($form);
|
|
|
|
|
2015-10-21 05:49:09 +02:00
|
|
|
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
|
2019-07-23 15:04:14 +02:00
|
|
|
if ($errors && count($errors) > 0) {
|
2015-10-21 05:49:09 +02:00
|
|
|
// check if error already added from fields' validate method
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
if ($error['fieldName'] == $this->Name) {
|
|
|
|
$foundError = $error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($foundError !== false) {
|
|
|
|
// use error messaging already set from validate method
|
|
|
|
$form->addErrorMessage($this->Name, $foundError['message'], $foundError['messageType'], false);
|
|
|
|
} else {
|
|
|
|
// fallback to custom message set in CMS or default message if none set
|
|
|
|
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
|
|
|
|
}
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFieldValidationOptions()
|
|
|
|
{
|
|
|
|
return new FieldList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRequired()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
return 'spamprotection/images/' . strtolower($this->class) . '.png';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function showInReports()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
}
|