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