Config
4 months ago
Db
3 months ago
Handler
2 years ago
Visit
1 month ago
Action.php
3 months ago
ActionPageview.php
2 years ago
BotRequest.php
3 months ago
BotRequestProcessor.php
1 month ago
Cache.php
6 months ago
Db.php
1 year ago
Failures.php
6 months ago
FingerprintSalt.php
1 year ago
GoalManager.php
1 month ago
Handler.php
2 years ago
IgnoreCookie.php
1 year ago
LogTable.php
1 year ago
Model.php
6 months ago
PageUrl.php
2 weeks ago
Request.php
1 month ago
RequestHandlerTrait.php
4 months ago
RequestProcessor.php
1 month ago
RequestSet.php
6 months ago
Response.php
3 months ago
ScheduledTasksRunner.php
1 year ago
Settings.php
3 months ago
TableLogAction.php
6 months ago
TrackerCodeGenerator.php
1 year ago
TrackerConfig.php
1 month ago
Visit.php
3 months ago
VisitExcluded.php
3 months ago
VisitInterface.php
3 months ago
Visitor.php
1 month ago
VisitorNotFoundInDb.php
1 month ago
VisitorRecognizer.php
1 year ago
RequestSet.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Tracker; |
| 10 | |
| 11 | use Piwik\Request\AuthenticationToken; |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | use Piwik\Piwik; |
| 14 | class RequestSet |
| 15 | { |
| 16 | /** |
| 17 | * The set of visits to track. |
| 18 | * |
| 19 | * @var Request[] |
| 20 | */ |
| 21 | private $requests = null; |
| 22 | /** |
| 23 | * The token auth supplied with a bulk visits POST. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | private $tokenAuth = null; |
| 28 | private $env = array(); |
| 29 | public function setRequests($requests) |
| 30 | { |
| 31 | $this->requests = array(); |
| 32 | if (empty($requests) || !is_array($requests)) { |
| 33 | return; |
| 34 | } |
| 35 | foreach ($requests as $request) { |
| 36 | if (empty($request) && !is_array($request)) { |
| 37 | continue; |
| 38 | } |
| 39 | if (!$request instanceof \Piwik\Tracker\Request) { |
| 40 | $request = new \Piwik\Tracker\Request($request, $this->getTokenAuth()); |
| 41 | } |
| 42 | $this->requests[] = $request; |
| 43 | } |
| 44 | } |
| 45 | public function setTokenAuth( |
| 46 | #[\SensitiveParameter] |
| 47 | $tokenAuth) |
| 48 | { |
| 49 | $this->tokenAuth = $tokenAuth; |
| 50 | } |
| 51 | public function getNumberOfRequests() |
| 52 | { |
| 53 | if (is_array($this->requests)) { |
| 54 | return count($this->requests); |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | public function getRequests() |
| 59 | { |
| 60 | if (!$this->areRequestsInitialized()) { |
| 61 | return array(); |
| 62 | } |
| 63 | return $this->requests; |
| 64 | } |
| 65 | public function getTokenAuth() |
| 66 | { |
| 67 | if (!is_null($this->tokenAuth)) { |
| 68 | return $this->tokenAuth; |
| 69 | } |
| 70 | return StaticContainer::get(AuthenticationToken::class)->getAuthToken(); |
| 71 | } |
| 72 | private function areRequestsInitialized() |
| 73 | { |
| 74 | return !is_null($this->requests); |
| 75 | } |
| 76 | public function initRequestsAndTokenAuth() |
| 77 | { |
| 78 | if ($this->areRequestsInitialized()) { |
| 79 | return; |
| 80 | } |
| 81 | /** |
| 82 | * Triggered when detecting tracking requests. A plugin can use this event to set |
| 83 | * requests that should be tracked by calling the {@link RequestSet::setRequests()} method. |
| 84 | * For example the BulkTracking plugin uses this event to detect tracking requests and auth token based on |
| 85 | * a sent JSON instead of default $_GET+$_POST. It would allow you for example to track requests based on |
| 86 | * XML or you could import tracking requests stored in a file. |
| 87 | * |
| 88 | * @param \Piwik\Tracker\RequestSet &$requestSet Call {@link setRequests()} to initialize requests and |
| 89 | * {@link setTokenAuth()} to set a detected auth token. |
| 90 | * |
| 91 | * @ignore This event is not public yet as the RequestSet API is not really stable yet |
| 92 | */ |
| 93 | Piwik::postEvent('Tracker.initRequestSet', array($this)); |
| 94 | if (!$this->areRequestsInitialized()) { |
| 95 | $this->requests = array(); |
| 96 | if (!empty($_GET) || !empty($_POST)) { |
| 97 | $this->setRequests(array($_GET + $_POST)); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | public function hasRequests() |
| 102 | { |
| 103 | return !empty($this->requests); |
| 104 | } |
| 105 | protected function getAllSiteIdsWithinRequest() |
| 106 | { |
| 107 | if (empty($this->requests)) { |
| 108 | return array(); |
| 109 | } |
| 110 | $siteIds = array(); |
| 111 | foreach ($this->requests as $request) { |
| 112 | $siteIds[] = (int) $request->getIdSite(); |
| 113 | } |
| 114 | return array_values(array_unique($siteIds)); |
| 115 | } |
| 116 | public function getState() |
| 117 | { |
| 118 | $requests = array('requests' => array(), 'env' => $this->getEnvironment(), 'tokenAuth' => $this->getTokenAuth(), 'time' => time()); |
| 119 | foreach ($this->getRequests() as $request) { |
| 120 | $requests['requests'][] = $request->getRawParams(); |
| 121 | } |
| 122 | return $requests; |
| 123 | } |
| 124 | public function restoreState($state) |
| 125 | { |
| 126 | $backupEnv = $this->getCurrentEnvironment(); |
| 127 | $this->setEnvironment($state['env']); |
| 128 | $this->setTokenAuth($state['tokenAuth']); |
| 129 | $this->restoreEnvironment(); |
| 130 | $this->setRequests($state['requests']); |
| 131 | foreach ($this->getRequests() as $request) { |
| 132 | $request->setCurrentTimestamp($state['time']); |
| 133 | } |
| 134 | $this->resetEnvironment($backupEnv); |
| 135 | } |
| 136 | public function rememberEnvironment() |
| 137 | { |
| 138 | $this->setEnvironment($this->getEnvironment()); |
| 139 | } |
| 140 | public function setEnvironment($env) |
| 141 | { |
| 142 | $this->env = $env; |
| 143 | } |
| 144 | protected function getEnvironment() |
| 145 | { |
| 146 | if (!empty($this->env)) { |
| 147 | return $this->env; |
| 148 | } |
| 149 | return $this->getCurrentEnvironment(); |
| 150 | } |
| 151 | public function restoreEnvironment() |
| 152 | { |
| 153 | if (empty($this->env)) { |
| 154 | return; |
| 155 | } |
| 156 | $this->resetEnvironment($this->env); |
| 157 | } |
| 158 | private function resetEnvironment($env) |
| 159 | { |
| 160 | $_SERVER = $env['server']; |
| 161 | $_COOKIE = isset($env['cookie']) ? $env['cookie'] : array(); |
| 162 | } |
| 163 | private function getCurrentEnvironment() |
| 164 | { |
| 165 | return array('server' => $_SERVER, 'cookie' => $_COOKIE); |
| 166 | } |
| 167 | } |
| 168 |