You are here

function ascii_art_captcha_captcha in CAPTCHA Pack 5

Same name and namespace in other branches
  1. 8 ascii_art_captcha/ascii_art_captcha.module \ascii_art_captcha_captcha()
  2. 6 ascii_art_captcha/ascii_art_captcha.module \ascii_art_captcha_captcha()
  3. 7 ascii_art_captcha/ascii_art_captcha.module \ascii_art_captcha_captcha()

Implementation of hook_captcha

1 call to ascii_art_captcha_captcha()
ascii_art_captcha_help in ascii_art_captcha/ascii_art_captcha.module
Implementation of hook_help().

File

ascii_art_captcha/ascii_art_captcha.module, line 126

Code

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

        // get settings
        $allowed_chars = _ascii_art_captcha_get_allowed_characters();
        $code_length = (int) variable_get('ascii_art_captcha_code_length', 6);

        // load font
        $font_name = variable_get('ascii_art_captcha_font', 'standard');
        if (!(include_once 'fonts/ascii_art_captcha_font_' . $font_name . '.inc')) {
          return;
        }
        $font = call_user_func('ascii_art_captcha_font_' . $font_name);
        if (!$font) {
          return;
        }

        // build solution and ASCII art array
        $solution = '';
        $ascii_lines = array();
        for ($i = 0; $i < $font['height']; $i++) {
          $ascii_lines[$i] = '';
        }
        for ($i = 0; $i < $code_length; $i++) {
          $character = $allowed_chars[array_rand($allowed_chars)];
          $solution .= $character;
          foreach ($font[$character] as $l => $cline) {
            $ascii_lines[$l] .= ' ' . check_plain($cline);
          }
        }

        // build CAPTCHA array
        $captcha = array();
        $captcha['solution'] = $solution;
        $style = 'line-height:1.1;';
        if (variable_get('ascii_art_captcha_font_size', 0)) {
          $style .= 'font-size:' . variable_get('ascii_art_captcha_font_size', 0) . 'pt;';
        }
        $captcha['form']['ascii'] = array(
          '#type' => 'markup',
          '#value' => '<pre class="ascii_art_captcha" style="' . $style . '">' . implode('<br />', $ascii_lines) . '</pre>',
        );
        $captcha['form']['captcha_response'] = array(
          '#type' => 'textfield',
          '#title' => t('Enter the code above'),
          '#size' => 10,
          '#maxlength' => 10,
          '#required' => TRUE,
          '#description' => t('Enter the code depicted in ASCII art style.'),
        );
        $captcha['preprocess'] = FALSE;
        return $captcha;
      }
      break;
  }
}