Drupal patch monkey, Captcha for drupal.

Body

Well, I backported the captcha API patch for the current 4.6 version of the drupal captcha module.

Let me state it, I hate captchas, So when I enable a captcha module be sure that I'll do it right!

You'll be asked to do a simple mathematical calculation and fill in the result not to read an ambiguous image.

Now all people can post comments without me approving them first.

As usual, Here's the patch:

--- captcha.module.orig 2005-07-15 16:04:05.000000000 +0300
+++ captcha.module 2006-03-02 16:45:29.000000000 +0200
@@ -48,52 +48,122 @@

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.'));
+  $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 meet a challenge during user registration.');

+  $output .= form_checkbox(t('Check during anonymous comments'), 'captcha_comment_anonymous', 'true', _captcha_istrue("captcha_comment_anonymous"), 'If enabled, anonymous users will be asked to meet a challenge while posting .');

+  $output .= form_checkbox(t('Check during registered user comments'), 'captcha_comment_registered', 'true', _captcha_istrue("captcha_comment_registered"), 'If enabled, registered users will be asked to meet a challenge while posting .');

+  // Select captcha method.
+  $modules = module_implements('captcha');
+  $select = array();
+  foreach ($modules as $m) {
+    $description = module_invoke($m, 'captcha', 'description');
+    if ($description) {
+      $desc[$m] = $description;
+    }
+  }
+  if (count($desc) > 1) {
+    $output .= form_radios(t('Captcha method'), 'captcha_method', variable_get('captcha_method', 'captcha'), $desc, NULL, TRUE);
+  }
   else {
+    $output .= form_hidden('captcha_method', 'captcha');
+  }

-    $fonts_path = variable_get("captcha_fonts_path", "");
-
-    //check for TTF support
-    if (!function_exists(imagettftext))
-      form_set_error('No TTF support', t('Your image library does not seem to have TrueType font support. Captcha will work, but will use the default inbuilt font.'));
-
-    else {
-
-      //check for valid font path
-      if ($fonts_path!="" && !is_dir($fonts_path))
-        form_set_error('Invalid font path', t('The current font path is invalid. The default font will be used.'));
+  // Settings for each captcha method.
+  foreach ($modules as $m) {
+    $group = module_invoke($m, 'captcha', 'settings');
+    if ($group) {
+      $output .= form_group($desc[$m], $group);
     }
-
   }

-  $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.');
+  return $output;
+}

-  $output .= form_checkbox(t('Check during anonymous comments'), 'captcha_comment_anonymous', 'true', _captcha_istrue("captcha_comment_anonymous"), 'If enabled, anonymous users will be asked to recognize an image while posting .');
+/**
+* Captcha API: Form items for a captcha test.
+*
+* @return
+*   Form items to be presented to the user as a captcha test.
+*/
+function captcha_form()
+{
+  $module = variable_get('captcha_method', 'captcha');
+  $output = module_invoke($module, 'captcha', 'form');
+  if (!$output) {
+    // If the configured captcha implementation is somehow unavailable,
+    // fall back on the default implemented by this module.
+    variable_set('captcha_method', 'captcha');
+    $output = captcha_captcha('form');
+  }
+   return $output;
+ }

-  $output .= form_checkbox(t('Check during registered user comments'), 'captcha_comment_registered', 'true', _captcha_istrue("captcha_comment_registered"), 'If enabled, registered users will be asked to recognize an image while posting .');
+/**
+* Captcha API: Tests the entered captcha response.
+*
+* @param $edit
+*   Input form data including the captcha_form() items.
+*/
+function captcha_test($edit) {
+  $module = variable_get('captcha_method', 'captcha');
+  return module_invoke($module, 'captcha', 'test', $edit);
+}

-  $output .= form_textfield(t('TrueType Fonts Path'), 'captcha_fonts_path', $fonts_path, 30, 255, 'Location of the directory where the Truetype (.ttf) fonts are stored. If you do not provide any fonts, the module will use the default font for text.');

-  if (isset($fonts_path)) {
-    $imagefontinfo .= t('Number of fonts found: ').count(_captcha_font_list());
-  }
+/**
+* Implementation of hook_captcha().
+*/
+function captcha_captcha($op, $edit = NULL) {
+  switch ($op) {

-  $gdinfo = gd_info();
-  $imagefontinfo .= '<br />'.t('GD Version: ').$gdinfo["GD Version"];
-  $imagefontinfo .= '<br />'.t(' FreeType Support: ');
-  $imagefontinfo .= ($gdinfo["FreeType Support"]==true) ? 'True' : 'False';
-  $imagefontinfo .= '<br />';
+    case 'description':
+      // Brief description of this method; used in the catpcha settings form.
+      // In third-party captcha modules, this could test whether the particular
+      // captcha method is supported in the current configuration.
+      // The captcha module itself implements the default method,
+      // which is always available (though not always supported).
+      return t('Image recognition');
+
+    case 'settings':
+      // Settings for this captcha method.
+
+      //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.'));
+      }
+      else {
+        $fonts_path = variable_get("captcha_fonts_path", "");
+        //check for TTF support
+        if (!function_exists(imagettftext) || !function_exists(imagettfbox)) {
+          form_set_error('No TTF support', t('Your image library does not seem to have TrueType font support. Captcha will work, but will use the default inbuilt font.'));
+        }
+        else {
+          //check for valid font path
+          if ($fonts_path!="" && !is_dir($fonts_path))
+            form_set_error('Invalid font path', t('The current font path is invalid. The default font will be used.'));
+        }
+      }
+      return form_textfield(t('TrueType Fonts Path'), 'captcha_fonts_path', $fonts_path, 30, 255, 'Location of the directory where the Truetype (.ttf) fonts are stored. If you do not provide any fonts, the module will use the default font for text.'); 

-  $output .= form_item("Image and font information", '', $imagefontinfo);
+    case 'form':
+      // Return form items for this captcha method.
+      $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);
+      return $output;
+
+    case 'test':
+      // Return TRUE or FALSE according to whether the supplied form values
+      // satisfy the captcha test.
+      return ($edit != NULL) && (strtolower($edit['captchaword']) == strtolower($_SESSION['captcha']));

-  return $output;
+  }
}

+
/**
-* Implementation of hook_menu(), for adding form elements & validation.
+* Implementation of hook_user(), for adding form elements & validation.
*/
function captcha_user($type, &$edit, &$newuser, $category = NULL) {

@@ -105,16 +175,11 @@
   switch ($type) {
     case '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, t('Please type in the letters/numbers that are shown in the image above.'), NULL, TRUE);
-
-    return array(array('title' => t('Verify Registration'), 'data'=>$output));
-
+return array(array('title' => t('Verify Registration'), 'data' => captcha_form()));
     break;
     case '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);
     }
@@ -144,7 +209,7 @@
       // 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';
           //reset captcha variable to prevent session highjacking vulnerability #26741  
           $_SESSION['captcha']='';
@@ -162,9 +227,7 @@
     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);
-        $form_html = form_group(t('Verify comment authorship'), $output);
+$form_html = form_group(t('Verify comment authorship'), captcha_form());
       }
       return $form_html;
   }

Add new comment

The content of this field is kept private and will not be shown publicly.