forked from aphexcreations/drupal-ajax
/
ajax_captcha.module
executable file
·104 lines (98 loc) · 2.59 KB
/
ajax_captcha.module
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
<?php
/**
* AJAX Captcha Plugin
*
* @see http://drupal.org/project/ajax
* @see irc://freenode.net/#drupy
* @depends Drupal 6
* @author brendoncrawford
* @note This file uses a 79 character width limit.
*
*/
/**
* validation fail hook
*
* @param $form Assoc
* @param $form_state Assoc
* @param $data Assoc
* @return Bool
*/
function ajax_captcha_ajax_validate_fail(&$form, &$form_state, &$data) {
ajax_captcha_process($form, $form_state, $data);
return TRUE;
}
/**
* validation fail hook
*
* @param $form Assoc
* @param $form_state Assoc
* @param $data Assoc
* @return Bool
*/
function ajax_captcha_ajax_validate_pass(&$form, &$form_state, &$data, &$pass) {
ajax_captcha_process($form, $form_state, $data);
return TRUE;
}
/**
* Captcha functionality
* This depends on the provided captcha patches
*
* @param $form Assoc
* @param $form_state Assoc
* @param $data Assoc
* @return Bool
*/
function ajax_captcha_process(&$form, &$form_state, &$data) {
//print_r($data);
if (array_key_exists('captcha_token', $form_state['values']) &&
array_key_exists('captcha_type', $form_state['values']) &&
array_key_exists('captcha_query', $form_state['values'])) {
//$data['debug'] .= 'B';
// Conditional show
if (_captcha_required_for_user($form_state['values']['captcha_sid'], $form_id) == NULL) {
$data['updaters'][] = array(
'type' => 'hide',
'selector' => 'fieldset.captcha',
'value' => NULL
);
//$data['debug'] .= 'C';
}
//$data['debug'] .= 'D';
// MATH
if ($form_state['values']['captcha_type'] === 'math') {
$selector =
"#edit-captcha-response-wrapper .field-prefix";
$type = 'html_in';
//$data['debug'] .= 'E';
}
elseif ($form_state['values']['captcha_type'] === 'text') {
$selector =
"#edit-captcha-response-wrapper label[for='edit-captcha-response']";
$type = 'html_in';
//$data['debug'] .= 'F';
}
elseif ($form_state['values']['captcha_type'] === 'image') {
$selector =
".captcha img";
$type = 'html_out';
//$data['debug'] .= 'G';
}
$data['updaters'][] = array(
'type' => 'field',
'selector' => '#edit-captcha-token',
'value' => $form_state['values']['captcha_token']
);
$data['updaters'][] = array(
'type' => $type,
'selector' => $selector,
'value' => $form_state['values']['captcha_query']
);
$data['updaters'][] = array(
'type' => 'field',
'selector' => '#edit-captcha-response',
'value' => ''
);
theme_captcha($form);
}
return TRUE;
}