? captcha_fapi.patch
Index: captcha.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/captcha/captcha.module,v
retrieving revision 1.30
diff -u -r1.30 captcha.module
--- captcha.module	13 Jan 2007 17:48:05 -0000	1.30
+++ captcha.module	19 Feb 2007 20:17:59 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: captcha.module,v 1.30 2007/01/13 17:48:05 wundo Exp $
+// $Id: captcha.module,v 1.32 2007/01/30 19:44:46 wundo Exp $
 
 /**
  * Implementation of hook_help().
@@ -128,10 +128,19 @@
     }
   }
   if ($flag && isset($trigger)) {
+    
+    // The validate hook is the important one, as that's where we'll be checking the captcha
+    $form['#validate'] = array('_captcha_validate' => array()) + $form['#validate'];
+    
+    // The submit hook is really just for cleanup
     $form['#submit'] = array('captcha_submit' => array()) + $form['#submit'];
-    if (!_captcha_validate($_POST['captcha_response'])) {
+    
+    if (!(isset($_SESSION['captcha_correct']) && $_SESSION['captcha_correct'] == TRUE)) {
       if (module_hook($captcha_type, 'captchachallenge')) {
+        // Call the challenge hook to add the captcha field(s)
         call_user_func_array($captcha_type .'_captchachallenge', array(&$form, &$_SESSION['captcha']));
+        
+        // Mutate the description field if necessary
         if(variable_get('captcha_override_module_description', 0)) {
           unset($form['captcha_response']['#description']);
         }
@@ -143,8 +152,10 @@
             $form['captcha_response']['#description'] = variable_get('captcha_description', '');
           }
         }
+        
       }
     }
+    
   }
 }
 
@@ -171,9 +182,6 @@
   if (is_array($captcha_response)) {
     $captcha_response = $captcha_response['#value'];
   }
-  if (trim($captcha_response) == '') {
-    return FALSE;
-  }
 
   global $user;
   $captcha_type = variable_get("captcha_type", NULL);
@@ -191,15 +199,15 @@
  */
 function captcha_captchachallenge(&$form, &$captcha) {
 
-  $x = rand(1,10);
-  $y = rand(1,10);
-
-  $captcha = (string)($x + $y);
+  if (!isset($_POST['captcha_response'])) {
+    $_SESSION['captcha_x'] = rand(1,10);
+    $_SESSION['captcha_y'] = $y = rand(1,10);
+    $captcha = (string)($_SESSION['captcha_x'] + $_SESSION['captcha_y']);
+  }
   $form['captcha_response'] = array (
     '#type' => 'textfield',
-    '#title' => t('Math Question: What is %problem?', array('%problem' => $x .' + '. $y)),
+    '#title' => t('Math Question: What is %problem?', array('%problem' => $_SESSION['captcha_x'] .' + '. $_SESSION['captcha_y'])),
     '#description' => t('Please solve the math problem above and type in the result. e.g. for 1+1, type 2.'),
-    '#weight' => 0,
     '#required' => TRUE,
     '#validate' => array('_captcha_validate' => array()),
   );
@@ -210,7 +218,8 @@
  */
 function captcha_captchavalidate(&$captcha_word, &$correct) {
   $captcha_word = drupal_strtolower($captcha_word);
-  if ($captcha_word == $_SESSION['captcha']) {
+  
+  if (($_SESSION['captcha'] != '') && ($captcha_word == $_SESSION['captcha'])) {
     $correct = TRUE;
   }
   else {
