Projet

Général

Profil

Paste
Télécharger (26,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / piwik / piwik.admin.inc @ de644da6

1
<?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
  // Required for automated form save testing only.
44
  $form['account']['piwik_url_skiperror'] = array(
45
    '#type' => 'hidden',
46
    '#default_value' => FALSE,
47
  );
48

    
49
  // Visibility settings.
50
  $form['tracking_title'] = array(
51
    '#type' => 'item',
52
    '#title' => t('Tracking scope'),
53
  );
54
  $form['tracking'] = array(
55
    '#type' => 'vertical_tabs',
56
    '#attached' => array(
57
      'js' => array(drupal_get_path('module', 'piwik') . '/piwik.admin.js'),
58
    ),
59
  );
60

    
61
  $form['tracking']['domain_tracking'] = array(
62
    '#type' => 'fieldset',
63
    '#title' => t('Domains'),
64
  );
65

    
66
  global $cookie_domain;
67
  $multiple_sub_domains = array();
68
  foreach (array('www', 'app', 'shop') as $subdomain) {
69
    if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
70
      $multiple_sub_domains[] = $subdomain . $cookie_domain;
71
    }
72
    // IP addresses or localhost.
73
    else {
74
      $multiple_sub_domains[] = $subdomain . '.example.com';
75
    }
76
  }
77

    
78
  $form['tracking']['domain_tracking']['piwik_domain_mode'] = array(
79
    '#type' => 'radios',
80
    '#title' => t('What are you tracking?'),
81
    '#options' => array(
82
      0 => t('A single domain (default)') . '<div class="description">' . t('Domain: @domain', array('@domain' => $_SERVER['HTTP_HOST'])) . '</div>',
83
      1 => t('One domain with multiple subdomains') . '<div class="description">' . t('Examples: @domains', array('@domains' => implode(', ', $multiple_sub_domains))) . '</div>',
84
    ),
85
    '#default_value' => variable_get('piwik_domain_mode', 0),
86
  );
87

    
88
  // Page specific visibility configurations.
89
  $php_access = user_access('use PHP for tracking visibility');
90
  $visibility = variable_get('piwik_visibility_pages', 0);
91
  $pages = variable_get('piwik_pages', PIWIK_PAGES);
92

    
93
  $form['tracking']['page_vis_settings'] = array(
94
    '#type' => 'fieldset',
95
    '#title' => t('Pages'),
96
    '#collapsible' => TRUE,
97
    '#collapsed' => TRUE,
98
  );
99

    
100
  if ($visibility == 2 && !$php_access) {
101
    $form['tracking']['page_vis_settings'] = array();
102
    $form['tracking']['page_vis_settings']['piwik_visibility_pages'] = array('#type' => 'value', '#value' => 2);
103
    $form['tracking']['page_vis_settings']['piwik_pages'] = array('#type' => 'value', '#value' => $pages);
104
  }
105
  else {
106
    $options = array(
107
      t('Every page except the listed pages'),
108
      t('The listed pages only')
109
    );
110
    $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>'));
111

    
112
    if (module_exists('php') && $php_access) {
113
      $options[] = t('Pages on which this PHP code returns <code>TRUE</code> (experts only)');
114
      $title = t('Pages or PHP code');
115
      $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 ?>'));
116
    }
117
    else {
118
      $title = t('Pages');
119
    }
120
    $form['tracking']['page_vis_settings']['piwik_visibility_pages'] = array(
121
      '#type' => 'radios',
122
      '#title' => t('Add tracking to specific pages'),
123
      '#options' => $options,
124
      '#default_value' => $visibility,
125
    );
126
    $form['tracking']['page_vis_settings']['piwik_pages'] = array(
127
      '#type' => 'textarea',
128
      '#title' => $title,
129
      '#title_display' => 'invisible',
130
      '#default_value' => $pages,
131
      '#description' => $description,
132
      '#rows' => 10,
133
    );
134
  }
135

    
136
  // Render the role overview.
137
  $form['tracking']['role_vis_settings'] = array(
138
    '#type' => 'fieldset',
139
    '#title' => t('Roles'),
140
  );
