1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Install, update and uninstall functions for the CAPTCHA module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implementation of hook_schema().
|
10
|
*/
|
11
|
function captcha_schema() {
|
12
|
// Table for positions and types of the challenges.
|
13
|
$schema['captcha_points'] = array(
|
14
|
'description' => 'This table describes which challenges should be added to which forms.',
|
15
|
'fields' => array(
|
16
|
'form_id' => array(
|
17
|
'description' => 'The form_id of the form to add a CAPTCHA to.',
|
18
|
'type' => 'varchar',
|
19
|
'length' => 128,
|
20
|
'not null' => TRUE,
|
21
|
'default' => '',
|
22
|
),
|
23
|
'module' => array(
|
24
|
'description' => 'The module that provides the challenge.',
|
25
|
'type' => 'varchar',
|
26
|
'length' => 64,
|
27
|
),
|
28
|
'captcha_type' => array(
|
29
|
'description' => 'The challenge type to use.',
|
30
|
'type' => 'varchar',
|
31
|
'length' => 64,
|
32
|
),
|
33
|
),
|
34
|
'primary key' => array('form_id'),
|
35
|
);
|
36
|
// Table for the CAPTCHA sessions.
|
37
|
$schema['captcha_sessions'] = array(
|
38
|
'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
|
39
|
'fields' => array(
|
40
|
'csid' => array(
|
41
|
'description' => 'CAPTCHA session ID.',
|
42
|
'type' => 'serial',
|
43
|
'not null' => TRUE,
|
44
|
),
|
45
|
'token' => array(
|
46
|
'description' => 'One time CAPTCHA token.',
|
47
|
'type' => 'varchar',
|
48
|
'length' => 64,
|
49
|
'not null' => FALSE,
|
50
|
),
|
51
|
'uid' => array(
|
52
|
'description' => "User's {users}.uid.",
|
53
|
'type' => 'int',
|
54
|
'not null' => TRUE,
|
55
|
'default' => 0,
|
56
|
),
|
57
|
'sid' => array(
|
58
|
'description' => "Session ID of the user.",
|
59
|
'type' => 'varchar',
|
60
|
'length' => 64,
|
61
|
'not null' => TRUE,
|
62
|
'default' => '',
|
63
|
),
|
64
|
'ip_address' => array(
|
65
|
'description' => 'IP address of the visitor.',
|
66
|
'type' => 'varchar',
|
67
|
'length' => 128,
|
68
|
'not null' => FALSE,
|
69
|
),
|
70
|
'timestamp' => array(
|
71
|
'description' => 'A Unix timestamp indicating when the challenge was generated.',
|
72
|
'type' => 'int',
|
73
|
'not null' => TRUE,
|
74
|
'default' => 0,
|
75
|
),
|
76
|
'form_id' => array(
|
77
|
'description' => 'The form_id of the form where the CAPTCHA is added to.',
|
78
|
'type' => 'varchar',
|
79
|
'length' => 128,
|
80
|
'not null' => TRUE,
|
81
|
),
|
82
|
'solution' => array(
|
83
|
'description' => 'Solution of the challenge.',
|
84
|
'type' => 'varchar',
|
85
|
'length' => 128,
|
86
|
'not null' => TRUE,
|
87
|
'default' => '',
|
88
|
),
|
89
|
'status' => array(
|
90
|
'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
|
91
|
'type' => 'int',
|
92
|
'not null' => TRUE,
|
93
|
'default' => 0,
|
94
|
),
|
95
|
'attempts' => array(
|
96
|
'description' => 'The number of attempts.',
|
97
|
'type' => 'int',
|
98
|
'not null' => TRUE,
|
99
|
'default' => 0,
|
100
|
)
|
101
|
),
|
102
|
'primary key' => array('csid'),
|
103
|
'indexes' => array(
|
104
|
'csid_ip' => array('csid', 'ip_address'),
|
105
|
),
|
106
|
);
|
107
|
|
108
|
return $schema;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Implementation of hook_requirements().
|
113
|
*/
|
114
|
function captcha_requirements($phase) {
|
115
|
$requirements = array();
|
116
|
$t = get_t();
|
117
|
if ($phase == 'runtime' && variable_get('captcha_enable_stats', FALSE)) {
|
118
|
// Show the wrong response counter in the status report.
|
119
|
$requirements['captcha_wrong_response_counter'] = array(
|
120
|
'title' => $t('CAPTCHA'),
|
121
|
'value' => format_plural(
|
122
|
variable_get('captcha_wrong_response_counter', 0),
|
123
|
'Already 1 blocked form submission',
|
124
|
'Already @count blocked form submissions'
|
125
|
),
|
126
|
'severity' => REQUIREMENT_INFO,
|
127
|
);
|
128
|
}
|
129
|
return $requirements;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Implementation of hook_install().
|
134
|
*/
|
135
|
function captcha_install() {
|
136
|
$t = get_t();
|
137
|
// Insert some default CAPTCHA points.
|
138
|
$form_ids = array(
|
139
|
'contact_site_form', 'contact_personal_form',
|
140
|
'user_register_form', 'user_pass', 'user_login', 'user_login_block',
|
141
|
'forum_node_form'
|
142
|
);
|
143
|
// Add form_ids of all currently known node types too.
|
144
|
foreach (node_type_get_names() as $type => $name) {
|
145
|
$form_ids[] = 'comment_node_' . $type . '_form';
|
146
|
}
|
147
|
foreach ($form_ids as $form_id) {
|
148
|
db_insert('captcha_points')
|
149
|
->fields(array(
|
150
|
'form_id' => $form_id,
|
151
|
'module' => NULL,
|
152
|
'captcha_type' => NULL,
|
153
|
))
|
154
|
->execute();
|
155
|
}
|
156
|
|
157
|
// Be friendly to your users: what to do after install?
|
158
|
drupal_set_message($t('You can now <a href="!captcha_admin">configure the CAPTCHA module</a> for your site.',
|
159
|
array('!captcha_admin' => url('admin/config/people/captcha'))), 'status');
|
160
|
|
161
|
// Explain to users that page caching may be disabled.
|
162
|
if (variable_get('cache', 0) != 0) {
|
163
|
drupal_set_message($t('Note that the CAPTCHA module disables <a href="!performance_admin">page caching</a> of pages that include a CAPTCHA challenge.',
|
164
|
array('!performance_admin' => url('admin/config/development/performance'))), 'warning');
|
165
|
}
|
166
|
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Implementation of hook_uninstall().
|
171
|
*/
|
172
|
function captcha_uninstall() {
|
173
|
drupal_uninstall_schema('captcha');
|
174
|
db_query("DELETE FROM {variable} WHERE name LIKE 'captcha_%'");
|
175
|
cache_clear_all('variables', 'cache');
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Implementation of hook_update_N()
|
180
|
*/
|
181
|
function captcha_update_6200() {
|
182
|
$items = array();
|
183
|
|
184
|
// Table for the CAPTCHA sessions.
|
185
|
$schema['captcha_sessions'] = array(
|
186
|
'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
|
187
|
'fields' => array(
|
188
|
'csid' => array(
|
189
|
'description' => 'CAPTCHA session ID.',
|
190
|
'type' => 'serial',
|
191
|
'not null' => TRUE,
|
192
|
),
|
193
|
'uid' => array(
|
194
|
'description' => "User's {users}.uid.",
|
195
|
'type' => 'int',
|
196
|
'not null' => TRUE,
|
197
|
'default' => 0,
|
198
|
),
|
199
|
'sid' => array(
|
200
|
'description' => "Session ID of the user.",
|
201
|
'type' => 'varchar',
|
202
|
'length' => 64,
|
203
|
'not null' => TRUE,
|
204
|
'default' => '',
|
205
|
),
|
206
|
'ip_address' => array(
|
207
|
'description' => 'IP address of the visitor.',
|
208
|
'type' => 'varchar',
|
209
|
'length' => 128,
|
210
|
'not null' => FALSE,
|
211
|
),
|
212
|
'timestamp' => array(
|
213
|
'description' => 'A Unix timestamp indicating when the challenge was generated.',
|
214
|
'type' => 'int',
|
215
|
'not null' => TRUE,
|
216
|
'default' => 0,
|
217
|
),
|
218
|
'form_id' => array(
|
219
|
'description' => 'The form_id of the form where the CAPTCHA is added to.',
|
220
|
'type' => 'varchar',
|
221
|
'length' => 128,
|
222
|
'not null' => TRUE,
|
223
|
),
|
224
|
'solution' => array(
|
225
|
'description' => 'Solution of the challenge.',
|
226
|
'type' => 'varchar',
|
227
|
'length' => 128,
|
228
|
'not null' => TRUE,
|
229
|
'default' => '',
|
230
|
),
|
231
|
'status' => array(
|
232
|
'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
|
233
|
'type' => 'int',
|
234
|
'not null' => TRUE,
|
235
|
'default' => 0,
|
236
|
),
|
237
|
'attempts' => array(
|
238
|
'description' => 'The number of attempts.',
|
239
|
'type' => 'int',
|
240
|
'not null' => TRUE,
|
241
|
'default' => 0,
|
242
|
)
|
243
|
),
|
244
|
'primary key' => array('csid'),
|
245
|
'indexes' => array(
|
246
|
'csid_ip' => array('csid', 'ip_address'),
|
247
|
),
|
248
|
);
|
249
|
|
250
|
db_create_table($items, 'captcha_sessions', $schema['captcha_sessions']);
|
251
|
|
252
|
return $items;
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Implementation of hook_update_N()
|
257
|
* Change the captcha points with the old text CAPTCHA, which was
|
258
|
* removed from the 6.x-2.x branch, to the simple math CAPTCHA.
|
259
|
*/
|
260
|
function captcha_update_6201() {
|
261
|
$items = array();
|
262
|
$items[] = update_sql("UPDATE {captcha_points} SET module = 'captcha', type = 'Math' WHERE module = 'text_captcha' AND type = 'Text';");
|
263
|
return $items;
|
264
|
}
|
265
|
|
266
|
|
267
|
/**
|
268
|
* Implementation of hook_update_N()
|
269
|
* Add a CAPTCHA token column to captcha_sessions table.
|
270
|
*/
|
271
|
function captcha_update_6202() {
|
272
|
$ret = array();
|
273
|
db_add_column($ret, 'captcha_sessions', 'token', 'varchar(64)');
|
274
|
return $ret;
|
275
|
}
|
276
|
|
277
|
|
278
|
|
279
|
/**
|
280
|
* Implementation of hook_update_N()
|
281
|
* Rename the type field to captcha_type in captcha_points.
|
282
|
*/
|
283
|
function captcha_update_6203() {
|
284
|
$ret = array();
|
285
|
db_change_field($ret, 'captcha_points', 'type', 'captcha_type', array('type' => 'varchar', 'length' => 64));
|
286
|
return $ret;
|
287
|
}
|
288
|
|
289
|
|
290
|
/**
|
291
|
* Migrate form configuration for changed form ids in Drupal 7.
|
292
|
*/
|
293
|
function captcha_update_7000() {
|
294
|
// 'user_register' became 'user_register_form'.
|
295
|
db_update('captcha_points')
|
296
|
->fields(array('form_id' => 'user_register_form'))
|
297
|
->condition('form_id', 'user_register')
|
298
|
->execute();
|
299
|
// 'contact_mail_page' became 'contact_site_form'.
|
300
|
db_update('captcha_points')
|
301
|
->fields(array('form_id' => 'contact_site_form'))
|
302
|
->condition('form_id', 'contact_mail_page')
|
303
|
->execute();
|
304
|
// 'contact_mail_user' became 'contact_personal_form'.
|
305
|
db_update('captcha_points')
|
306
|
->fields(array('form_id' => 'contact_personal_form'))
|
307
|
->condition('form_id', 'contact_mail_user')
|
308
|
->execute();
|
309
|
|
310
|
// The D6-style comment_form form_id is split per node type
|
311
|
// in D7: comment_node_{type}_form, e.g. comment_node_page_form.
|
312
|
// Get the current settings for 'comment_form'.
|
313
|
$captcha_point = db_query(
|
314
|
"SELECT * FROM {captcha_points} WHERE form_id = :comment_form_id",
|
315
|
array(':comment_form_id' => 'comment_form')
|
316
|
)->fetchObject();
|
317
|
if ($captcha_point !== FALSE) {
|
318
|
// Create entries for D7-style node form IDs.
|
319
|
$module = $captcha_point->module;
|
320
|
$captcha_type = $captcha_point->captcha_type;
|
321
|
foreach (node_type_get_names() as $type => $name) {
|
322
|
$form_id = 'comment_node_' . $type . '_form';
|
323
|
db_insert('captcha_points')
|
324
|
->fields(array(
|
325
|
'form_id' => $form_id,
|
326
|
'module' => $module,
|
327
|
'captcha_type' => $captcha_type,
|
328
|
))
|
329
|
->execute();
|
330
|
}
|
331
|
// Delete outdated entry.
|
332
|
db_delete('captcha_points')
|
333
|
->condition('form_id', 'comment_form')
|
334
|
->execute();
|
335
|
}
|
336
|
}
|