Projet

Général

Profil

Paste
Télécharger (9,71 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / captcha / captcha.install @ ac1bc5de

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
 * Implements 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
 * Implements 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
 * Implements 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
 * Implements 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
 * Implements of hook_update_N().
257
 *
258
 * Change the captcha points with the old text CAPTCHA, which was
259
 * removed from the 6.x-2.x branch, to the simple math CAPTCHA.
260
 */
261
function captcha_update_6201() {
262
  $items = array();
263
  $items[] = update_sql("UPDATE {captcha_points} SET module = 'captcha', type = 'Math' WHERE module = 'text_captcha' AND type = 'Text';");
264
  return $items;
265
}
266

    
267
/**
268
 * Implements of hook_update_N().
269
 *
270
 * Add a CAPTCHA token column to captcha_sessions table.
271
 */
272
function captcha_update_6202() {
273
  $ret = array();
274
  db_add_column($ret, 'captcha_sessions', 'token', 'varchar(64)');
275
  return $ret;
276
}
277

    
278
/**
279
 * Implements of hook_update_N().
280
 *
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
 * Migrate form configuration for changed form ids in Drupal 7.
291
 */
292
function captcha_update_7000() {
293
  // 'user_register' became 'user_register_form'.
294
  db_update('captcha_points')
295
    ->fields(array('form_id' => 'user_register_form'))
296
    ->condition('form_id', 'user_register')
297
    ->execute();
298
  // 'contact_mail_page' became 'contact_site_form'.
299
  db_update('captcha_points')
300
    ->fields(array('form_id' => 'contact_site_form'))
301
    ->condition('form_id', 'contact_mail_page')
302
    ->execute();
303
  // 'contact_mail_user' became 'contact_personal_form'.
304
  db_update('captcha_points')
305
    ->fields(array('form_id' => 'contact_personal_form'))
306
    ->condition('form_id', 'contact_mail_user')
307
    ->execute();
308

    
309
  // The D6-style comment_form form_id is split per node type
310
  // in D7: comment_node_{type}_form, e.g. comment_node_page_form.
311
  // Get the current settings for 'comment_form'.
312
  $captcha_point = db_query(
313
    "SELECT * FROM {captcha_points} WHERE form_id = :comment_form_id",
314
    array(':comment_form_id' => 'comment_form')
315
  )->fetchObject();
316
  if ($captcha_point !== FALSE) {
317
    // Create entries for D7-style node form IDs.
318
    $module = $captcha_point->module;
319
    $captcha_type = $captcha_point->captcha_type;
320
    foreach (node_type_get_names() as $type => $name) {
321
      $form_id = 'comment_node_' . $type . '_form';
322
      db_insert('captcha_points')
323
        ->fields(array(
324
          'form_id' => $form_id,
325
          'module' => $module,
326
          'captcha_type' => $captcha_type,
327
        ))
328
        ->execute();
329
    }
330
    // Delete outdated entry.
331
    db_delete('captcha_points')
332
      ->condition('form_id', 'comment_form')
333
      ->execute();
334
  }
335
}