141

    
142
  $form['tracking']['role_vis_settings']['piwik_visibility_roles'] = array(
143
    '#type' => 'radios',
144
    '#title' => t('Add tracking for specific roles'),
145
    '#options' => array(
146
      t('Add to the selected roles only'),
147
      t('Add to every role except the selected ones'),
148
    ),
149
    '#default_value' => variable_get('piwik_visibility_roles', 0),
150
  );
151

    
152
  $role_options = array_map('check_plain', user_roles());
153
  $form['tracking']['role_vis_settings']['piwik_roles'] = array(
154
    '#type' => 'checkboxes',
155
    '#title' => t('Roles'),
156
    '#default_value' => variable_get('piwik_roles', array()),
157
    '#options' => $role_options,
158
    '#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).'),
159
  );
160

    
161
  // Standard tracking configurations.
162
  $form['tracking']['user_vis_settings'] = array(
163
    '#type' => 'fieldset',
164
    '#title' => t('Users'),
165
  );
166
  $t_permission = array('%permission' => t('opt-in or out of tracking'));
167
  $form['tracking']['user_vis_settings']['piwik_custom'] = array(
168
    '#type' => 'radios',
169
    '#title' => t('Allow users to customize tracking on their account page'),
170
    '#options' => array(
171
      t('No customization allowed'),
172
      t('Tracking on by default, users with %permission permission can opt out', $t_permission),
173
      t('Tracking off by default, users with %permission permission can opt in', $t_permission)
174
    ),
175
    '#default_value' => variable_get('piwik_custom', 0),
176
  );
177
  $form['tracking']['user_vis_settings']['piwik_trackuserid'] = array(
178
    '#type' => 'checkbox',
179
    '#title' => t('Track User ID'),
180
    '#default_value' => variable_get('piwik_trackuserid', 0),
181
    '#description' => t('User ID enables the analysis of groups of sessions, across devices, using a unique, persistent, and non-personally identifiable ID string representing a user. <a href="@url">Learn more about the benefits of using User ID</a>.', array('@url' => 'http://piwik.org/docs/user-id/')),
182
  );
183

    
184
  // Link specific configurations.
185
  $form['tracking']['linktracking'] = array(
186
    '#type' => 'fieldset',
187
    '#title' => t('Links and downloads'),
188
  );
189
  $form['tracking']['linktracking']['piwik_trackmailto'] = array(
190
    '#type' => 'checkbox',
191
    '#title' => t('Track clicks on mailto links'),
192
    '#default_value' => variable_get('piwik_trackmailto', 1),
193
  );
194
  $form['tracking']['linktracking']['piwik_track'] = array(
195
    '#type' => 'checkbox',
196
    '#title' => t('Track clicks on outbound links and downloads (clicks on file links) for the following extensions'),
197
    '#default_value' => variable_get('piwik_track', 1),
198
  );
199
  $form['tracking']['linktracking']['piwik_trackfiles_extensions'] = array(
200
    '#title' => t('List of download file extensions'),
201
    '#title_display' => 'invisible',
202
    '#type' => 'textfield',
203
    '#default_value' => variable_get('piwik_trackfiles_extensions', PIWIK_TRACKFILES_EXTENSIONS),
204
    '#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' => PIWIK_TRACKFILES_EXTENSIONS)),
205
    '#maxlength' => 500,
206
    '#states' => array(
207
      'enabled' => array(
208
        ':input[name="piwik_track"]' => array('checked' => TRUE),
209
      ),
210
      # Note: Form required marker is not visible as title is invisible.
211
      'required' => array(
212
        ':input[name="piwik_track"]' => array('checked' => TRUE),
213
      ),
214
    ),
215
  );
216

    
217
  // Message specific configurations.
218
  $form['tracking']['messagetracking'] = array(
219
    '#type' => 'fieldset',
220
    '#title' => t('Messages'),
221
  );
