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

Requests.php « Tracker « BulkTracking « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 333333c0832bc40be0ee3ce668edd30739565d7f (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
<?php
/**
 * Piwik - 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\BulkTracking\Tracker;

use Exception;
use Piwik\Common;
use Piwik\Tracker\Request;
use Piwik\Tracker\TrackerConfig;

class Requests
{

    public function requiresAuthentication()
    {
        $requiresAuth = TrackerConfig::getConfigValue('bulk_requests_require_authentication');

        return !empty($requiresAuth);
    }

    /**
     * @param Request[] $requests
     * @throws Exception
     */
    public function authenticateRequests($requests)
    {
        foreach ($requests as $request) {
            $this->checkTokenAuthNotEmpty($request->getTokenAuth());

            if (!$request->isAuthenticated()) {
                $msg = sprintf("token_auth specified does not have Admin permission for idsite=%s", $request->getIdSite());
                throw new Exception($msg);
            }
        }
    }

    private function checkTokenAuthNotEmpty($token)
    {
        if (empty($token)) {
            throw new Exception("token_auth must be specified when using Bulk Tracking Import. "
                . " See <a href='https://developer.piwik.org/api-reference/tracking-api'>Tracking Doc</a>");
        }
    }

    /**
     * @return string
     */
    public function getRawBulkRequest()
    {
        return file_get_contents("php://input");
    }

    public function isUsingBulkRequest($rawData)
    {
        if (!empty($rawData)) {
            return strpos($rawData, '"requests"') || strpos($rawData, "'requests'");
        }

        return false;
    }

    public function getRequestsArrayFromBulkRequest($rawData)
    {
        $rawData = trim($rawData);
        $rawData = Common::sanitizeLineBreaks($rawData);

        // POST data can be array of string URLs or array of arrays w/ visit info
        $jsonData = json_decode($rawData, $assoc = true);

        $tokenAuth = Common::getRequestVar('token_auth', false, 'string', $jsonData);

        $requests = array();
        if (isset($jsonData['requests'])) {
            $requests = $jsonData['requests'];
        }

        return array($requests, $tokenAuth);
    }

    public function initRequestsAndTokenAuth($rawData)
    {
        list($requests, $tokenAuth) = $this->getRequestsArrayFromBulkRequest($rawData);

        $validRequests = array();

        if (!empty($requests)) {

            foreach ($requests as $index => $request) {
                // if a string is sent, we assume its a URL and try to parse it
                if (is_string($request)) {
                    $params = array();

                    $url = @parse_url($request);
                    if (!empty($url['query'])) {
                        @parse_str($url['query'], $params);
                        $validRequests[] = new Request($params, $tokenAuth);
                    }
                } else {
                    $validRequests[] = new Request($request, $tokenAuth);
                }
            }
        }

        return array($validRequests, $tokenAuth);
    }
}