t('No CAPTCHA support'), 1 => t('CAPTCHA support'))); $group .= form_radios(t('comments'), 'captcha_comment', variable_get('captcha_comment', 0), array(0 => t('No CAPTCHA support'), 1 => t('CAPTCHA support'), 2 => t('CAPTCHA support only for anonymous user'))); foreach (node_list() as $type) { $group .= form_radios(t(node_invoke($type,'node_name')), "captcha_node_$type", variable_get("captcha_node_$type", 0), array(0 => t('No CAPTCHA support'), 1 => t('CAPTCHA support'), 2 => t('CAPTCHA support only for anonymous user'))); } $output .= form_group(t('Form Options'),$group); return $output; } /** * Implementation of hook_nodeapi(). */ function captcha_nodeapi(&$node, $op, $arg) { global $user; $type = $node->type; // check if captcha is enabled for form type if (variable_get("captcha_node_$type",0) != 1 && !(variable_get("captcha_node_$type",0) == 2 && !$user->uid)) return; switch ($op) { case 'validate': $node->captcha = trim($node->captcha); // only validate captcha if "Submit" button is used if ($_POST['op'] == t('Preview') && (variable_get('captcha_code','') == '' || $node->captcha != variable_get('captcha_code',''))) { form_set_error('captcha', t('The submission verification response you entered is not correct.')); } break; case 'form post': // only show captcha form if the "Submit" button is visible if (!variable_get('node_preview', 0) || ($_POST['op'] == t('Preview')) || ($_POST['op'] == t('Submit'))) { $qa = _captcha_qa(); variable_set('captcha_code', $qa['answer']); $group = form_item(t('Question'), $qa['question']); $group .= form_textfield(t('Answer'), 'captcha', NULL, 15, 15, t('This helps prevent automated submissions.'), NULL, TRUE); $output = form_group(t('Verify your submission'),$group); return $output; } } } /** * User callback; adds captcha field to new user form. */ function captcha_user($type, &$edit, &$user, $category = NULL) { global $user; // check if user is logged in if ($user->uid) return; // check if captcha is enabled for form type if (variable_get('captcha_newuser',0) == 0) return; switch ($type) { case 'validate': if ($edit['captcha'] == '' || variable_get('captcha_code','') == '' || $edit['captcha'] != variable_get('captcha_code','')) { form_set_error('captcha', t('The submission verification response you entered is not correct.')); } break; case 'register': $qa = _captcha_qa(); variable_set('captcha_code', $qa['answer']); $group = form_item(t('Question'), $qa['question']); $group .= form_textfield(t('Answer'), 'captcha', NULL, 15, 15, t('This helps prevent automated submissions.'), NULL, TRUE); return array(array('title' => t('Verify your submission'), 'data' => $group)); } } /** * Comment callback; adds captcha field to new comment form. */ function captcha_comment($op,$edit) { global $user; // check if captcha is enabled for form type if (variable_get('captcha_comment',0) != 1 && !(variable_get('captcha_comment',0) == 2 && !$user->uid)) return; switch ($op) { case 'validate': // only validate captcha if "Post comment" button is used if ($_POST['op'] == t('Post comment') && (variable_get('captcha_code','') == '' || $edit['captcha'] != variable_get('captcha_code',''))) { form_set_error('captcha', t('The submission verification response you entered is not correct.')); } break; case 'form': // only show captcha form if the "Post comment" button is visible if (!variable_get('comment_preview', 1) || ($_POST['op'] == t('Preview comment')) || ($_POST['op'] == t('Post comment'))) { $qa = _captcha_qa(); variable_set('captcha_code', $qa['answer']); $group = form_item(t('Question'), $qa['question']); $group .= form_textfield(t('Answer'), 'captcha', NULL, 15, 15, t('This helps prevent automated submissions.'), NULL, TRUE); $output = form_group(t('Verify your submission'),$group); return $output; } } } /** * Returns a question and answer. */ function _captcha_qa() { $numbers = array( 1 => t('one'), 2 => t('two'), 3 => t('three'), 4 => t('four'), 5 => t('five'), 6 => t('six'), 7 => t('seven'), 8 => t('eight'), 9 => t('nine'), 10 => t('ten'), 11 => t('eleven'), 12 => t('twelve') ); $operators = array( 0 => array(name => t('plus'), symbol => '+'), 1 => array(name => t('minus'), symbol => '-'), 2 => array(name => t('multiplied by'), symbol => '*'), 3 => array(name => t('divided by'), symbol => '/') ); $operator = rand(0,count($operators)-1); // choose numbers based on operator to keep the math simple switch($operator) { case 0: // do addition logic $firstNumber = rand(1,count($numbers)-1); $secondNumber = rand(1,count($numbers)-1); $result = $firstNumber + $secondNumber; break; case 1: // do subtraction logic $firstNumber = rand(6,count($numbers)-1); $secondNumber = rand(1,5); $result = $firstNumber - $secondNumber; break; case 2: // do multiplication logic $firstNumber = rand(1,8); $secondNumber = rand(1,8); $result = $firstNumber * $secondNumber; break; case 3: // do division logic $secondNumber = rand(2,4); $firstNumber = $secondNumber * rand(1,4); $result = $firstNumber / $secondNumber; break; } $equation = $numbers[$firstNumber].' '.$operators[$operator]['name'].' '.$numbers[$secondNumber]; $output = array('question' => t('What is ') . $equation . '?', t('answer') => $result); return $output; } ?>