You are here

captcha_questions.module in Captcha Questions 7

Same filename and directory in other branches
  1. 8 captcha_questions.module

Module file for the Captcha questions module

The module hooks into forms and adds a configurable question field and a custom validate function returning an error if the answer is wrong.

File

captcha_questions.module
View source
<?php

/**
 * @file
 * Module file for the Captcha questions module
 *
 * The module hooks into forms and adds a configurable question field and a
 * custom validate function returning an error if the answer is wrong.
 */

/**
 * Implements hook_help().
 */
function captcha_questions_help($path, $arg) {
  if ($path == 'admin/help#captcha_questions') {
    return t('Captcha questions allows the site administrator to add a question to selected forms');
  }
}

/**
 * Implements hook_menu().
 */
function captcha_questions_menu() {
  $items = array();
  $items['admin/config/people/captcha_questions'] = array(
    'title' => 'Captcha questions',
    'description' => 'Administer Captcha questions.',
    'file' => 'captcha_questions.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'captcha_questions_admin_settings',
    ),
    'access arguments' => array(
      'administer captcha questions settings',
    ),
  );
  $items['admin/config/people/captcha_questions/settings'] = array(
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function captcha_questions_permission() {
  return array(
    'administer captcha questions settings' => array(
      'title' => t('Administer captcha questions settings'),
    ),
  );
}

/**
 * Implements hook_form_alter().
 */
function captcha_questions_form_alter(&$form, &$form_state, $form_id) {

  // Only show captcha questions if user is anonymous.
  if (user_is_anonymous()) {

    // Fetching variables for form ids, question and description.
    $captcha_questions_form_ids = variable_get('captcha_questions_form_ids', array());
    $question = variable_get('captcha_questions_question', NULL);
    $description = variable_get('captcha_questions_description', t('Please answer the question.'));
    foreach ($captcha_questions_form_ids as $key => $value) {
      if ($value === $form_id) {

        // If we're on a multi page form, find current page.
        $form_page_count = isset($form['details']['page_num']['#value']) ? $form['details']['page_num']['#value'] : 1;

        // Set field to hidden unless on the first page.
        $form['captcha_questions_answer_given'] = array(
          '#type' => $form_page_count == 1 ? 'textfield' : 'hidden',
          '#description' => t("@description", array(
            '@description' => $description,
          )),
          '#title' => check_plain($question),
          '#size' => 60,
          '#required' => TRUE,
        );
        $form['#validate'][] = 'captcha_questions_form_validate';
      }
    }
    return $form;
  }
}

/**
 * Implements hook_form_validate().
 */
function captcha_questions_form_validate($form, &$form_state) {
  global $user;
  $ip = $user->hostname;
  $form_id = $form_state['values']['form_id'];
  $question_asked = variable_get('captcha_questions_question', NULL);
  $answers = variable_get('captcha_questions_answers', array());
  $answer_given = $form_state['values']['captcha_questions_answer_given'];

  // Comparison is done in lowercase, ensure answers are lowercase.
  $answers = array_map('drupal_strtolower', $answers);
  $answer_given = drupal_strtolower($answer_given);

  // Check answer.
  if (in_array($answer_given, $answers) == FALSE) {

    // Log to watchdog if enabled.
    if (variable_get('captcha_questions_watchdog', 0)) {
      $variables = array(
        '!form_id' => $form_id,
        '%answer_given' => $form_state['values']['captcha_questions_answer_given'],
      );
      watchdog('Captcha questions', 'Blocked submission of form with form_id !form_id. Answer given was %answer_given', $variables);
    }

    // Log to dblog if enabled and module exists and enabled.
    $dblog_enabled = variable_get('captcha_questions_dblog', 0);
    $dblog_module_exists = module_exists('captcha_questions_dblog') ? TRUE : FALSE;
    if ($dblog_enabled && $dblog_module_exists) {
      db_insert('captcha_questions_dblog')
        ->fields(array(
        'timestamp' => REQUEST_TIME,
        'ip' => $ip,
        'form_id' => $form_id,
        'question_asked' => check_plain($question_asked),
        'answer_given' => check_plain($answer_given),
        'answer_correct' => implode(",", array_map('check_plain', $answers)),
      ))
        ->execute();
    }

    // Display error.
    form_set_error('captcha_questions_answer_given', t('Invalid answer'));
  }
}

Functions