Projet

Général

Profil

Paste
Télécharger (23,8 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / piwik / piwik.admin.inc @ 3cb08e71

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Administrative page callbacks for the piwik module.
6
 */
7
8
/**
9
 * Implements hook_admin_settings() for configuring the module.
10
 */
11
function piwik_admin_settings_form($form_state) {
12
  $form['account'] = array(
13
    '#type' => 'fieldset',
14
    '#title' => t('General settings'),
15
  );
16
17
  $form['account']['piwik_site_id'] = array(
18
    '#type' => 'textfield',
19
    '#title' => t('Piwik site ID'),
20
    '#default_value' => variable_get('piwik_site_id', ''),
21
    '#size' => 15,
22
    '#maxlength' => 20,
23
    '#required' => TRUE,
24
    '#description' => t('The user account number is unique to the websites domain. Click the <strong>Settings</strong> link in your Piwik account, then the <strong>Websites</strong> tab and enter the appropriate site <strong>ID</strong> into this field.'),
25
  );
26
  $form['account']['piwik_url_http'] = array(
27
    '#type' => 'textfield',
28
    '#title' => t('Piwik HTTP URL'),
29
    '#default_value' => variable_get('piwik_url_http', ''),
30
    '#size' => 80,
31
    '#maxlength' => 255,
32
    '#required' => TRUE,
33
    '#description' => t('The URL to your Piwik base directory. Example: "http://www.example.com/piwik/".'),
34
  );
35
  $form['account']['piwik_url_https'] = array(
36
    '#type' => 'textfield',
37
    '#title' => t('Piwik HTTPS URL'),
38
    '#default_value' => variable_get('piwik_url_https', ''),
39
    '#size' => 80,
40
    '#maxlength' => 255,
41
    '#description' => t('The URL to your Piwik base directory with SSL certificate installed. Required if you track a SSL enabled website. Example: "https://www.example.com/piwik/".'),
42
  );
43
44
  // Visibility settings.
45
  $form['tracking_title'] = array(
46
    '#type' => 'item',
47
    '#title' => t('Tracking scope'),
48
  );
49
  $form['tracking'] = array(
50
    '#type' => 'vertical_tabs',
51
    '#attached' => array(
52
      'js' => array(drupal_get_path('module', 'piwik') . '/piwik.admin.js'),
53
    ),
54
  );
55
56
  $form['tracking']['domain_tracking'] = array(
57
    '#type' => 'fieldset',
58
    '#title' => t('Domains'),
59
  );
60
61
  global $cookie_domain;
62
  $multiple_sub_domains = array();
63
  foreach (array('www', 'app', 'shop') as $subdomain) {
64
    if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
65
      $multiple_sub_domains[] = $subdomain . $cookie_domain;
66
    }
67
    // IP addresses or localhost.
68
    else {
69
      $multiple_sub_domains[] = $subdomain . '.example.com';
70
    }
71
  }
72
73
  $form['tracking']['domain_tracking']['piwik_domain_mode'] = array(
74
    '#type' => 'radios',
75
    '#title' => t('What are you tracking?'),
76
    '#options' => array(
77
      0 => t('A single domain (default)') . '<div class="description">' . t('Domain: @domain', array('@domain' => $_SERVER['HTTP_HOST'])) . '</div>',
78
      1 => t('One domain with multiple subdomains') . '<div class="description">' . t('Examples: @domains', array('@domains' => implode(', ', $multiple_sub_domains))) . '</div>',
79
    ),
80
    '#default_value' => variable_get('piwik_domain_mode', 0),
81
  );
82
83
  // Page specific visibility configurations.
84
  $php_access = user_access('use PHP for tracking visibility');
85
  $visibility = variable_get('piwik_visibility_pages', 0);
86
  $pages = variable_get('piwik_pages', '');
87
88
  $form['tracking']['page_vis_settings'] = array(
89
    '#type' => 'fieldset',
90
    '#title' => t('Pages'),
91
    '#collapsible' => TRUE,
92
    '#collapsed' => TRUE,
93
  );
94
95
  if ($visibility == 2 && !$php_access) {
96
    $form['tracking']['page_vis_settings'] = array();
97
    $form['tracking']['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
98
    $form['tracking']['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $pages);
99
  }
100
  else {
101
    $options = array(
102
      t('Every page except the listed pages'),
103
      t('The listed pages only')
104
    );
105
    $description = t("Specify pages by using their paths. Enter one path per line. 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>'));
106
107
    if (module_exists('php') && $php_access) {
108
      $options[] = t('Pages on which this PHP code returns <code>TRUE</code> (experts only)');
109
      $title = t('Pages or PHP code');
110
      $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
111
    }
112
    else {
113
      $title = t('Pages');
114
    }
115
    $form['tracking']['page_vis_settings']['piwik_visibility_pages'] = array(
116
      '#type' => 'radios',
117
      '#title' => t('Add tracking to specific pages'),
118
      '#options' => $options,
119
      '#default_value' => $visibility,
120
    );
121
    $form['tracking']['page_vis_settings']['piwik_pages'] = array(
122
      '#type' => 'textarea',
123
      '#title' => $title,
124
      '#title_display' => 'invisible',
125
      '#default_value' => $pages,
126
      '#description' => $description,
127
      '#rows' => 10,
128
    );
129
  }
130
131
  // Render the role overview.
132
  $form['tracking']['role_vis_settings'] = array(
133
    '#type' => 'fieldset',
134
    '#title' => t('Roles'),
135
  );
136
137
  $form['tracking']['role_vis_settings']['piwik_visibility_roles'] = array(
138
    '#type' => 'radios',
139
    '#title' => t('Add tracking for specific roles'),
140
    '#options' => array(
141
      t('Add to the selected roles only'),
142
      t('Add to every role except the selected ones'),
143
    ),
144
    '#default_value' => variable_get('piwik_visibility_roles', 0),
145
  );
146
147
  $role_options = array_map('check_plain', user_roles());
148
  $form['tracking']['role_vis_settings']['piwik_roles'] = array(
149
    '#type' => 'checkboxes',
150
    '#title' => t('Roles'),
151
    '#default_value' => variable_get('piwik_roles', array()),
152
    '#options' => $role_options,
153
    '#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).'),
154
  );
155
156
  // Standard tracking configurations.
157
  $form['tracking']['user_vis_settings'] = array(
158
    '#type' => 'fieldset',
159
    '#title' => t('Users'),
160
  );
161
  $t_permission = array('%permission' => t('opt-in or out of tracking'));
162
  $form['tracking']['user_vis_settings']['piwik_custom'] = array(
163
    '#type' => 'radios',
164
    '#title' => t('Allow users to customize tracking on their account page'),
165
    '#options' => array(
166
      t('No customization allowed'),
167
      t('Tracking on by default, users with %permission permission can opt out', $t_permission),
168
      t('Tracking off by default, users with %permission permission can opt in', $t_permission)
169
    ),
170
    '#default_value' => variable_get('piwik_custom', 0),
171
  );
172
173
  // Link specific configurations.
174
  $form['tracking']['linktracking'] = array(
175
    '#type' => 'fieldset',
176
    '#title' => t('Links and downloads'),
177
  );
178
  $form['tracking']['linktracking']['piwik_track'] = array(
179
    '#type' => 'checkbox',
180
    '#title' => t('Track clicks on outbound links and downloads (clicks on file links) for the following extensions'),
181
    '#default_value' => variable_get('piwik_track', 1),
182
  );
183
  $form['tracking']['linktracking']['piwik_trackfiles_extensions'] = array(
184
    '#title' => t('List of download file extensions'),
185
    '#title_display' => 'invisible',
186
    '#type' => 'textfield',
187
    '#default_value' => variable_get('piwik_trackfiles_extensions', PK_TRACKFILES_EXTENSIONS),
188
    '#description' => t('A file extension list separated by the | character that will be tracked when clicked. Regular expressions are supported. For example: !extensions', array('!extensions' => PK_TRACKFILES_EXTENSIONS)),
189
    '#maxlength' => 255,
190
  );
191
192
  $form['tracking']['search'] = array(
193
    '#type' => 'fieldset',
194
    '#title' => t('Search'),
195
  );
196
197
  $site_search_dependencies = '<div class="admin-requirements">';
198
  $site_search_dependencies .= t('Requires: !module-list', array('!module-list' => (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')))));
199
  $site_search_dependencies .= '</div>';
200
201
  $form['tracking']['search']['piwik_site_search'] = array(
202
    '#type' => 'checkbox',
203
    '#title' => t('Track internal search'),
204
    '#description' => t('If checked, internal search keywords are tracked.') . $site_search_dependencies,
205
    '#default_value' => variable_get('piwik_site_search', FALSE),
206
    '#disabled' => (module_exists('search') ? FALSE : TRUE),
207
  );
208
209
  // Privacy specific configurations.
210
  $form['tracking']['privacy'] = array(
211
    '#type' => 'fieldset',
212
    '#title' => t('Privacy'),
213
  );
214
  $form['tracking']['privacy']['piwik_privacy_donottrack'] = array(
215
    '#type' => 'checkbox',
216
    '#title' => t('Universal web tracking opt-out'),
217
    '#description' => t('If enabled and your Piwik server receives the <a href="http://donottrack.us/">Do-Not-Track</a> header from the client browser, the Piwik server will not track the user. 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.'),
218
    '#default_value' => variable_get('piwik_privacy_donottrack', 1),
219
  );
220
221
  // Piwik page title tree view settings.
222
  $form['page_title_hierarchy'] = array(
223
    '#type' => 'fieldset',
224
    '#title' => t('Page titles hierarchy'),
225
    '#description' => t('This functionality enables a dynamically expandable tree view of your site page titles in your Piwik statistics. See in Piwik statistics under <em>Actions</em> > <em>Page titles</em>.'),
226
    '#collapsible' => TRUE,
227
    '#collapsed' => FALSE,
228
  );
229
  $form['page_title_hierarchy']['piwik_page_title_hierarchy'] = array(
230
    '#type' => 'checkbox',
231
    '#title' => t("Show page titles as hierarchy like breadcrumbs"),
232
    '#description' => t('By default Piwik tracks the current page title and shows you a flat list of the most popular titles. This enables a breadcrumbs like tree view.'),
233
    '#default_value' => variable_get('piwik_page_title_hierarchy', FALSE),
234
  );
235
  $form['page_title_hierarchy']['piwik_page_title_hierarchy_exclude_home'] = array(
236
    '#type' => 'checkbox',
237
    '#title' => t('Hide home page from hierarchy'),
238
    '#description' => t('If enabled, the "Home" item will be removed from the hierarchy to flatten the structure in the Piwik statistics. Hits to the home page will still be counted, but for other pages the hierarchy will start at level Home+1.'),
239
    '#default_value' => variable_get('piwik_page_title_hierarchy_exclude_home', TRUE),
240
  );
241
242
  $form['piwik_custom_var'] = array(
243
    '#collapsible' => TRUE,
244
  	'#collapsed' => TRUE,
245
    '#description' => t('You can add Piwiks <a href="!custom_var_documentation">Custom Variables</a> here. These will be added to every page that Piwik tracking code appears on. Custom variable names and values are limited to 200 characters in length. Keep the names and values as short as possible and expect long values to get trimmed. You may use tokens in custom variable names and values. Global and user tokens are always available; on node pages, node tokens are also available.', array('!custom_var_documentation' => 'http://piwik.org/docs/custom-variables/')),
246
    '#theme' => 'piwik_admin_custom_var_table',
247
    '#title' => t('Custom variables'),
248
    '#tree' => TRUE,
249
    '#type' => 'fieldset',
250
  );
251
252
  $piwik_custom_vars = variable_get('piwik_custom_var', array());
253
254
  // Piwik supports up to 5 custom variables.
255
  for ($i = 1; $i < 6; $i++) {
256
    $form['piwik_custom_var']['slots'][$i]['slot'] = array(
257
      '#default_value' => $i,
258
      '#description' => t('Slot number'),
259
      '#disabled' => TRUE,
260
      '#size' => 1,
261
      '#title' => t('Custom variable slot #@slot', array('@slot' => $i)),
262
      '#title_display' => 'invisible',
263
      '#type' => 'textfield',
264
    );
265
    $form['piwik_custom_var']['slots'][$i]['name'] = array(
266
      '#default_value' => !empty($piwik_custom_vars['slots'][$i]['name']) ? $piwik_custom_vars['slots'][$i]['name'] : '',
267
      '#description' => t('The custom variable name.'),
268
      '#maxlength' => 100,
269
    	'#size' => 20,
270
      '#title' => t('Custom variable name #@slot', array('@slot' => $i)),
271
      '#title_display' => 'invisible',
272
      '#type' => 'textfield',
273
    );
274
    $form['piwik_custom_var']['slots'][$i]['value'] = array(
275
      '#default_value' => !empty($piwik_custom_vars['slots'][$i]['value']) ? $piwik_custom_vars['slots'][$i]['value'] : '',
276
      '#description' => t('The custom variable value.'),
277
      '#maxlength' => 255,
278
      '#title' => t('Custom variable value #@slot', array('@slot' => $i)),
279
      '#title_display' => 'invisible',
280
      '#type' => 'textfield',
281
      '#element_validate' => array('piwik_token_element_validate'),
282
      '#token_types' => array('node'),
283
    );
284
    if (module_exists('token')) {
285
      $form['piwik_custom_var']['slots'][$i]['value']['#element_validate'][] = 'token_element_validate';
286
    }
287
    $form['piwik_custom_var']['slots'][$i]['scope'] = array(
288
      '#default_value' => !empty($piwik_custom_vars['slots'][$i]['scope']) ? $piwik_custom_vars['slots'][$i]['scope'] : 'visit',
289
      '#description' => t('The scope for the custom variable.'),
290
      '#title' => t('Custom variable slot #@slot', array('@slot' => $i)),
291
      '#title_display' => 'invisible',
292
      '#type' => 'select',
293
      '#options' => array(
294
        'visit' => t('Visit'),
295
        'page' => t('Page'),
296
      ),
297
    );
298
  }
299
300
  $form['piwik_custom_var']['piwik_custom_var_description'] = array(
301
    '#type' => 'item',
302
    '#description' => t('You can supplement Piwiks\' basic IP address tracking of visitors by segmenting users based on custom variables. 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 Piwik\' service.'),
303
  );
304
  $form['piwik_custom_var']['piwik_custom_var_token_tree'] = array(
305
    '#theme' => 'token_tree',
306
    '#token_types' => array('node'),
307
    '#dialog' => TRUE,
308
  );
309
310
  // Advanced feature configurations.
311
  $form['advanced'] = array(
312
    '#type' => 'fieldset',
313
    '#title' => t('Advanced settings'),
314
    '#collapsible' => TRUE,
315
    '#collapsed' => TRUE,
316
  );
317
318
  $form['advanced']['piwik_cache'] = array(
319
    '#type' => 'checkbox',
320
    '#title' => t('Locally cache tracking code file'),
321
    '#description' => t('If checked, the tracking code file is retrieved from your Piwik site and cached locally. It is updated daily to ensure updates to tracking code are reflected in the local copy.'),
322
    '#default_value' => variable_get('piwik_cache', 0),
323
  );
324
325
  // Allow for tracking of the originating node when viewing translation sets.
326
  if (module_exists('translation')) {
327
    $form['advanced']['piwik_translation_set'] = array(
328
      '#type' => 'checkbox',
329
      '#title' => t('Track translation sets as one unit'),
330
      '#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.'),
331
      '#default_value' => variable_get('piwik_translation_set', 0),
332
    );
333
  }
334
335
  // Code snippet settings.
336
  $form['advanced']['codesnippet'] = array(
337
    '#type' => 'fieldset',
338
    '#title' => t('Custom JavaScript code'),
339
    '#collapsible' => TRUE,
340
    '#collapsed' => TRUE,
341
    '#description' => t('You can add custom Piwik <a href="@snippets">code snippets</a> here. These will be added to every page that Piwik appears on. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array('@snippets' => 'http://piwik.org/docs/javascript-tracking/'))
342
  );
343
  $form['advanced']['codesnippet']['piwik_codesnippet_before'] = array(
344
    '#type' => 'textarea',
345
    '#title' => t('Code snippet (before)'),
346
    '#default_value' => variable_get('piwik_codesnippet_before', ''),
347
    '#rows' => 5,
348
    '#description' => t('Code in this textarea will be added <strong>before</strong> _paq.push(["trackPageView"]).')
349
  );
350
  $form['advanced']['codesnippet']['piwik_codesnippet_after'] = array(
351
    '#type' => 'textarea',
352
    '#title' => t('Code snippet (after)'),
353
    '#default_value' => variable_get('piwik_codesnippet_after', ''),
354
    '#rows' => 5,
355
    '#description' => t('Code in this textarea will be added <strong>after</strong> _paq.push(["trackPageView"]). This is useful if you\'d like to track a site in two accounts.')
356
  );
357
358
  $form['advanced']['piwik_js_scope'] = array(
359
    '#type' => 'select',
360
    '#title' => t('JavaScript scope'),
361
    '#description' => t("<strong>Warning:</strong> Adding the external JavaScript files to the footer region is recommended for performance reasons."),
362
    '#options' => array(
363
      'footer' => t('Footer'),
364
      'header' => t('Header'),
365
    ),
366
    '#default_value' => variable_get('piwik_js_scope', 'footer'),
367
  );
368
369
  return system_settings_form($form);
370
}
371
372
function piwik_admin_settings_form_validate($form, &$form_state) {
373
  // Custom variables validation.
374
  foreach ($form_state['values']['piwik_custom_var']['slots'] as $custom_var) {
375
    $form_state['values']['piwik_custom_var']['slots'][$custom_var['slot']]['name'] = trim($custom_var['name']);
376
    $form_state['values']['piwik_custom_var']['slots'][$custom_var['slot']]['value'] = trim($custom_var['value']);
377
378
    // Validate empty names/values.
379
    if (empty($custom_var['name']) && !empty($custom_var['value'])) {
380
      form_set_error("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.', array('@slot-number' => $custom_var['slot'])));
381
    }
382
    elseif (!empty($custom_var['name']) && empty($custom_var['value'])) {
383
      form_set_error("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.', array('@slot-number' => $custom_var['slot'])));
384
    }
385
  }
386
387
  // Trim some text area values.
388
  $form_state['values']['piwik_site_id'] = trim($form_state['values']['piwik_site_id']);
389
  $form_state['values']['piwik_pages'] = trim($form_state['values']['piwik_pages']);
390
  $form_state['values']['piwik_codesnippet_before'] = trim($form_state['values']['piwik_codesnippet_before']);
391
  $form_state['values']['piwik_codesnippet_after'] = trim($form_state['values']['piwik_codesnippet_after']);
392
393
  if (!preg_match('/^\d{1,}$/', $form_state['values']['piwik_site_id'])) {
394
    form_set_error('piwik_site_id', t('A valid Piwik site ID is an integer only.'));
395
  }
396
397
  $url = $form_state['values']['piwik_url_http'] . 'piwik.php';
398
  $result = drupal_http_request($url);
399
  if ($result->code != 200) {
400
    form_set_error('piwik_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)));
401
  }
402
403
  if (!empty($form_state['values']['piwik_url_https'])) {
404
    $url = $form_state['values']['piwik_url_https'] . 'piwik.php';
405
    $result = drupal_http_request($url);
406
    if ($result->code != 200) {
407
      form_set_error('piwik_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)));
408
    }
409
  }
410
411
  // Delete obsolete local cache file.
412
  if (empty($form_state['values']['piwik_cache']) && $form['advanced']['piwik_cache']['#default_value']) {
413
    piwik_clear_js_cache();
414
  }
415
416
  // This is for the Newbie's who cannot read a text area description.
417
  if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['piwik_codesnippet_before'])) {
418
    form_set_error('piwik_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
419
  }
420
  if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['piwik_codesnippet_after'])) {
421
    form_set_error('piwik_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
422
  }
423
}
424
425
/**
426
 * Layout for the custom variables table in the admin settings form.
427
 */
428
function theme_piwik_admin_custom_var_table($variables) {
429
  $form = $variables['form'];
430
431
  $header = array(
432
    array('data' => t('Slot')),
433
    array('data' => t('Name')),
434
    array('data' => t('Value')),
435
    array('data' => t('Scope')),
436
  );
437
438
  $rows = array();
439
  foreach (element_children($form['slots']) as $key => $id) {
440
    $rows[] = array(
441
      'data' => array(
442
        drupal_render($form['slots'][$id]['slot']),
443
        drupal_render($form['slots'][$id]['name']),
444
        drupal_render($form['slots'][$id]['value']),
445
        drupal_render($form['slots'][$id]['scope']),
446
      ),
447
    );
448
  }
449
450
  $output = theme('table', array('header' => $header, 'rows' => $rows));
451
  $output .= drupal_render($form['piwik_custom_var_description']);
452
  $output .= drupal_render($form['piwik_custom_var_token_tree']);
453
454
  return $output;
455
}
456
457
/**
458
 * Validate a form element that should have tokens in it.
459
 *
460
 * For example:
461
 * @code
462
 * $form['my_node_text_element'] = array(
463
 *   '#type' => 'textfield',
464
 *   '#title' => t('Some text to token-ize that has a node context.'),
465
 *   '#default_value' => 'The title of this node is [node:title].',
466
 *   '#element_validate' => array('piwik_token_element_validate'),
467
 * );
468
 * @endcode
469
 */
470
function piwik_token_element_validate(&$element, &$form_state) {
471
  $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
472
473
  if (!drupal_strlen($value)) {
474
    // Empty value needs no further validation since the element should depend
475
    // on using the '#required' FAPI property.
476
    return $element;
477
  }
478
479
  $tokens = token_scan($value);
480
  $invalid_tokens = _piwik_get_forbidden_tokens($tokens);
481
  if ($invalid_tokens) {
482
    form_error($element, t('The %element-title is using the following forbidden tokens with personal identifying information: @invalid-tokens.', array('%element-title' => $element['#title'], '@invalid-tokens' => implode(', ', $invalid_tokens))));
483
  }
484
485
  return $element;
486
}
487
488
function _piwik_get_forbidden_tokens($value) {
489
  $invalid_tokens = array();
490
  $value_tokens = is_string($value) ? token_scan($value) : $value;
491
492
  foreach ($value_tokens as $type => $tokens) {
493
    if (array_filter($tokens, '_piwik_contains_forbidden_token')) {
494
      $invalid_tokens = array_merge($invalid_tokens, array_values($tokens));
495
    }
496
  }
497
498
  array_unique($invalid_tokens);
499
  return $invalid_tokens;
500
}
501
502
/**
503
 * Validate if a string contains forbidden tokens not allowed by privacy rules.
504
 *
505
 * @param $token_string
506
 *   A string with one or more tokens to be validated.
507
 * @return boolean
508
 *   TRUE if blacklisted token has been found, otherwise FALSE.
509
 */
510
function _piwik_contains_forbidden_token($token_string) {
511
  // List of strings in tokens with personal identifying information not allowed
512
  // for privacy reasons. See section 8.1 of the Google Analytics terms of use
513
  // for more detailed information.
514
  //
515
  // This list can never ever be complete. For this reason it tries to use a
516
  // regex and may kill a few other valid tokens, but it's the only way to
517
  // protect users as much as possible from admins with illegal ideas.
518
  //
519
  // User tokens are not prefixed with colon to catch 'current-user' and 'user'.
520
  //
521
  // TODO: If someone have better ideas, share them, please!
522
  $token_blacklist = array(
523
    ':author]',
524
    ':author:edit-url]',
525
    ':author:url]',
526
    ':author:path]',
527
    ':current-user]',
528
    ':current-user:original]',
529
    ':fid]',
530
    ':mail]',
531
    ':name]',
532
    ':uid]',
533
    ':one-time-login-url]',
534
    ':owner]',
535
    ':owner:cancel-url]',
536
    ':owner:edit-url]',
537
    ':owner:url]',
538
    ':owner:path]',
539
    'user:cancel-url]',
540
    'user:edit-url]',
541
    'user:url]',
542
    'user:path]',
543
    'user:picture]',
544
    // addressfield_tokens.module
545
    ':first-name]',
546
    ':last-name]',
547
    ':name-line]',
548
    ':mc-address]',
549
    ':thoroughfare]',
550
    ':premise]',
551
    // realname.module
552
    ':name-raw]',
553
    // token.module
554
    ':ip-address]',
555
  );
556
557
  return preg_match('/' . implode('|', array_map('preg_quote', $token_blacklist)) . '/i', $token_string);
558
}