Projet

Général

Profil

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

root / drupal7 / sites / all / modules / captcha / captcha.install @ 00c2605a

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
    'export' => array(
16
      'key' => 'form_id',
17
      'identifier' => 'captcha',
18
      'default hook' => 'captcha_default_points',  // Function hook name.
19
      'status' => 'mark_status',
20
      'api' => array(
21
        'owner' => 'captcha',
22
        'api' => 'captcha',  // Base name for api include files.
23
        'minimum_version' => 1,
24
        'current_version' => 1,
25
      ),
26
    ),
27
    'fields' => array(
28
      'form_id' => array(
29
        'description' => 'The form_id of the form to add a CAPTCHA to.',
30
        'type' => 'varchar',
31
        'length' => 128,
32
        'not null' => TRUE,
33
        'default' => '',
34
      ),
35
      'module' => array(
36
        'description' => 'The module that provides the challenge.',
37
        'type' => 'varchar',
38
        'length' => 64,
39
      ),
40
      'captcha_type' => array(
41
        'description' => 'The challenge type to use.',
42
        'type' => 'varchar',
43
        'length' => 64,
44
      ),
45
    ),
46
    'primary key' => array('form_id'),
47
  );
48
  // Table for the CAPTCHA sessions.
49
  $schema['captcha_sessions'] = array(
50
    'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
51
    'fields' => array(
52
      'csid' => array(
53
        'description' => 'CAPTCHA session ID.',
54
        'type' => 'serial',
55
        'not null' => TRUE,
56
      ),
57
      'token' => array(
58
        'description' => 'One time CAPTCHA token.',
59
        'type' => 'varchar',
60
        'length' => 64,
61
        'not null' => FALSE,
62
      ),
63
      'uid' => array(
64
        'description' => "User's {users}.uid.",
65
        'type' => 'int',
66
        'not null' => TRUE,
67
        'default' => 0,
68
      ),
69
      'sid' => array(
70
        'description' => "Session ID of the user.",
71
        'type' => 'varchar',
72
        'length' => 64,
73
        'not null' => TRUE,
74
        'default' => '',
75
      ),
76
      'ip_address' => array(
77
        'description' => 'IP address of the visitor.',
78
        'type' => 'varchar',
79
        'length' => 128,
80
        'not null' => FALSE,
81
      ),
82
      'timestamp' => array(
83
        'description' => 'A Unix timestamp indicating when the challenge was generated.',
84
        'type' => 'int',
85
        'not null' => TRUE,
86
        'default' => 0,
87
      ),
88
      'form_id' => array(
89
        'description' => 'The form_id of the form where the CAPTCHA is added to.',
90
        'type' => 'varchar',
91
        'length' => 128,
92
        'not null' => TRUE,
93
      ),
94
      'solution' => array(
95
        'description' => 'Solution of the challenge.',
96
        'type' => 'varchar',
97
        'length' => 128,
98
        'not null' => TRUE,
99
        'default' => '',
100
      ),
101
      'status' => array(
102
        'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
103
        'type' => 'int',
104
        'not null' => TRUE,
105
        'default' => 0,
106
      ),
107
      'attempts' => array(
108
        'description' => 'The number of attempts.',
109
        'type' => 'int',
110
        'not null' => TRUE,
111
        'default' => 0,
112
      ),
113
    ),
114
    'primary key' => array('csid'),
115
    'indexes' => array(
116
      'csid_ip' => array('csid', 'ip_address'),
117
    ),
118
  );
119

    
120
  return $schema;
121
}
122

    
123
/**
124
 * Implements of hook_requirements().
125
 */
126
function captcha_requirements($phase) {
127
  $requirements = array();
128
  $t = get_t();
129
  if ($phase == 'runtime' && variable_get('captcha_enable_stats', FALSE)) {
130
    // Show the wrong response counter in the status report.
131
    $requirements['captcha_wrong_response_counter'] = array(
132
      'title' => $t('CAPTCHA'),
133
      'value' => format_plural(
134
        variable_get('captcha_wrong_response_counter', 0),
135
        'Already 1 blocked form submission',
136
        'Already @count blocked form submissions'
137
      ),
138
      'severity' => REQUIREMENT_INFO,
139
    );
140
  }
141
  return $requirements;
142
}
143

    
144
/**
145
 * Implements of hook_install().
146
 */
147
function captcha_install() {
148
  $t = get_t();
149

    
150
  // Be friendly to your users: what to do after install?
151
  drupal_set_message($t('You can now <a href="!captcha_admin">configure the CAPTCHA module</a> for your site.',
152
    array('!captcha_admin' => url('admin/config/people/captcha'))), 'status');
153

    
154
  // Explain to users that page caching may be disabled.
155
  if (variable_get('cache', 0) != 0) {
156
    drupal_set_message($t('Note that the CAPTCHA module disables <a href="!performance_admin">page caching</a> of pages that include a CAPTCHA challenge.',
157
      array('!performance_admin' => url('admin/config/development/performance'))), 'warning');
158
  }
159

    
160
}
161

    
162
/**
163
 * Implements of hook_uninstall().
164
 */
