1
|
|
|
<?php namespace StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField; |
2
|
|
|
|
3
|
|
|
use FormField; |
4
|
|
|
|
5
|
|
|
class HoneypotField extends \HiddenField |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The number of seconds before you can submit a valid request. |
9
|
|
|
* |
10
|
|
|
* @var int |
11
|
|
|
* @config |
12
|
|
|
*/ |
13
|
|
|
private static $time_limit = 5; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Reject the field if the honeypot has been filled or if the form has been submitted to quickly. |
17
|
|
|
* |
18
|
|
|
* @param $validator |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
6 |
|
public function validate($validator) |
23
|
|
|
{ |
24
|
6 |
|
$timeLimit = $this->config()->time_limit; |
25
|
|
|
|
26
|
6 |
|
$timestamp = $this->getForm()->getController()->getRequest()->postVar($this->getName() . '_Timestamp'); |
27
|
|
|
|
28
|
6 |
|
if (!empty($this->value) || ($timeLimit > 0 && ($timestamp + $timeLimit) > time())) { |
29
|
3 |
|
$validator->validationError( |
30
|
3 |
|
$this->name, |
31
|
3 |
|
_t( |
32
|
3 |
|
'HoneypotField.SPAM', |
33
|
|
|
'Your submission has been rejected because it was treated as spam.' |
34
|
3 |
|
), |
35
|
|
|
'error' |
36
|
3 |
|
); |
37
|
|
|
|
38
|
3 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Override the Type to remove the class namespace. |
46
|
|
|
* |
47
|
|
|
* @codeCoverageIgnore |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function Type() |
52
|
|
|
{ |
53
|
|
|
return 'honeypotspamprotector'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Override the Field to add the Captcha and Timestamp fields. |
58
|
|
|
* |
59
|
|
|
* @codeCoverageIgnore |
60
|
|
|
* |
61
|
|
|
* @param array $properties |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function Field($properties = array()) |
66
|
|
|
{ |
67
|
|
|
return $this->createHoneypotField() . $this->createTimestampField(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create the Captcha Field. |
72
|
|
|
* |
73
|
|
|
* @codeCoverageIgnore |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function createHoneypotField() |
78
|
|
|
{ |
79
|
|
|
return FormField::create_tag( |
80
|
|
|
'input', |
81
|
|
|
array( |
82
|
|
|
'type' => 'text', |
83
|
|
|
'id' => $this->ID(), |
84
|
|
|
'name' => $this->getName(), |
85
|
|
|
'value' => $this->Value(), |
86
|
|
|
'style' => 'display:none!important', |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create the Timestamp Field. |
93
|
|
|
* |
94
|
|
|
* @codeCoverageIgnore |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function createTimestampField() |
99
|
|
|
{ |
100
|
|
|
return FormField::create_tag( |
101
|
|
|
'input', |
102
|
|
|
array( |
103
|
|
|
'type' => 'text', |
104
|
|
|
'id' => $this->ID() . '_Timestamp', |
105
|
|
|
'name' => $this->getName() . '_Timestamp', |
106
|
|
|
'value' => time(), |
107
|
|
|
'style' => 'display:none!important', |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.