PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.3
5.13.0 5.12.1 5.12.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 2 years ago Handler 2 years ago Visit 2 years ago Action.php 2 years ago ActionPageview.php 2 years ago Cache.php 2 years ago Db.php 2 years ago Failures.php 2 years ago FingerprintSalt.php 2 years ago GoalManager.php 2 years ago Handler.php 2 years ago IgnoreCookie.php 2 years ago LogTable.php 2 years ago Model.php 2 years ago PageUrl.php 2 years ago Request.php 2 years ago RequestProcessor.php 2 years ago RequestSet.php 2 years ago Response.php 2 years ago ScheduledTasksRunner.php 2 years ago Settings.php 2 years ago TableLogAction.php 2 years ago TrackerCodeGenerator.php 2 years ago TrackerConfig.php 2 years ago Visit.php 2 years ago VisitExcluded.php 2 years ago VisitInterface.php 2 years ago Visitor.php 2 years ago VisitorNotFoundInDb.php 2 years ago VisitorRecognizer.php 2 years ago
Response.php
195 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik\Tracker;
11
12 use Exception;
13 use Piwik\Common;
14 use Piwik\Config;
15 use Piwik\Profiler;
16 use Piwik\Timer;
17 use Piwik\Tracker;
18 use Piwik\Tracker\Db as TrackerDb;
19 use Piwik\Url;
20 class Response
21 {
22 private $timer;
23 private $content;
24 public function init(Tracker $tracker)
25 {
26 ob_start();
27 // we use ob_start only because of Common::printDebug, we should actually not really use ob_start
28 if ($tracker->isDebugModeEnabled() && \Piwik\Tracker\TrackerConfig::getConfigValue('enable_sql_profiler')) {
29 $this->timer = new Timer();
30 TrackerDb::enableProfiling();
31 }
32 }
33 public function getOutput()
34 {
35 $this->outputAccessControlHeaders();
36 if (is_null($this->content) && ob_get_level() > 0) {
37 $this->content = ob_get_clean();
38 }
39 return $this->content;
40 }
41 /**
42 * Echos an error message & other information, then exits.
43 *
44 * @param Tracker $tracker
45 * @param Exception $e
46 * @param int $statusCode eg 500
47 */
48 public function outputException(Tracker $tracker, Exception $e, $statusCode)
49 {
50 Common::sendResponseCode($statusCode);
51 $this->logExceptionToErrorLog($e);
52 if ($tracker->isDebugModeEnabled()) {
53 echo "\nAn exception occurred: " . $this->getMessageFromException($e) . "\n\n";
54 } else {
55 $this->outputApiResponse($tracker);
56 }
57 }
58 public function outputResponse(Tracker $tracker)
59 {
60 if (!$tracker->shouldRecordStatistics()) {
61 Common::sendResponseCode(503);
62 $this->outputApiResponse($tracker);
63 Common::printDebug("Logging disabled, display transparent logo");
64 } elseif (!$tracker->hasLoggedRequests()) {
65 if (!$this->isHttpGetRequest() || !empty($_GET) || !empty($_POST)) {
66 Common::sendResponseCode(400);
67 }
68 Common::printDebug("Empty request => Matomo page");
69 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";
70 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";
71 } else {
72 $this->outputApiResponse($tracker);
73 Common::printDebug("Nothing to notice => default behaviour");
74 }
75 Common::printDebug("End of the page.");
76 if ($tracker->isDebugModeEnabled() && $tracker->isDatabaseConnected() && TrackerDb::isProfilingEnabled()) {
77 $db = Tracker::getDatabase();
78 $db->recordProfiling();
79 Profiler::displayDbTrackerProfile($db);
80 }
81 if ($tracker->isDebugModeEnabled()) {
82 Common::printDebug($_COOKIE);
83 Common::printDebug((string) $this->timer);
84 }
85 }
86 private function outputAccessControlHeaders()
87 {
88 if (!$this->isHttpGetRequest()) {
89 $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
90 Common::sendHeader('Access-Control-Allow-Origin: ' . $origin);
91 Common::sendHeader('Access-Control-Allow-Credentials: true');
92 }
93 }
94 private function isHttpGetRequest()
95 {
96 $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
97 return strtoupper($requestMethod) === 'GET';
98 }
99 private function getOutputBuffer()
100 {
101 return ob_get_contents();
102 }
103 protected function hasAlreadyPrintedOutput()
104 {
105 return strlen($this->getOutputBuffer()) > 0;
106 }
107 private function outputApiResponse(Tracker $tracker)
108 {
109 if ($tracker->isDebugModeEnabled()) {
110 return;
111 }
112 if ($this->hasAlreadyPrintedOutput()) {
113 return;
114 }
115 $request = $_GET + $_POST;
116 if ($this->isHttpGetRequest()) {
117 Common::sendHeader('Cache-Control: no-store');
118 }
119 if (array_key_exists('send_image', $request) && $request['send_image'] === '0') {
120 Common::sendResponseCode(204);
121 return;
122 }
123 // Check for a custom tracking image
124 $customImage = Config::getInstance()->Tracker['custom_image'];
125 if (!empty($customImage) && $this->outputCustomImage($customImage)) {
126 return;
127 }
128 // No custom image defined, so output the default 1x1 base64 transparent gif
129 $this->outputTransparentGif();
130 }
131 /**
132 * Output a 1px x 1px transparent gif
133 */
134 private function outputTransparentGif()
135 {
136 $transGifBase64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
137 Common::sendHeader('Content-Type: image/gif');
138 echo base64_decode($transGifBase64);
139 }
140 /**
141 * Output a custom tracking image
142 *
143 * @param string $customImage The custom image setting specified in the config
144 *
145 * @return bool True if the custom image was successfully output, else false
146 */
147 private function outputCustomImage(string $customImage) : bool
148 {
149 $supportedMimeTypes = ['image/png', 'image/gif', 'image/jpeg'];
150 $img = null;
151 $size = null;
152 if (strlen($customImage) > 2 && substr($customImage, -2) == '==') {
153 // Base64 image string
154 $img = base64_decode($customImage);
155 $size = getimagesizefromstring($img);
156 } elseif (is_file($customImage) && is_readable($customImage)) {
157 // Image file
158 $img = file_get_contents($customImage);
159 $size = getimagesize($customImage);
160 // imagesize is used to get the mime type
161 }
162 // Must have valid image data and a valid mime type to proceed
163 if ($img && $size && isset($size['mime']) && in_array($size['mime'], $supportedMimeTypes)) {
164 Common::sendHeader('Content-Type: ' . $size['mime']);
165 echo $img;
166 return true;
167 }
168 return false;
169 }
170 /**
171 * Gets the error message to output when a tracking request fails.
172 *
173 * @param Exception $e
174 * @return string
175 */
176 protected function getMessageFromException($e)
177 {
178 // Note: duplicated from FormDatabaseSetup.isAccessDenied
179 // Avoid leaking the username/db name when access denied
180 if ($e->getCode() == 1044 || $e->getCode() == 42000) {
181 return "Error while connecting to the Matomo database - please check your credentials in config/config.ini.php file";
182 }
183 if (Common::isPhpCliMode()) {
184 return $e->getMessage() . "\n" . $e->getTraceAsString();
185 }
186 return $e->getMessage();
187 }
188 protected function logExceptionToErrorLog($e)
189 {
190 $hostname = Url::getRFCValidHostname();
191 $hostStr = $hostname ? "[{$hostname}]" : '-';
192 error_log(sprintf("{$hostStr} Error in Matomo (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e))));
193 }
194 }
195