function ascii_art_captcha_captcha in CAPTCHA Pack 8
Same name and namespace in other branches
- 5 ascii_art_captcha/ascii_art_captcha.module \ascii_art_captcha_captcha()
- 6 ascii_art_captcha/ascii_art_captcha.module \ascii_art_captcha_captcha()
- 7 ascii_art_captcha/ascii_art_captcha.module \ascii_art_captcha_captcha()
Implements hook_captcha().
File
- ascii_art_captcha/
ascii_art_captcha.module, line 64 - Contains general functionality of the module.
Code
function ascii_art_captcha_captcha($op, $captcha_type = '') {
switch ($op) {
case 'list':
return [
'ASCII art CAPTCHA',
];
case 'generate':
if ($captcha_type == "ASCII art CAPTCHA") {
$config = Drupal::config('ascii_art_captcha.settings');
// Get settings.
$allowed_chars = _ascii_art_captcha_get_allowed_characters();
$code_length = (int) $config
->get('ascii_art_captcha_code_length');
// Load font.
$font_name = $config
->get('ascii_art_captcha_font');
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 = [];
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] .= ' ' . Html::escape($cline);
}
}
// Build CAPTCHA array.
$captcha = [];
$captcha['solution'] = $solution;
$style = 'line-height:1.2;';
if ($config
->get('ascii_art_captcha_font_size')) {
$style .= 'font-size:' . $config
->get('ascii_art_captcha_font_size') . 'pt;';
}
$captcha['form']['ascii'] = [
'#type' => 'inline_template',
'#template' => '<pre class="ascii_art_captcha" style="' . $style . '">' . implode('<br />', $ascii_lines) . '</pre>',
'#cache' => [
'max-age' => 0,
],
];
$captcha['form']['captcha_response'] = [
'#type' => 'textfield',
'#title' => t('Enter the code above'),
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE,
'#description' => t('Enter the code depicted in ASCII art style.'),
'#cache' => [
'max-age' => 0,
],
];
\Drupal::service('page_cache_kill_switch')
->trigger();
return $captcha;
}
break;
}
}