165
function captcha_uninstall() {
166
  drupal_uninstall_schema('captcha');
167
  db_query("DELETE FROM {variable} WHERE name LIKE 'captcha_%'");
168
  cache_clear_all('variables', 'cache');
169
}
170

    
171
/**
172
 * Implements of hook_update_N().
173
 */
174
function captcha_update_6200() {
175
  $items = array();
176

    
177
  // Table for the CAPTCHA sessions.
178
  $schema['captcha_sessions'] = array(
179
    'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
180
    'fields' => array(
181
      'csid' => array(
182
        'description' => 'CAPTCHA session ID.',
183
        'type' => 'serial',
184
        'not null' => TRUE,
185
      ),
186
      'uid' => array(
187
        'description' => "User's {users}.uid.",
188
        'type' => 'int',
189
        'not null' => TRUE,
190
        'default' => 0,
191
      ),
192
      'sid' => array(
193
        'description' => "Session ID of the user.",
194
        'type' => 'varchar',
195
        'length' => 64,
196
        'not null' => TRUE,
197
        'default' => '',
198
      ),
199
      'ip_address' => array(
200
        'description' => 'IP address of the visitor.',
201
        'type' => 'varchar',
202
        'length' => 128,
203
        'not null' => FALSE,
204
      ),
205
      'timestamp' => array(
206
        'description' => 'A Unix timestamp indicating when the challenge was generated.',
207
        'type' => 'int',
208
        'not null' => TRUE,
209
        'default' => 0,
210
      ),
211
      'form_id' => array(
212
        'description' => 'The form_id of the form where the CAPTCHA is added to.',
213
        'type' => 'varchar',
214
        'length' => 128,
215
        'not null' => TRUE,
216
      ),
217
      'solution' => array(
218
        'description' => 'Solution of the challenge.',
219
        'type' => 'varchar',
220
        'length' => 128,
221
        'not null' => TRUE,
222
        'default' => '',
223
      ),
224
      'status' => array(
225
        'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
226
        'type' => 'int',
227
        'not null' => TRUE,
228
        'default' => 0,
229
      ),
230
      'attempts' => array(
231
        'description' => 'The number of attempts.',
232
        'type' => 'int',
233
        'not null' => TRUE,
234
        'default' => 0,
235
      ),
236
    ),
237
    'primary key' => array('csid'),
238
    'indexes' => array(
239
      'csid_ip' => array('csid', 'ip_address'),
240
    ),
241
  );
242

    
243
  db_create_table($items, 'captcha_sessions', $schema['captcha_sessions']);
244

    
245
  return $items;
246
}
247

    
248
/**
249
 * Implements of hook_update_N().
250
 *
251
 * Change the captcha points with the old text CAPTCHA, which was
252
 * removed from the 6.x-2.x branch, to the simple math CAPTCHA.
253
 */
254
function captcha_update_6201() {
255
  $items = array();
256
  $items[] = update_sql("UPDATE {captcha_points} SET module = 'captcha', type = 'Math' WHERE module = 'text_captcha' AND type = 'Text';");
257
  return $items;
258
}
259

    
260
/**
261
 * Implements of hook_update_N().
262
 *
263
 * Add a CAPTCHA token column to captcha_sessions table.
264
 */
265
function captcha_update_6202() {
266
  $ret = array();
267
  db_add_column($ret, 'captcha_sessions', 'token', 'varchar(64)');
268
  return $ret;
269
}
270

    
271
/**
272
 * Implements of hook_update_N().
273
 *
274
 * Rename the type field to captcha_type in captcha_points.
275
 */
276
function captcha_update_6203() {
277
  $ret = array();
278
  db_change_field($ret, 'captcha_points', 'type', 'captcha_type', array('type' => 'varchar', 'length' => 64));
279
  return $ret;
280
}
281

    
282
/**
283
 * Migrate form configuration for changed form ids in Drupal 7.
284
 */
285
function captcha_update_7000() {
286
  // 'user_register' became 'user_register_form'.
287
  db_update('captcha_points')
288
    ->fields(array('form_id' => 'user_register_form'))
289
    ->condition('form_id', 'user_register')
290
    ->execute();
291
  // 'contact_mail_page' became 'contact_site_form'.
292
  db_update('captcha_points')
293
    ->fields(array('form_id' => 'contact_site_form'))
294
    ->condition('form_id', 'contact_mail_page')
295
    ->execute();
296
  // 'contact_mail_user' became 'contact_personal_form'.
297
  db_update('captcha_points')
298
    ->fields(array('form_id' => 'contact_personal_form'))
299
    ->condition('form_id', 'contact_mail_user')
300
    ->execute();
301

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