222
  $form['tracking']['messagetracking']['piwik_trackmessages'] = array(
223
    '#type' => 'checkboxes',
224
    '#title' => t('Track messages of type'),
225
    '#default_value' => variable_get('piwik_trackmessages', array()),
226
    '#description' => t('This will track the selected message types shown to users. Tracking of form validation errors may help you identifying usability issues in your site. Every message is tracked as one individual event. Messages from excluded pages cannot tracked.'),
227
    '#options' => array(
228
      'status' => t('Status message'),
229
      'warning' => t('Warning message'),
230
      'error' => t('Error message'),
231
    ),
232
  );
233

    
234
  $form['tracking']['search'] = array(
235
    '#type' => 'fieldset',
236
    '#title' => t('Search'),
237
  );
238

    
239
  $site_search_dependencies = '<div class="admin-requirements">';
240
  $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')))));
241
  $site_search_dependencies .= '</div>';
242

    
243
  $form['tracking']['search']['piwik_site_search'] = array(
244
    '#type' => 'checkbox',
245
    '#title' => t('Track internal search'),
246
    '#description' => t('If checked, internal search keywords are tracked.') . $site_search_dependencies,
247
    '#default_value' => variable_get('piwik_site_search', FALSE),
248
    '#disabled' => (module_exists('search') ? FALSE : TRUE),
249
  );
250

    
251
  // Privacy specific configurations.
252
  $form['tracking']['privacy'] = array(
253
    '#type' => 'fieldset',
254
    '#title' => t('Privacy'),
255
  );
256
  $form['tracking']['privacy']['piwik_privacy_donottrack'] = array(
257
    '#type' => 'checkbox',
258
    '#title' => t('Universal web tracking opt-out'),
259
    '#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.'),
260
    '#default_value' => variable_get('piwik_privacy_donottrack', 1),
261
  );
262

    
263
  // Piwik page title tree view settings.
264
  $form['page_title_hierarchy'] = array(
265
    '#type' => 'fieldset',
266
    '#title' => t('Page titles hierarchy'),
267
    '#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>.'),
268
    '#collapsible' => TRUE,
269
    '#collapsed' => FALSE,
270
  );
271
  $form['page_title_hierarchy']['piwik_page_title_hierarchy'] = array(
272
    '#type' => 'checkbox',
273
    '#title' => t("Show page titles as hierarchy like breadcrumbs"),
274
    '#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.'),
275
    '#default_value' => variable_get('piwik_page_title_hierarchy', FALSE),
276
  );
277
  $form['page_title_hierarchy']['piwik_page_title_hierarchy_exclude_home'] = array(
278
    '#type' => 'checkbox',
279
    '#title' => t('Hide home page from hierarchy'),
280
    '#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.'),
281
    '#default_value' => variable_get('piwik_page_title_hierarchy_exclude_home', TRUE),
282
  );
283

    
284
  $form['piwik_custom_var'] = array(
285
    '#collapsible' => TRUE,
286
  	'#collapsed' => TRUE,
287
    '#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/')),
288
    '#theme' => 'piwik_admin_custom_var_table',
289
    '#title' => t('Custom variables'),
290
    '#tree' => TRUE,
291
    '#type' => 'fieldset',
292
  );
293

    
294
  $piwik_custom_vars = variable_get('piwik_custom_var', array());
295

    
296
  // Piwik supports up to 5 custom variables.
