|
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
|
|
|
* Instantiate a SpamProtector instance |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $options Configuration options |
|
48
|
|
|
* @return SpamProtector |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function get_protector($options = null) |
|
51
|
|
|
{ |
|
52
|
|
|
// generate the spam protector |
|
53
|
|
|
if (isset($options['protector'])) { |
|
54
|
|
|
$protector = $options['protector']; |
|
55
|
|
|
} else { |
|
56
|
|
|
$protector = Config::inst()->get('FormSpamProtectionExtension', 'default_spam_protector'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($protector && class_exists($protector)) { |
|
60
|
|
|
return Injector::inst()->create($protector); |
|
61
|
|
|
} else { |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Activates the spam protection module. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $options |
|
70
|
|
|
*/ |
|
71
|
|
|
public function enableSpamProtection($options = array()) |
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// captcha form field name (must be unique) |
|
75
|
|
|
if (isset($options['name'])) { |
|
76
|
|
|
$name = $options['name']; |
|
77
|
|
|
} else if($field_name = Config::inst()->get('FormSpamProtectionExtension', 'field_name')) { |
|
78
|
|
|
$name = $field_name; |
|
79
|
|
|
} else { |
|
80
|
|
|
$name = 'Captcha'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// captcha field title |
|
84
|
|
|
if (isset($options['title'])) { |
|
85
|
|
|
$title = $options['title']; |
|
86
|
|
|
} else { |
|
87
|
|
|
$title = ''; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// set custom mapping on this form |
|
91
|
|
|
$protector = self::get_protector($options); |
|
92
|
|
|
|
|
93
|
|
|
if (isset($options['mapping'])) { |
|
94
|
|
|
$protector->setFieldMapping($options['mapping']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($protector) { |
|
98
|
|
|
// add the form field |
|
99
|
|
|
if ($field = $protector->getFormField($name, $title)) { |
|
100
|
|
|
$field->setForm($this->owner); |
|
101
|
|
|
|
|
102
|
|
|
// Add before field specified by insertBefore |
|
103
|
|
|
$inserted = false; |
|
104
|
|
|
if (!empty($options['insertBefore'])) { |
|
105
|
|
|
$inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']); |
|
106
|
|
|
} |
|
107
|
|
|
if (!$inserted) { |
|
108
|
|
|
// Add field to end if not added already |
|
109
|
|
|
$this->owner->Fields()->push($field); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->owner; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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.