-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha_questions.module
98 lines (88 loc) · 3.97 KB
/
captcha_questions.module
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
<?php
/**
* @file
* Module file for the Captcha questions module.
*
* The module hooks into forms and adds a configurable question field and a
* custom validate function returning an error if the answer is wrong.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\HttpFoundation;
/**
* Implements hook_help().
*/
function captcha_questions_help($route_name, RouteMatchInterface $route_match) {
if ($route_name == 'help.page.captcha_questions') {
return t('Captcha questions allows the site administrator to add a question to selected forms');
}
}
/**
* Implements hook_form_alter().
*/
function captcha_questions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only show captcha questions if user is anonymous.
if (\Drupal::currentUser()->isAnonymous()) {
// Fetching variables for form ids, question and description.
$captcha_questions_form_ids = \Drupal::config('captcha_questions.settings')->get('captcha_questions_form_ids');
$question = \Drupal::config('captcha_questions.settings')->get('captcha_questions_question');
$description = \Drupal::config('captcha_questions.settings')->get('captcha_questions_description');
foreach ($captcha_questions_form_ids as $key => $value) {
if ($value === $form_id) {
// If we're on a multi page form, find current page.
$form_page_count = isset($form['details']['page_num']['#value']) ? $form['details']['page_num']['#value'] : 1;
// Set field to hidden unless on the first page.
$form['captcha_questions_answer_given'] = array(
'#type' => $form_page_count == 1 ? 'textfield' : 'hidden',
'#description' => t("@description", array('@description' => $description)),
'#title' => Html::escape($question),
'#size' => 60,
'#required' => TRUE,
);
$form['#validate'][] = 'captcha_questions_form_validate';
}
}
return $form;
}
}
/**
* Implements hook_form_validate().
*/
function captcha_questions_form_validate(&$form, FormStateInterface $form_state) {
$user = \Drupal::currentUser();
$ip = \Drupal::request()->getClientIp();
$form_id = $form_state->getValue('form_id');
$question_asked = \Drupal::config('captcha_questions.settings')->get('captcha_questions_question');
$answers = \Drupal::config('captcha_questions.settings')->get('captcha_questions_answers');
$answer_given = $form_state->getValue('captcha_questions_answer_given');
// Comparison is done in lowercase, ensure answers are lowercase.
$answers = array_map('strtolower', $answers);
$answer_given = Unicode::strtolower($answer_given);
// Check answer.
if (in_array($answer_given, $answers) == FALSE) {
// Log to watchdog if enabled.
if (\Drupal::config('captcha_questions.settings')->get('captcha_questions_watchdog')) {
$message = t('Blocked submission of form with form_id !form_id. Answer given was %answer_given', array('!form_id' => $form_id, '%answer_given' => $form_state->getValue('captcha_questions_answer_given')));
\Drupal::logger('captcha_questions')->error($message);
}
// Log to dblog if enabled and module exists and enabled.
$dblog_enabled = \Drupal::config('captcha_questions.settings')->get('captcha_questions_dblog');
$dblog_module_exists = \Drupal::moduleHandler()->moduleExists('captcha_questions_dblog') ? TRUE : FALSE;
if ($dblog_enabled && $dblog_module_exists) {
db_insert('captcha_questions_dblog')
->fields(array(
'timestamp' => REQUEST_TIME,
'ip' => $ip,
'form_id' => $form_id,
'question_asked' => Html::escape($question_asked),
'answer_given' => Html::escape($answer_given),
'answer_correct' => implode(",", $answers),
))
->execute();
}
// Display error.
$form_state->setErrorByName('captcha_questions_answer_given', t('Invalid answer'));
}
}