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