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