You are here

function googleanalytics_admin_settings_form_validate in Google Analytics 6.4

Same name and namespace in other branches
  1. 5 googleanalytics.module \googleanalytics_admin_settings_form_validate()
  2. 6 googleanalytics.admin.inc \googleanalytics_admin_settings_form_validate()
  3. 6.2 googleanalytics.admin.inc \googleanalytics_admin_settings_form_validate()
  4. 6.3 googleanalytics.admin.inc \googleanalytics_admin_settings_form_validate()
  5. 7.2 googleanalytics.admin.inc \googleanalytics_admin_settings_form_validate()
  6. 7 googleanalytics.admin.inc \googleanalytics_admin_settings_form_validate()

File

./googleanalytics.admin.inc, line 465
Administrative page callbacks for the googleanalytics module.

Code

function googleanalytics_admin_settings_form_validate($form, &$form_state) {

  // Trim custom dimensions and metrics.
  foreach ($form_state['values']['googleanalytics_custom_dimension']['indexes'] as $dimension) {
    $form_state['values']['googleanalytics_custom_dimension']['indexes'][$dimension['index']]['value'] = trim($dimension['value']);

    // Remove empty values from the array.
    if (!drupal_strlen($form_state['values']['googleanalytics_custom_dimension']['indexes'][$dimension['index']]['value'])) {
      unset($form_state['values']['googleanalytics_custom_dimension']['indexes'][$dimension['index']]);
    }

    // If token module is not available, block tokens with personally identifying
    // information with an "ugly" value check. Normally the values are validated
    // via '#element_validate' functions that are not available without token module.
    if (!module_exists('token')) {
      if (_googleanalytics_contains_forbidden_token($dimension['value'])) {
        form_set_error('googleanalytics_custom_dimension][indexes][' . $dimension['index'] . '][value', t('The dimension #@index is using forbidden tokens with personal identifying information.', array(
          '@index' => $dimension['index'],
        )));
      }
    }
  }
  $form_state['values']['googleanalytics_custom_dimension'] = $form_state['values']['googleanalytics_custom_dimension']['indexes'];
  foreach ($form_state['values']['googleanalytics_custom_metric']['indexes'] as $metric) {
    $form_state['values']['googleanalytics_custom_metric']['indexes'][$metric['index']]['value'] = trim($metric['value']);

    // Remove empty values from the array.
    if (!drupal_strlen($form_state['values']['googleanalytics_custom_metric']['indexes'][$metric['index']]['value'])) {
      unset($form_state['values']['googleanalytics_custom_metric']['indexes'][$metric['index']]);
    }

    // If token module is not available, block tokens with personally identifying
    // information with an "ugly" value check. Normally the values are validated
    // via '#element_validate' functions that are not available without token module.
    if (!module_exists('token')) {
      if (_googleanalytics_contains_forbidden_token($metric['value'])) {
        form_set_error('googleanalytics_custom_dimension][indexes][' . $metric['index'] . '][value', t('The metric #@index is using forbidden tokens with personal identifying information.', array(
          '@index' => $metric['index'],
        )));
      }
    }
  }
  $form_state['values']['googleanalytics_custom_metric'] = $form_state['values']['googleanalytics_custom_metric']['indexes'];

  // Trim some text values.
  $form_state['values']['googleanalytics_account'] = trim($form_state['values']['googleanalytics_account']);
  $form_state['values']['googleanalytics_pages'] = trim($form_state['values']['googleanalytics_pages']);
  $form_state['values']['googleanalytics_cross_domains'] = trim($form_state['values']['googleanalytics_cross_domains']);
  $form_state['values']['googleanalytics_codesnippet_create'] = _googleanalytics_extract_create_field_values($form_state['values']['googleanalytics_codesnippet_create']);
  $form_state['values']['googleanalytics_codesnippet_before'] = trim($form_state['values']['googleanalytics_codesnippet_before']);
  $form_state['values']['googleanalytics_codesnippet_after'] = trim($form_state['values']['googleanalytics_codesnippet_after']);

  // Replace all type of dashes (n-dash, m-dash, minus) with the normal dashes.
  $form_state['values']['googleanalytics_account'] = str_replace(array(
    '–',
    '—',
    '−',
  ), '-', $form_state['values']['googleanalytics_account']);
  if (!preg_match('/^UA-\\d+-\\d+$/', $form_state['values']['googleanalytics_account'])) {
    form_set_error('googleanalytics_account', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
  }

  // If multiple top-level domains has been selected, a domain names list is required.
  if ($form_state['values']['googleanalytics_domain_mode'] == 2 && empty($form_state['values']['googleanalytics_cross_domains'])) {
    form_set_error('googleanalytics_cross_domains', t('A list of top-level domains is required if <em>Multiple top-level domains</em> has been selected.'));
  }

  // Clear the domain list if cross domains are disabled.
  if ($form_state['values']['googleanalytics_domain_mode'] != 2) {
    $form_state['values']['googleanalytics_cross_domains'] = '';
  }

  // Disallow empty list of download file extensions.
  if ($form_state['values']['googleanalytics_trackfiles'] && empty($form_state['values']['googleanalytics_trackfiles_extensions'])) {
    form_set_error('googleanalytics_trackfiles_extensions', t('List of download file extensions cannot empty.'));
  }

  // Clear obsolete local cache if cache has been disabled.
  if (empty($form_state['values']['googleanalytics_cache']) && $form['advanced']['googleanalytics_cache']['#default_value']) {
    googleanalytics_clear_js_cache();
  }

  // This is for the Newbie's who cannot read a text area description.
  if (stristr($form_state['values']['googleanalytics_codesnippet_before'], 'google-analytics.com/analytics.js')) {
    form_set_error('googleanalytics_codesnippet_before', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
  }
  if (stristr($form_state['values']['googleanalytics_codesnippet_after'], 'google-analytics.com/analytics.js')) {
    form_set_error('googleanalytics_codesnippet_after', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
  }
  if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_before'])) {
    form_set_error('googleanalytics_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  }
  if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_after'])) {
    form_set_error('googleanalytics_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  }
}