From 84aa0bb4f5d1d2f636a676c745a6900c1fad4e85 Mon Sep 17 00:00:00 2001
From: Mario Trojan <mario.trojan@uni-tuebingen.de>
Date: Tue, 5 Feb 2019 17:47:33 +0100
Subject: [PATCH] Add config to disable Piwik cookies. (#1309)

---
 config/vufind/config.ini                       |  3 +++
 .../src/VuFind/View/Helper/Root/Piwik.php      | 18 +++++++++++++++++-
 .../VuFind/View/Helper/Root/PiwikFactory.php   |  3 ++-
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index e1b155064eb..b69cf8ad8e0 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -1661,6 +1661,9 @@ treeSearchLimit = 100
 ; the string, for example "SiteA|Backend|Search Terms." Most users will want to
 ; leave this disabled.
 ;searchPrefix = "SiteA|"
+; Uncomment the following setting to disable cookies for privacy reasons.
+; see https://matomo.org/faq/general/faq_157/ for more information.
+;disableCookies = true
 
 ; Uncomment portions of this section to activate tabs in the search box for switching
 ; between search modules. Keys are search backend names, values are labels for use in
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
index 7ce3270573a..b73e716e999 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
@@ -59,6 +59,13 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
      */
     protected $searchPrefix;
 
+    /**
+     * Whether to disable cookies (see config.ini for details)
+     *
+     * @var bool
+     */
+    protected $disableCookies;
+
     /**
      * Whether to track use custom variables to track additional information
      *
@@ -123,6 +130,7 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
         if (is_array($options)) {
             $this->siteId = $options['siteId'];
             $this->searchPrefix = $options['searchPrefix'] ?? '';
+            $this->disableCookies = $options['disableCookies'] ?? '';
         } else {
             $this->siteId = $options;
         }
@@ -426,7 +434,7 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
     protected function getOpeningTrackingCode()
     {
         $escape = $this->getView()->plugin('escapejs');
-        return <<<EOT
+        $code = <<<EOT
 
 function initVuFindPiwikTracker{$this->timestamp}(){
     var VuFindPiwikTracker = Piwik.getTracker();
@@ -436,6 +444,14 @@ function initVuFindPiwikTracker{$this->timestamp}(){
     VuFindPiwikTracker.setCustomUrl('{$escape($this->getCustomUrl())}');
 
 EOT;
+        if ($this->disableCookies) {
+            $code .= <<<EOT
+    VuFindPiwikTracker.disableCookies();
+
+EOT;
+        }
+
+        return $code;
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/PiwikFactory.php b/module/VuFind/src/VuFind/View/Helper/Root/PiwikFactory.php
index 2720a2d3b2f..92eb3b1c78a 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/PiwikFactory.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/PiwikFactory.php
@@ -66,7 +66,8 @@ class PiwikFactory implements FactoryInterface
         $url = $config->Piwik->url ?? false;
         $settings = [
             'siteId' => $config->Piwik->site_id ?? 1,
-            'searchPrefix' => $config->Piwik->searchPrefix ?? null
+            'searchPrefix' => $config->Piwik->searchPrefix ?? null,
+            'disableCookies' => $config->Piwik->disableCookies ?? false
         ];
         $customVars = $config->Piwik->custom_variables ?? false;
         $request = $container->get('Request');
-- 
GitLab