You are here

public function PiwikAdminSettingsForm::validateForm in Piwik Web Analytics 8

Form validation 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 FormBase::validateForm

File

src/Form/PiwikAdminSettingsForm.php, line 467

Class

PiwikAdminSettingsForm
Configure Piwik settings for this site.

Namespace

Drupal\piwik\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  // Custom variables validation.
  foreach ($form_state
    ->getValue([
    'piwik_custom_var',
    'slots',
  ]) as $custom_var) {
    $form_state
      ->setValue([
      'piwik_custom_var',
      'slots',
      $custom_var['slot'],
      'name',
    ], trim($custom_var['name']));
    $form_state
      ->setValue([
      'piwik_custom_var',
      'slots',
      $custom_var['slot'],
      'value',
    ], trim($custom_var['value']));

    // Validate empty names/values.
    if (empty($custom_var['name']) && !empty($custom_var['value'])) {
      $form_state
        ->setErrorByName("piwik_custom_var][slots][" . $custom_var['slot'] . "][name", t('The custom variable @slot-number requires a <em>Name</em> if a <em>Value</em> has been provided.', [
        '@slot-number' => $custom_var['slot'],
      ]));
    }
    elseif (!empty($custom_var['name']) && empty($custom_var['value'])) {
      $form_state
        ->setErrorByName("piwik_custom_var][slots][" . $custom_var['slot'] . "][value", t('The custom variable @slot-number requires a <em>Value</em> if a <em>Name</em> has been provided.', [
        '@slot-number' => $custom_var['slot'],
      ]));
    }
  }
  $form_state
    ->setValue('piwik_custom_var', $form_state
    ->getValue([
    'piwik_custom_var',
    'slots',
  ]));

  // Trim some text area values.
  $form_state
    ->setValue('piwik_site_id', trim($form_state
    ->getValue('piwik_site_id')));
  $form_state
    ->setValue('piwik_visibility_request_path_pages', trim($form_state
    ->getValue('piwik_visibility_request_path_pages')));
  $form_state
    ->setValue('piwik_codesnippet_before', trim($form_state
    ->getValue('piwik_codesnippet_before')));
  $form_state
    ->setValue('piwik_codesnippet_after', trim($form_state
    ->getValue('piwik_codesnippet_after')));
  $form_state
    ->setValue('piwik_visibility_user_role_roles', array_filter($form_state
    ->getValue('piwik_visibility_user_role_roles')));
  $form_state
    ->setValue('piwik_trackmessages', array_filter($form_state
    ->getValue('piwik_trackmessages')));
  if (!preg_match('/^\\d{1,}$/', $form_state
    ->getValue('piwik_site_id'))) {
    $form_state
      ->setErrorByName('piwik_site_id', t('A valid Piwik site ID is an integer only.'));
  }
  $url = $form_state
    ->getValue('piwik_url_http') . 'piwik.php';
  try {
    $result = \Drupal::httpClient()
      ->get($url);
    if ($result
      ->getStatusCode() != 200 && $form_state
      ->getValue('piwik_url_skiperror') == FALSE) {
      $form_state
        ->setErrorByName('piwik_url_http', t('The validation of "@url" failed with error "@error" (HTTP code @code).', [
        '@url' => UrlHelper::filterBadProtocol($url),
        '@error' => $result
          ->getReasonPhrase(),
        '@code' => $result
          ->getStatusCode(),
      ]));
    }
  } catch (RequestException $exception) {
    $form_state
      ->setErrorByName('piwik_url_http', t('The validation of "@url" failed with an exception "@error" (HTTP code @code).', [
      '@url' => UrlHelper::filterBadProtocol($url),
      '@error' => $exception
        ->getMessage(),
      '@code' => $exception
        ->getCode(),
    ]));
  }
  $piwik_url_https = $form_state
    ->getValue('piwik_url_https');
  if (!empty($piwik_url_https)) {
    $url = $piwik_url_https . 'piwik.php';
    try {
      $result = \Drupal::httpClient()
        ->get($url);
      if ($result
        ->getStatusCode() != 200 && $form_state
        ->getValue('piwik_url_skiperror') == FALSE) {
        $form_state
          ->setErrorByName('piwik_url_https', t('The validation of "@url" failed with error "@error" (HTTP code @code).', [
          '@url' => UrlHelper::filterBadProtocol($url),
          '@error' => $result
            ->getReasonPhrase(),
          '@code' => $result
            ->getStatusCode(),
        ]));
      }
    } catch (RequestException $exception) {
      $form_state
        ->setErrorByName('piwik_url_https', t('The validation of "@url" failed with an exception "@error" (HTTP code @code).', [
        '@url' => UrlHelper::filterBadProtocol($url),
        '@error' => $exception
          ->getMessage(),
        '@code' => $exception
          ->getCode(),
      ]));
    }
  }

  // Verify that every path is prefixed with a slash, but don't check PHP
  // code snippets.
  if ($form_state
    ->getValue('piwik_visibility_request_path_mode') != 2) {
    $pages = preg_split('/(\\r\\n?|\\n)/', $form_state
      ->getValue('piwik_visibility_request_path_pages'));
    foreach ($pages as $page) {
      if (strpos($page, '/') !== 0 && $page !== '<front>') {
        $form_state
          ->setErrorByName('piwik_visibility_request_path_pages', t('Path "@page" not prefixed with slash.', [
          '@page' => $page,
        ]));

        // Drupal forms show one error only.
        break;
      }
    }
  }

  // Clear obsolete local cache if cache has been disabled.
  if ($form_state
    ->isValueEmpty('piwik_cache') && $form['advanced']['piwik_cache']['#default_value']) {
    piwik_clear_js_cache();
  }

  // This is for the Newbie's who cannot read a text area description.
  if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state
    ->getValue('piwik_codesnippet_before'))) {
    $form_state
      ->setErrorByName('piwik_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  }
  if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state
    ->getValue('piwik_codesnippet_after'))) {
    $form_state
      ->setErrorByName('piwik_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  }
}