name = "Hiragana CAPTCHA" description = "Provides a simple text based hiragana CAPTCHA." package = "Spam control" dependencies = captcha version = "5.x-3.0"
/**
* Implementation of hook_help().
*/ function hiragana_captcha_help($section){ switch($section){ case'admin/user/captcha/hiragana_captcha': return'<p>'. t('The hiragana CAPTCHA ask for the n<sup>th</sup> word of a predetermined phrase like text CAPTCHA.') .'</p>'; } }
/**
* Administration form.
*/ function hiragana_captcha_settings_form(){ $form = array(); $form['hiragana_captcha_words'] = array( '#type' => 'fieldset', '#title' => t('Words settings'), '#description' => t('Input the generated word length and number of words in the phrase for hiragana CAPTCHA.'), ); $form['hiragana_captcha_words']['hiragana_captcha_length_min'] = array( '#type' => 'textfield', '#title' => t('Minimum number of characters'), '#default_value' => variable_get('hiragana_captcha_length_min', 3), '#size' => 2, '#maxlength' => 2, ); $form['hiragana_captcha_words']['hiragana_captcha_length_max'] = array( '#type' => 'textfield', '#title' => t('Maximum number of characters'), '#default_value' => variable_get('hiragana_captcha_length_max', 3), '#size' => 2, '#maxlength' => 2, ); $form['hiragana_captcha_words']['hiragana_captcha_quantity'] = array( '#type' => 'textfield', '#title' => t('Number of words in the phrase'), '#default_value' => variable_get('hiragana_captcha_quantity', 5), '#size' => 2, '#maxlength' => 2, ); returnsystem_settings_form($form); }
/**
* Validate function of the administration form.
*/ function hiragana_captcha_settings_form_validate($form_id, $form_values){ if($form_id == 'hiragana_captcha_settings_form'){ if($form_values['hiragana_captcha_length_min'] < 1 || $form_values['hiragana_captcha_length_max'] < 1){ form_set_error('hiragana_captcha_length', t('Minimum and maximum number of characters should be more than \'1\'.')); } elseif($form_values['hiragana_captcha_length_min'] > $form_values['hiragana_captcha_length_max']){ form_set_error('hiragana_captcha_length', t('Maximum number of characters should be more than minimum number of characters.')); } if($form_values['hiragana_captcha_quantity'] < 4 || $form_values['hiragana_captcha_quantity'] > 10){ form_set_error('hiragana_captcha_quantity', t('Number of words in the phrase should be between 4 and 10.')); } } }
/**
* function for generating a random nonsense word of a given number of characters.
*/ function _hiragana_captcha_generate_word($min, $max){ $kana = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん"; $end = mb_strlen($kana) - 1; $length = mt_rand($min, $max); $word = ''; for($i = 0; $i < $length; ++$i){ $word .= mb_substr($kana, mt_rand(0, $end), 1); } return$word; }
/**
* function for generating an array of words.
*/ function _hiragana_captcha_generate_phrase($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 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"; } }
/**
* On uninstall: remove module variables and clear variable cache
*/ function hiragana_captcha_uninstall(){ db_query("DELETE FROM {variable} WHERE name LIKE 'hiragana_captcha_%'"); cache_clear_all('variables', 'cache'); }
name = "Hiragana CAPTCHA" description = "Provides a simple text based hiragana CAPTCHA." package = "Spam control"
dependencies[] = captcha core =6.x version = "6.x-1.0"
/**
* Implementation of hook_help().
*/ function hiragana_captcha_help($path, $arg){ switch($path){ case'admin/user/captcha/hiragana_captcha': return'<p>'. t('The hiragana CAPTCHA ask for the n<sup>th</sup> word of a predetermined phrase like text CAPTCHA.') .'</p>'; } }
/**
* On uninstall: remove module variables and clear variable cache
*/ function hiragana_captcha_uninstall(){ db_query("DELETE FROM {variable} WHERE name LIKE 'hiragana_captcha_%'"); cache_clear_all('variables', 'cache'); }
/**
* Administration form.
*/ function hiragana_captcha_settings_form(){ $form = array(); $form['hiragana_captcha_words'] = array( '#type' => 'fieldset', '#title' => t('Words settings'), '#description' => t('Input the generated word length and number of words in the phrase for hiragana CAPTCHA.'), ); $form['hiragana_captcha_words']['hiragana_captcha_length_min'] = array( '#type' => 'textfield', '#title' => t('Minimum number of characters'), '#default_value' => variable_get('hiragana_captcha_length_min', 3), '#size' => 2, '#maxlength' => 2, ); $form['hiragana_captcha_words']['hiragana_captcha_length_max'] = array( '#type' => 'textfield', '#title' => t('Maximum number of characters'), '#default_value' => variable_get('hiragana_captcha_length_max', 3), '#size' => 2, '#maxlength' => 2, ); $form['hiragana_captcha_words']['hiragana_captcha_quantity'] = array( '#type' => 'textfield', '#title' => t('Number of words in the phrase'), '#default_value' => variable_get('hiragana_captcha_quantity', 5), '#size' => 2, '#maxlength' => 2, ); $form['#validate'][] = 'hiragana_captcha_settings_form_validate'; returnsystem_settings_form($form); }
/**
* Validate function of the administration form.
*/ function hiragana_captcha_settings_form_validate($form, &$form_state){ if($form_state['values']['hiragana_captcha_length_min'] < 1 || $form_state['values']['hiragana_captcha_length_max'] < 1){ form_set_error('hiragana_captcha_length', t('Minimum and maximum number of characters should be more than \'1\'.')); } elseif($form_state['values']['hiragana_captcha_length_min'] > $form_state['values']['hiragana_captcha_length_max']){ form_set_error('hiragana_captcha_length', t('Maximum number of characters should be more than minimum number of characters.')); } if($form_state['values']['hiragana_captcha_quantity'] < 4 || $form_state['values']['hiragana_captcha_quantity'] > 10){ form_set_error('hiragana_captcha_quantity', t('Number of words in the phrase should be between 4 and 10.')); } }
/**
* function for generating a random nonsense word of a given number of characters.
*/ function _hiragana_captcha_generate_word($min, $max){ $kana = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん"; $end = mb_strlen($kana) - 1; $length = mt_rand($min, $max); $word = ''; for($i = 0; $i < $length; ++$i){ $word .= mb_substr($kana, mt_rand(0, $end), 1); } return$word; }
/**
* function for generating an array of words.
*/ function _hiragana_captcha_generate_phrase($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 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"; } }