You are here

function lost_character_captcha_captcha in CAPTCHA Pack 5

Same name and namespace in other branches
  1. 8 text_captcha/modules/lost_character_captcha/lost_character_captcha.module \lost_character_captcha_captcha()
  2. 6 text_captcha/lost_character_captcha/lost_character_captcha.module \lost_character_captcha_captcha()
  3. 7 text_captcha/lost_character_captcha/lost_character_captcha.module \lost_character_captcha_captcha()

Implementation of hook_captcha().

File

text_captcha/lost_character_captcha/lost_character_captcha.module, line 100

Code

function lost_character_captcha_captcha($op, $captcha_type = '', $response = '') {
  switch ($op) {
    case 'list':
      return array(
        "Lost characters",
      );
    case 'generate':
      if ($captcha_type == "Lost characters") {

        // get the word pool
        $words = _text_captcha_word_pool_get_content('lost_character_captcha_word_pool', NULL, LOST_CHARACTER_CAPTCHA_DEFAULT_WORD_POOL, TRUE);

        // pick a random word
        $word = $words[array_rand($words)];

        // split in characters
        $characters = _text_captcha_utf8_split($word);

        // lose characters
        $lost = array();
        $lose_quantity = variable_get('lost_character_captcha_quantity', 1);
        for ($i = 0; $i < $lose_quantity; $i++) {

          // pick a random character
          $n = array_rand($characters);
          while ($characters[$n] == LOST_CHARACTER_CAPTCHA_HINTER) {
            $n = array_rand($characters);
          }

          // save it for building the solution
          $lost[] = $characters[$n];

          // and lose it in the given word
          if (variable_get('lost_character_captcha_enable_hint', TRUE)) {
            $characters[$n] = LOST_CHARACTER_CAPTCHA_HINTER;
          }
          else {
            unset($characters[$n]);
          }
        }

        // build the CAPTCHA
        sort($lost);
        $given_word = implode('', $characters);
        $solution = implode('', $lost);
        if ($lose_quantity == 1) {
          $title = t('Enter the missing character from the following word');
        }
        else {
          $title = t('Enter the @num missing characters from the following word', array(
            '@num' => $lose_quantity,
          ));
        }

        //
        $captcha = array();
        $captcha['solution'] = $solution;
        $captcha['form']['captcha_response'] = array(
          '#type' => 'textfield',
          '#title' => $title,
          '#field_prefix' => "{$given_word}: ",
          '#size' => 3,
          '#maxlength' => 3,
          '#required' => TRUE,
        );
        $captcha['preprocess'] = TRUE;
        return $captcha;
      }
      break;
    case 'preprocess':
      if ($captcha_type == "Lost characters") {

        // remove white spaces
        $parts = _text_captcha_whitespace_explode($response);
        $response = implode('', $parts);

        // split in utf8 characters, sort and rejoin
        $characters = _text_captcha_utf8_split($response);
        sort($characters);
        $response = implode('', $characters);
        return $response;
      }
      break;
  }
}