--- image_captcha.module 2011-02-07 01:45:12.000000000 +0500 +++ image_captcha_new.module 2011-04-23 18:16:13.015625000 +0600 @@ -60,6 +60,12 @@ function image_captcha_menu() { 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); + // callback for re-freshing image + $items['image_captcha_refresh'] = array( + 'page callback' => 'image_captcha_captcha_refresh', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK + ); return $items; } @@ -220,9 +226,12 @@ function image_captcha_captcha($op, $cap // client side caching: subsequent images of the same CAPTCHA session // have the same URL, but should display a different code). $img_src = check_url(url("image_captcha/$captcha_sid/". time())); + $result['image_url'] = $img_src; + $result['form']['captcha_image'] = array( '#type' => 'markup', - '#value' => ''. t('Image CAPTCHA') .'', + '#value' => ''. t('Image CAPTCHA') .'', + '#suffix' => ''.t('Refresh code').'', '#weight' => -2, ); $result['form']['captcha_response'] = array( @@ -244,10 +253,40 @@ function image_captcha_captcha($op, $cap break; } - + drupal_add_js(array( + 'image_captcha' => array( + 'ajaxUrl' => url('image_captcha_refresh/'.$captcha_sid.'/', array('absolute'=>true)), + ), + ), 'setting'); + drupal_add_js(' + function image_captcha_change_image() { + $.get( + Drupal.settings.image_captcha.ajaxUrl + + $("input[@name=\'form_id\']").val() + + "/" + + $("input[@name=\'captcha_token\']").val(), + function(response) { + $("#image-captcha-image").attr("src",response); + $("#edit-captcha-response").val(""); + } + ); + return false; + } + ', 'inline'); return $result; } break; } } + +/** + * menu callback function that refresh the CAPTCHA image + */ +function image_captcha_captcha_refresh($captcha_sid, $form_id, $captcha_token) { + $result = image_captcha_captcha('generate', 'Image', $captcha_sid); + db_query("UPDATE {captcha_sessions} SET solution='%s' WHERE %d=csid AND '%s'=token", $result['solution'], $captcha_sid, $captcha_token); + + echo $result['image_url']; + exit; +}