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
Failures.php
198 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 | |
| 10 | namespace Piwik\Tracker; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Date; |
| 14 | use Piwik\Exception\InvalidRequestParameterException; |
| 15 | use Piwik\Exception\UnexpectedWebsiteFoundException; |
| 16 | use Piwik\Piwik; |
| 17 | use Piwik\Site; |
| 18 | use Piwik\Db as PiwikDb; |
| 19 | |
| 20 | class Failures |
| 21 | { |
| 22 | const CLEANUP_OLD_FAILURES_DAYS = 2; |
| 23 | const FAILURE_ID_INVALID_SITE = 1; |
| 24 | const FAILURE_ID_NOT_AUTHENTICATED = 2; |
| 25 | |
| 26 | private $table = 'tracking_failure'; |
| 27 | private $tablePrefixed; |
| 28 | private $now; |
| 29 | |
| 30 | public function __construct() |
| 31 | { |
| 32 | $this->tablePrefixed = Common::prefixTable($this->table); |
| 33 | } |
| 34 | |
| 35 | public function setNow(Date $now) |
| 36 | { |
| 37 | $this->now = $now; |
| 38 | } |
| 39 | |
| 40 | private function getNow() |
| 41 | { |
| 42 | if (isset($this->now)) { |
| 43 | return $this->now; |
| 44 | } |
| 45 | return Date::now(); |
| 46 | } |
| 47 | |
| 48 | public function logFailure($idFailure, Request $request) |
| 49 | { |
| 50 | $isVisitExcluded = $request->getMetadata('CoreHome', 'isVisitExcluded'); |
| 51 | |
| 52 | if ($isVisitExcluded === null) { |
| 53 | try { |
| 54 | $visitExcluded = new VisitExcluded($request); |
| 55 | $isVisitExcluded = $visitExcluded->isExcluded(); |
| 56 | } catch (InvalidRequestParameterException $e) { |
| 57 | // we ignore this error and assume visit is not excluded... happens eg when using `cip` and request was |
| 58 | // not authenticated... |
| 59 | $isVisitExcluded = false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if ($isVisitExcluded) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $idSite = (int) $request->getIdSiteUnverified(); |
| 68 | $idFailure = (int) $idFailure; |
| 69 | |
| 70 | if ($idSite > 9999999 || $idSite < 0 || $this->hasLoggedFailure($idSite, $idFailure)) { |
| 71 | return; // we prevent creating huge amount of entries in the cache |
| 72 | } |
| 73 | |
| 74 | $params = $this->getParamsWithTokenAnonymized($request); |
| 75 | $sql = sprintf('INSERT INTO %s (`idsite`, `idfailure`, `date_first_occurred`, `request_url`) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE idsite=idsite;', $this->tablePrefixed); |
| 76 | |
| 77 | PiwikDb::get()->query($sql, array($idSite, $idFailure, $this->getNow()->getDatetime(), http_build_query($params))); |
| 78 | } |
| 79 | |
| 80 | private function hasLoggedFailure($idSite, $idFailure) |
| 81 | { |
| 82 | $sql = sprintf('SELECT idsite FROM %s WHERE idsite = ? and idfailure = ?', $this->tablePrefixed); |
| 83 | $row = PiwikDb::fetchRow($sql, array($idSite, $idFailure)); |
| 84 | |
| 85 | return !empty($row); |
| 86 | } |
| 87 | |
| 88 | private function getParamsWithTokenAnonymized(Request $request) |
| 89 | { |
| 90 | // eg if there is a typo in the token auth we want to replace it as well to not accidentally leak a token |
| 91 | // eg imagine a super user tries to issue an API request for a site and sending the wrong parameter for a token... |
| 92 | // an admin may have view access for this and can see the super users token |
| 93 | $token = $request->getTokenAuth(); |
| 94 | $params = $request->getRawParams(); |
| 95 | foreach (array('token_auth', 'token', 'tokenauth', 'token__auth') as $key) { |
| 96 | if (isset($params[$key])) { |
| 97 | $params[$key] = '__TOKEN_AUTH__'; |
| 98 | } |
| 99 | } |
| 100 | foreach ($params as $key => $value) { |
| 101 | if (!empty($token) && $value === $token) { |
| 102 | $params[$key] = '__TOKEN_AUTH__'; // user accidentally posted the token in a wrong field |
| 103 | } elseif (!empty($value) && is_string($value) |
| 104 | && Common::mb_strlen($value) >= 29 && Common::mb_strlen($value) <= 36 |
| 105 | && ctype_xdigit($value)) { |
| 106 | $params[$key] = '__TOKEN_AUTH__'; // user maybe posted a token in a different field... it looks like it might be a token |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $params; |
| 111 | } |
| 112 | |
| 113 | public function removeFailuresOlderThanDays($days) |
| 114 | { |
| 115 | $minutesAgo = $this->getNow()->subDay($days)->getDatetime(); |
| 116 | |
| 117 | PiwikDb::query(sprintf('DELETE FROM %s WHERE date_first_occurred < ?', $this->tablePrefixed), array($minutesAgo)); |
| 118 | } |
| 119 | |
| 120 | public function getAllFailures() |
| 121 | { |
| 122 | $failures = PiwikDb::fetchAll(sprintf('SELECT * FROM %s', $this->tablePrefixed)); |
| 123 | return $this->enrichFailures($failures); |
| 124 | } |
| 125 | |
| 126 | public function getFailuresForSites($idSites) |
| 127 | { |
| 128 | if (empty($idSites)) { |
| 129 | return array(); |
| 130 | } |
| 131 | $idSites = array_map('intval', $idSites); |
| 132 | $idSites = implode(',', $idSites); |
| 133 | $failures = PiwikDb::fetchAll(sprintf('SELECT * FROM %s WHERE idsite IN (%s)', $this->tablePrefixed, $idSites)); |
| 134 | return $this->enrichFailures($failures); |
| 135 | } |
| 136 | |
| 137 | public function deleteTrackingFailure($idSite, $idFailure) |
| 138 | { |
| 139 | PiwikDb::query(sprintf('DELETE FROM %s WHERE idsite = ? and idfailure = ?', $this->tablePrefixed), array($idSite, $idFailure)); |
| 140 | } |
| 141 | |
| 142 | public function deleteTrackingFailures($idSites) |
| 143 | { |
| 144 | if (!empty($idSites)) { |
| 145 | $idSites = array_map('intval', $idSites); |
| 146 | $idSites = implode(',', $idSites); |
| 147 | PiwikDb::query(sprintf('DELETE FROM %s WHERE idsite IN(%s)', $this->tablePrefixed, $idSites)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public function deleteAllTrackingFailures() |
| 152 | { |
| 153 | PiwikDb::query(sprintf('DELETE FROM %s', $this->tablePrefixed)); |
| 154 | } |
| 155 | |
| 156 | private function enrichFailures($failures) |
| 157 | { |
| 158 | foreach ($failures as &$failure) { |
| 159 | try { |
| 160 | $failure['site_name'] = Site::getNameFor($failure['idsite']); |
| 161 | } catch (UnexpectedWebsiteFoundException $e) { |
| 162 | $failure['site_name'] = Piwik::translate('General_Unknown'); |
| 163 | } |
| 164 | $failure['pretty_date_first_occurred'] = Date::factory($failure['date_first_occurred'])->getLocalized(Date::DATETIME_FORMAT_SHORT); |
| 165 | parse_str($failure['request_url'], $params); |
| 166 | if (empty($params['url'])) { |
| 167 | $params['url'] = ' ';// workaround it using the default provider in request constructor |
| 168 | } |
| 169 | $request = new Request($params); |
| 170 | $failure['url'] = trim($request->getParam('url')); |
| 171 | $failure['problem'] = ''; |
| 172 | $failure['solution'] = ''; |
| 173 | $failure['solution_url'] = ''; |
| 174 | |
| 175 | switch ($failure['idfailure']) { |
| 176 | case self::FAILURE_ID_INVALID_SITE: |
| 177 | $failure['problem'] = Piwik::translate('CoreAdminHome_TrackingFailureInvalidSiteProblem'); |
| 178 | $failure['solution'] = Piwik::translate('CoreAdminHome_TrackingFailureInvalidSiteSolution'); |
| 179 | $failure['solution_url'] = 'https://matomo.org/faq/how-to/faq_30838/'; |
| 180 | break; |
| 181 | case self::FAILURE_ID_NOT_AUTHENTICATED: |
| 182 | $failure['problem'] = Piwik::translate('CoreAdminHome_TrackingFailureAuthenticationProblem'); |
| 183 | $failure['solution'] = Piwik::translate('CoreAdminHome_TrackingFailureAuthenticationSolution'); |
| 184 | $failure['solution_url'] = 'https://matomo.org/faq/how-to/faq_30835/'; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @ignore |
| 191 | * internal use only |
| 192 | */ |
| 193 | Piwik::postEvent('Tracking.makeFailuresHumanReadable', array(&$failures)); |
| 194 | |
| 195 | return $failures; |
| 196 | } |
| 197 | } |
| 198 |