You are here

function matomo_admin_settings_form_validate in Matomo Analytics 7.2

File

./matomo.admin.inc, line 436
Administrative page callbacks for the matomo module.

Code

function matomo_admin_settings_form_validate($form, &$form_state) {

  // Custom variables validation.
  foreach ($form_state['values']['matomo_custom_var']['slots'] as $custom_var) {
    $form_state['values']['matomo_custom_var']['slots'][$custom_var['slot']]['name'] = trim($custom_var['name']);
    $form_state['values']['matomo_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_set_error("matomo_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.', array(
        '@slot-number' => $custom_var['slot'],
      )));
    }
    elseif (!empty($custom_var['name']) && empty($custom_var['value'])) {
      form_set_error("matomo_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.', array(
        '@slot-number' => $custom_var['slot'],
      )));
    }
  }

  // Trim some text area values.
  $form_state['values']['matomo_site_id'] = trim($form_state['values']['matomo_site_id']);
  $form_state['values']['matomo_pages'] = trim($form_state['values']['matomo_pages']);
  $form_state['values']['matomo_codesnippet_before'] = trim($form_state['values']['matomo_codesnippet_before']);
  $form_state['values']['matomo_codesnippet_after'] = trim($form_state['values']['matomo_codesnippet_after']);
  if (!preg_match('/^\\d{1,}$/', $form_state['values']['matomo_site_id'])) {
    form_set_error('matomo_site_id', t('A valid Matomo site ID is an integer only.'));
  }
  $matomo_url = $form_state['values']['matomo_url_http'];
  if ('/' != drupal_substr($matomo_url, -1, 1)) {
    $matomo_url = $matomo_url . '/';
    $form_state['values']['matomo_url_http'] = $matomo_url;
  }
  $url = $matomo_url . 'matomo.php';
  $result = drupal_http_request($url);
  if ($result->code != 200 && $form_state['values']['matomo_url_skiperror'] == FALSE) {
    form_set_error('matomo_url_http', t('The validation of "@url" failed with error "@error" (HTTP code @code).', array(
      '@url' => check_url($url),
      '@error' => $result->error,
      '@code' => $result->code,
    )));
  }
  if (!empty($form_state['values']['matomo_url_https'])) {
    $matomo_url = $form_state['values']['matomo_url_https'];
    if ('/' != drupal_substr($matomo_url, -1, 1)) {
      $matomo_url = $matomo_url . '/';
      $form_state['values']['matomo_url_https'] = $matomo_url;
    }
    $url = $matomo_url . 'matomo.php';
    $result = drupal_http_request($url);
    if ($result->code != 200 && $form_state['values']['matomo_url_skiperror'] == FALSE) {
      form_set_error('matomo_url_https', t('The validation of "@url" failed with error "@error" (HTTP code @code).', array(
        '@url' => check_url($url),
        '@error' => $result->error,
        '@code' => $result->code,
      )));
    }
  }

  // Delete obsolete local cache file.
  if (empty($form_state['values']['matomo_cache']) && $form['advanced']['matomo_cache']['#default_value']) {
    matomo_clear_js_cache();
  }

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