Completed
Pull Request — master (#13)
by Nathan
16:09 queued 01:17
created

HoneypotField::Title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField;
2
3
use SilverStripe\Core\Config\Config;
4
use SilverStripe\Forms\TextField;
5
use SilverStripe\View\HTML;
6
use SilverStripe\ORM\FieldType\DBHTMLText;
7
8
class HoneypotField extends TextField
9
{
10
    /**
11
     * The number of seconds before you can submit a valid request.
12
     *
13
     * @var int
14
     * @config
15
     */
16
    private static $time_limit = 5;
0 ignored issues
show
Unused Code introduced by
The property $time_limit is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    /**
19
     * Reject the field if the honeypot has been filled or if the form has been submitted to quickly.
20
     *
21
     * @param $validator
22 6
     *
23
     * @return bool
24 6
     */
25
    public function validate($validator)
26 6
    {
27
        $timeLimit = $this->config()->time_limit;
28 6
29 3
        $timestamp = $this->getForm()->getController()->getRequest()->postVar($this->getName() . '_Timestamp');
30 3
31 3
        if (!empty($this->value) || ($timeLimit > 0 && ($timestamp + $timeLimit) > time())) {
32 3
            $validator->validationError(
33
                $this->name,
34 3
                _t(
35
                    'HoneypotField.SPAM',
36 3
                    'Your submission has been rejected because it was treated as spam.'
37
                ),
38 3
                'error'
39
            );
40
41 3
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * Since this isn't a hidden field, the title will continue to show in the form.
49
     * This prevents that from happening, since a hidden field will not show the validation message.
50
     *
51
     * @codeCoverageIgnore
52
     *
53
     * @return string
54
     */
55
    public function Title()
56
    {
57
        return '';
58
    }
59
60
    /**
61
     * Override the Type to remove the class namespace.
62
     *
63
     * @codeCoverageIgnore
64
     *
65
     * @return string
66
     */
67
    public function Type()
68
    {
69
        return 'honeypotspamprotector';
70
    }
71
72
    /**
73
     * Override the Field to add the Captcha and Timestamp fields.
74
     *
75
     * @codeCoverageIgnore
76
     *
77
     * @param array $properties
78
     *
79
     * @return string
80
     */
81
    public function Field($properties = array())
82
    {
83
        $field = DBHTMLText::create($this->getName());
84
        $field->setValue($this->createHoneypotField() . $this->createTimestampField());
85
        return $field;
86
    }
87
88
    /**
89
     * Create the Captcha Field.
90
     *
91
     * @codeCoverageIgnore
92
     *
93
     * @return string
94
     */
95
    protected function createHoneypotField()
96
    {
97
        return HTML::createTag(
98
            'input',
99
            array(
100
                'type'  => 'text',
101
                'id'    => $this->ID(),
102
                'name'  => $this->getName(),
103
                'value' => $this->Value(),
104
                'style' => $this->getFieldStyle(),
105
            )
106
        );
107
    }
108
109
    /**
110
     * Create the Timestamp Field.
111
     *
112
     * @codeCoverageIgnore
113
     *
114
     * @return string
115
     */
116
    protected function createTimestampField()
117
    {
118
        return HTML::createTag(
119
            'input',
120
            array(
121
                'type'  => 'text',
122
                'id'    => $this->ID() . '_Timestamp',
123
                'name'  => $this->getName() . '_Timestamp',
124
                'value' => time(),
125
                'style' => $this->getFieldStyle(),
126
            )
127
        );
128
    }
129
    
130
    /**
131
     * Return a configured style rule for the fields, if none is configured use a default display:none rule
132
     *
133
     * @codeCoverageIgnore
134
     *
135
     * @return string
136
     */
137
    public function getFieldStyle()
138
    {
139
        $default_css_rule = 'display:none!important';
140
        $css_rule = Config::inst()->get(__CLASS__, 'field_style_rule');
141
        if (!$css_rule) {
142
            return $default_css_rule;
143
        } else {
144
            return $css_rule;
145
        }
146
    }
147
}
148