blob: dd77b168c608f141e3631ac4d45ad52406e7c90c (
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
|
<?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\BulkTracking\Tracker;
use Piwik\AuthResult;
use Piwik\Container\StaticContainer;
use Piwik\Exception\InvalidRequestParameterException;
use Piwik\Exception\UnexpectedWebsiteFoundException;
use Piwik\Piwik;
use Piwik\Tracker;
use Piwik\Tracker\RequestSet;
use Piwik\Tracker\TrackerConfig;
use Exception;
class Handler extends Tracker\Handler
{
private $transactionId = null;
public function __construct()
{
parent::__construct();
$this->setResponse(new Response());
}
public function onStartTrackRequests(Tracker $tracker, RequestSet $requestSet)
{
if ($this->isTransactionSupported()) {
$this->beginTransaction();
}
}
public function onAllRequestsTracked(Tracker $tracker, RequestSet $requestSet)
{
$this->commitTransaction();
// Do not run schedule task if we are importing logs or doing custom tracking (as it could slow down)
}
public function process(Tracker $tracker, RequestSet $requestSet)
{
$isAuthenticated = $this->isBulkTrackingRequestAuthenticated($requestSet);
/** @var Response $response */
$response = $this->getResponse();
$response->setIsAuthenticated($isAuthenticated);
$invalidRequests = array();
foreach ($requestSet->getRequests() as $index => $request) {
try {
$tracker->trackRequest($request);
} catch (UnexpectedWebsiteFoundException $ex) {
$invalidRequests[] = $index;
} catch (InvalidRequestParameterException $ex) {
$invalidRequests[] = $index;
} catch (Exception $e) {
if (stripos($e->getMessage(), 'Lock wait timeout exceeded')
|| Tracker::getDatabase()->isErrNo($e, 1205)) {
continue;
} else {
throw $e;
}
}
}
$response->setInvalidRequests($invalidRequests);
}
public function onException(Tracker $tracker, RequestSet $requestSet, Exception $e)
{
$this->rollbackTransaction();
parent::onException($tracker, $requestSet, $e);
}
private function beginTransaction()
{
if (empty($this->transactionId)) {
$this->transactionId = $this->getDb()->beginTransaction();
}
}
private function commitTransaction()
{
if (!empty($this->transactionId)) {
$this->getDb()->commit($this->transactionId);
$this->transactionId = null;
}
}
private function rollbackTransaction()
{
if (!empty($this->transactionId)) {
$this->getDb()->rollback($this->transactionId);
$this->transactionId = null;
}
}
private function getDb()
{
return Tracker::getDatabase();
}
/**
* @return bool
*/
private function isTransactionSupported()
{
return (bool) TrackerConfig::getConfigValue('bulk_requests_use_transaction');
}
private function isBulkTrackingRequestAuthenticated(RequestSet $requestSet)
{
$tokenAuth = $requestSet->getTokenAuth();
if (empty($tokenAuth)) {
return false;
}
Piwik::postEvent('Request.initAuthenticationObject');
/** @var \Piwik\Auth $auth */
$auth = StaticContainer::get('Piwik\Auth');
$auth->setTokenAuth($tokenAuth);
$auth->setLogin(null);
$auth->setPassword(null);
$auth->setPasswordHash(null);
$access = $auth->authenticate();
return $access->getCode() == AuthResult::SUCCESS_SUPERUSER_AUTH_CODE;
}
}
|