View source  
  <?php
function boost_captcha_init() {
  drupal_add_js(drupal_get_path('module', 'boost_captcha') . '/boost_captcha.js', 'module');
  if (module_exists('recaptcha')) {
    $settings = array(
      'recaptcha_public_key' => variable_get('recaptcha_public_key', ''),
    );
    drupal_add_js(array(
      'boost_captcha' => $settings,
    ), 'setting');
  }
}
function boost_captcha_menu() {
  $items = array();
  $items['admin/config/system/boost-captcha'] = array(
    'title' => 'Boost Captcha',
    'description' => 'Configure boost captcha settings form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'boost_captcha_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['ajax/boost-captcha/get-captcha/%'] = array(
    'title' => 'Captcha retrieval',
    'description' => 'Retrieve updated captcha for form.',
    'page callback' => 'boost_captcha_retrieve_captcha',
    'page arguments' => array(
      3,
    ),
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}
function boost_captcha_form_alter(&$form, $form_state, $form_id) {
  static $form_id_regexp;
  $boost_captcha_filter_forms = variable_get('boost_captcha_filter_forms', '');
  
  if (!isset($form_id_regexp)) {
    $form_id_regexp = '/^(' . preg_replace(array(
      '/(\\r\\n?|\\n)/',
      '/\\\\\\*/',
    ), array(
      '|',
      '.*',
    ), preg_quote($boost_captcha_filter_forms, '/')) . ')$/';
  }
  $is_form_excluded = FALSE;
  $filter_by_form = variable_get('boost_captcha_filter_by_form', 0);
  
  if ($boost_captcha_filter_forms) {
    if ($filter_by_form > 0) {
      $form_match = preg_match($form_id_regexp, $form_id);
      
      if ($filter_by_form == 1) {
        $is_form_excluded = $form_match;
      }
      else {
        $is_form_excluded = !$form_match;
      }
    }
  }
  else {
    if ($filter_by_form == 1) {
      $is_form_excluded = FALSE;
    }
    else {
      $is_form_excluded = TRUE;
    }
  }
  
  if (!$is_form_excluded) {
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#prefix'] = '<div class="boost-captcha-process-form">' . $form['#prefix'];
    $form['#suffix'] = $form['#suffix'] . '</div>';
  }
}
function boost_captcha_retrieve_captcha($form_id) {
  
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  
  $form = drupal_retrieve_form($form_id, $form_state);
  drupal_prepare_form($form_id, $form, $form_state);
  drupal_process_form($form_id, $form, $form_state);
  
  $form_captcha = _boost_captcha_helper_get_form_item($form, 'captcha');
  print drupal_render($form_captcha);
  exit;
}
function &_boost_captcha_helper_get_form_item(&$form, $parents = FALSE) {
  if ($parents == '<form>' || !$parents) {
    return $form;
  }
  
  if (is_string($parents)) {
    if (strpos($parents, ']') !== FALSE) {
      $parents = explode('][', $parents);
    }
    else {
      $parents = array(
        $parents,
      );
    }
  }
  
  if (count($parents)) {
    $parent = array_shift($parents);
    if (isset($form[$parent])) {
      return _boost_captcha_helper_get_form_item($form[$parent], $parents);
    }
    else {
      return NULL;
    }
  }
  else {
    return $form;
  }
}
function boost_captcha_settings_form() {
  $form = array();
  $form['boost_captcha_filter_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Form level filtering options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $options = array(
    t('Apply on all forms.'),
    t('Apply on every form except the listed forms.'),
    t('Apply only on the listed forms.'),
  );
  $description = t("Enter one form_id per line. The '*' character is a wildcard.");
  $form['boost_captcha_filter_options']['boost_captcha_filter_by_form'] = array(
    '#type' => 'radios',
    '#title' => t('Apply filters by form'),
    '#options' => $options,
    '#default_value' => variable_get('boost_captcha_filter_by_form', 2),
  );
  $form['boost_captcha_filter_options']['boost_captcha_filter_forms'] = array(
    '#type' => 'textarea',
    '#title' => t('Forms'),
    '#default_value' => variable_get('boost_captcha_filter_forms', ''),
    '#description' => $description,
  );
  $form['boost_captcha_debug'] = array(
    '#type' => 'fieldset',
    '#title' => t('Debug options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['boost_captcha_debug']['boost_captcha_enable_debug'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable debug mode'),
    '#default_value' => variable_get('boost_captcha_enable_debug', '0'),
    '#description' => t('Check this to enable debug information.'),
  );
  return system_settings_form($form);
}
function boost_captcha_settings_form_validate($form, &$form_state) {
}