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