-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcaptcha.install
147 lines (138 loc) · 4.33 KB
/
captcha.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the CAPTCHA module.
*/
use Drupal\captcha\Entity\CaptchaPoint;
use Drupal\Core\Url;
/**
* Implements hook_schema().
*/
function captcha_schema() {
$schema['captcha_sessions'] = array(
'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
'fields' => array(
'csid' => array(
'description' => 'CAPTCHA session ID.',
'type' => 'serial',
'not null' => TRUE,
),
'token' => array(
'description' => 'One time CAPTCHA token.',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'uid' => array(
'description' => "User's {users}.uid.",
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'sid' => array(
'description' => "Session ID of the user.",
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
),
'ip_address' => array(
'description' => 'IP address of the visitor.',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
'timestamp' => array(
'description' => 'A Unix timestamp indicating when the challenge was generated.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'form_id' => array(
'description' => 'The form_id of the form where the CAPTCHA is added to.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'solution' => array(
'description' => 'Solution of the challenge.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'status' => array(
'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'attempts' => array(
'description' => 'The number of attempts.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('csid'),
'indexes' => array(
'csid_ip' => array('csid', 'ip_address'),
),
);
return $schema;
}
/**
* Implements hook_requirements().
*/
function captcha_requirements($phase) {
$requirements = array();
$config = \Drupal::config('captcha.settings');
if ($phase == 'runtime' && $config->get('enable_stats')) {
// Show the wrong response counter in the status report.
$requirements['captcha_wrong_response_counter'] = array(
'title' => \Drupal::translation()->translate('CAPTCHA'),
'value' => \Drupal::translation()->formatPlural(
$config->get('wrong_response_counter'),
'Already 1 blocked form submission',
'Already @count blocked form submissions'
),
'severity' => REQUIREMENT_INFO,
);
}
return $requirements;
}
/**
* Implements hook_install().
*/
function captcha_install() {
// Insert some default CAPTCHA points.
$form_ids = array(
'contact_site_form',
'contact_personal_form',
'user_register_form',
'user_pass',
'user_login_form',
'user_login_block',
'forum_node_form',
);
// Add form_ids of all currently known node types too.
foreach (node_type_get_names() as $type => $name) {
$form_ids[] = $type . '_node_form';
}
foreach ($form_ids as $form_id) {
$values = array(
'formId' => $form_id,
'captchaType' => 'none',
);
$captcha_point = new CaptchaPoint($values, 'captcha_point');
$captcha_point->save();
}
// Be friendly to your users: what to do after install?
drupal_set_message(\Drupal::translation()->translate('You can now <a href="!captcha_admin">configure the CAPTCHA module</a> for your site.',
array('!captcha_admin' => Url::fromRoute('captcha_settings')->toString())), 'status');
// Explain to users that page caching may be disabled.
if ($config = \Drupal::config('system.performance')->get('cache.page.use_internal') != 0) {
drupal_set_message(\Drupal::translation()->translate('Note that the CAPTCHA module disables <a href="!performance_admin">page caching</a> of pages that include a CAPTCHA challenge.',
array('!performance_admin' => Url::fromRoute('system.performance_settings')->toString())), 'warning');
}
}