1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-21 22:47:43 +00:00
Files
matomo/core/Tracker/RequestSet.php
Stefan Giehl aa6585c860 Fix sending secure token in bulk request body & add support for "Authorization: Bearer" header (#23335)
* Centralize auth token retrival

* deprecate API\Request::isTokenAuthProvidedSecurely instead of removing it

* Add Unit tests

* Add changelog for auth header support

* Adds integration test

* Allow creating secure app specific tokens through API

* Add test for secure token in bulk tracking

* updates expected UI test files

* apply review feedback

* fix typo

Co-authored-by: caddoo <1169490+caddoo@users.noreply.github.com>

---------

Co-authored-by: caddoo <1169490+caddoo@users.noreply.github.com>
2025-06-23 15:23:45 +12:00

216 خطوط
5.2 KiB
PHP

<?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
);
}
}