matomo.admin.inc 28 KB

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