PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Tracker / Response.php
matomo / app / core / Tracker Last commit date
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
Response.php
190 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 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 &rsaquo; 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>.<br>\n";
83 echo "This file is the endpoint for the Matomo tracking API. If you want to access the Matomo UI or use the Reporting API, please use <a href='index.php'>index.php</a> instead.";
84 } else {
85 $this->outputApiResponse($tracker);
86 Common::printDebug("Nothing to notice => default behaviour");
87 }
88
89 Common::printDebug("End of the page.");
90
91 if ($tracker->isDebugModeEnabled()
92 && $tracker->isDatabaseConnected()
93 && TrackerDb::isProfilingEnabled()
94 ) {
95 $db = Tracker::getDatabase();
96 $db->recordProfiling();
97 Profiler::displayDbTrackerProfile($db);
98 }
99
100 if ($tracker->isDebugModeEnabled()) {
101 Common::printDebug($_COOKIE);
102 Common::printDebug((string)$this->timer);
103 }
104 }
105
106 private function outputAccessControlHeaders()
107 {
108 if (!$this->isHttpGetRequest()) {
109 $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
110 Common::sendHeader('Access-Control-Allow-Origin: ' . $origin);
111 Common::sendHeader('Access-Control-Allow-Credentials: true');
112 }
113 }
114
115 private function isHttpGetRequest()
116 {
117 $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
118
119 return strtoupper($requestMethod) === 'GET';
120 }
121
122 private function getOutputBuffer()
123 {
124 return ob_get_contents();
125 }
126
127 protected function hasAlreadyPrintedOutput()
128 {
129 return strlen($this->getOutputBuffer()) > 0;
130 }
131
132 private function outputApiResponse(Tracker $tracker)
133 {
134 if ($tracker->isDebugModeEnabled()) {
135 return;
136 }
137
138 if ($this->hasAlreadyPrintedOutput()) {
139 return;
140 }
141
142 $request = $_GET + $_POST;
143
144 if ($this->isHttpGetRequest()) {
145 Common::sendHeader('Cache-Control: no-store');
146 }
147
148 if (array_key_exists('send_image', $request) && $request['send_image'] === '0') {
149 Common::sendResponseCode(204);
150 return;
151 }
152
153 $this->outputTransparentGif();
154 }
155
156 private function outputTransparentGif()
157 {
158 $transGifBase64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
159 Common::sendHeader('Content-Type: image/gif');
160
161 echo base64_decode($transGifBase64);
162 }
163
164 /**
165 * Gets the error message to output when a tracking request fails.
166 *
167 * @param Exception $e
168 * @return string
169 */
170 protected function getMessageFromException($e)
171 {
172 // Note: duplicated from FormDatabaseSetup.isAccessDenied
173 // Avoid leaking the username/db name when access denied
174 if ($e->getCode() == 1044 || $e->getCode() == 42000) {
175 return "Error while connecting to the Matomo database - please check your credentials in config/config.ini.php file";
176 }
177
178 if (Common::isPhpCliMode()) {
179 return $e->getMessage() . "\n" . $e->getTraceAsString();
180 }
181
182 return $e->getMessage();
183 }
184
185 protected function logExceptionToErrorLog($e)
186 {
187 error_log(sprintf("Error in Matomo (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e))));
188 }
189 }
190