297
  for ($i = 1; $i < 6; $i++) {
298
    $form['piwik_custom_var']['slots'][$i]['slot'] = array(
299
      '#default_value' => $i,
300
      '#description' => t('Slot number'),
301
      '#disabled' => TRUE,
302
      '#size' => 1,
303
      '#title' => t('Custom variable slot #@slot', array('@slot' => $i)),
304
      '#title_display' => 'invisible',
305
      '#type' => 'textfield',
306
    );
307
    $form['piwik_custom_var']['slots'][$i]['name'] = array(
308
      '#default_value' => !empty($piwik_custom_vars['slots'][$i]['name']) ? $piwik_custom_vars['slots'][$i]['name'] : '',
309
      '#description' => t('The custom variable name.'),
310
      '#maxlength' => 100,
311
    	'#size' => 20,
312
      '#title' => t('Custom variable name #@slot', array('@slot' => $i)),
313
      '#title_display' => 'invisible',
314
      '#type' => 'textfield',
315
    );
316
    $form['piwik_custom_var']['slots'][$i]['value'] = array(
317
      '#default_value' => !empty($piwik_custom_vars['slots'][$i]['value']) ? $piwik_custom_vars['slots'][$i]['value'] : '',
318
      '#description' => t('The custom variable value.'),
319
      '#maxlength' => 255,
320
      '#title' => t('Custom variable value #@slot', array('@slot' => $i)),
321
      '#title_display' => 'invisible',
322
      '#type' => 'textfield',
323
      '#element_validate' => array('piwik_token_element_validate'),
324
      '#token_types' => array('node'),
325
    );
326
    if (module_exists('token')) {
327
      $form['piwik_custom_var']['slots'][$i]['value']['#element_validate'][] = 'token_element_validate';
328
    }
329
    $form['piwik_custom_var']['slots'][$i]['scope'] = array(
330
      '#default_value' => !empty($piwik_custom_vars['slots'][$i]['scope']) ? $piwik_custom_vars['slots'][$i]['scope'] : 'visit',
331
      '#description' => t('The scope for the custom variable.'),
332
      '#title' => t('Custom variable slot #@slot', array('@slot' => $i)),
333
      '#title_display' => 'invisible',
334
      '#type' => 'select',
335
      '#options' => array(
336
        'visit' => t('Visit'),
337
        'page' => t('Page'),
338
      ),
339
    );
340
  }
341

    
342
  $form['piwik_custom_var']['piwik_custom_var_description'] = array(
343
    '#type' => 'item',
344
    '#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.'),
345
  );
346
  $form['piwik_custom_var']['piwik_custom_var_token_tree'] = array(
347
    '#theme' => 'token_tree',
348
    '#token_types' => array('node'),
349
    '#dialog' => TRUE,
350
  );
351

    
352
  // Advanced feature configurations.
353
  $form['advanced'] = array(
354
    '#type' => 'fieldset',
355
    '#title' => t('Advanced settings'),
356
    '#collapsible' => TRUE,
357
    '#collapsed' => TRUE,
358
  );
359

    
360
  $form['advanced']['piwik_cache'] = array(
361
    '#type' => 'checkbox',
362
    '#title' => t('Locally cache tracking code file'),
363
    '#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.'),
364
    '#default_value' => variable_get('piwik_cache', 0),
365
  );
366

    
367
  // Allow for tracking of the originating node when viewing translation sets.
368
  if (module_exists('translation')) {
369
    $form['advanced']['piwik_translation_set'] = array(
370
      '#type' => 'checkbox',
371
      '#title' => t('Track translation sets as one unit'),
372
      '#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.'),
373
      '#default_value' => variable_get('piwik_translation_set', 0),
374
    );
375
  }
376

    
377
  // Code snippet settings.
378
  $form['advanced']['codesnippet'] = array(
379
    '#type' => 'fieldset',
380
    '#title' => t('Custom JavaScript code'),
381
    '#collapsible' => TRUE,
382
    '#collapsed' => TRUE,
383
    '#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/'))
384
  );
385
  $form['advanced']['codesnippet']['piwik_codesnippet_before'] = array(
386
    '#type' => 'textarea',
387
    '#title' => t('Code snippet (before)'),
388
    '#default_value' => variable_get('piwik_codesnippet_before', ''),
389
    '#rows' => 5,
390
    '#description' => t('Code in this textarea will be added <strong>before</strong> _paq.push(["trackPageView"]).')
391
  );
392
  $form['advanced']['codesnippet']['piwik_codesnippet_after'] = array(
393
    '#type' => 'textarea',
394
    '#title' => t('Code snippet (after)'),
395
    '#default_value' => variable_get('piwik_codesnippet_after', ''),
396
    '#rows' => 5,
397
    '#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.')
398
  );
