[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/google_analytics/ -> googleanalytics.admin.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Administrative page callbacks for the googleanalytics module.
   6   */
   7  
   8  /**
   9   * Implementation of hook_admin_settings() for configuring the module
  10   */
  11  function googleanalytics_admin_settings_form(&$form_state) {
  12    $form['account'] = array(
  13      '#type' => 'fieldset',
  14      '#title' => t('General settings'),
  15      '#collapsible' => FALSE,
  16    );
  17  
  18    $form['account']['googleanalytics_account'] = array(
  19      '#type' => 'textfield',
  20      '#title' => t('Web Property ID'),
  21      '#default_value' => variable_get('googleanalytics_account', 'UA-'),
  22      '#size' => 15,
  23      '#maxlength' => 20,
  24      '#required' => TRUE,
  25      '#description' => t('This ID is unique to each site you want to track separately, and is in the form of UA-xxxxxxx-yy. To get a Web Property ID, <a href="@analytics">register your site with Google Analytics</a>, or if you already have registered your site, go to your Google Analytics Settings page to see the ID next to every site profile. <a href="@webpropertyid">Find more information in the documentation</a>.', array('@analytics' => 'http://www.google.com/analytics/', '@webpropertyid' => 'http://code.google.com/apis/analytics/docs/concepts/gaConceptsAccounts.html#webProperty')),
  26    );
  27  
  28    $form['domain_tracking'] = array(
  29      '#type' => 'fieldset',
  30      '#title' => t('Domains'),
  31        '#collapsible' => TRUE,
  32    );
  33  
  34    global $cookie_domain;
  35    $multiple_sub_domains = array();
  36    foreach (array('www', 'app', 'shop') as $subdomain) {
  37      if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
  38        $multiple_sub_domains[] = $subdomain . $cookie_domain;
  39      }
  40      // IP addresses or localhost.
  41      else {
  42        $multiple_sub_domains[] = $subdomain . '.example.com';
  43      }
  44    }
  45  
  46    $form['domain_tracking']['googleanalytics_domain_mode'] = array(
  47      '#type' => 'radios',
  48      '#title' => t('What are you tracking?'),
  49      '#options' => array(
  50        0 => t('A single domain (default)') . '<div class="description">' . t('Domain: @domain', array('@domain' => $_SERVER['HTTP_HOST'])) . '</div>',
  51        1 => t('One domain with multiple subdomains') . '<div class="description">' . t('Examples: @domains', array('@domains' => implode(', ', $multiple_sub_domains))) . '</div>',
  52      ),
  53      '#default_value' => variable_get('googleanalytics_domain_mode', 0),
  54    );
  55  
  56    // Page specific visibility configurations.
  57    $form['page_vis_settings'] = array(
  58      '#type' => 'fieldset',
  59      '#title' => t('Page specific tracking settings'),
  60      '#collapsible' => TRUE,
  61      '#collapsed' => TRUE,
  62    );
  63  
  64    $access = user_access('use PHP for tracking visibility');
  65    $visibility = variable_get('googleanalytics_visibility', 0);
  66    $pages = variable_get('googleanalytics_pages', GOOGLEANALYTICS_PAGES);
  67  
  68    if ($visibility == 2 && !$access) {
  69      $form['page_vis_settings'] = array();
  70      $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
  71      $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $pages);
  72    }
  73    else {
  74      $options = array(t('Add to every page except the listed pages.'), t('Add to the listed pages only.'));
  75      $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
  76  
  77      if ($access) {
  78        $options[] = t('Add if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
  79        $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
  80      }
  81      $form['page_vis_settings']['googleanalytics_visibility'] = array(
  82        '#type' => 'radios',
  83        '#title' => t('Add tracking to specific pages'),
  84        '#options' => $options,
  85        '#default_value' => $visibility,
  86      );
  87      $form['page_vis_settings']['googleanalytics_pages'] = array(
  88        '#type' => 'textarea',
  89        '#title' => t('Pages'),
  90        '#default_value' => $pages,
  91        '#description' => $description,
  92        '#wysiwyg' => FALSE,
  93        '#rows' => 10,
  94      );
  95    }
  96  
  97    // Render the role overview.
  98    $form['role_vis_settings'] = array(
  99      '#type' => 'fieldset',
 100      '#title' => t('Role specific tracking settings'),
 101      '#collapsible' => TRUE,
 102    );
 103  
 104    $form['role_vis_settings']['googleanalytics_visibility_roles'] = array(
 105      '#type' => 'radios',
 106      '#title' => t('Add tracking for specific roles'),
 107      '#options' => array(
 108        t('Add to the selected roles only'),
 109        t('Add to every role except the selected ones'),
 110      ),
 111      '#default_value' => variable_get('googleanalytics_visibility_roles', 0),
 112    );
 113  
 114    $roles = user_roles();
 115    $role_options = array();
 116    foreach ($roles as $rid => $name) {
 117      $role_options[$rid] = $name;
 118    }
 119    $form['role_vis_settings']['googleanalytics_roles'] = array(
 120      '#type' => 'checkboxes',
 121      '#title' => t('Roles'),
 122      '#default_value' => variable_get('googleanalytics_roles', array()),
 123      '#options' => $role_options,
 124      '#description' => t('If none of the roles are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked (or excluded, depending on the setting above).'),
 125    );
 126  
 127    // Standard tracking configurations.
 128    $form['user_vis_settings'] = array(
 129      '#type' => 'fieldset',
 130      '#title' => t('User specific tracking settings'),
 131      '#collapsible' => TRUE,
 132    );
 133    $form['user_vis_settings']['googleanalytics_custom'] = array(
 134      '#type' => 'radios',
 135      '#title' => t('Custom tracking settings'),
 136      '#options' => array(
 137        t('Users cannot control whether they are tracked or not.'),
 138        t('Track users by default, but let individual users to opt out.'),
 139        t('Do not track users by default, but let individual users to opt in.')
 140      ),
 141      '#description' => t('Allow individual users to customize the visibility of tracking in their account settings. Only users with %permission permission are allowed to set their own preference.', array('%permission' => t('opt-in or out of tracking'))),
 142      '#default_value' => variable_get('googleanalytics_custom', 0),
 143    );
 144  
 145    // Link specific configurations.
 146    $form['linktracking'] = array(
 147      '#type' => 'fieldset',
 148      '#title' => t('Link tracking settings'),
 149      '#collapsible' => TRUE,
 150      '#collapsed' => FALSE,
 151    );
 152    $form['linktracking']['googleanalytics_trackoutgoing'] = array(
 153      '#type' => 'checkbox',
 154      '#title' => t('Track outbound links'),
 155      '#default_value' => variable_get('googleanalytics_trackoutgoing', 1),
 156      '#description' => t('Enables tracking of clicks on outgoing links.')
 157    );
 158    $form['linktracking']['googleanalytics_trackmailto'] = array(
 159      '#type' => 'checkbox',
 160      '#title' => t('Track mailto links'),
 161      '#default_value' => variable_get('googleanalytics_trackmailto', 1),
 162      '#description' => t('Enables tracking of clicks on mailto links.')
 163    );
 164    $form['linktracking']['googleanalytics_trackfiles'] = array(
 165      '#type' => 'checkbox',
 166      '#title' => t('Track download links'),
 167      '#default_value' => variable_get('googleanalytics_trackfiles', 1),
 168      '#description' => t('Enables tracking of clicks on links to files based on the file extensions list below.')
 169    );
 170    $form['linktracking']['googleanalytics_trackfiles_extensions'] = array(
 171      '#type' => 'textfield',
 172      '#title' => t('File extensions to track'),
 173      '#default_value' => variable_get('googleanalytics_trackfiles_extensions', GOOGLEANALYTICS_TRACKFILES_EXTENSIONS),
 174      '#description' => t('A pipe separated list of file extensions that should be tracked when clicked with regular expression support. Example: !extensions', array('!extensions' => GOOGLEANALYTICS_TRACKFILES_EXTENSIONS)),
 175      '#maxlength' => 255,
 176    );
 177    $form['linktracking']['googleanalytics_trackoutboundaspageview'] = array(
 178      '#type' => 'checkbox',
 179      '#title' => t('Track clicks on outbound links as page views'),
 180      '#default_value' => variable_get('googleanalytics_trackoutboundaspageview', 0),
 181      '#description' => t('By default outbound links are tracked as <em>Events</em>. In rare situations like tracking of <em>Goals</em> it may be required to track outbound clicks as page views.'),
 182    );
 183  
 184    // Privacy specific configurations.
 185    $form['tracking']['privacy'] = array(
 186      '#type' => 'fieldset',
 187      '#title' => t('Privacy'),
 188      '#collapsible' => TRUE,
 189      '#collapsed' => FALSE,
 190    );
 191    $form['tracking']['privacy']['googleanalytics_tracker_anonymizeip'] = array(
 192      '#type' => 'checkbox',
 193      '#title' => t('Anonymize visitors IP address'),
 194      '#description' => t('Tell Google Analytics to anonymize the information sent by the tracker objects by removing the last octet of the IP address prior to its storage. Note that this will slightly reduce the accuracy of geographic reporting. In some countries it is not allowed to collect personally identifying information for privacy reasons and this setting may help you to comply with the local laws.'),
 195      '#default_value' => variable_get('googleanalytics_tracker_anonymizeip', 0),
 196    );
 197    $form['tracking']['privacy']['googleanalytics_privacy_donottrack'] = array(
 198      '#type' => 'checkbox',
 199      '#title' => t('Universal web tracking opt-out'),
 200      '#description' => t('If enabled and your server receives the <a href="http://donottrack.us/">Do-Not-Track</a> header from the client browser, the Google Analytics module will not embed any tracking code into your site. Compliance with Do Not Track could be purely voluntary, enforced by industry self-regulation, or mandated by state or federal law. Please accept your visitors privacy. If they have opt-out from tracking and advertising, you should accept their personal decision. This feature is currently limited to logged in users and disabled page caching.'),
 201      '#default_value' => variable_get('googleanalytics_privacy_donottrack', 1),
 202    );
 203  
 204    // Backward compatibility only.
 205    // TODO: If currently not in use, hide the UI and later remove the code.
 206    $segmentation = variable_get('googleanalytics_segmentation', array());
 207  
 208    if (!empty($segmentation) || variable_get('googleanalytics_segmentation_DEPRECATED', FALSE)) {
 209      $profile_enabled = module_exists('profile');
 210      $form['segmentation'] = array(
 211        '#type' => 'fieldset',
 212        '#title' => t('User segmentation settings'),
 213        '#collapsible' => TRUE,
 214      );
 215  
 216      // Compile a list of fields to show.
 217      $fields = variable_get('googleanalytics_segmentation_default_fields', array('roles' => t('User roles')));
 218      if ($profile_enabled) {
 219        $result = db_query('SELECT name, title FROM {profile_fields} ORDER BY weight');
 220        while ($record = db_fetch_object($result)) {
 221          $fields[$record->name] = $record->title;
 222        }
 223      }
 224  
 225      $form['segmentation']['googleanalytics_segmentation'] = array(
 226        '#type' => 'select',
 227        '#title' => t('Add segmentation information to tracking code') . ' <span class="admin-disabled">***DEPRECATED***</span>',
 228        '#description' => t('Segment users based on different properties, additionally to the basic IP address based tracking provided by Google Analytics.') . ' <span class="admin-disabled">' . t('For most situations, Google recommend that you use Custom Variables to segment your visitors.') . '</span>' . (!$profile_enabled ? ' '. t('<a href="@module_list">Enable the profile module</a> to be able to use profile fields for more granular tracking.', array('@module_list' => url('admin/build/modules'))) : '') .' '. t('Make sure you will not associate (or permit any third party to associate) any data gathered from Your Website(s) (or such third parties\' website(s)) with any personally identifying information from any source as part of Your use (or such third parties\' use) of the Google Analytics service. For more information see section 8.1 in the <a href="@ga_tos">Google Analytics terms of use</a>.', array('@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html')) .' '. t('You can select multiple values.'),
 229        '#default_value' => $segmentation,
 230        '#options' => $fields,
 231        '#size' => count($fields)>3 ? 10 : 3,
 232        '#multiple' => TRUE
 233      );
 234    }
 235  
 236    $form['googleanalytics_custom_var'] = array(
 237        '#collapsed' => TRUE,
 238        '#collapsible' => TRUE,
 239      '#description' => t('You can add Google Analytics <a href="!custom_var_documentation">Custom Variables</a> here. These will be added to every page that Google Analytics tracking code appears on. Google Analytics will only accept custom variables if the <em>name</em> and <em>value</em> combined are less than 64 bytes after URL encoding. Keep the names as short as possible and expect long values to get trimmed. You may use tokens in custom variable values. Global and user tokens are always available; on node pages, node tokens are also available.', array('!custom_var_documentation' => 'http://code.google.com/intl/en/apis/analytics/docs/tracking/gaTrackingCustomVariables.html')),
 240      '#theme' => 'googleanalytics_admin_custom_var_table',
 241      '#title' => t('Custom variables'),
 242      '#tree' => TRUE,
 243      '#type' => 'fieldset',
 244    );
 245  
 246    $token_enabled = module_exists('token');
 247    $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
 248  
 249    // Google Analytics supports up to 5 custom variables.
 250    for ($i = 1; $i < 6; $i++) {
 251      // TODO: '#default_value' is currently broken, see http://drupal.org/node/410926.
 252      $form['googleanalytics_custom_var']['slots'][$i]['slot'] = array(
 253        //'#attributes' => array('readonly' => 'readonly'),
 254        //'#default_value' => $i,
 255        '#description' => t('Slot number'),
 256        '#disabled' => TRUE,
 257        '#size' => 1,
 258        '#type' => 'textfield',
 259        '#value' => $i,
 260      );
 261      $form['googleanalytics_custom_var']['slots'][$i]['name'] = array(
 262        '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['name']) ? $googleanalytics_custom_vars['slots'][$i]['name'] : '',
 263        '#description' => t('The custom variable name.'),
 264        '#size' => 20,
 265        '#type' => 'textfield',
 266      );
 267      $form['googleanalytics_custom_var']['slots'][$i]['value'] = array(
 268        '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['value']) ? $googleanalytics_custom_vars['slots'][$i]['value'] : '',
 269        '#description' => ($token_enabled ? t('The custom variable value. You may use tokens in this field.') : t('The custom variable value.')),
 270        '#type' => 'textfield',
 271      );
 272      if ($token_enabled) {
 273        $form['googleanalytics_custom_var']['slots'][$i]['value']['#element_validate'][] = 'googleanalytics_token_element_validate';
 274        $form['googleanalytics_custom_var']['slots'][$i]['value']['#element_validate'][] = 'token_element_validate';
 275        $form['googleanalytics_custom_var']['slots'][$i]['value']['#token_types'][] = 'node';
 276        $form['googleanalytics_custom_var']['slots'][$i]['value']['#token_types'][] = 'user';
 277      }
 278      $form['googleanalytics_custom_var']['slots'][$i]['scope'] = array(
 279        '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['scope']) ? $googleanalytics_custom_vars['slots'][$i]['scope'] : 3,
 280        '#description' => t('The scope for the custom variable.'),
 281        '#type' => 'select',
 282        '#options' => array(
 283          1 => t('Visitor'),
 284          2 => t('Session'),
 285          3 => t('Page'),
 286        ),
 287      );
 288    }
 289  
 290    $form['googleanalytics_custom_var']['googleanalytics_custom_var_description'] = array(
 291      '#type' => 'item',
 292      '#description' => t('You can supplement Google Analytics\' basic IP address tracking of visitors by segmenting users based on custom variables.') . (!$token_enabled ? ' '. t('<a href="@module_list">Enable the token module</a> to be able to use tokens for more granular tracking.', array('@module_list' => url('admin/build/modules'))) : '') .' '. t('Section 8.1 of the <a href="@ga_tos">Google Analytics terms of use</a> requires you to make sure you will not associate (or permit any third party to associate) any data gathered from your websites (or such third parties\' websites) with any personally identifying information from any source as part of your use (or such third parties\' use) of the Google Analytics\' service.', array('@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html')),
 293    );
 294    $form['googleanalytics_custom_var']['googleanalytics_custom_var_token_tree'] = array(
 295      '#theme' => 'token_tree',
 296      '#token_types' => array('node', 'user'),
 297    );
 298  
 299    // Advanced feature configurations.
 300    $form['advanced'] = array(
 301      '#type' => 'fieldset',
 302      '#title' => t('Advanced settings'),
 303      '#collapsible' => TRUE,
 304      '#collapsed' => TRUE,
 305    );
 306  
 307    $form['advanced']['googleanalytics_cache'] = array(
 308      '#type' => 'checkbox',
 309      '#title' => t('Locally cache tracking code file'),
 310      '#description' => t("If checked, the tracking code file is retrieved from Google Analytics and cached locally. It is updated daily from Google's servers to ensure updates to tracking code are reflected in the local copy. Do not activate this until after Google Analytics has confirmed that site tracking is working!"),
 311      '#default_value' => variable_get('googleanalytics_cache', 0),
 312    );
 313    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
 314      $form['advanced']['googleanalytics_cache']['#disabled'] = TRUE;
 315      $form['advanced']['googleanalytics_cache']['#description'] .= ' '. t('<a href="@url">Public file transfers</a> must be enabled to allow local caching.', array('@url' => url('admin/settings/file-system', array('query' => drupal_get_destination()))));
 316    }
 317  
 318    // Allow for tracking of the originating node when viewing translation sets.
 319    if (module_exists('translation')) {
 320      $form['advanced']['googleanalytics_translation_set'] = array(
 321        '#type' => 'checkbox',
 322        '#title' => t('Track translation sets as one unit'),
 323        '#description' => t('When a node is part of a translation set, record statistics for the originating node instead. This allows for a translation set to be treated as a single unit.'),
 324        '#default_value' => variable_get('googleanalytics_translation_set', 0),
 325      );
 326    }
 327  
 328    $site_search_dependencies = '<div class="admin-dependencies">';
 329    $site_search_dependencies .= t('Depends on: !dependencies', array('!dependencies' => (module_exists('search') ? t('@module (<span class="admin-enabled">enabled</span>)', array('@module' => 'Search')) : t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => 'Search')))));
 330    $site_search_dependencies .= '</div>';
 331  
 332    // Google already have many translations, if not - they display a note to change the language.
 333    global $language;
 334    $form['advanced']['googleanalytics_site_search'] = array(
 335      '#type' => 'checkbox',
 336      '#title' => t('Track internal search'),
 337      '#description' => t('If checked, internal search keywords are tracked. You must configure your Google account to use the internal query parameter <strong>search</strong>. For more information see <a href="@url">How do I set up Site Search for my profile</a>.', array('@url' => 'http://www.google.com/support/analytics/bin/answer.py?hl='. $language->language .'&answer=75817')) . $site_search_dependencies,
 338      '#default_value' => variable_get('googleanalytics_site_search', FALSE),
 339      '#disabled' => (module_exists('search') ? FALSE : TRUE),
 340    );
 341  
 342    $form['advanced']['googleanalytics_trackadsense'] = array(
 343      '#type' => 'checkbox',
 344      '#title' => t('Track AdSense ads'),
 345      '#description' => t('If checked, your AdSense ads will be tracked in your Google Analytics account.'),
 346      '#default_value' => variable_get('googleanalytics_trackadsense', FALSE),
 347    );
 348  
 349    $form['advanced']['codesnippet'] = array(
 350      '#type' => 'fieldset',
 351      '#title' => t('Custom JavaScript code'),
 352      '#collapsible' => TRUE,
 353      '#collapsed' => TRUE,
 354      '#description' => t('You can add custom Google Analytics <a href="@snippets">code snippets</a> here. These will be added to every page that Google Analytics appears on. Before you add custom code to the below textarea\'s you should read <a href="@ga_concepts_overview">Google Analytics Tracking Code - Functional Overview</a> and the <a href="@ga_js_api">Google Analytics Tracking API</a> documentation. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array('@snippets' => 'http://drupal.org/node/248699', '@ga_concepts_overview' => 'http://code.google.com/apis/analytics/docs/gaConceptsOverview.html', '@ga_js_api' => 'http://code.google.com/apis/analytics/docs/gaJSApi.html'))
 355    );
 356    $form['advanced']['codesnippet']['googleanalytics_codesnippet_before'] = array(
 357      '#type' => 'textarea',
 358      '#title' => t('Code snippet (before)'),
 359      '#default_value' => variable_get('googleanalytics_codesnippet_before', ''),
 360      '#rows' => 5,
 361      '#wysiwyg' => FALSE,
 362      '#description' => t("Code in this textarea will be added <strong>before</strong> _gaq.push(['_trackPageview'])."),
 363    );
 364    $form['advanced']['codesnippet']['googleanalytics_codesnippet_after'] = array(
 365      '#type' => 'textarea',
 366      '#title' => t('Code snippet (after)'),
 367      '#default_value' => variable_get('googleanalytics_codesnippet_after', ''),
 368      '#rows' => 5,
 369      '#wysiwyg' => FALSE,
 370      '#description' => t("Code in this textarea will be added <strong>after</strong> _gaq.push(['_trackPageview']). This is useful if you'd like to track a site in two accounts."),
 371    );
 372  
 373    /* hook_footer() is not able to add code to head. Upgrade to D7 required.
 374    $form['advanced']['googleanalytics_js_scope'] = array(
 375      '#type' => 'select',
 376      '#title' => t('JavaScript scope'),
 377      '#description' => t("Google recommends adding the external JavaScript files to header for performance reasons."),
 378      '#options' => array(
 379        'footer' => t('Footer'),
 380        'header' => t('Header'),
 381      ),
 382      '#default_value' => variable_get('googleanalytics_js_scope', 'header'),
 383    ); */
 384  
 385    return system_settings_form($form);
 386  }
 387  
 388  function googleanalytics_admin_settings_form_validate($form, &$form_state) {
 389    // Custom variables validation.
 390    foreach ($form_state['values']['googleanalytics_custom_var']['slots'] as $custom_var) {
 391      // Validate empty names/values.
 392      if (empty($custom_var['name']) && !empty($custom_var['value'])) {
 393        form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][name", 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'])));
 394      }
 395      elseif (!empty($custom_var['name']) && empty($custom_var['value'])) {
 396        form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][value", 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'])));
 397      }
 398  
 399      // If token module is not available, block tokens with personally identifying
 400      // information with an "ugly" value check. Normally the values are validated
 401      // via '#element_validate' functions that are not available without token module.
 402      if (!module_exists('token') && _googleanalytics_contains_forbidden_token($custom_var['value'])) {
 403        form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][value", t('The custom variable %element-title is using forbidden tokens with personal identifying information.', array('%element-title' => $custom_var['name'] ? $custom_var['name'] : $custom_var['slot'])));
 404      }
 405    }
 406  
 407    // Trim some text values.
 408    $form_state['values']['googleanalytics_account'] = trim($form_state['values']['googleanalytics_account']);
 409    $form_state['values']['googleanalytics_pages'] = trim($form_state['values']['googleanalytics_pages']);
 410    $form_state['values']['googleanalytics_codesnippet_before'] = trim($form_state['values']['googleanalytics_codesnippet_before']);
 411    $form_state['values']['googleanalytics_codesnippet_after'] = trim($form_state['values']['googleanalytics_codesnippet_after']);
 412  
 413    if (!preg_match('/^UA-\d{4,}-\d+$/', $form_state['values']['googleanalytics_account'])) {
 414      form_set_error('googleanalytics_account', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
 415    }
 416  
 417    // Clear obsolete local cache if cache has been disabled.
 418    if (empty($form_state['values']['googleanalytics_cache']) && $form['advanced']['googleanalytics_cache']['#default_value']) {
 419      googleanalytics_clear_js_cache();
 420    }
 421  
 422    // This is for the Newbie's who cannot read a text area description.
 423    if (stristr($form_state['values']['googleanalytics_codesnippet_before'], 'google-analytics.com/ga.js')) {
 424      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.'));
 425    }
 426    if (stristr($form_state['values']['googleanalytics_codesnippet_after'], 'google-analytics.com/ga.js')) {
 427      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.'));
 428    }
 429    if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_before'])) {
 430      form_set_error('googleanalytics_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
 431    }
 432    if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_after'])) {
 433      form_set_error('googleanalytics_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
 434    }
 435  }
 436  
 437  /**
 438   * Layout for the custom variables table in the admin settings form.
 439   */
 440  function theme_googleanalytics_admin_custom_var_table($form) {
 441  
 442    $header = array(
 443      array('data' => t('Slot')),
 444      array('data' => t('Name')),
 445      array('data' => t('Value')),
 446      array('data' => t('Scope')),
 447    );
 448  
 449    $rows = array();
 450    foreach (element_children($form['slots']) as $key => $id) {
 451      $rows[] = array(
 452        'data' => array(
 453          drupal_render($form['slots'][$id]['slot']),
 454          drupal_render($form['slots'][$id]['name']),
 455          drupal_render($form['slots'][$id]['value']),
 456          drupal_render($form['slots'][$id]['scope']),
 457        ),
 458      );
 459    }
 460  
 461    $output = theme('table', $header, $rows);
 462    $output .= drupal_render($form['googleanalytics_custom_var_description']);
 463    $output .= drupal_render($form['googleanalytics_custom_var_token_tree']);
 464  
 465    return $output;
 466  }
 467  
 468  /**
 469   * Validate a form element that should have tokens in it.
 470   *
 471   * For example:
 472   * @code
 473   * $form['my_node_text_element'] = array(
 474   *   '#type' => 'textfield',
 475   *   '#title' => t('Some text to token-ize that has a node context.'),
 476   *   '#default_value' => 'The title of this node is [node:title].',
 477   *   '#element_validate' => array('googleanalytics_token_element_validate'),
 478   * );
 479   * @endcode
 480   */
 481  function googleanalytics_token_element_validate(&$element, &$form_state) {
 482    $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
 483  
 484    if (!drupal_strlen($value)) {
 485      // Empty value needs no further validation since the element should depend
 486      // on using the '#required' FAPI property.
 487      return $element;
 488    }
 489  
 490    $tokens = token_scan($value);
 491    $title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
 492  
 493    $invalid_tokens = _googleanalytics_get_forbidden_tokens($tokens);
 494    if ($invalid_tokens) {
 495      form_error($element, t('The %element-title is using the following forbidden tokens with personal identifying information: @invalid-tokens.', array('%element-title' => $title, '@invalid-tokens' => implode(', ', $invalid_tokens))));
 496    }
 497  
 498    return $element;
 499  }
 500  
 501  function _googleanalytics_get_forbidden_tokens($value) {
 502    $invalid_tokens = array();
 503    $value_tokens = is_string($value) ? token_scan($value) : $value;
 504  
 505    // For porting/compatibility reasons with _googleanalytics_contains_forbidden_token()
 506    // the leading and trailing token separator need to be added to every value.
 507    $value_tokens = token_prepare_tokens($value_tokens);
 508  
 509    foreach ($value_tokens as $token) {
 510      // If token is forbidden, add it to invalid tokens array.
 511      if (_googleanalytics_contains_forbidden_token($token)) {
 512        $invalid_tokens[] = $token;
 513      }
 514    }
 515  
 516    array_unique($invalid_tokens);
 517    return $invalid_tokens;
 518  }
 519  
 520  /**
 521   * Validate if a string contains forbidden tokens not allowed by privacy rules.
 522   *
 523   * @param $token_string
 524   *   A string with one or more tokens to be validated.
 525   * @return boolean
 526   *   TRUE if blacklisted token has been found, otherwise FALSE.
 527   */
 528  function _googleanalytics_contains_forbidden_token($token_string) {
 529    // List of strings in tokens with personal identifying information not allowed
 530    // for privacy reasons. See section 8.1 of the Google Analytics terms of use
 531    // for more detailed information.
 532    //
 533    // This list can never ever be complete. For this reason it tries to use a
 534    // regex and may kill a few other valid tokens, but it's the only way to
 535    // protect users as much as possible from admins with illegal ideas.
 536    //
 537    // User tokens are not prefixed with colon to catch 'current-user' and 'user'.
 538    //
 539    // TODO: If someone have better ideas, share them, please!
 540    $token_blacklist = array(
 541      'author-uid]',
 542      'author-name',
 543      'author-mail',
 544      'author-homepage]',
 545      '[user-name]',
 546      '[user-id]',
 547      '[user-mail]',
 548      // [user] tokens
 549      '[user]',
 550      '[user-raw]',
 551      '[uid]',
 552      '[mail]',
 553      '[account-url]',
 554      '[account-edit]',
 555    );
 556  
 557    return preg_match('/' . implode('|', array_map('preg_quote', $token_blacklist)) . '/i', $token_string);
 558  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7