View source
<?php
function random_captcha_type_help($path, $arg) {
switch ($path) {
case 'admin/config/people/captcha/random_captcha_type':
return '<p>' . t('This CAPTCHA type is a "meta" CAPTCHA type, which randomly picks one of the selected CAPTCHA types.') . '</p>';
}
}
function random_captcha_type_menu() {
$items = array();
$items['admin/config/people/captcha/random_captcha_type'] = array(
'title' => 'Random CAPTCHA type',
'file' => 'random_captcha_type.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'random_captcha_type_settings_form',
),
'access arguments' => array(
'administer CAPTCHA settings',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function random_captcha_type_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
switch ($op) {
case 'list':
$enabled_types = _random_captcha_type_get_enabled_types();
if (count($enabled_types) < 2) {
return;
}
return array(
'Random CAPTCHA type',
);
case 'generate':
if ($captcha_type == 'Random CAPTCHA type') {
if (isset($_POST['random_captcha_type'])) {
$module_and_type = $_POST['random_captcha_type'];
}
else {
$types = _random_captcha_type_get_enabled_types();
$module_and_type = $types[array_rand($types)];
}
list($module, $type) = explode('/', $module_and_type);
$captcha = module_invoke($module, 'captcha', 'generate', $type, $captcha_sid);
$captcha['form']['random_captcha_type'] = array(
'#type' => 'hidden',
'#value' => $module_and_type,
);
return $captcha;
}
}
}
function _random_captcha_type_get_all_types() {
$captcha_types = array();
foreach (module_implements('captcha') as $module) {
if ($module == 'random_captcha_type') {
continue;
}
$result = module_invoke($module, 'captcha', 'list');
if (is_array($result)) {
foreach ($result as $type) {
$captcha_types["{$module}/{$type}"] = "{$type} ({$module})";
}
}
}
return $captcha_types;
}
function _random_captcha_type_get_enabled_types() {
$enabled_types = array();
$_enabled_types = variable_get('random_captcha_type_enabled_types', NULL);
if ($_enabled_types === NULL) {
foreach (_random_captcha_type_get_all_types() as $key => $value) {
$enabled_types[$key] = $key;
}
variable_set('random_captcha_type_enabled_types', $enabled_types);
}
else {
foreach ($_enabled_types as $key => $value) {
if ($value) {
list($module, $type) = explode('/', $value);
$list = module_invoke($module, 'captcha', 'list');
if ($list && in_array($type, $list)) {
$enabled_types[$key] = $value;
}
}
}
}
return $enabled_types;
}