View source
<?php
function _text_captcha_whitespace_explode($string) {
return preg_split('/\\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
}
function _text_captcha_utf8_split($str) {
$characters = array();
$len = strlen($str);
for ($i = 0; $i < $len;) {
$chr = ord($str[$i]);
if (($chr & 0x80) == 0x0) {
$width = 1;
}
else {
if (($chr & 0xe0) == 0xc0) {
$width = 2;
}
elseif (($chr & 0xf0) == 0xe0) {
$width = 3;
}
elseif (($chr & 0xf8) == 0xf0) {
$width = 4;
}
else {
watchdog('captcha', t('Encountered an illegal byte while splitting an utf8 string in characters.'), WATCHDOG_ERROR);
return $characters;
}
}
$characters[] = substr($str, $i, $width);
$i += $width;
}
return $characters;
}
function _text_captcha_word_pool_get_content($name_base, $lang_code, $default_value, $explode = FALSE) {
if (module_exists('locale')) {
if (!$lang_code) {
global $locale;
$lang_code = $locale;
}
$content = variable_get("{$name_base}_{$lang_code}", t($default_value));
}
else {
$content = variable_get($name_base, t($default_value));
}
if ($explode) {
$content = _text_captcha_whitespace_explode($content);
}
return $content;
}
function _text_captcha_word_pool_form_items(&$form, $name_base, $title, $description, $default_value, $rows = 3) {
if (module_exists('locale')) {
global $locale;
$languages = locale_supported_languages();
$form[$name_base] = array(
'#type' => 'fieldset',
'#title' => t($title),
'#description' => t($description),
'#collapsible' => TRUE,
);
foreach ($languages['name'] as $lang_code => $lang_name) {
$form[$name_base]["{$name_base}_{$lang_code}"] = array(
'#type' => 'textarea',
'#title' => t('For language %lang_name (code %lang_code)', array(
'%lang_name' => $lang_name,
'%lang_code' => $lang_code,
)),
'#default_value' => _text_captcha_word_pool_get_content($name_base, $lang_code, $default_value),
'#rows' => $rows,
);
}
}
else {
$form[$name_base] = array(
'#type' => 'textarea',
'#title' => t($title),
'#description' => t($description),
'#default_value' => _text_captcha_word_pool_get_content($name_base, NULL, $default_value),
'#rows' => $rows,
);
}
}
function _text_captcha_word_pool_validate_word_length($name, $words, $minimum_length, $too_short_message) {
$too_short = array();
foreach ($words as $word) {
if (count(_text_captcha_utf8_split($word)) < $minimum_length) {
$too_short[] = $word;
}
}
if (count($too_short)) {
form_set_error($name, t($too_short_message, array(
'@minimum_length' => $minimum_length,
'@words' => implode(', ', $too_short),
)));
}
}
function _text_captcha_word_pool_validate($name_base, $form_values, $minimum_count, $minimum_length = NULL, $too_short_message = '') {
if (module_exists('locale')) {
global $locale;
$languages = locale_supported_languages();
foreach ($languages['name'] as $lang_code => $lang_name) {
$words = _text_captcha_whitespace_explode($form_values["{$name_base}_{$lang_code}"]);
if (count($words) < $minimum_count) {
form_set_error("{$name_base}_{$lang_code}", t('You should provide at least @num words', array(
'@num' => $minimum_count,
)));
}
if ($minimum_length) {
_text_captcha_word_pool_validate_word_length("{$name_base}_{$lang_code}", $words, $minimum_length, $too_short_message);
}
}
}
else {
$words = _text_captcha_whitespace_explode($form_values[$name_base]);
if (count($words) < $minimum_count) {
form_set_error($name_base, t('You should provide at least @num words', array(
'@num' => $minimum_count,
)));
}
if ($minimum_length) {
_text_captcha_word_pool_validate_word_length($name_base, $words, $minimum_length, $too_short_message);
}
}
}