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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tracker;
use Piwik\Request\AuthenticationToken;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
class RequestSet
{
/**
* The set of visits to track.
*
* @var Request[]
*/
private $requests = null;
/**
* The token auth supplied with a bulk visits POST.
*
* @var string
*/
private $tokenAuth = null;
private $env = array();
public function setRequests($requests)
{
$this->requests = array();
if (empty($requests) || !is_array($requests)) {
return;
}
foreach ($requests as $request) {
if (empty($request) && !is_array($request)) {
continue;
}
if (!$request instanceof Request) {
$request = new Request($request, $this->getTokenAuth());
}
$this->requests[] = $request;
}
}
public function setTokenAuth(
#[\SensitiveParameter]
$tokenAuth
) {
$this->tokenAuth = $tokenAuth;
}
public function getNumberOfRequests()
{
if (is_array($this->requests)) {
return count($this->requests);
}
return 0;
}
public function getRequests()
{
if (!$this->areRequestsInitialized()) {
return array();
}
return $this->requests;
}
public function getTokenAuth()
{
if (!is_null($this->tokenAuth)) {
return $this->tokenAuth;
}
return StaticContainer::get(AuthenticationToken::class)->getAuthToken();
}
private function areRequestsInitialized()
{
return !is_null($this->requests);
}
public function initRequestsAndTokenAuth()
{
if ($this->areRequestsInitialized()) {
return;
}
/**
* Triggered when detecting tracking requests. A plugin can use this event to set
* requests that should be tracked by calling the {@link RequestSet::setRequests()} method.
* For example the BulkTracking plugin uses this event to detect tracking requests and auth token based on
* a sent JSON instead of default $_GET+$_POST. It would allow you for example to track requests based on
* XML or you could import tracking requests stored in a file.
*
* @param \Piwik\Tracker\RequestSet &$requestSet Call {@link setRequests()} to initialize requests and
* {@link setTokenAuth()} to set a detected auth token.
*
* @ignore This event is not public yet as the RequestSet API is not really stable yet
*/
Piwik::postEvent('Tracker.initRequestSet', array($this));
if (!$this->areRequestsInitialized()) {
$this->requests = array();
if (!empty($_GET) || !empty($_POST)) {
$this->setRequests(array($_GET + $_POST));
}
}
}
public function hasRequests()
{
return !empty($this->requests);
}
protected function getAllSiteIdsWithinRequest()
{
if (empty($this->requests)) {
return array();
}
$siteIds = array();
foreach ($this->requests as $request) {
$siteIds[] = (int) $request->getIdSite();
}
return array_values(array_unique($siteIds));
}
public function getState()
{
$requests = array(
'requests' => array(),
'env' => $this->getEnvironment(),
'tokenAuth' => $this->getTokenAuth(),
'time' => time(),
);
foreach ($this->getRequests() as $request) {
$requests['requests'][] = $request->getRawParams();
}
return $requests;
}
public function restoreState($state)
{
$backupEnv = $this->getCurrentEnvironment();
$this->setEnvironment($state['env']);
$this->setTokenAuth($state['tokenAuth']);
$this->restoreEnvironment();
$this->setRequests($state['requests']);
foreach ($this->getRequests() as $request) {
$request->setCurrentTimestamp($state['time']);
}
$this->resetEnvironment($backupEnv);
}
public function rememberEnvironment()
{
$this->setEnvironment($this->getEnvironment());
}
public function setEnvironment($env)
{
$this->env = $env;
}
protected function getEnvironment()
{
if (!empty($this->env)) {
return $this->env;
}
return $this->getCurrentEnvironment();
}
public function restoreEnvironment()
{
if (empty($this->env)) {
return;
}
$this->resetEnvironment($this->env);
}
private function resetEnvironment($env)
{
$_SERVER = $env['server'];
$_COOKIE = isset($env['cookie']) ? $env['cookie'] : array();
}
private function getCurrentEnvironment()
{
return array(
'server' => $_SERVER,
'cookie' => $_COOKIE,
);
}
}
|