2014-02-10 08:53:59 +01:00
|
|
|
<?php
|
2017-08-28 00:53:32 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\SpamProtection\Extension;
|
2016-10-10 07:44:28 +02:00
|
|
|
|
2017-08-28 03:44:53 +02:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
2016-10-10 07:44:28 +02:00
|
|
|
use SilverStripe\Core\Extension;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2014-02-10 08:53:59 +01:00
|
|
|
|
|
|
|
/**
|
2017-07-07 04:23:01 +02:00
|
|
|
* An extension to the {@link Form} class which provides the method
|
2014-02-10 08:53:59 +01:00
|
|
|
* {@link enableSpamProtection()} helper.
|
|
|
|
*
|
|
|
|
* @package spamprotection
|
|
|
|
*/
|
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
class FormSpamProtectionExtension extends Extension
|
|
|
|
{
|
2017-08-28 03:44:53 +02:00
|
|
|
use Configurable;
|
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
2014-02-10 08:53:59 +01:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*
|
2017-07-07 04:23:01 +02:00
|
|
|
* The {@link enableSpamProtection} method will define which of the form
|
2015-11-21 07:15:15 +01:00
|
|
|
* 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'
|
|
|
|
);
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2017-08-24 12:16:19 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* The field name to use for the {@link SpamProtector} {@link FormField}
|
|
|
|
*
|
|
|
|
* @var string $spam_protector
|
|
|
|
*/
|
|
|
|
private static $field_name = "Captcha";
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
/**
|
|
|
|
* Instantiate a SpamProtector instance
|
|
|
|
*
|
|
|
|
* @param array $options Configuration options
|
2017-08-28 00:53:32 +02:00
|
|
|
* @return SpamProtector|null
|
2015-11-21 07:15:15 +01:00
|
|
|
*/
|
|
|
|
public static function get_protector($options = null)
|
|
|
|
{
|
|
|
|
// generate the spam protector
|
|
|
|
if (isset($options['protector'])) {
|
|
|
|
$protector = $options['protector'];
|
|
|
|
} else {
|
2017-11-02 02:02:01 +01:00
|
|
|
$protector = self::config()->get('default_spam_protector');
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
2014-02-10 08:53:59 +01:00
|
|
|
|
2022-04-13 03:47:43 +02:00
|
|
|
if ($protector && class_exists($protector ?? '')) {
|
2015-11-21 07:15:15 +01:00
|
|
|
return Injector::inst()->create($protector);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
/**
|
|
|
|
* Activates the spam protection module.
|
|
|
|
*
|
|
|
|
* @param array $options
|
2016-10-10 07:44:28 +02:00
|
|
|
* @return Object
|
2015-11-21 07:15:15 +01:00
|
|
|
*/
|
|
|
|
public function enableSpamProtection($options = array())
|
|
|
|
{
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
// captcha form field name (must be unique)
|
|
|
|
if (isset($options['name'])) {
|
|
|
|
$name = $options['name'];
|
|
|
|
} else {
|
2017-11-02 02:02:01 +01:00
|
|
|
$name = $this->config()->get('field_name');
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
// captcha field title
|
|
|
|
if (isset($options['title'])) {
|
|
|
|
$title = $options['title'];
|
|
|
|
} else {
|
|
|
|
$title = '';
|
|
|
|
}
|
2014-02-10 08:53:59 +01:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
// set custom mapping on this form
|
|
|
|
$protector = self::get_protector($options);
|
2014-02-10 08:53:59 +01:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
if (isset($options['mapping'])) {
|
|
|
|
$protector->setFieldMapping($options['mapping']);
|
|
|
|
}
|
2014-11-11 23:24:21 +01:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
if ($protector) {
|
|
|
|
// add the form field
|
|
|
|
if ($field = $protector->getFormField($name, $title)) {
|
|
|
|
$field->setForm($this->owner);
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
// Add before field specified by insertBefore
|
|
|
|
$inserted = false;
|
|
|
|
if (!empty($options['insertBefore'])) {
|
2022-11-24 05:36:45 +01:00
|
|
|
$inserted = $this->owner->Fields()->insertBefore($options['insertBefore'], $field);
|
2015-11-21 07:15:15 +01:00
|
|
|
}
|
|
|
|
if (!$inserted) {
|
|
|
|
// Add field to end if not added already
|
|
|
|
$this->owner->Fields()->push($field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 00:53:32 +02:00
|
|
|
|
2015-11-21 07:15:15 +01:00
|
|
|
return $this->owner;
|
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
}
|