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 | * Implementation of hook_requirements(). |
---|
125 | */ |
---|
126 | function captcha_requirements($phase) { |
---|
127 | $requirements = array(); |
---|
128 | $t = get_t(); |
---|
129 | if ($phase == 'runtime') { |
---|
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 | * Implementation of hook_install(). |
---|
146 | */ |
---|
147 | function captcha_install() { |
---|
148 | $t = get_t(); |
---|
149 | drupal_install_schema('captcha'); |
---|
150 | |
---|
151 | |
---|
152 | // Be friendly to your users: what to do after install? |
---|
153 | drupal_set_message($t('You can now <a href="!captcha_admin">configure the CAPTCHA module</a> for your site.', |
---|
154 | array('!captcha_admin' => url('admin/user/captcha'))), 'status'); |
---|
155 | |
---|
156 | // Explain to users that page caching may be disabled. |
---|
157 | if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) { |
---|
158 | drupal_set_message($t('Note that the CAPTCHA module disables <a href="!performance_admin">page caching</a> of pages that include a CAPTCHA challenge.', |
---|
159 | array('!performance_admin' => url('admin/settings/performance'))), 'warning'); |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Implementation of hook_uninstall(). |
---|
166 | */ |
---|
167 | function captcha_uninstall() { |
---|
168 | drupal_uninstall_schema('captcha'); |
---|
169 | db_query("DELETE FROM {variable} WHERE name LIKE 'captcha_%'"); |
---|
170 | cache_clear_all('variables', 'cache'); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Implementation of hook_update_N() |
---|
175 | */ |
---|
176 | function captcha_update_1() { |
---|
177 | $items = array(); |
---|
178 | switch ($GLOBALS['db_type']) { |
---|
179 | case 'mysql': |
---|
180 | case 'mysqli': |
---|
181 | $items[] = update_sql("CREATE TABLE {captcha_points} ( |
---|
182 | form_id varchar(128) NOT NULL, |
---|
183 | module varchar(64) default NULL, |
---|
184 | type varchar(64) default NULL, |
---|
185 | PRIMARY KEY (form_id) |
---|
186 | ) /*!40100 DEFAULT CHARACTER SET utf8 */;" |
---|
187 | ); |
---|
188 | $succes = TRUE; |
---|
189 | break; |
---|
190 | case 'pgsql': |
---|
191 | $items[] = update_sql("CREATE TABLE {captcha_points} ( |
---|
192 | form_id varchar(128) NOT NULL, |
---|
193 | module varchar(64) default NULL, |
---|
194 | type varchar(64) default NULL, |
---|
195 | PRIMARY KEY (form_id) |
---|
196 | );" |
---|
197 | ); |
---|
198 | $succes = TRUE; |
---|
199 | break; |
---|
200 | default: |
---|
201 | drupal_set_message(t('Unsupported database.'), 'error'); |
---|
202 | $succes = FALSE; |
---|
203 | break; |
---|
204 | } |
---|
205 | if ($succes) { |
---|
206 | // insert some defaults |
---|
207 | $form_ids = array('comment_form', 'contact_mail_user', 'contact_mail_page', |
---|
208 | 'user_register', 'user_pass'); |
---|
209 | foreach ($form_ids as $form_id) { |
---|
210 | $items[] = update_sql("INSERT INTO {captcha_points} (form_id, module, type) VALUES ('$form_id', NULL, NULL)"); |
---|
211 | } |
---|
212 | } |
---|
213 | return $items; |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Implementation of hook_update_N() |
---|
218 | */ |
---|
219 | function captcha_update_2() { |
---|
220 | $items = array(); |
---|
221 | // insert some defaults |
---|
222 | $form_ids = array('user_login', 'user_login_block'); |
---|
223 | foreach ($form_ids as $form_id) { |
---|
224 | $items[] = update_sql("INSERT INTO {captcha_points} (form_id, module, type) VALUES ('$form_id', NULL, NULL)"); |
---|
225 | } |
---|
226 | return $items; |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * Implementation of hook_update_N() |
---|
231 | */ |
---|
232 | function captcha_update_6200() { |
---|
233 | $items = array(); |
---|
234 | |
---|
235 | // Table for the CAPTCHA sessions. |
---|
236 | $schema['captcha_sessions'] = array( |
---|
237 | 'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).', |
---|
238 | 'fields' => array( |
---|
239 | 'csid' => array( |
---|
240 | 'description' => 'CAPTCHA session ID.', |
---|
241 | 'type' => 'serial', |
---|
242 | 'not null' => TRUE, |
---|
243 | ), |
---|
244 | 'uid' => array( |
---|
245 | 'description' => "User's {users}.uid.", |
---|
246 | 'type' => 'int', |
---|
247 | 'not null' => TRUE, |
---|
248 | 'default' => 0, |
---|
249 | ), |
---|
250 | 'sid' => array( |
---|
251 | 'description' => "Session ID of the user.", |
---|
252 | 'type' => 'varchar', |
---|
253 | 'length' => 64, |
---|
254 | 'not null' => TRUE, |
---|
255 | 'default' => '', |
---|
256 | ), |
---|
257 | 'ip_address' => array( |
---|
258 | 'description' => 'IP address of the visitor.', |
---|
259 | 'type' => 'varchar', |
---|
260 | 'length' => 128, |
---|
261 | 'not null' => FALSE, |
---|
262 | ), |
---|
263 | 'timestamp' => array( |
---|
264 | 'description' => 'A Unix timestamp indicating when the challenge was generated.', |
---|
265 | 'type' => 'int', |
---|
266 | 'not null' => TRUE, |
---|
267 | 'default' => 0, |
---|
268 | ), |
---|
269 | 'form_id' => array( |
---|
270 | 'description' => 'The form_id of the form where the CAPTCHA is added to.', |
---|
271 | 'type' => 'varchar', |
---|
272 | 'length' => 128, |
---|
273 | 'not null' => TRUE, |
---|
274 | ), |
---|
275 | 'solution' => array( |
---|
276 | 'description' => 'Solution of the challenge.', |
---|
277 | 'type' => 'varchar', |
---|
278 | 'length' => 128, |
---|
279 | 'not null' => TRUE, |
---|
280 | 'default' => '', |
---|
281 | ), |
---|
282 | 'status' => array( |
---|
283 | 'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)', |
---|
284 | 'type' => 'int', |
---|
285 | 'not null' => TRUE, |
---|
286 | 'default' => 0, |
---|
287 | ), |
---|
288 | 'attempts' => array( |
---|
289 | 'description' => 'The number of attempts.', |
---|
290 | 'type' => 'int', |
---|
291 | 'not null' => TRUE, |
---|
292 | 'default' => 0, |
---|
293 | ) |
---|
294 | ), |
---|
295 | 'primary key' => array('csid'), |
---|
296 | 'indexes' => array( |
---|
297 | 'csid_ip' => array('csid', 'ip_address'), |
---|
298 | ), |
---|
299 | ); |
---|
300 | |
---|
301 | db_create_table($items, 'captcha_sessions', $schema['captcha_sessions']); |
---|
302 | |
---|
303 | return $items; |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | * Implementation of hook_update_N() |
---|
308 | * Change the captcha points with the old text CAPTCHA, which was |
---|
309 | * removed from the 6.x-2.x branch, to the simple math CAPTCHA. |
---|
310 | */ |
---|
311 | function captcha_update_6201() { |
---|
312 | $items = array(); |
---|
313 | $items[] = update_sql("UPDATE {captcha_points} SET module = 'captcha', type = 'Math' WHERE module = 'text_captcha' AND type = 'Text';"); |
---|
314 | return $items; |
---|
315 | } |
---|
316 | |
---|
317 | |
---|
318 | /** |
---|
319 | * Implementation of hook_update_N() |
---|
320 | * Add a CAPTCHA token column to captcha_sessions table. |
---|
321 | */ |
---|
322 | function captcha_update_6202() { |
---|
323 | $ret = array(); |
---|
324 | db_add_column($ret, 'captcha_sessions', 'token', 'varchar(64)'); |
---|
325 | return $ret; |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * Implementation of hook_update_N() |
---|
330 | * Rename the type field to captcha_type in captcha_points. |
---|
331 | */ |
---|
332 | function captcha_update_6203() { |
---|
333 | $ret = array(); |
---|
334 | db_change_field($ret, 'captcha_points', 'type', 'captcha_type', array('type' => 'varchar', 'length' => 64)); |
---|
335 | return $ret; |
---|
336 | } |
---|