You are here

public function TfaTrustedBrowserSetup::getSetupForm in TFA Basic plugins 7

@copydoc TfaSetupPluginInterface::getSetupForm()

Overrides TfaSetupPluginInterface::getSetupForm

File

includes/tfa_trusted_browser.inc, line 214
classes for TFA basic plugin

Class

TfaTrustedBrowserSetup
Class TfaTrustedBrowserSetup

Code

public function getSetupForm(array $form, array &$form_state) {
  $existing = $this
    ->getTrustedBrowsers();
  $time = variable_get('tfa_basic_trust_cookie_expiration', 3600 * 24 * 30) / (3600 * 24);
  $form['info'] = array(
    '#type' => 'markup',
    '#markup' => '<p>' . t("Trusted browsers are a method for simplifying login by avoiding verification code entry for a set amount of time, !time days from marking a browser as trusted. After !time days, to log in you'll need to enter a verification code with your username and password during which you can again mark the browser as trusted.", array(
      '!time' => $time,
    )) . '</p>',
  );

  // Present option to trust this browser if its not currently trusted.
  if (isset($_COOKIE[$this->cookieName]) && ($browser_id = $this
    ->trustedBrowser($_COOKIE[$this->cookieName])) !== FALSE) {
    $current_trusted = $browser_id;
  }
  else {
    $current_trusted = FALSE;
    $form['trust'] = array(
      '#type' => 'checkbox',
      '#title' => t('Trust this browser?'),
      '#default_value' => empty($existing) ? 1 : 0,
    );

    // Optional field to name this browser.
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name this browser'),
      '#maxlength' => 255,
      '#description' => t('Optionally, name the browser on your browser (e.g. "home firefox" or "office desktop windows"). Your current browser user agent is %browser', array(
        '%browser' => $_SERVER['HTTP_USER_AGENT'],
      )),
      '#default_value' => $this
        ->getAgent(),
      '#states' => array(
        'visible' => array(
          ':input[name="trust"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  if (!empty($existing)) {
    $form['existing'] = array(
      '#type' => 'fieldset',
      '#title' => t('Existing browsers'),
      '#description' => t('Leave checked to keep these browsers in your trusted log in list.'),
      '#tree' => TRUE,
    );
    foreach ($existing as $browser) {
      $vars = array(
        '!expiration' => format_date($browser['created'] + variable_get('tfa_basic_trust_cookie_expiration', 3600 * 24 * 30)),
        '!time' => format_date($browser['last_used']),
      );
      if ($current_trusted == $browser['id']) {
        $name = '<strong>' . t('@name (current browser)', array(
          '@name' => $browser['name'],
        )) . '</strong>';
      }
      else {
        $name = check_plain($browser['name']);
      }
      if (empty($browser['last_used'])) {
        $message = t('Expires !expiration', $vars);
      }
      else {
        $message = t('Expires !expiration, last used for log in !time', $vars);
      }
      $form['existing']['trusted_browser_' . $browser['id']] = array(
        '#type' => 'checkbox',
        '#title' => $name,
        '#description' => $message,
        '#default_value' => 1,
      );
    }
  }
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}