--- captcha.module	Wed Jan 26 10:00:44 2005
+++ captcha.module	Wed Apr 13 15:16:06 2005
@@ -41,8 +41,8 @@ function captcha_menu($may_cache) {
 function captcha_settings() {
 
   //check for GD
-  if (!function_exists(imagecreate)) 
-    form_set_error('No GD', t('Image library not available. Captcha needs the GD library extension to be installed. Please install GD.'));
+  if (!function_exists('imagecreatetruecolor'))
+    form_set_error('No GD', t('Image library not available. Captcha will use the arithmetic challenge if the GD library extension is not installed. Please install GD.'));
 
   else {
   
@@ -60,6 +60,8 @@ function captcha_settings() {
     }
   
   }
+
+  $output .= form_radios(t('Captcha challenge'), 'captcha_challenge', variable_get('captcha_challenge', 0), array(0 => t('Image recognition challenge'), 1 => t('Arithmetic challenge'))); 
     
   $output .= form_checkbox(t('Check during user registration'), 'captcha_user_register', 'true', _captcha_istrue("captcha_user_register", "true"), 'If enabled, users will be asked to recognize an image during user registration.');
   
@@ -85,15 +87,13 @@ function captcha_user($type, &$edit, &$n
     case t("register"):
     // Add two items to the resigtration form.
     
-    $output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
-    $output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, 'Please type in the letters/numbers that are shown in the image above.', NULL, TRUE);
-
+    $output = captcha_form();
     return array(array('title' => t('Verify Registration'), 'data'=>$output));
 
     break;
     case t("validate"):
     // The user has filled out the form and checked the "accept" box.
-    if (strtolower($edit['captchaword']) == strtolower($_SESSION['captcha'])) {
+    if (captcha_test($edit)) {
       // on success return the values you want to store
       return array("captcha_correct" => 1);
     }
@@ -123,7 +123,7 @@ function captcha_comment($op,$edit) {
       // this implementation basically sets a flag when you've successfully validated a captcha;
       // any successive comment inserted uses and invalidates the set flag.
       if ($_SESSION['captcha_comment_correct']!='ok') {
-        if (strtolower($edit['captchaword']) != '' && strtolower($edit['captchaword']) == strtolower($_SESSION['captcha'])) {
+        if (captcha_test($edit)) {
           $_SESSION['captcha_comment_correct'] = 'ok';
         }
         else {
@@ -139,12 +139,99 @@ function captcha_comment($op,$edit) {
     case 'form':
       $form_html = "";
       if ($_SESSION['captcha_comment_correct']!='ok') {
-        $output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
-        $output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, 'Please type in the letters/numbers that are shown in the image above.', NULL, TRUE);
+        $output .= captcha_form();
         $form_html = form_group(t('Verify comment authorship'), $output);
       }
       return $form_html;
   }
+} 
+
+/**
+* Returns a random string for use in a captcha
+*/
+function captcha_form()
+{
+  if (!variable_get('captcha_challenge', 0) && function_exists('imagecreatetruecolor')) {
+    $output = form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
+    $output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, 'Please type in the letters/numbers that are shown in the image above.', NULL, TRUE);
+  }
+  else {
+    $qa = _captcha_qa(); 
+    $_SESSION['captcha'] = $qa['answer'];
+    $output = form_item(t('Question'), $qa['question']); 
+    $output .= form_textfield(t('Answer'), 'captchaword', NULL, 15, 15, t('Please answer the question above. Use digits, not words.'), NULL, TRUE); 
+  }
+  return $output;
+}
+
+/**
+* Tests the entered captcha response.
+*/
+function captcha_test($edit) {
+  $response = strtolower(trim($edit['captchaword']));
+  return ($response != '' && $response == $_SESSION['captcha']);
+}
+
+/** 
+* Returns a question and answer. 
+*/ 
+function _captcha_qa() {
+  $numbers = array( 
+    1 => t('one'), 
+    2 => t('two'), 
+    3 => t('three'), 
+    4 => t('four'), 
+    5 => t('five'), 
+    6 => t('six'), 
+    7 => t('seven'), 
+    8 => t('eight'), 
+    9 => t('nine'), 
+    10 => t('ten'), 
+    11 => t('eleven'), 
+    12 => t('twelve') 
+  ); 
+
+  $operators = array( 
+    0 => array(name => t('plus'), symbol => '+'), 
+    1 => array(name => t('minus'), symbol => '-'), 
+    2 => array(name => t('multiplied by'), symbol => '*'), 
+    3 => array(name => t('divided by'), symbol => '/') 
+  ); 
+
+  $operator = rand(0,count($operators)-1); 
+
+  // choose numbers based on operator to keep the math simple 
+  switch($operator) { 
+    case 0: 
+      // do addition logic 
+      $firstNumber = rand(1,count($numbers)-1); 
+      $secondNumber = rand(1,count($numbers)-1); 
+      $result = $firstNumber + $secondNumber; 
+      break; 
+    case 1: 
+      // do subtraction logic 
+      $firstNumber = rand(6,count($numbers)-1); 
+      $secondNumber = rand(1,5); 
+      $result = $firstNumber - $secondNumber; 
+      break; 
+    case 2: 
+      // do multiplication logic 
+      $firstNumber = rand(1,8); 
+      $secondNumber = rand(1,8); 
+      $result = $firstNumber * $secondNumber; 
+      break; 
+    case 3: 
+      // do division logic 
+      $secondNumber = rand(2,4); 
+      $firstNumber = $secondNumber * rand(1,4); 
+      $result = $firstNumber / $secondNumber; 
+      break; 
+  } 
+
+  $equation = $numbers[$firstNumber].' '.$operators[$operator]['name'].' '.$numbers[$secondNumber]; 
+  $output = array('question' => t('What is ') . $equation . '?', 'answer' => $result); 
+
+  return $output; 
 } 
 
 /**
