2014-02-10 08:53:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An extension to the {@link Form} class which provides the method
|
|
|
|
* {@link enableSpamProtection()} helper.
|
|
|
|
*
|
|
|
|
* @package spamprotection
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 {@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(
|
2014-02-15 11:00:42 +01:00
|
|
|
'id',
|
2014-02-10 08:53:59 +01:00
|
|
|
'title',
|
|
|
|
'body',
|
|
|
|
'contextUrl',
|
|
|
|
'contextTitle',
|
|
|
|
'authorName',
|
|
|
|
'authorMail',
|
|
|
|
'authorUrl',
|
|
|
|
'authorIp',
|
|
|
|
'authorId'
|
|
|
|
);
|
2014-02-18 04:19:29 +01:00
|
|
|
|
2014-02-10 08:53:59 +01:00
|
|
|
/**
|
2014-02-18 04:19:29 +01:00
|
|
|
* Instantiate a SpamProtector instance
|
2014-02-18 04:19:29 +01:00
|
|
|
*
|
2014-02-18 04:19:29 +01:00
|
|
|
* @param array $options Configuration options
|
|
|
|
* @return SpamProtector
|
2014-02-10 08:53:59 +01:00
|
|
|
*/
|
2014-02-18 04:19:29 +01:00
|
|
|
public static function get_protector($options = null) {
|
2014-02-10 08:53:59 +01:00
|
|
|
// generate the spam protector
|
|
|
|
if(isset($options['protector'])) {
|
|
|
|
$protector = $options['protector'];
|
|
|
|
} else {
|
|
|
|
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector');
|
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
|
|
|
|
if($protector && class_exists($protector)) {
|
2014-09-25 22:16:59 +02:00
|
|
|
return Injector::inst()->create($protector);
|
2014-02-18 04:19:29 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activates the spam protection module.
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
public function enableSpamProtection($options = array()) {
|
2014-02-10 08:53:59 +01:00
|
|
|
|
|
|
|
// 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 = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// set custom mapping on this form
|
2014-02-18 04:19:29 +01:00
|
|
|
$protector = self::get_protector($options);
|
2014-11-11 23:24:21 +01:00
|
|
|
|
2014-02-10 08:53:59 +01:00
|
|
|
if(isset($options['mapping'])) {
|
2014-02-18 04:19:29 +01:00
|
|
|
$protector->setFieldMapping($options['mapping']);
|
2014-02-10 08:53:59 +01:00
|
|
|
}
|
|
|
|
|
2014-11-11 23:24:21 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-03-07 05:00:55 +01:00
|
|
|
}
|
2014-02-10 08:53:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->owner;
|
|
|
|
}
|
2014-02-18 04:19:29 +01:00
|
|
|
}
|