You are here

function antibot_protect_form in Antibot 8

Same name and namespace in other branches
  1. 7 antibot.module \antibot_protect_form()

Helper function to enable Antibot protection for a given form.

Parameters

array &$form: The form to enable Antibot protection on.

1 call to antibot_protect_form()
antibot_form_alter in ./antibot.module
Implements hook_form_alter().

File

./antibot.module, line 104
Implements the antibot module.

Code

function antibot_protect_form(array &$form) {

  // Stop if the form is already protected.
  if (!empty($form['#antibot_key'])) {
    return;
  }

  // Generate a key for this form.
  $key = Crypt::hmacBase64($form['#form_id'], Settings::getHashSalt());

  // Store the key in the form.
  $form['#antibot_key'] = $key;

  // Add a hidden value which will receive the key via JS.
  // The point of this is to add an additonal layer of protection for remotely
  // POSTed forms. Since the key will not be present in that scenario, the
  // remote post will fail.
  $form['antibot_key'] = [
    '#type' => 'hidden',
    '#value' => '',
  ];

  // Provide a message in the event that the user does not have JavaScript.
  $form['antibot_no_js'] = [
    '#theme' => 'antibot_no_js',
    '#message' => t('You must have JavaScript enabled to use this form.'),
    '#weight' => -500,
  ];

  // Add a pre-render function.
  $form['#pre_render'][] = [
    AntibotFormAlter::class,
    'preRender',
  ];

  // Add validation for the key.
  $form['#validate'][] = 'antibot_form_validation';
}