You are here

function captcha_questions_form_validate in Captcha Questions 7

Same name and namespace in other branches
  1. 8 captcha_questions.module \captcha_questions_form_validate()

Implements hook_form_validate().

1 string reference to 'captcha_questions_form_validate'
captcha_questions_form_alter in ./captcha_questions.module
Implements hook_form_alter().

File

./captcha_questions.module, line 89
Module file for the Captcha questions module

Code

function captcha_questions_form_validate($form, &$form_state) {
  global $user;
  $ip = $user->hostname;
  $form_id = $form_state['values']['form_id'];
  $question_asked = variable_get('captcha_questions_question', NULL);
  $answers = variable_get('captcha_questions_answers', array());
  $answer_given = $form_state['values']['captcha_questions_answer_given'];

  // Comparison is done in lowercase, ensure answers are lowercase.
  $answers = array_map('drupal_strtolower', $answers);
  $answer_given = drupal_strtolower($answer_given);

  // Check answer.
  if (in_array($answer_given, $answers) == FALSE) {

    // Log to watchdog if enabled.
    if (variable_get('captcha_questions_watchdog', 0)) {
      $variables = array(
        '!form_id' => $form_id,
        '%answer_given' => $form_state['values']['captcha_questions_answer_given'],
      );
      watchdog('Captcha questions', 'Blocked submission of form with form_id !form_id. Answer given was %answer_given', $variables);
    }

    // Log to dblog if enabled and module exists and enabled.
    $dblog_enabled = variable_get('captcha_questions_dblog', 0);
    $dblog_module_exists = module_exists('captcha_questions_dblog') ? TRUE : FALSE;
    if ($dblog_enabled && $dblog_module_exists) {
      db_insert('captcha_questions_dblog')
        ->fields(array(
        'timestamp' => REQUEST_TIME,
        'ip' => $ip,
        'form_id' => $form_id,
        'question_asked' => check_plain($question_asked),
        'answer_given' => check_plain($answer_given),
        'answer_correct' => implode(",", array_map('check_plain', $answers)),
      ))
        ->execute();
    }

    // Display error.
    form_set_error('captcha_questions_answer_given', t('Invalid answer'));
  }
}