<?php
// $Id: hiragana_captcha.user.inc,v 1.1 2008/11/29 0829 Exp $
/**
* function for generating the phrase of Q or A from an array of words.
*/
function _hiragana_captcha_generate_phrase($words, $which) {
$phrase = array();
foreach ($words as $key => $value) {
foreach ($value as $class => $word) {
if ($class == $which) {
$phrase[] = $word;
}
}
}
return $phrase;
}
/**
* function for generating an array of words.
*/
function _hiragana_captcha_generate_words($quantity) {
$length = variable_get('hiragana_captcha_length_min', 3);
$words = array();
for ($w = 0; $w < $quantity; ++$w) {
$words[] = _hiragana_captcha_generate_word(variable_get('hiragana_captcha_length_min', 3), variable_get('hiragana_captcha_length_max', 3));
}
return $words;
}
/**
* function for generating a random nonsense word of a given number of characters.
*/
function _hiragana_captcha_generate_word($min, $max) {
$katakana = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン';
$hiragana = 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん';
$type = variable_get('hiragana_captcha_kanatype', 0);
$switch = ($type == 2) ? mt_rand(0, 1) : $type;
$kana = ($switch == 1) ? $katakana : $hiragana;
$length = mt_rand($min, $max);
$word = array();
for ($i = 0; $i < $length; ++$i) {
$n = mt_rand(0, 45);
$word['question'] .= mb_substr($kana, $n, 1);
$word['answer'] .= mb_substr($hiragana, $n, 1);
}
return $word;
}
/**
* function that returns a textual represention of an ordinal.
*/
function _hiragana_captcha_ordinal($n) {
$ordinalmap = array(
1 => t('first'),
2 => t('second'),
3 => t('third'),
4 => t('fourth'),
5 => t('fifth'),
6 => t('sixth'),
7 => t('seventh'),
8 => t('eighth'),
9 => t('ninth'),
10 => t('tenth')
);
if (array_key_exists($n, $ordinalmap)) {
return $ordinalmap[$n];
}
else {
return "{$n}th";
}
}
// $Id: hiragana_captcha.user.inc,v 1.1 2008/11/29 0829 Exp $
/**
* function for generating the phrase of Q or A from an array of words.
*/
function _hiragana_captcha_generate_phrase($words, $which) {
$phrase = array();
foreach ($words as $key => $value) {
foreach ($value as $class => $word) {
if ($class == $which) {
$phrase[] = $word;
}
}
}
return $phrase;
}
/**
* function for generating an array of words.
*/
function _hiragana_captcha_generate_words($quantity) {
$length = variable_get('hiragana_captcha_length_min', 3);
$words = array();
for ($w = 0; $w < $quantity; ++$w) {
$words[] = _hiragana_captcha_generate_word(variable_get('hiragana_captcha_length_min', 3), variable_get('hiragana_captcha_length_max', 3));
}
return $words;
}
/**
* function for generating a random nonsense word of a given number of characters.
*/
function _hiragana_captcha_generate_word($min, $max) {
$katakana = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン';
$hiragana = 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん';
$type = variable_get('hiragana_captcha_kanatype', 0);
$switch = ($type == 2) ? mt_rand(0, 1) : $type;
$kana = ($switch == 1) ? $katakana : $hiragana;
$length = mt_rand($min, $max);
$word = array();
for ($i = 0; $i < $length; ++$i) {
$n = mt_rand(0, 45);
$word['question'] .= mb_substr($kana, $n, 1);
$word['answer'] .= mb_substr($hiragana, $n, 1);
}
return $word;
}
/**
* function that returns a textual represention of an ordinal.
*/
function _hiragana_captcha_ordinal($n) {
$ordinalmap = array(
1 => t('first'),
2 => t('second'),
3 => t('third'),
4 => t('fourth'),
5 => t('fifth'),
6 => t('sixth'),
7 => t('seventh'),
8 => t('eighth'),
9 => t('ninth'),
10 => t('tenth')
);
if (array_key_exists($n, $ordinalmap)) {
return $ordinalmap[$n];
}
else {
return "{$n}th";
}
}

