Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DoNotTrackHeaderChecker.php « PrivacyManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8c6f9df9b7c44b6582bf3609bd3ff142b15dfb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\PrivacyManager;

use Piwik\Common;
use Piwik\Tracker\IgnoreCookie;
use Piwik\Tracker\Request;

/**
 * Excludes visits where user agent's request contains either:
 *
 * - X-Do-Not-Track header (used by AdBlockPlus and NoScript)
 * - DNT header (used by Mozilla)
 *
 * Note: visits from Internet Explorer and other browsers that have DoNoTrack enabled by default will be tracked anyway.
 */
class DoNotTrackHeaderChecker
{
    /**
     * @param Config $config
     */
    public function __construct($config = null)
    {
        $this->config = $config ?: new Config();
    }

    /**
     * Checks for DoNotTrack headers and if found, sets `$exclude` to `true`.
     */
    public function checkHeaderInTracker(&$exclude)
    {
        if ($exclude) {
            Common::printDebug("Visit is already excluded, no need to check DoNotTrack support.");
            return;
        }

        $exclude = $this->isDoNotTrackFound();

        if($exclude) {

            IgnoreCookie::deleteThirdPartyCookieUIDIfExists();

            // this is an optional supplement to the site's tracking status resource at:
            //     /.well-known/dnt
            // per Tracking Preference Expression
            
            //Tracking Perference Expression has been updated to require Tk: N rather than Tk: 1
            Common::sendHeader('Tk: N');
        }
    }

    /**
     * @return bool
     */
    public function isDoNotTrackFound()
    {
        if (!$this->isActive()) {
            Common::printDebug("DoNotTrack support is not enabled, skip check");
            return false;
        }

        if (!$this->isHeaderDntFound()) {
            Common::printDebug("DoNotTrack header not found");
            return false;
        }

        $request = new Request($_REQUEST);
        $userAgent = $request->getUserAgent();

        if ($this->isUserAgentWithDoNotTrackAlwaysEnabled($userAgent)) {
            Common::printDebug("INTERNET EXPLORER enable DoNotTrack by default; so Piwik ignores DNT IE browsers...");
            return false;
        }

        Common::printDebug("DoNotTrack header found!");
        return true;
    }

    /**
     * Deactivates DoNotTrack header checking. This function will not be called by the Tracker.
     */
    public function deactivate()
    {
        $this->config->doNotTrackEnabled = false;
    }

    /**
     * Activates DoNotTrack header checking. This function will not be called by the Tracker.
     */
    public function activate()
    {
        $this->config->doNotTrackEnabled = true;
    }

    /**
     * Returns true if server side DoNotTrack support is enabled, false if otherwise.
     *
     * @return bool
     */
    public function isActive()
    {
        return $this->config->doNotTrackEnabled;
    }

    /**
     * @return bool
     */
    protected function isHeaderDntFound()
    {
        return (isset($_SERVER['HTTP_X_DO_NOT_TRACK']) && $_SERVER['HTTP_X_DO_NOT_TRACK'] === '1')
            || (isset($_SERVER['HTTP_DNT']) && substr($_SERVER['HTTP_DNT'], 0, 1) === '1');
    }

    /**
     *
     * @param $userAgent
     * @return bool
     */
    protected function isUserAgentWithDoNotTrackAlwaysEnabled($userAgent)
    {
        $browsersWithDnt = $this->getBrowsersWithDNTAlwaysEnabled();
        foreach($browsersWithDnt as $userAgentBrowserFragment) {
            if (stripos($userAgent, $userAgentBrowserFragment) !== false) {
                return true;
            }
        }
        return false;
    }

    /**
     * Some browsers have DNT enabled by default. For those we will ignore DNT and always track those users.
     *
     * @return array
     */
    protected function getBrowsersWithDNTAlwaysEnabled()
    {
        return array(
            // IE
            'MSIE',
            'Trident',

            // Maxthon
            'Maxthon',
            
            // Epiphany - https://github.com/piwik/piwik/issues/8682
            'Epiphany',
        );
    }
}