Skip to content

Commit

Permalink
updating captcha module support, addresses http://drupal.org/node/562318
Browse files Browse the repository at this point in the history
  • Loading branch information
bholtsclaw committed Dec 8, 2011
1 parent 53a10a9 commit 47c9307
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 57 deletions.
4 changes: 4 additions & 0 deletions ajax.js
Expand Up @@ -212,6 +212,10 @@ Drupal.Ajax.updater = function(updaters) {
else if(updaters[i].type === 'remove') {
elm.remove();
}
// HIDE
else if(updaters[i].type === 'hide') {
elm.hide();
}
}
return true;
};
Expand Down
83 changes: 41 additions & 42 deletions plugins/captcha/ajax_captcha.module
Expand Up @@ -46,60 +46,59 @@ function ajax_captcha_ajax_validate_pass(&$form, &$form_state, &$data, &$pass) {
* @param $data Assoc
* @return Bool
*/

function ajax_captcha_process(&$form, &$form_state, &$data) {
//$data['debug'] = 'A';
//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_persistence_skip($form['form_id']['#value'])) {
// Conditional show
if (_captcha_required_for_user($form_state['values']['captcha_sid'], $form_id) == NULL) {
$data['updaters'][] = array(
'type' => 'remove',
'type' => 'hide',
'selector' => 'fieldset.captcha',
'value' => NULL
);
//$data['debug'] .= 'C';
}
// ALways Show
else {
//$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' => ''
);
captcha_pre_render($form);
//$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;
}
34 changes: 19 additions & 15 deletions plugins/captcha/patches/captcha.module.patch
@@ -1,32 +1,36 @@
--- captcha.module.orig 2008-11-14 02:28:11.000000000 -0800
+++ captcha.module 2008-11-14 03:02:59.000000000 -0800
@@ -240,7 +240,14 @@ function captcha_form_alter(&$form, $for
'#type' => 'value',
'#value' => $captcha['solution'],
);
-
+ $form['captcha']['captcha_query'] = array(
diff --git a/captcha.module b/captcha.module
index 19744a1..0ff5179 100644
--- a/captcha.module
+++ b/captcha.module
@@ -365,6 +365,14 @@ function captcha_form_alter(&$form, $form_state, $form_id) {
'#type' => 'captcha',
'#captcha_type' => $captcha_point->module .'/'. $captcha_point->captcha_type,
);
+ $captcha_element['captcha_query'] = array(
+ '#type' => 'value',
+ '#value' => $captcha['query']
+ );
+ $form['captcha']['captcha_type'] = array(
+ $captcha_element['captcha_type'] = array(
+ '#type' => 'value',
+ '#value' => $captcha['type']
+ );
// The CAPTCHA token is used to differentiate between different instances
// of the same form. This makes it possible to request the same form a
// couple of times before submitting them. The solution of the CAPTCHA of
@@ -446,12 +453,15 @@ function captcha_captcha($op, $captcha_t
// Add a CAPTCHA description if required.
if (variable_get('captcha_add_captcha_description', TRUE)) {
$captcha_element['#description'] = _captcha_get_description();
@@ -705,7 +713,10 @@ function captcha_captcha($op, $captcha_type = '') {
$answer = mt_rand(1, 20);
$x = mt_rand(1, $answer);
$y = $answer - $x;
+ $query = t('@x + @y = ', array('@x' => $x, '@y' => $y));
$result['solution'] = "$answer";
+ $result['query'] = $query;
+ $result['type'] = 'math';
$result['form']['captcha_response'] = array(
// Build challenge widget.
// Note that we also use t() for the math challenge itself. This makes
// it possible to 'rephrase' the challenge a bit through localization
@@ -714,7 +725,7 @@ function captcha_captcha($op, $captcha_type = '') {
'#type' => 'textfield',
'#title' => t('Math Question'),
'#title' => t('Math question'),
'#description' => t('Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.'),
- '#field_prefix' => t('@x + @y = ', array('@x' => $x, '@y' => $y)),
+ '#field_prefix' => $query,
Expand Down

0 comments on commit 47c9307

Please sign in to comment.