You are here

function css_captcha_captcha in CAPTCHA Pack 6

Same name and namespace in other branches
  1. 8 css_captcha/css_captcha.module \css_captcha_captcha()
  2. 5 css_captcha/css_captcha.module \css_captcha_captcha()
  3. 7 css_captcha/css_captcha.module \css_captcha_captcha()

Implementation of hook_captcha()

File

css_captcha/css_captcha.module, line 36

Code

function css_captcha_captcha($op, $captcha_type = '', $response = '') {
  switch ($op) {
    case 'list':
      return array(
        "CSS CAPTCHA",
      );
    case 'generate':
      if ($captcha_type == "CSS CAPTCHA") {

        // get settings
        $allowed_chars = _text_captcha_utf8_split(variable_get('css_captcha_allowed_characters', CSS_CAPTCHA_DEFAULT_ALLOWED_CHARACTERS));
        $code_length = (int) variable_get('css_captcha_code_length', 6);

        // build solution and $code_struct
        $solution = '';
        $code_struct = array();
        for ($i = 0; $i < $code_length; $i++) {
          $character = $allowed_chars[array_rand($allowed_chars)];
          $code_struct[$i] = array(
            'content' => $character,
            'size' => 1.25,
          );
          $solution .= $character;
        }

        // generate css scrambling
        while (count($code_struct) > 1) {

          // pick random neighbouring items from $code_struct
          $i = mt_rand(0, count($code_struct) - 2);
          $item_left = $code_struct[$i];
          $item_right = $code_struct[$i + 1];

          // merge
          $merged_size = $item_left['size'] + $item_right['size'] + 0.25;
          if (mt_rand(0, 1)) {

            // normal merge: $item_left first and $item_right second
            $merged_content = "<div style=\"width:{$item_left['size']}em;float:left;\">{$item_left['content']}</div>" . "<div style=\"width:{$item_right['size']}em;float:left;\">{$item_right['content']}</div>";
          }
          else {

            // mirror merge: $item_right first and $item_left second
            $merged_content = "<div style=\"width:{$item_right['size']}em;float:right;\">{$item_right['content']}</div>" . "<div style=\"width:{$item_left['size']}em;float:left;\">{$item_left['content']}</div>";
          }

          // build merged item
          $item_merged = array(
            'content' => "<div style=\"width:{$merged_size}em;height:1.5em;\">{$merged_content}</div>",
            'size' => $merged_size,
          );

          // replace the two items with the merged one
          array_splice($code_struct, $i, 2, array(
            $item_merged,
          ));

          // make array indices contiguous again
          $code_struct = array_values($code_struct);
        }

        // build CAPTCHA
        $captcha = array();
        $captcha['solution'] = $solution;
        $captcha['form']['captcha_response'] = array(
          '#type' => 'textfield',
          '#title' => t('Enter the code above'),
          '#size' => 10,
          '#prefix' => '<div class="css-captcha-code">' . $code_struct[0]['content'] . '</div>',
          '#maxlength' => 10,
          '#required' => TRUE,
          '#description' => t('Enter the code without spaces.'),
        );
        return $captcha;
      }
      break;
  }
}