ascii_art_captcha.module in CAPTCHA Pack 8
Same filename and directory in other branches
Contains general functionality of the module.
File
ascii_art_captcha/ascii_art_captcha.moduleView source
<?php
/**
* @file
* Contains general functionality of the module.
*/
use Drupal\Component\Utility\Html;
/**
* Function to get a list of available fonts.
*
* @TODO: Rebuild loading fonts with plugins.
*
* @return array
* returns list of available fonts.
*/
function _ascii_art_captcha_available_fonts() {
$available_fonts = [];
$fontsdirectory = drupal_get_path('module', 'ascii_art_captcha') . '/fonts';
$pattern = '/ascii_art_captcha_font_([a-zA-Z0-9]+)\\.[iI][nN][cC]$/';
$directory_files = file_scan_directory($fontsdirectory, $pattern);
foreach ($directory_files as $font) {
$regs = [];
preg_match($pattern, $font->filename, $regs);
$available_fonts[$regs[1]] = $regs[1];
}
asort($available_fonts);
return $available_fonts;
}
/**
* Helper function for generating a code, taking the allowed chars into account.
*/
function _ascii_art_captcha_get_allowed_characters() {
$allowed_chars = [];
$config = Drupal::config('ascii_art_captcha.settings');
$allowed_chars_settings = $config
->get('ascii_art_captcha_allowed_characters');
if ($allowed_chars_settings['upper']) {
$allowed_chars = array_merge($allowed_chars, [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
]);
}
if ($allowed_chars_settings['lower']) {
$allowed_chars = array_merge($allowed_chars, [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
]);
}
if ($allowed_chars_settings['digit']) {
$allowed_chars += array_merge($allowed_chars, [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
]);
}
return $allowed_chars;
}
/**
* Implements hook_captcha().
*/
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;
}
}
Functions
Name | Description |
---|---|
ascii_art_captcha_captcha | Implements hook_captcha(). |
_ascii_art_captcha_available_fonts | Function to get a list of available fonts. |
_ascii_art_captcha_get_allowed_characters | Helper function for generating a code, taking the allowed chars into account. |