File size: 2,767 Bytes
e852054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker;

use Piwik\API\Request;
use Piwik\Container\StaticContainer;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Plugins\SegmentEditor\Services\StoredSegmentService;

class CustomVariables
{
    /**
     * We send this data via server to not expose eg PHP version to users
     * @return array
     */
    public function getServerVisitCustomVariables()
    {
        $users = Request::processRequest('UsersManager.getUsers', array('filter_limit' => '-1'));
        $websites = Request::processRequest('SitesManager.getAllSites', array('filter_limit' => '-1'));

        $db = Db::get();
        $mySqlVersion = null;
        if (method_exists($db, 'getServerVersion')) {
            $mySqlVersion = $db->getServerVersion();
        }

        $customVars = array(
            array(
                'id' => 1,
                'name' => 'Matomo Version',
                'value' => StaticContainer::get('AnonymousPiwikUsageMeasurement.piwikVersion'),
            ),
            array(
                'id' => 2,
                'name' => 'PHP Version',
                'value' => StaticContainer::get('AnonymousPiwikUsageMeasurement.phpVersion'),
            ),
            array(
                'id' => 3,
                'name' => 'Num Users',
                'value' => count($users),
            ),
            array(
                'id' => 4,
                'name' => 'Num Websites',
                'value' => count($websites),
            ),
        );

        $service = StaticContainer::get(StoredSegmentService::class);
        $segments = $service->getAllSegmentsAndIgnoreVisibility();
        $customVars[] = array(
            'id' => 5,
            'name' => 'Num Segments',
            'value' => count($segments),
        );

        if (isset($mySqlVersion)) {
            $customVars[] = array(
                'id' => 6,
                'name' => 'MySQL Version',
                'value' => $mySqlVersion,
            );
        }

        return $customVars;
    }

    public function getClientVisitCustomVariables()
    {
        if (Piwik::hasUserSuperUserAccess()) {
            $access = 'superuser';
        } elseif (Piwik::isUserIsAnonymous()) {
            $access = 'anonymous';
        } else {
            // I do not check between view/admin as it could trigger slow DB queries to fetch sites with access
            $access = 'user';
        }

        return array(
            array(
                'id' => 1,
                'name' => 'Access',
                'value' => $access,
            )
        );
    }
}