You are here

boost_captcha.module in Boost Captcha 7

Same filename and directory in other branches
  1. 6 boost_captcha.module

File

boost_captcha.module
View source
<?php

/* @file
 * The boost_captcha module file
 */

/**
 * Implements hook_init().
 */
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');
  }
}

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Implements hook_form_alter().
 */
function boost_captcha_form_alter(&$form, $form_state, $form_id) {
  static $form_id_regexp;
  $boost_captcha_filter_forms = variable_get('boost_captcha_filter_forms', '');

  // Clean up the form filter and save statically as this maybe called for
  // multiple 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);

  // Check form level filtering if some patterns are set
  if ($boost_captcha_filter_forms) {
    if ($filter_by_form > 0) {
      $form_match = preg_match($form_id_regexp, $form_id);

      // If $filter_by_form is 1 then listed forms are excluded from any filtering
      // and if 2 then filtering is applied only on listed forms.
      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 form is not excluded then mark the form as one where boost captcha logic
  // has to be applied
  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>';
  }
}

/**
 * Retrieve a captcha to be sent via AJAX to a form in a cached page.
 *
 * Exits after call, meant to be called via AJAX.
 *
 * @param $form_id
 *   The id of the form in which the captcha is being rebuit.
 * @return
 *   HTML corresponding to the captcha.
 */
function boost_captcha_retrieve_captcha($form_id) {

  // Retrieve the form
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );

  // print ($form_id);
  $form = drupal_retrieve_form($form_id, $form_state);
  drupal_prepare_form($form_id, $form, $form_state);
  drupal_process_form($form_id, $form, $form_state);

  // print_r($form);
  $form_captcha = _boost_captcha_helper_get_form_item($form, 'captcha');
  print drupal_render($form_captcha);
  exit;
}

/**
 * Get the form item from a form array using the item id.
 *
 * Given a form by reference and an array of parents, return the corresponding
 * form item by reference. If $parents is FALSE or '<form>', the entire $form
 * array will be returned. If it doesn't exist, return NULL.
 * This function is copied over from ahah_helper module.
 *
 * @param $form
 *   A form.
 * @param $parents
 *   An array of parrents by which the wanted form item is identified, or
 *   imploded with ']['.
 * @return
 *   The requested form item.
 */
function &_boost_captcha_helper_get_form_item(&$form, $parents = FALSE) {
  if ($parents == '<form>' || !$parents) {
    return $form;
  }

  // Allow $parents to be either an array of the element's parents or the name
  // of an element.
  if (is_string($parents)) {
    if (strpos($parents, ']') !== FALSE) {
      $parents = explode('][', $parents);
    }
    else {
      $parents = array(
        $parents,
      );
    }
  }

  // Recursively seek the form element.
  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;
  }
}

/**
 * Boost captcha admin settings 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);
}

/**
 * Validation function for boost_captcha_settings_form.
 */
function boost_captcha_settings_form_validate($form, &$form_state) {
}

Functions

Namesort descending Description
boost_captcha_form_alter Implements hook_form_alter().
boost_captcha_init Implements hook_init().
boost_captcha_menu Implements hook_menu().
boost_captcha_retrieve_captcha Retrieve a captcha to be sent via AJAX to a form in a cached page.
boost_captcha_settings_form Boost captcha admin settings form
boost_captcha_settings_form_validate Validation function for boost_captcha_settings_form.
_boost_captcha_helper_get_form_item Get the form item from a form array using the item id.