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
Response.php
189 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 Exception; |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Profiler; |
| 14 | use Piwik\Timer; |
| 15 | use Piwik\Tracker; |
| 16 | use Piwik\Tracker\Db as TrackerDb; |
| 17 | |
| 18 | class Response |
| 19 | { |
| 20 | private $timer; |
| 21 | |
| 22 | private $content; |
| 23 | |
| 24 | public function init(Tracker $tracker) |
| 25 | { |
| 26 | ob_start(); // we use ob_start only because of Common::printDebug, we should actually not really use ob_start |
| 27 | |
| 28 | if ($tracker->isDebugModeEnabled() && TrackerConfig::getConfigValue('enable_sql_profiler')) { |
| 29 | $this->timer = new Timer(); |
| 30 | |
| 31 | TrackerDb::enableProfiling(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function getOutput() |
| 36 | { |
| 37 | $this->outputAccessControlHeaders(); |
| 38 | |
| 39 | if (is_null($this->content) && ob_get_level() > 0) { |
| 40 | $this->content = ob_get_clean(); |
| 41 | } |
| 42 | |
| 43 | return $this->content; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Echos an error message & other information, then exits. |
| 48 | * |
| 49 | * @param Tracker $tracker |
| 50 | * @param Exception $e |
| 51 | * @param int $statusCode eg 500 |
| 52 | */ |
| 53 | public function outputException(Tracker $tracker, Exception $e, $statusCode) |
| 54 | { |
| 55 | Common::sendResponseCode($statusCode); |
| 56 | $this->logExceptionToErrorLog($e); |
| 57 | |
| 58 | if ($tracker->isDebugModeEnabled()) { |
| 59 | Common::sendHeader('Content-Type: text/html; charset=utf-8'); |
| 60 | $trailer = '<span style="color: #888888">Backtrace:<br /><pre>' . $e->getTraceAsString() . '</pre></span>'; |
| 61 | $headerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Morpheus/templates/simpleLayoutHeader.tpl'); |
| 62 | $footerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Morpheus/templates/simpleLayoutFooter.tpl'); |
| 63 | $headerPage = str_replace('{$HTML_TITLE}', 'Matomo › Error', $headerPage); |
| 64 | |
| 65 | echo $headerPage . '<p>' . $this->getMessageFromException($e) . '</p>' . $trailer . $footerPage; |
| 66 | } else { |
| 67 | $this->outputApiResponse($tracker); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public function outputResponse(Tracker $tracker) |
| 72 | { |
| 73 | if (!$tracker->shouldRecordStatistics()) { |
| 74 | Common::sendResponseCode(503); |
| 75 | $this->outputApiResponse($tracker); |
| 76 | Common::printDebug("Logging disabled, display transparent logo"); |
| 77 | } elseif (!$tracker->hasLoggedRequests()) { |
| 78 | if (!$this->isHttpGetRequest() || !empty($_GET) || !empty($_POST)) { |
| 79 | Common::sendResponseCode(400); |
| 80 | } |
| 81 | Common::printDebug("Empty request => Matomo page"); |
| 82 | echo "This resource is part of Matomo. Keep full control of your data with the leading free and open source <a href='https://matomo.org' target='_blank' rel='noopener noreferrer nofollow'>web analytics & conversion optimisation platform</a>."; |
| 83 | } else { |
| 84 | $this->outputApiResponse($tracker); |
| 85 | Common::printDebug("Nothing to notice => default behaviour"); |
| 86 | } |
| 87 | |
| 88 | Common::printDebug("End of the page."); |
| 89 | |
| 90 | if ($tracker->isDebugModeEnabled() |
| 91 | && $tracker->isDatabaseConnected() |
| 92 | && TrackerDb::isProfilingEnabled() |
| 93 | ) { |
| 94 | $db = Tracker::getDatabase(); |
| 95 | $db->recordProfiling(); |
| 96 | Profiler::displayDbTrackerProfile($db); |
| 97 | } |
| 98 | |
| 99 | if ($tracker->isDebugModeEnabled()) { |
| 100 | Common::printDebug($_COOKIE); |
| 101 | Common::printDebug((string)$this->timer); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private function outputAccessControlHeaders() |
| 106 | { |
| 107 | if (!$this->isHttpGetRequest()) { |
| 108 | $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*'; |
| 109 | Common::sendHeader('Access-Control-Allow-Origin: ' . $origin); |
| 110 | Common::sendHeader('Access-Control-Allow-Credentials: true'); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private function isHttpGetRequest() |
| 115 | { |
| 116 | $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
| 117 | |
| 118 | return strtoupper($requestMethod) === 'GET'; |
| 119 | } |
| 120 | |
| 121 | private function getOutputBuffer() |
| 122 | { |
| 123 | return ob_get_contents(); |
| 124 | } |
| 125 | |
| 126 | protected function hasAlreadyPrintedOutput() |
| 127 | { |
| 128 | return strlen($this->getOutputBuffer()) > 0; |
| 129 | } |
| 130 | |
| 131 | private function outputApiResponse(Tracker $tracker) |
| 132 | { |
| 133 | if ($tracker->isDebugModeEnabled()) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if ($this->hasAlreadyPrintedOutput()) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | $request = $_GET + $_POST; |
| 142 | |
| 143 | if ($this->isHttpGetRequest()) { |
| 144 | Common::sendHeader('Cache-Control: no-store'); |
| 145 | } |
| 146 | |
| 147 | if (array_key_exists('send_image', $request) && $request['send_image'] === '0') { |
| 148 | Common::sendResponseCode(204); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $this->outputTransparentGif(); |
| 153 | } |
| 154 | |
| 155 | private function outputTransparentGif() |
| 156 | { |
| 157 | $transGifBase64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="; |
| 158 | Common::sendHeader('Content-Type: image/gif'); |
| 159 | |
| 160 | echo base64_decode($transGifBase64); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Gets the error message to output when a tracking request fails. |
| 165 | * |
| 166 | * @param Exception $e |
| 167 | * @return string |
| 168 | */ |
| 169 | protected function getMessageFromException($e) |
| 170 | { |
| 171 | // Note: duplicated from FormDatabaseSetup.isAccessDenied |
| 172 | // Avoid leaking the username/db name when access denied |
| 173 | if ($e->getCode() == 1044 || $e->getCode() == 42000) { |
| 174 | return "Error while connecting to the Matomo database - please check your credentials in config/config.ini.php file"; |
| 175 | } |
| 176 | |
| 177 | if (Common::isPhpCliMode()) { |
| 178 | return $e->getMessage() . "\n" . $e->getTraceAsString(); |
| 179 | } |
| 180 | |
| 181 | return $e->getMessage(); |
| 182 | } |
| 183 | |
| 184 | protected function logExceptionToErrorLog($e) |
| 185 | { |
| 186 | error_log(sprintf("Error in Matomo (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e)))); |
| 187 | } |
| 188 | } |
| 189 |