1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\SpamProtection\Extension; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\Core\Extension; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An extension to the {@link Form} class which provides the method |
11
|
|
|
* {@link enableSpamProtection()} helper. |
12
|
|
|
* |
13
|
|
|
* @package spamprotection |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class FormSpamProtectionExtension extends Extension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @config |
20
|
|
|
* |
21
|
|
|
* The default spam protector class name to use. Class should implement the |
22
|
|
|
* {@link SpamProtector} interface. |
23
|
|
|
* |
24
|
|
|
* @var string $spam_protector |
25
|
|
|
*/ |
26
|
|
|
private static $default_spam_protector; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @config |
30
|
|
|
* |
31
|
|
|
* The {@link enableSpamProtection} method will define which of the form |
32
|
|
|
* values correlates to this form mapped fields list. Totally custom forms |
33
|
|
|
* and subclassed SpamProtector instances are define their own mapping |
34
|
|
|
* |
35
|
|
|
* @var array $mappable_fields |
36
|
|
|
*/ |
37
|
|
|
private static $mappable_fields = array( |
|
|
|
|
38
|
|
|
'id', |
39
|
|
|
'title', |
40
|
|
|
'body', |
41
|
|
|
'contextUrl', |
42
|
|
|
'contextTitle', |
43
|
|
|
'authorName', |
44
|
|
|
'authorMail', |
45
|
|
|
'authorUrl', |
46
|
|
|
'authorIp', |
47
|
|
|
'authorId' |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @config |
52
|
|
|
* |
53
|
|
|
* The field name to use for the {@link SpamProtector} {@link FormField} |
54
|
|
|
* |
55
|
|
|
* @var string $spam_protector |
56
|
|
|
*/ |
57
|
|
|
private static $field_name = "Captcha"; |
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Instantiate a SpamProtector instance |
61
|
|
|
* |
62
|
|
|
* @param array $options Configuration options |
63
|
|
|
* @return SpamProtector|null |
64
|
|
|
*/ |
65
|
|
|
public static function get_protector($options = null) |
66
|
|
|
{ |
67
|
|
|
// generate the spam protector |
68
|
|
|
if (isset($options['protector'])) { |
69
|
|
|
$protector = $options['protector']; |
70
|
|
|
} else { |
71
|
|
|
$protector = Config::inst()->get(self::class, 'default_spam_protector'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($protector && class_exists($protector)) { |
75
|
|
|
return Injector::inst()->create($protector); |
76
|
|
|
} else { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Activates the spam protection module. |
83
|
|
|
* |
84
|
|
|
* @param array $options |
85
|
|
|
* @return Object |
86
|
|
|
*/ |
87
|
|
|
public function enableSpamProtection($options = array()) |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
// captcha form field name (must be unique) |
91
|
|
|
if (isset($options['name'])) { |
92
|
|
|
$name = $options['name']; |
93
|
|
|
} else { |
94
|
|
|
$name = Config::inst()->get(self::class, 'field_name'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// captcha field title |
98
|
|
|
if (isset($options['title'])) { |
99
|
|
|
$title = $options['title']; |
100
|
|
|
} else { |
101
|
|
|
$title = ''; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// set custom mapping on this form |
105
|
|
|
$protector = self::get_protector($options); |
106
|
|
|
|
107
|
|
|
if (isset($options['mapping'])) { |
108
|
|
|
$protector->setFieldMapping($options['mapping']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($protector) { |
112
|
|
|
// add the form field |
113
|
|
|
if ($field = $protector->getFormField($name, $title)) { |
114
|
|
|
$field->setForm($this->owner); |
115
|
|
|
|
116
|
|
|
// Add before field specified by insertBefore |
117
|
|
|
$inserted = false; |
118
|
|
|
if (!empty($options['insertBefore'])) { |
119
|
|
|
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']); |
120
|
|
|
} |
121
|
|
|
if (!$inserted) { |
122
|
|
|
// Add field to end if not added already |
123
|
|
|
$this->owner->Fields()->push($field); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->owner; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.