You are here

public function LockrCSRForm::submitForm in Lockr 8.2

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/LockrCSRForm.php, line 127

Class

LockrCSRForm

Namespace

Drupal\lockr\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $dn = [
    'countryName' => $form_state
      ->getValue('country'),
    'stateOrProvinceName' => $form_state
      ->getValue('state'),
    'localityName' => $form_state
      ->getValue('city'),
    'organizationName' => $form_state
      ->getValue('org'),
  ];
  $this->state
    ->set('lockr.cert_dn', $dn);
  $site_client = $this->clientFactory
    ->getSiteClient();
  try {
    $result = $site_client
      ->createCert($dn);
  } catch (LockrClientException $e) {
    watchdog_exception('lockr', $e);
    drupal_set_message('Please check form inputs.', 'error');
    return;
  } catch (LockrServerException $e) {
    watchdog_exception('lockr', $e);
    drupal_set_message('Lockr encountered an unexpected server error, please try again.', 'error');
  }
  $this->streamWrapperManager
    ->registerWrapper('private', PrivateStream::class, PrivateStream::getType());
  $dir = 'private://lockr/dev';
  mkdir($dir, 0700, TRUE);
  $key_file = "{$dir}/key.pem";
  $key_fd = fopen($key_file, 'w');
  fwrite($key_fd, $result['key_text']);
  fclose($key_fd);
  chmod($key_file, 0600);
  $cert_file = "{$dir}/crt.pem";
  $cert_fd = fopen($cert_file, 'w');
  fwrite($cert_fd, $result['cert_text']);
  fclose($cert_fd);
  chmod($cert_file, 0600);
  $pair_file = "{$dir}/pair.pem";
  $pair_fd = fopen($pair_file, 'w');
  fwrite($pair_fd, $result['key_text']);
  fwrite($pair_fd, $result['cert_text']);
  fclose($pair_fd);
  chmod($pair_file, 0600);
  $private_stream = new PrivateStream();
  $private_stream
    ->setUri("{$dir}/pair.pem");
  $this->state
    ->set('lockr.cert', $private_stream
    ->realpath());
  $this->state
    ->set('lockr.custom', TRUE);
}