You are here

function botcha_form_alter in BOTCHA Spam Prevention 7

Same name and namespace in other branches
  1. 6 botcha.module \botcha_form_alter()
  2. 6.2 botcha.module \botcha_form_alter()
  3. 7.2 botcha.module \botcha_form_alter()

Implements hook_form_alter().

This function adds BOTCHA protection to forms for untrusted users if needed and adds BOTCHA administration links for site administrators if this option is enabled.

File

./botcha.module, line 116
BOTCHA - Spam Prevention It modifies forms by adding various botcha's.

Code

function botcha_form_alter(&$form, &$form_state, $form_id) {
  $botcha = FALSE;
  if (!user_access('skip BOTCHA')) {

    // Visitor does not have permission to skip the BOTCHA
    module_load_include('inc', 'botcha');

    // Get BOTCHA type for given form_id.
    $botcha_point = botcha_get_form_id_setting($form_id);
    if ($botcha_point) {
      $botcha = !$botcha_point->botcha_type || $botcha_point->botcha_type == 'none' ? FALSE : $botcha_point->botcha_type;
    }
    elseif (variable_get('botcha_on_captcha_forms', TRUE) && botcha_is_captcha_installed()) {

      // Check captcha.module settings. If there is a captcha, there is a botcha.
      // Botcha's don't hurt humans, so we don't implement any logic to bypass
      // the Botcha.
      $captcha_point = botcha_get_captcha_point($form_id, TRUE);
      if ($captcha_point && $captcha_point != 'none') {
        $botcha = 'default';
      }
    }
  }
  if (variable_get('botcha_administration_mode', FALSE) && user_access('administer BOTCHA settings') && (arg(0) != 'admin' || variable_get('botcha_allow_on_admin_pages', FALSE) || $form_id == 'user_register')) {

    // Add BOTCHA administration tools.
    module_load_include('inc', 'botcha');
    $botcha_point = botcha_get_form_id_setting($form_id);

    // For administrators: show BOTCHA info and offer link to configure it
    $botcha_element = array(
      '#type' => 'fieldset',
      '#title' => t('BOTCHA'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#attributes' => array(
        'class' => array(
          'botcha-admin-links',
        ),
      ),
    );
    if ($botcha_point !== NULL && $botcha_point->botcha_type && $botcha_point->botcha_type != 'none') {
      $botcha_element['#title'] = t('BOTCHA: protection enabled (@type cookbook)', array(
        '@type' => $botcha_point->botcha_type,
      ));
      $botcha_element['#description'] = t('Untrusted users will have form %form_id protected by BOTCHA (!settings).', array(
        '%form_id' => $form_id,
        '!settings' => l(t('general BOTCHA settings'), 'admin/config/people/botcha'),
      ));
      $botcha_element['protection'] = array(
        '#type' => 'item',
        '#title' => t('Enabled protection'),
        '#markup' => t('"@type" cookbook (!change, !disable)', array(
          '@type' => $botcha_point->botcha_type,
          '!change' => l(t('change'), "admin/config/people/botcha/botcha_point/{$form_id}", array(
            'query' => drupal_get_destination(),
            'html' => TRUE,
          )),
          '!disable' => l(t('disable'), "admin/config/people/botcha/botcha_point/{$form_id}/disable", array(
            'query' => drupal_get_destination(),
            'html' => TRUE,
          )),
        )),
      );
    }
    else {
      $botcha_element['#title'] = t('BOTCHA: no protection enabled');
      $botcha_element['add_botcha'] = array(
        '#markup' => l(t('Add BOTCHA protection on form %form_id for untrusted users.', array(
          '%form_id' => $form_id,
        )), "admin/config/people/botcha/botcha_point/{$form_id}", array(
          'query' => drupal_get_destination(),
          'html' => TRUE,
        )),
      );
    }

    // Get placement in form and insert in form.
    $botcha_placement = _botcha_get_botcha_placement($form_id, $form);
    _botcha_insert_botcha_element($form, $botcha_placement, $botcha_element);
  }
  switch ($form_id) {

    /* UNUSED
        case 'user_admin_settings':
          // Insert BOTCHA settings into admin/user/settings
          module_load_include('inc', 'botcha', 'botcha.pages');
          $myform = _botcha_admin_settings($form_state);
          $form['register'] = array(
            '#type' => 'fieldset',
            '#title' => t('User Register settings'),
            '#description' => t('These options adjust User Register form (provided by <em>BOTCHA</em> module)'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          );
          $form['register'] += $myform;
          $form['register']['#weight'] = $form['registration']['#weight'] + 0.001;
          foreach ($myform['#submit'] as $func) {
            $form['#submit'][] = $func;
          }
          break;
    */
    case 'user_register':
      if (FALSE === strpos($form['#action'], 'user/register')) {
        if (!variable_get('botcha_allow_on_admin_pages', FALSE)) {
          $botcha = FALSE;
        }

        // Only change the registration form. There is also 'user_register' form at /admin/config/people/user/create path, but we leave it alone.
      }
      break;
  }
  if ($botcha) {
    $form['#process'] = array(
      'botcha_fprocess',
    );
    module_load_include('inc', 'botcha', 'botcha.botcha');
    botcha_form_alter_botcha($form, $form_state, $form_id, $botcha);
    $form_state['no_cache'] = TRUE;
  }

  // UNUSED
  //  // Add a warning about caching on the Perfomance settings page.
  //  if ($form_id == 'system_performance_settings') {
  //    $icon = theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('warning'), 'title' => t('warning')));
  //    $form['caching']['botcha'] = array(
  //      '#type' => 'item',
  //      '#title' => t('BOTCHA'),
  //      '#markup' => t('!icon The BOTCHA module will disable the caching of pages that contain forms processed by BOTCHA.', array(
  //        '!icon' => '<span class="icon">' . $icon . '</span>')
  //      ),
  //      '#attributes' => array('class' => array('warning')),
  //    );
  //  }
}