399

    
400
  $form['advanced']['piwik_js_scope'] = array(
401
    '#type' => 'select',
402
    '#title' => t('JavaScript scope'),
403
    '#description' => t("Piwik recommends adding the tracking code to the header for performance reasons."),
404
    '#options' => array(
405
      'footer' => t('Footer'),
406
      'header' => t('Header'),
407
    ),
408
    '#default_value' => variable_get('piwik_js_scope', 'header'),
409
  );
410

    
411
  return system_settings_form($form);
412
}
413

    
414
function piwik_admin_settings_form_validate($form, &$form_state) {
415
  // Custom variables validation.
416
  foreach ($form_state['values']['piwik_custom_var']['slots'] as $custom_var) {
417
    $form_state['values']['piwik_custom_var']['slots'][$custom_var['slot']]['name'] = trim($custom_var['name']);
418
    $form_state['values']['piwik_custom_var']['slots'][$custom_var['slot']]['value'] = trim($custom_var['value']);
419

    
420
    // Validate empty names/values.
421
    if (empty($custom_var['name']) && !empty($custom_var['value'])) {
422
      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'])));
423
    }
424
    elseif (!empty($custom_var['name']) && empty($custom_var['value'])) {
425
      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'])));
426
    }
427
  }
428

    
429
  // Trim some text area values.
430
  $form_state['values']['piwik_site_id'] = trim($form_state['values']['piwik_site_id']);
431
  $form_state['values']['piwik_pages'] = trim($form_state['values']['piwik_pages']);
432
  $form_state['values']['piwik_codesnippet_before'] = trim($form_state['values']['piwik_codesnippet_before']);
433
  $form_state['values']['piwik_codesnippet_after'] = trim($form_state['values']['piwik_codesnippet_after']);
434

    
435
  if (!preg_match('/^\d{1,}$/', $form_state['values']['piwik_site_id'])) {
436
    form_set_error('piwik_site_id', t('A valid Piwik site ID is an integer only.'));
437
  }
438

    
439
  $piwik_url = $form_state['values']['piwik_url_http'];
440
  if ('/' != drupal_substr($piwik_url, -1, 1)) {
441
    $piwik_url = $piwik_url . '/';
442
    $form_state['values']['piwik_url_http'] = $piwik_url;
443
  }
444
  $url = $piwik_url . 'piwik.php';
445
  $result = drupal_http_request($url);
446
  if ($result->code != 200 && $form_state['values']['piwik_url_skiperror'] == FALSE) {
447
    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)));
448
  }
449

    
450
  if (!empty($form_state['values']['piwik_url_https'])) {
451
    $piwik_url = $form_state['values']['piwik_url_https'];
452
    if ('/' != drupal_substr($piwik_url, -1, 1)) {
453
      $piwik_url = $piwik_url . '/';
454
      $form_state['values']['piwik_url_https'] = $piwik_url;
455
    }
456
    $url = $piwik_url . 'piwik.php';
457
    $result = drupal_http_request($url);
458
    if ($result->code != 200 && $form_state['values']['piwik_url_skiperror'] == FALSE) {
459
      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)));
460
    }
461
  }
462

    
463
  // Delete obsolete local cache file.
464
  if (empty($form_state['values']['piwik_cache']) && $form['advanced']['piwik_cache']['#default_value']) {
465
    piwik_clear_js_cache();
466
  }
467

    
468
  // This is for the Newbie's who cannot read a text area description.
469
  if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['piwik_codesnippet_before'])) {
470
    form_set_error('piwik_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
471
  }
472
  if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['piwik_codesnippet_after'])) {
473
    form_set_error('piwik_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
474
  }
475
}
476

    
477
/**
478
 * Layout for the custom variables table in the admin settings form.
479
 */
