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