You are here

word_list_captcha.module in CAPTCHA Pack 5

File

text_captcha/word_list_captcha/word_list_captcha.module
View source
<?php

require_once drupal_get_path('module', 'word_list_captcha') . '/../text_captcha.inc';
define('WORD_LIST_CAPTCHA_WORD_POOL1', 'green red blue yellow black white magenta cyan orange violet purple gold brown pink');
define('WORD_LIST_CAPTCHA_WORD_POOL2', 'bird elephant dog cat crocodile lion fish cow horse sheep frog beetle worm spider bat giraffe lizard goat monkey rabbit chimpanzee');

/**
 * Implementation of hook_help().
 */
function word_list_captcha_help($section) {
  switch ($section) {
    case 'admin/user/captcha/word_list_captcha':
      return '<p>' . t('The unrelated word CAPTCHA consists of a list of closely related words with one non-related word, which the user has to select. To generate this list, two word pools are needed: one for the related words and one for the non-related word.') . '</p>';
  }
}

/**
 * Implementation of hook_menu().
 */
function word_list_captcha_menu($may_cache) {
  $items = array();
  if ($may_cache) {

    // add an administration tab for phrase_captcha
    $items[] = array(
      'path' => 'admin/user/captcha/word_list_captcha',
      'title' => t('Unrelated word'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'word_list_captcha_settings_form',
      ),
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}

/**
 * Function for the settings form
 */
function word_list_captcha_settings_form() {
  $form = array();

  // form element for the number of words in the word list
  $form['word_list_captcha_list_size'] = array(
    '#type' => 'select',
    '#title' => t('Number of words in word list'),
    '#default_value' => variable_get('word_list_captcha_list_size', 5),
    '#options' => array(
      4 => 4,
      5 => 5,
      6 => 6,
      7 => 7,
      8 => 8,
      9 => 9,
      10 => 10,
    ),
  );

  // form elements for the word pools
  _text_captcha_word_pool_form_items($form, 'word_list_captcha_word_pool_1', t('Word pool @num', array(
    '@num' => 1,
  )), 'Enter a bunch of related words (space separated, no punctuation). Make sure they are not related in the same way to the words of the other word pool.', WORD_LIST_CAPTCHA_WORD_POOL1, 2);
  _text_captcha_word_pool_form_items($form, 'word_list_captcha_word_pool_2', t('Word pool @num', array(
    '@num' => 2,
  )), 'Enter a bunch of related words (space separated, no punctuation). Make sure they are not related in the same way to the words of the other word pool.', WORD_LIST_CAPTCHA_WORD_POOL2, 2);

  // add buttons and return
  return system_settings_form($form);
}

/**
 * Validation function for the settings form
 */
function word_list_captcha_settings_form_validate($form_id, $form_values) {
  if ($form_id == 'word_list_captcha_settings_form') {

    // check the number of words in the pools
    $list_size = (int) $form_values['word_list_captcha_list_size'];
    _text_captcha_word_pool_validate('word_list_captcha_word_pool_1', $form_values, $list_size, 0, '');
    _text_captcha_word_pool_validate('word_list_captcha_word_pool_2', $form_values, $list_size, 0, '');
  }
}

/**
 * helper function for generating a word list CAPTCHA
 */
function _word_list_captcha_get_word_list_captcha($list_size) {
  if (mt_rand(0, 1)) {
    $normal_words = _text_captcha_word_pool_get_content('word_list_captcha_word_pool_1', NULL, WORD_LIST_CAPTCHA_WORD_POOL1, TRUE);
    $other_words = _text_captcha_word_pool_get_content('word_list_captcha_word_pool_2', NULL, WORD_LIST_CAPTCHA_WORD_POOL2, TRUE);
  }
  else {
    $normal_words = _text_captcha_word_pool_get_content('word_list_captcha_word_pool_2', NULL, WORD_LIST_CAPTCHA_WORD_POOL2, TRUE);
    $other_words = _text_captcha_word_pool_get_content('word_list_captcha_word_pool_1', NULL, WORD_LIST_CAPTCHA_WORD_POOL1, TRUE);
  }
  shuffle($normal_words);
  $word_list = array_slice($normal_words, 0, $list_size - 1);
  $wrong_word = $other_words[array_rand($other_words)];
  $word_list[] = $wrong_word;
  shuffle($word_list);
  return array(
    $word_list,
    $wrong_word,
  );
}

/**
 * Implementation of hook_captcha().
 */
function word_list_captcha_captcha($op, $captcha_type = '', $post_data = array()) {
  switch ($op) {
    case 'list':
      return array(
        'Pick the unrelated word',
      );
      break;
    case 'generate':
      if ($captcha_type == 'Pick the unrelated word') {
        $list_size = (int) variable_get('word_list_captcha_list_size', 5);
        list($word_list, $wrong_word) = _word_list_captcha_get_word_list_captcha($list_size);

        // build options list
        $options = array();
        foreach ($word_list as $word) {
          $options[$word] = $word;
        }
        $captcha = array();
        $captcha['solution'] = $wrong_word;
        $captcha['form']['captcha_response'] = array(
          '#type' => 'radios',
          '#title' => t('Which word does not belong to the list?'),
          '#options' => $options,
          // extra class needed for additional CSS'ing of the options
          '#attributes' => array(
            'class' => 'text-captcha-word-list-radios',
          ),
          // The following entry '#DANGEROUS_SKIP_CHECK' is needed to prevent
          // that Drupal checks during validation phase if a submitted option
          // is in the list of possible options. (see includes/form.inc)
          // The options are randomly generated on each call and consequently
          // almost never the same during the generate phase and the validation
          // phase.
          '#DANGEROUS_SKIP_CHECK' => TRUE,
          //
          '#required' => TRUE,
        );

        // additional text CAPTCHA CSS rules
        drupal_add_css(drupal_get_path('module', 'word_list_captcha') . '/../text_captcha.css');
        return $captcha;
      }
      break;
  }
}

Functions

Namesort descending Description
word_list_captcha_captcha Implementation of hook_captcha().
word_list_captcha_help Implementation of hook_help().
word_list_captcha_menu Implementation of hook_menu().
word_list_captcha_settings_form Function for the settings form
word_list_captcha_settings_form_validate Validation function for the settings form
_word_list_captcha_get_word_list_captcha helper function for generating a word list CAPTCHA

Constants