Db
6 years ago
Handler
6 years ago
TableLogAction
6 years ago
Visit
6 years ago
Action.php
6 years ago
ActionPageview.php
6 years ago
Cache.php
6 years ago
Db.php
6 years ago
Failures.php
6 years ago
FingerprintSalt.php
6 years ago
GoalManager.php
6 years ago
Handler.php
6 years ago
IgnoreCookie.php
6 years ago
LogTable.php
6 years ago
Model.php
6 years ago
PageUrl.php
6 years ago
Request.php
5 years ago
RequestProcessor.php
6 years ago
RequestSet.php
6 years ago
Response.php
6 years ago
ScheduledTasksRunner.php
6 years ago
Settings.php
5 years ago
TableLogAction.php
6 years ago
TrackerCodeGenerator.php
6 years ago
TrackerConfig.php
6 years ago
Visit.php
5 years ago
VisitExcluded.php
6 years ago
VisitInterface.php
6 years ago
Visitor.php
6 years ago
VisitorNotFoundInDb.php
6 years ago
VisitorRecognizer.php
6 years ago
RequestSet.php
258 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - 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 | use Piwik\Plugins\SitesManager\SiteUrls; |
| 14 | use Piwik\Url; |
| 15 | |
| 16 | class RequestSet |
| 17 | { |
| 18 | /** |
| 19 | * The set of visits to track. |
| 20 | * |
| 21 | * @var Request[] |
| 22 | */ |
| 23 | private $requests = null; |
| 24 | |
| 25 | /** |
| 26 | * The token auth supplied with a bulk visits POST. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | private $tokenAuth = null; |
| 31 | |
| 32 | private $env = array(); |
| 33 | |
| 34 | public function setRequests($requests) |
| 35 | { |
| 36 | $this->requests = array(); |
| 37 | |
| 38 | foreach ($requests as $request) { |
| 39 | if (empty($request) && !is_array($request)) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | if (!$request instanceof Request) { |
| 44 | $request = new Request($request, $this->getTokenAuth()); |
| 45 | } |
| 46 | |
| 47 | $this->requests[] = $request; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function setTokenAuth($tokenAuth) |
| 52 | { |
| 53 | $this->tokenAuth = $tokenAuth; |
| 54 | } |
| 55 | |
| 56 | public function getNumberOfRequests() |
| 57 | { |
| 58 | if (is_array($this->requests)) { |
| 59 | return count($this->requests); |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | public function getRequests() |
| 66 | { |
| 67 | if (!$this->areRequestsInitialized()) { |
| 68 | return array(); |
| 69 | } |
| 70 | |
| 71 | return $this->requests; |
| 72 | } |
| 73 | |
| 74 | public function getTokenAuth() |
| 75 | { |
| 76 | if (!is_null($this->tokenAuth)) { |
| 77 | return $this->tokenAuth; |
| 78 | } |
| 79 | |
| 80 | return Common::getRequestVar('token_auth', false); |
| 81 | } |
| 82 | |
| 83 | private function areRequestsInitialized() |
| 84 | { |
| 85 | return !is_null($this->requests); |
| 86 | } |
| 87 | |
| 88 | public function initRequestsAndTokenAuth() |
| 89 | { |
| 90 | if ($this->areRequestsInitialized()) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Triggered when detecting tracking requests. A plugin can use this event to set |
| 96 | * requests that should be tracked by calling the {@link RequestSet::setRequests()} method. |
| 97 | * For example the BulkTracking plugin uses this event to detect tracking requests and auth token based on |
| 98 | * a sent JSON instead of default $_GET+$_POST. It would allow you for example to track requests based on |
| 99 | * XML or you could import tracking requests stored in a file. |
| 100 | * |
| 101 | * @param \Piwik\Tracker\RequestSet &$requestSet Call {@link setRequests()} to initialize requests and |
| 102 | * {@link setTokenAuth()} to set a detected auth token. |
| 103 | * |
| 104 | * @ignore This event is not public yet as the RequestSet API is not really stable yet |
| 105 | */ |
| 106 | Piwik::postEvent('Tracker.initRequestSet', array($this)); |
| 107 | |
| 108 | if (!$this->areRequestsInitialized()) { |
| 109 | $this->requests = array(); |
| 110 | |
| 111 | if (!empty($_GET) || !empty($_POST)) { |
| 112 | $this->setRequests(array($_GET + $_POST)); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public function hasRequests() |
| 118 | { |
| 119 | return !empty($this->requests); |
| 120 | } |
| 121 | |
| 122 | protected function getRedirectUrl() |
| 123 | { |
| 124 | return Common::getRequestVar('redirecturl', false, 'string'); |
| 125 | } |
| 126 | |
| 127 | protected function hasRedirectUrl() |
| 128 | { |
| 129 | $redirectUrl = $this->getRedirectUrl(); |
| 130 | |
| 131 | return !empty($redirectUrl); |
| 132 | } |
| 133 | |
| 134 | protected function getAllSiteIdsWithinRequest() |
| 135 | { |
| 136 | if (empty($this->requests)) { |
| 137 | return array(); |
| 138 | } |
| 139 | |
| 140 | $siteIds = array(); |
| 141 | foreach ($this->requests as $request) { |
| 142 | $siteIds[] = (int) $request->getIdSite(); |
| 143 | } |
| 144 | |
| 145 | return array_values(array_unique($siteIds)); |
| 146 | } |
| 147 | |
| 148 | // TODO maybe move to response? or somewhere else? not sure where! |
| 149 | public function shouldPerformRedirectToUrl() |
| 150 | { |
| 151 | if (!$this->hasRedirectUrl()) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | if (!$this->hasRequests()) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | $redirectUrl = $this->getRedirectUrl(); |
| 160 | $host = Url::getHostFromUrl($redirectUrl); |
| 161 | |
| 162 | if (empty($host)) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | $urls = new SiteUrls(); |
| 167 | $siteUrls = $urls->getAllCachedSiteUrls(); |
| 168 | $siteIds = $this->getAllSiteIdsWithinRequest(); |
| 169 | |
| 170 | foreach ($siteIds as $siteId) { |
| 171 | if (empty($siteUrls[$siteId])) { |
| 172 | $siteUrls[$siteId] = array(); |
| 173 | } |
| 174 | |
| 175 | if (Url::isHostInUrls($host, $siteUrls[$siteId])) { |
| 176 | return $redirectUrl; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | public function getState() |
| 184 | { |
| 185 | $requests = array( |
| 186 | 'requests' => array(), |
| 187 | 'env' => $this->getEnvironment(), |
| 188 | 'tokenAuth' => $this->getTokenAuth(), |
| 189 | 'time' => time() |
| 190 | ); |
| 191 | |
| 192 | foreach ($this->getRequests() as $request) { |
| 193 | $requests['requests'][] = $request->getRawParams(); |
| 194 | } |
| 195 | |
| 196 | return $requests; |
| 197 | } |
| 198 | |
| 199 | public function restoreState($state) |
| 200 | { |
| 201 | $backupEnv = $this->getCurrentEnvironment(); |
| 202 | |
| 203 | $this->setEnvironment($state['env']); |
| 204 | $this->setTokenAuth($state['tokenAuth']); |
| 205 | |
| 206 | $this->restoreEnvironment(); |
| 207 | $this->setRequests($state['requests']); |
| 208 | |
| 209 | foreach ($this->getRequests() as $request) { |
| 210 | $request->setCurrentTimestamp($state['time']); |
| 211 | } |
| 212 | |
| 213 | $this->resetEnvironment($backupEnv); |
| 214 | } |
| 215 | |
| 216 | public function rememberEnvironment() |
| 217 | { |
| 218 | $this->setEnvironment($this->getEnvironment()); |
| 219 | } |
| 220 | |
| 221 | public function setEnvironment($env) |
| 222 | { |
| 223 | $this->env = $env; |
| 224 | } |
| 225 | |
| 226 | protected function getEnvironment() |
| 227 | { |
| 228 | if (!empty($this->env)) { |
| 229 | return $this->env; |
| 230 | } |
| 231 | |
| 232 | return $this->getCurrentEnvironment(); |
| 233 | } |
| 234 | |
| 235 | public function restoreEnvironment() |
| 236 | { |
| 237 | if (empty($this->env)) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | $this->resetEnvironment($this->env); |
| 242 | } |
| 243 | |
| 244 | private function resetEnvironment($env) |
| 245 | { |
| 246 | $_SERVER = $env['server']; |
| 247 | $_COOKIE = isset($env['cookie']) ? $env['cookie'] : array(); |
| 248 | } |
| 249 | |
| 250 | private function getCurrentEnvironment() |
| 251 | { |
| 252 | return array( |
| 253 | 'server' => $_SERVER, |
| 254 | 'cookie' => $_COOKIE |
| 255 | ); |
| 256 | } |
| 257 | } |
| 258 |