/
MollomField.php
286 lines (234 loc) · 6.26 KB
/
MollomField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Mollom Form Field.
*
* The {@link FormField} which is inserted into your form fields via the
* spam protector class.
*
* @package mollom
*/
class MollomField extends FormField {
/**
* @var array
*/
private $fieldMapping = array();
/**
* @config
*
* @see setAlwaysShowCaptcha
*/
private static $always_show_captcha = false;
/**
* @config
*
* @see setForceCheckOnMembers
*/
private static $force_check_on_members = false;
/**
* @var string
*/
protected $template = 'MollomField';
/**
* @return string
*/
public function getCaptchaAudioFile() {
return MOLLOM_DIR . '/mollom-captcha-player.swf';
}
/**
* Returns the captcha url if we need to get it from the user
*
* @return string
*/
public function getCaptcha() {
if($this->getShowCaptcha()) {
Requirements::css(MOLLOM_DIR . '/css/mollom.css');
$result = $this->getMollom()->createCaptcha(array(
'type' => 'image'
));
if(is_array($result)) {
Session::set('mollom_session_id', $result['id']);
return $result['url'];
}
}
return null;
}
/**
* Determines if the current user is exempt from spam detection
*
* @return boolean True if the user is exempt from spam detection
*/
protected function exemptUser() {
// Never show captcha for admins
if (Permission::check('ADMIN')) {
return true;
}
// Allow logged in members to bypass captcha if allowed
if (Member::currentUser() && !Config::inst()->get('MollomField', 'force_check_on_members')) {
return true;
}
return false;
}
/**
* Return if we should show the captcha to the user.
*
* @return boolean
*/
public function getShowCaptcha() {
// If this user is eligible to bypass spam detection, don't show them the recaptcha
if($this->exemptUser()) return false;
// Show captcha if always requested
if (Config::inst()->get('MollomField', 'always_show_captcha')) return true;
// If a captcha is requested then we need to redisplay it to the user
if (Session::get('mollom_captcha_requested')) return true;
// If there are no field mappings, then the captcha is mandatory
return empty($this->fieldMapping);
}
/**
* @return MollomSpamProtector
*/
public function getMollom() {
if(!$this->_mollom) {
$this->_mollom = Injector::inst()->create('MollomSpamProtector');
$this->_mollom ->publicKey = Config::inst()->get('Mollom', 'public_key');
$this->_mollom ->privateKey = Config::inst()->get('Mollom', 'private_key');
if(Config::inst()->get('Mollom', 'dev')) {
$this->_mollom->server = 'dev.mollom.com';
}
}
return $this->_mollom;
}
/**
* @return array
*/
public function getSpamMappedData() {
if(empty($this->fieldMapping)) return null;
$result = array();
$data = $this->form->getData();
foreach($this->fieldMapping as $fieldName => $mappedName) {
$result[$mappedName] = (isset($data[$fieldName])) ? $data[$fieldName] : null;
}
return $result;
}
/**
* Validate the captcha information
*
* @param Validator $validator
*
* @return boolean
*/
public function validate($validator) {
// Bypass spam detection for eligible users
if($this->exemptUser()) {
$this->clearMollomSession();
return true;
}
$session_id = Session::get("mollom_session_id");
$mapped = $this->getSpamMappedData();
$data = array();
// prepare submission
foreach(array('authorName', 'authorUrl', 'authorMail', 'authorIp', 'authorId') as $k) {
if(isset($mapped[$k])) {
$data[$k] = $mapped[$k];
}
}
if($session_id) {
// session ID exists so has been checked by captcha
$data['id'] = $session_id;
$data['solution'] = $this->Value();
$result = $this->getMollom()->checkCaptcha($data);
if(is_array($result)) {
if($result['solved']) {
$this->clearMollomSession();
return true;
} else {
$this->requestMollom();
$validator->validationError(
$this->name,
_t(
'MollomCaptchaField.CAPTCHAREQUESTED',
"Please answer the captcha question",
"Mollom Captcha provides words in an image, and expects a user to type them in a textfield"
),
"warning"
);
return false;
}
}
} else {
$contentMap = array(
'id' => 'id',
'title' => 'postTitle',
'body' => 'postBody'
);
foreach($contentMap as $k => $v) {
if(isset($mapped[$k])) {
$data[$v] = $mapped[$k];
}
}
$result = $this->getMollom()->checkContent($data);
// Mollom can do much more useful things.
// @todo handle profanityScore, qualityScore, sentimentScore, reason
if(is_array($result)) {
switch($result['spamClassification']) {
case 'ham':
$this->clearMollomSession();
return true;
case 'unsure':
$this->requestMollom();
// we're unsure so request the captcha.
$validator->validationError(
$this->name,
_t(
'MollomCaptchaField.CAPTCHAREQUESTED',
"Please answer the captcha question",
"Mollom Captcha provides words in an image, and expects a user to type them in a textfield"
),
"warning"
);
return false;
case 'spam':
$this->clearMollomSession();
$this->requestMollom();
$validator->validationError(
$this->name,
_t(
'MollomCaptchaField.SPAM',
"Your submission has been rejected because it was treated as spam.",
"Mollom Captcha provides words in an image, and expects a user to type them in a textfield"
),
"error"
);
return false;
break;
}
}
}
return true;
}
/**
* @return void
*/
private function requestMollom() {
Session::set('mollom_captcha_requested', true);
}
/**
* Helper to quickly clear all the mollom session settings. For example
* after a successful post.
*
* @return void
*/
private function clearMollomSession() {
Session::clear('mollom_session_id');
Session::clear('mollom_captcha_requested');
}
/**
* Set the fields to map spam protection too
*
* @param array $fieldMapping array of Field Names, where the indexes of the array are
* the field names of the form and the values are the standard spamprotection
* fields used by the protector
*/
public function setFieldMapping($fieldMapping) {
$this->fieldMapping = $fieldMapping;
}
}