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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tracker;
use Exception;
use Piwik\Common;
use Piwik\Config;
use Piwik\Profiler;
use Piwik\Timer;
use Piwik\Tracker;
use Piwik\Tracker\Db as TrackerDb;
use Piwik\Url;
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 int $statusCode eg 500
*/
public function outputException(Tracker $tracker, Exception $e, $statusCode)
{
Common::sendResponseCode($statusCode);
$this->logExceptionToErrorLog($e);
if ($tracker->isDebugModeEnabled()) {
echo "\nAn exception occurred: " . $this->getMessageFromException($e) . "\n\n";
} 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>.<br>\n";
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.\n";
} 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;
}
// Check for a custom tracking image
$customImage = Config::getInstance()->Tracker['custom_image'];
if (!empty($customImage) && $this->outputCustomImage($customImage)) {
return;
}
// No custom image defined, so output the default 1x1 base64 transparent gif
$this->outputTransparentGif();
}
/**
* Output a 1px x 1px transparent gif
*/
private function outputTransparentGif()
{
$transGifBase64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
Common::sendHeader('Content-Type: image/gif');
echo base64_decode($transGifBase64);
}
/**
* Output a custom tracking image
*
* @param string $customImage The custom image setting specified in the config
*
* @return bool True if the custom image was successfully output, else false
*/
private function outputCustomImage(string $customImage): bool
{
$supportedMimeTypes = ['image/png', 'image/gif', 'image/jpeg'];
$img = null;
$size = null;
if (strlen($customImage) > 2 && substr($customImage, -2) == '==') {
// Base64 image string
$img = base64_decode($customImage);
$size = getimagesizefromstring($img);
} elseif (is_file($customImage) && is_readable($customImage)) {
// Image file
$img = file_get_contents($customImage);
$size = getimagesize($customImage); // imagesize is used to get the mime type
}
// Must have valid image data and a valid mime type to proceed
if ($img && $size && isset($size['mime']) && in_array($size['mime'], $supportedMimeTypes)) {
Common::sendHeader('Content-Type: ' . $size['mime']);
echo $img;
return true;
}
return false;
}
/**
* 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)
{
$hostname = Url::getRFCValidHostname();
$hostStr = $hostname ? "[$hostname]" : '-';
error_log(sprintf("$hostStr Error in Matomo (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e))));
}
}
|