480
function theme_piwik_admin_custom_var_table($variables) {
481
  $form = $variables['form'];
482

    
483
  $header = array(
484
    array('data' => t('Slot')),
485
    array('data' => t('Name')),
486
    array('data' => t('Value')),
487
    array('data' => t('Scope')),
488
  );
489

    
490
  $rows = array();
491
  foreach (element_children($form['slots']) as $key => $id) {
492
    $rows[] = array(
493
      'data' => array(
494
        drupal_render($form['slots'][$id]['slot']),
495
        drupal_render($form['slots'][$id]['name']),
496
        drupal_render($form['slots'][$id]['value']),
497
        drupal_render($form['slots'][$id]['scope']),
498
      ),
499
    );
500
  }
501

    
502
  $output = theme('table', array('header' => $header, 'rows' => $rows));
503
  $output .= drupal_render($form['piwik_custom_var_description']);
504
  $output .= drupal_render($form['piwik_custom_var_token_tree']);
505

    
506
  return $output;
507
}
508

    
509
/**
510
 * Validate a form element that should have tokens in it.
511
 *
512
 * For example:
513
 * @code
514
 * $form['my_node_text_element'] = array(
515
 *   '#type' => 'textfield',
516
 *   '#title' => t('Some text to token-ize that has a node context.'),
517
 *   '#default_value' => 'The title of this node is [node:title].',
518
 *   '#element_validate' => array('piwik_token_element_validate'),
519
 * );
520
 * @endcode
521
 */
522
function piwik_token_element_validate(&$element, &$form_state) {
523
  $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
524

    
525
  if (!drupal_strlen($value)) {
526
    // Empty value needs no further validation since the element should depend
527
    // on using the '#required' FAPI property.
528
    return $element;
529
  }
530

    
531
  $tokens = token_scan($value);
532
  $invalid_tokens = _piwik_get_forbidden_tokens($tokens);
533
  if ($invalid_tokens) {
534
    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))));
535
  }
536

    
537
  return $element;
538
}
539

    
540
function _piwik_get_forbidden_tokens($value) {
541
  $invalid_tokens = array();
542
  $value_tokens = is_string($value) ? token_scan($value) : $value;
543

    
544
  foreach ($value_tokens as $type => $tokens) {
545
    if (array_filter($tokens, '_piwik_contains_forbidden_token')) {
546
      $invalid_tokens = array_merge($invalid_tokens, array_values($tokens));
547
    }
548
  }
549

    
550
  array_unique($invalid_tokens);
551
  return $invalid_tokens;
552
}
553

    
554
/**
555
 * Validate if a string contains forbidden tokens not allowed by privacy rules.
556
 *
557
 * @param $token_string
558
 *   A string with one or more tokens to be validated.
559
 * @return boolean
560
 *   TRUE if blacklisted token has been found, otherwise FALSE.
561
 */
562
function _piwik_contains_forbidden_token($token_string) {
563
  // List of strings in tokens with personal identifying information not allowed
564
  // for privacy reasons. See section 8.1 of the Google Analytics terms of use
565
  // for more detailed information.
566
  //
567
  // This list can never ever be complete. For this reason it tries to use a
568
  // regex and may kill a few other valid tokens, but it's the only way to
569
  // protect users as much as possible from admins with illegal ideas.
570
  //
571
  // User tokens are not prefixed with colon to catch 'current-user' and 'user'.
572
  //
573
  // TODO: If someone have better ideas, share them, please!
574
  $token_blacklist = array(
575
    ':author]',
576
    ':author:edit-url]',
577
    ':author:url]',
578
    ':author:path]',
579
    ':current-user]',
580
    ':current-user:original]',
581
    ':fid]',
582
    ':mail]',
583
    ':name]',
584
    ':uid]',
585
    ':one-time-login-url]',
586
    ':owner]',
587
    ':owner:cancel-url]',
588
    ':owner:edit-url]',
589
    ':owner:url]',
590
    ':owner:path]',
591
    'user:cancel-url]',
592
    'user:edit-url]',
593
    'user:url]',
594
    'user:path]',
595
    'user:picture]',
596
    // addressfield_tokens.module
597
    ':first-name]',
598
    ':last-name]',
599
    ':name-line]',
600
    ':mc-address]',
601
    ':thoroughfare]',
602
    ':premise]',
603
    // realname.module
604
    ':name-raw]',
605
    // token.module
606
    ':ip-address]',
607
  );
608

    
609
  return preg_match('/' . implode('|', array_map('preg_quote', $token_blacklist)) . '/i', $token_string);
610
}