Lines: 95.98%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Controller; | |
| 4 | ||
| 5 | use Drupal\Component\Datetime\TimeInterface; | |
| 6 | use Drupal\Component\Utility\Unicode; | |
| 7 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 8 | use Drupal\Core\Controller\ControllerBase; | |
| 9 | use Drupal\Core\State\StateInterface; | |
| 10 | use Drupal\visitors\Helper\IpAnonymizer; | |
| 11 | use Drupal\visitors\Helper\UserIdAnonymizer; | |
| 12 | use Drupal\visitors\Helper\VisitorsUrl; | |
| 13 | use Drupal\visitors\VisitorsAiAgentInterface; | |
| 14 | use Drupal\visitors\VisitorsAiAssistantsInterface; | |
| 15 | use Drupal\visitors\VisitorsCampaignInterface; | |
| 16 | use Drupal\visitors\VisitorsCookieInterface; | |
| 17 | use Drupal\visitors\VisitorsCounterInterface; | |
| 18 | use Drupal\visitors\VisitorsDeviceInterface; | |
| 19 | use Drupal\visitors\VisitorsLocationResolverInterface; | |
| 20 | use Drupal\visitors\VisitorsReferrerInterface; | |
| 21 | use Drupal\visitors\VisitorsSearchEngineInterface; | |
| 22 | use Drupal\visitors\VisitorsSiteSearchDetectorInterface; | |
| 23 | use Drupal\visitors\VisitorsSocialNetworksInterface; | |
| 24 | use Drupal\visitors\VisitorsSpamInterface; | |
| 25 | use Drupal\visitors\VisitorsTrackerInterface; | |
| 26 | use Psr\Log\LoggerInterface; | |
| 27 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 28 | use Symfony\Component\HttpFoundation\Request; | |
| 29 | use Symfony\Component\HttpFoundation\Response; | |
| 30 | use Symfony\Component\HttpFoundation\ServerBag; | |
| 31 | ||
| 32 | /** | |
| 33 | * Visitors tracking controller. | |
| 34 | */ | |
| 35 | final class VisitorsController extends ControllerBase { | |
| 36 | ||
| 37 | /** | |
| 38 | * Maximum hits accepted in one bulk body. | |
| 39 | * | |
| 40 | * Matomo's tracker sends at most 50 hits in one POST. | |
| 41 | */ | |
| 42 | private const BULK_REQUEST_LIMIT = 50; | |
| 43 | ||
| 44 | /** | |
| 45 | * Maximum raw bulk body size, in bytes. | |
| 46 | * | |
| 47 | * 512 KiB covers 50 tracker query strings and rejects a body large | |
| 48 | * enough to be abusive. | |
| 49 | */ | |
| 50 | private const BULK_BODY_LIMIT = 524288; | |
| 51 | ||
| 52 | /** | |
| 53 | * The time service. | |
| 54 | * | |
| 55 | * @var \Drupal\Component\Datetime\TimeInterface | |
| 56 | */ | |
| 57 | protected $time; | |
| 58 | ||
| 59 | /** | |
| 60 | * The visitors settings. | |
| 61 | * | |
| 62 | * @var \Drupal\Core\Config\Config | |
| 63 | */ | |
| 64 | protected $settings; | |
| 65 | ||
| 66 | /** | |
| 67 | * The logger service. | |
| 68 | * | |
| 69 | * @var \Psr\Log\LoggerInterface | |
| 70 | */ | |
| 71 | protected $logger; | |
| 72 | ||
| 73 | /** | |
| 74 | * The counter service. | |
| 75 | * | |
| 76 | * @var \Drupal\visitors\VisitorsCounterInterface | |
| 77 | */ | |
| 78 | protected $counter; | |
| 79 | ||
| 80 | /** | |
| 81 | * The cookie service. | |
| 82 | * | |
| 83 | * @var \Drupal\visitors\VisitorsCookieInterface | |
| 84 | */ | |
| 85 | protected $cookie; | |
| 86 | ||
| 87 | /** | |
| 88 | * The device service. | |
| 89 | * | |
| 90 | * @var \Drupal\visitors\VisitorsDeviceInterface | |
| 91 | */ | |
| 92 | protected $device; | |
| 93 | ||
| 94 | /** | |
| 95 | * The tracker service. | |
| 96 | * | |
| 97 | * @var \Drupal\visitors\VisitorsTrackerInterface | |
| 98 | */ | |
| 99 | protected $tracker; | |
| 100 | ||
| 101 | /** | |
| 102 | * The search engine service. | |
| 103 | * | |
| 104 | * @var \Drupal\visitors\VisitorsSearchEngineInterface | |
| 105 | */ | |
| 106 | protected $searchEngine; | |
| 107 | ||
| 108 | /** | |
| 109 | * The site search detector. | |
| 110 | * | |
| 111 | * @var \Drupal\visitors\VisitorsSiteSearchDetectorInterface | |
| 112 | */ | |
| 113 | protected $siteSearch; | |
| 114 | ||
| 115 | /** | |
| 116 | * The spam service. | |
| 117 | * | |
| 118 | * @var \Drupal\visitors\VisitorsSpamInterface | |
| 119 | */ | |
| 120 | protected $spamService; | |
| 121 | ||
| 122 | /** | |
| 123 | * The social networks service. | |
| 124 | * | |
| 125 | * @var \Drupal\visitors\VisitorsSocialNetworksInterface | |
| 126 | */ | |
| 127 | protected $socialNetworksService; | |
| 128 | ||
| 129 | /** | |
| 130 | * The AI assistants service. | |
| 131 | * | |
| 132 | * @var \Drupal\visitors\VisitorsAiAssistantsInterface | |
| 133 | */ | |
| 134 | protected $aiAssistantsService; | |
| 135 | ||
| 136 | /** | |
| 137 | * The AI agent detector. | |
| 138 | * | |
| 139 | * @var \Drupal\visitors\VisitorsAiAgentInterface | |
| 140 | */ | |
| 141 | protected $aiAgent; | |
| 142 | ||
| 143 | /** | |
| 144 | * The campaign service. | |
| 145 | * | |
| 146 | * @var \Drupal\visitors\VisitorsCampaignInterface | |
| 147 | */ | |
| 148 | protected $campaignService; | |
| 149 | ||
| 150 | /** | |
| 151 | * The referrer parser. | |
| 152 | * | |
| 153 | * @var \Drupal\visitors\VisitorsReferrerInterface | |
| 154 | */ | |
| 155 | protected $referrer; | |
| 156 | ||
| 157 | /** | |
| 158 | * The location resolver. | |
| 159 | * | |
| 160 | * @var \Drupal\visitors\VisitorsLocationResolverInterface | |
| 161 | */ | |
| 162 | protected $locationResolver; | |
| 163 | ||
| 164 | /** | |
| 165 | * The state service. | |
| 166 | * | |
| 167 | * @var \Drupal\Core\State\StateInterface | |
| 168 | */ | |
| 169 | protected $state; | |
| 170 | ||
| 171 | /** | |
| 172 | * Visitor tracker. | |
| 173 | * | |
| 174 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | |
| 175 | * The config factory service. | |
| 176 | * @param \Drupal\Component\Datetime\TimeInterface $time | |
| 177 | * The time service. | |
| 178 | * @param \Psr\Log\LoggerInterface $logger | |
| 179 | * The logger service. | |
| 180 | * @param \Drupal\visitors\VisitorsCounterInterface $counter | |
| 181 | * The counter service. | |
| 182 | * @param \Drupal\visitors\VisitorsCookieInterface $cookie | |
| 183 | * The cookie service. | |
| 184 | * @param \Drupal\visitors\VisitorsDeviceInterface $device | |
| 185 | * The device service. | |
| 186 | * @param \Drupal\visitors\VisitorsTrackerInterface $tracker | |
| 187 | * The date service. | |
| 188 | * @param \Drupal\visitors\VisitorsSearchEngineInterface $search_engine | |
| 189 | * The search engine service. | |
| 190 | * @param \Drupal\visitors\VisitorsSiteSearchDetectorInterface $site_search | |
| 191 | * The site search detector. | |
| 192 | * @param \Drupal\visitors\VisitorsSpamInterface $spam_service | |
| 193 | * The spam service. | |
| 194 | * @param \Drupal\visitors\VisitorsSocialNetworksInterface $social_networks_service | |
| 195 | * The social networks service. | |
| 196 | * @param \Drupal\visitors\VisitorsAiAssistantsInterface $ai_assistants_service | |
| 197 | * The AI assistants service. | |
| 198 | * @param \Drupal\visitors\VisitorsAiAgentInterface $ai_agent | |
| 199 | * The AI agent detector. | |
| 200 | * @param \Drupal\visitors\VisitorsCampaignInterface $campaign_service | |
| 201 | * The campaign service. | |
| 202 | * @param \Drupal\visitors\VisitorsReferrerInterface $referrer | |
| 203 | * The referrer parser. | |
| 204 | * @param \Drupal\Core\State\StateInterface $state | |
| 205 | * The state service. Holds the user id salt. | |
| 206 | * @param \Drupal\visitors\VisitorsLocationResolverInterface $location_resolver | |
| 207 | * The location resolver. | |
| 208 | */ | |
| 209 | public function __construct( | |
| 210 | ConfigFactoryInterface $config_factory, | |
| 211 | TimeInterface $time, | |
| 212 | LoggerInterface $logger, | |
| 213 | VisitorsCounterInterface $counter, | |
| 214 | VisitorsCookieInterface $cookie, | |
| 215 | VisitorsDeviceInterface $device, | |
| 216 | VisitorsTrackerInterface $tracker, | |
| 217 | VisitorsSearchEngineInterface $search_engine, | |
| 218 | VisitorsSiteSearchDetectorInterface $site_search, | |
| 219 | VisitorsSpamInterface $spam_service, | |
| 220 | VisitorsSocialNetworksInterface $social_networks_service, | |
| 221 | VisitorsAiAssistantsInterface $ai_assistants_service, | |
| 222 | VisitorsAiAgentInterface $ai_agent, | |
| 223 | VisitorsCampaignInterface $campaign_service, | |
| 224 | VisitorsReferrerInterface $referrer, | |
| 225 | StateInterface $state, | |
| 226 | VisitorsLocationResolverInterface $location_resolver, | |
| 227 | ) { | |
| 228 | ||
| 229 | 76 | $this->settings = $config_factory->get('visitors.settings'); |
| 230 | ||
| 231 | 76 | $this->time = $time; |
| 232 | 76 | $this->logger = $logger; |
| 233 | 76 | $this->counter = $counter; |
| 234 | 76 | $this->cookie = $cookie; |
| 235 | 76 | $this->device = $device; |
| 236 | 76 | $this->tracker = $tracker; |
| 237 | 76 | $this->searchEngine = $search_engine; |
| 238 | 76 | $this->siteSearch = $site_search; |
| 239 | 76 | $this->spamService = $spam_service; |
| 240 | 76 | $this->socialNetworksService = $social_networks_service; |
| 241 | 76 | $this->aiAssistantsService = $ai_assistants_service; |
| 242 | 76 | $this->aiAgent = $ai_agent; |
| 243 | 76 | $this->campaignService = $campaign_service; |
| 244 | 76 | $this->referrer = $referrer; |
| 245 | 76 | $this->state = $state; |
| 246 | 76 | $this->locationResolver = $location_resolver; |
| 247 | } | |
| 248 | ||
| 249 | /** | |
| 250 | * {@inheritdoc} | |
| 251 | */ | |
| 252 | public static function create(ContainerInterface $container): VisitorsController { | |
| 253 | 76 | return new self( |
| 254 | 76 | $container->get('config.factory'), |
| 255 | 76 | $container->get('datetime.time'), |
| 256 | 76 | $container->get('logger.channel.visitors'), |
| 257 | 76 | $container->get('visitors.counter'), |
| 258 | 76 | $container->get('visitors.cookie'), |
| 259 | 76 | $container->get('visitors.device'), |
| 260 | 76 | $container->get('visitors.tracker'), |
| 261 | 76 | $container->get('visitors.search_engine'), |
| 262 | 76 | $container->get('visitors.site_search'), |
| 263 | 76 | $container->get('visitors.spam'), |
| 264 | 76 | $container->get('visitors.social_networks'), |
| 265 | 76 | $container->get('visitors.ai_assistants'), |
| 266 | 76 | $container->get('visitors.ai_agent'), |
| 267 | 76 | $container->get('visitors.campaign'), |
| 268 | 76 | $container->get('visitors.referrer'), |
| 269 | 76 | $container->get('state'), |
| 270 | 76 | $container->get('visitors.location_resolver'), |
| 271 | 76 | ); |
| 272 | ||
| 273 | } | |
| 274 | ||
| 275 | /** | |
| 276 | * Tracks one hit, or a Matomo bulk body. | |
| 277 | * | |
| 278 | * A query string is one hit. A JSON body is bulk: | |
| 279 | * {"requests":["?rec=1&url=...",{"url":"..."}],"send_image":0} | |
| 280 | * Each requests entry is a query string or a parameter object. | |
| 281 | * More than 50 entries, or a body over 512 KiB, is ignored. | |
| 282 | * No session or token is required. | |
| 283 | * | |
| 284 | * @param \Symfony\Component\HttpFoundation\Request $request | |
| 285 | * The tracking request. | |
| 286 | * | |
| 287 | * @return \Symfony\Component\HttpFoundation\Response | |
| 288 | * A GIF when send_image is set, otherwise an empty 204. | |
| 289 | */ | |
| 290 | public function track(Request $request): Response { | |
| 291 | 75 | $bulk = $this->bulkTracking($request); |
| 292 | 75 | $send_image = $bulk['send_image']; |
| 293 | 75 | if (!is_scalar($send_image)) { |
| 294 | 0 | $send_image = FALSE; |
| 295 | } | |
| 296 | 75 | $response = $this->getResponse($send_image); |
| 297 | ||
| 298 | // The script is still attached. The hit is dropped here. | |
| 299 | // A rejected bulk body must not fall through as a blank page view. | |
| 300 | 75 | if ($bulk['rejected'] || $this->isDoNotTrack($request)) { |
| 301 | 8 | return $response; |
| 302 | } | |
| 303 | ||
| 304 | 67 | foreach ($bulk['queries'] as $query) { |
| 305 | 67 | $this->trackHit($request, $query); |
| 306 | } | |
| 307 | 67 | return $response; |
| 308 | } | |
| 309 | ||
| 310 | /** | |
| 311 | * Stores one tracking hit. | |
| 312 | * | |
| 313 | * @param \Symfony\Component\HttpFoundation\Request $request | |
| 314 | * The tracking request. | |
| 315 | * @param array $query | |
| 316 | * The tracker parameters for this hit. | |
| 317 | */ | |
| 318 | protected function trackHit(Request $request, array $query): void { | |
| 319 | 67 | $server = $request->server; |
| 320 | ||
| 321 | // A heartbeat extends the open visit. It is not another event. | |
| 322 | 67 | if ($this->isPing($query)) { |
| 323 | 4 | $this->trackPing($request, $query); |
| 324 | 4 | return; |
| 325 | } | |
| 326 | ||
| 327 | // A media ping is not a page view. Parse it before that decision. | |
| 328 | 66 | $track_event = $this->trackEventFields($query); |
| 329 | // JavaScript errors are optional. Off drops the beacon. Drupal log | |
| 330 | // writes it and does not store a visit or an event. | |
| 331 | if ( | |
| 332 | 66 | $track_event !== NULL |
| 333 | 11 | && $track_event['event_category'] === 'JavaScript Errors' |
| 334 | 3 | && !$this->recordJavaScriptError($track_event, $query) |
| 335 | ) { | |
| 336 | 2 | return; |
| 337 | } | |
| 338 | 65 | $media = $this->mediaFields($query, $track_event); |
| 339 | if ( | |
| 340 | 65 | ($media !== NULL || $this->isMediaCategory($track_event)) |
| 341 | 4 | && !$this->isTrackingEnabled('track_media') |
| 342 | ) { | |
| 343 | 1 | return; |
| 344 | } | |
| 345 | ||
| 346 | // Downloads and outlinks share enableLinkTracking. Each is stored | |
| 347 | // only while its setting is on. A download wins when both are sent. | |
| 348 | 64 | $download_url = $this->downloadUrl($query); |
| 349 | 64 | $outlink_url = $download_url === NULL ? $this->outlinkUrl($query) : NULL; |
| 350 | if ( | |
| 351 | 64 | $download_url !== NULL |
| 352 | 3 | && !$this->isTrackingEnabled('track_downloads') |
| 353 | ) { | |
| 354 | 1 | return; |
| 355 | } | |
| 356 | if ( | |
| 357 | 64 | $outlink_url !== NULL |
| 358 | 3 | && !$this->isTrackingEnabled('track_outlinks') |
| 359 | ) { | |
| 360 | 1 | return; |
| 361 | } | |
| 362 | ||
| 363 | // A custom action without category and action is not a page view. | |
| 364 | // Matomo ignores that beacon, and so does this tracker. | |
| 365 | 63 | $page_view = $download_url === NULL |
| 366 | 63 | && $outlink_url === NULL |
| 367 | 63 | && $track_event === NULL |
| 368 | 63 | && $media === NULL; |
| 369 | 63 | if ($page_view && $this->isCustomAction($query)) { |
| 370 | 1 | return; |
| 371 | } | |
| 372 | ||
| 373 | 62 | $request_time = $this->time->getRequestTime(); |
| 374 | ||
| 375 | 62 | $visit = $this->getDefaultFields(); |
| 376 | 62 | $visit['ai_agent'] = $this->aiAgent->isAgent($request) ? 1 : 0; |
| 377 | 62 | $log = []; |
| 378 | ||
| 379 | 62 | $ip = $request->getClientIp(); |
| 380 | 62 | if (is_string($ip) && $this->settings->get('ip_anonymize')) { |
| 381 | 1 | $mask_length = (int) ($this->settings->get('ip_mask_length') ?? 1); |
| 382 | 1 | $ip = IpAnonymizer::apply($ip, $mask_length); |
| 383 | } | |
| 384 | 62 | $visit['location_ip'] = $ip; |
| 385 | 62 | $uid = $this->resolveUserId($query['uid'] ?? NULL); |
| 386 | 62 | $log['uid'] = $uid; |
| 387 | 62 | $visit['uid'] = $uid; |
| 388 | 62 | $visit['entry_time'] = $request_time; |
| 389 | 62 | $visit['exit_time'] = $request_time; |
| 390 | 62 | $visit['total_time'] = 0; |
| 391 | 62 | $visit['total_page_views'] = 0; |
| 392 | 62 | $visit['total_events'] = 0; |
| 393 | 62 | $visit['returning'] = 0; |
| 394 | 62 | $visit['total_visits'] = 1; |
| 395 | ||
| 396 | 62 | $log['title'] = $query['action_name'] ?? ''; |
| 397 | 62 | $log['page_view'] = $query['pv_id'] ?? NULL; |
| 398 | 62 | $log['created'] = $request_time; |
| 399 | ||
| 400 | 62 | $bot_retention_log = $this->settings->get('bot_retention_log'); |
| 401 | 62 | $discard_bot = ($bot_retention_log == -1); |
| 402 | 62 | $this->doDeviceDetect($visit, $server); |
| 403 | 62 | if ($discard_bot && $visit['bot']) { |
| 404 | 1 | return; |
| 405 | } | |
| 406 | ||
| 407 | 62 | $this->doVisitorId($visit, $query); |
| 408 | 62 | $this->doUrl($log, $query); |
| 409 | 62 | $this->doReferrer($log, $query); |
| 410 | ||
| 411 | 62 | $custom_page_var = $query['cvar'] ?? NULL; |
| 412 | 62 | $this->doCustom($log, $custom_page_var); |
| 413 | ||
| 414 | 62 | $custom_event_var = $query['e_cvar'] ?? NULL; |
| 415 | 62 | $this->doEvent($log, $custom_event_var); |
| 416 | 62 | if ($page_view) { |
| 417 | 52 | $this->doSiteSearch($log, $query); |
| 418 | } | |
| 419 | ||
| 420 | 62 | if ($download_url !== NULL) { |
| 421 | // The file address is the hit URL. The page stays on the page view | |
| 422 | // that shares this page_view id. event marks the hit as a download. | |
| 423 | 2 | $file = new VisitorsUrl($download_url); |
| 424 | 2 | $log['url'] = $file->getUrl(); |
| 425 | 2 | $log['url_prefix'] = $file->getPrefix(); |
| 426 | 2 | $log['event'] = 'download'; |
| 427 | } | |
| 428 | 61 | elseif ($outlink_url !== NULL) { |
| 429 | // The clicked address is the hit URL. The page stays on the page | |
| 430 | // view that shares this page_view id. | |
| 431 | 2 | $link = new VisitorsUrl($outlink_url); |
| 432 | 2 | $log['url'] = $link->getUrl(); |
| 433 | 2 | $log['url_prefix'] = $link->getPrefix(); |
| 434 | 2 | $log['event'] = 'outlink'; |
| 435 | } | |
| 436 | 60 | elseif ($track_event !== NULL) { |
| 437 | 9 | $log += $track_event; |
| 438 | } | |
| 439 | 54 | elseif ($media === NULL) { |
| 440 | 52 | $this->doCounter($custom_page_var); |
| 441 | } | |
| 442 | ||
| 443 | 62 | $this->doConfig($visit, $query); |
| 444 | 62 | $this->doPerformance($log, $query); |
| 445 | ||
| 446 | 62 | $this->doLocalTime($visit, $query); |
| 447 | ||
| 448 | 62 | $languages = $request->getLanguages() ?? []; |
| 449 | 62 | $this->doLanguage($visit, $languages); |
| 450 | 62 | $this->doLocation($visit, $ip, $languages); |
| 451 | ||
| 452 | 62 | $this->doRefererFields($visit, $query); |
| 453 | ||
| 454 | 62 | $visit_id = $this->tracker->getVisitId($visit, $request_time); |
| 455 | 62 | $log['visit_id'] = $visit_id; |
| 456 | ||
| 457 | // A progress ping updates the media row and keeps the visit open. | |
| 458 | // It is not an action in the visit log. | |
| 459 | 62 | $media_only = $media !== NULL |
| 460 | 62 | && $track_event === NULL |
| 461 | 62 | && $download_url === NULL |
| 462 | 62 | && $outlink_url === NULL; |
| 463 | 62 | if ($media_only) { |
| 464 | 2 | $media['page_view'] = $log['page_view'] ?? NULL; |
| 465 | 2 | $this->tracker->writeMedia($visit_id, $media, $request_time); |
| 466 | 2 | $this->tracker->touchVisit($visit_id, $request_time, $visit['uid'] ?? NULL); |
| 467 | 2 | return; |
| 468 | } | |
| 469 | ||
| 470 | // Write fields to database. | |
| 471 | 60 | $event_id = $this->tracker->writeEvent($log); |
| 472 | 60 | $page_view_id = $log['page_view'] ?? NULL; |
| 473 | 60 | $this->tracker->creditTime( |
| 474 | 60 | $visit_id, |
| 475 | 60 | $request_time, |
| 476 | 60 | is_string($page_view_id) && $page_view_id !== '' ? $page_view_id : NULL, |
| 477 | 60 | $event_id, |
| 478 | 60 | ); |
| 479 | 60 | if ($media !== NULL) { |
| 480 | 1 | $media['page_view'] = $log['page_view'] ?? NULL; |
| 481 | 1 | $this->tracker->writeMedia($visit_id, $media, $request_time); |
| 482 | } | |
| 483 | ||
| 484 | 60 | $uid = $visit['uid'] ?? NULL; |
| 485 | 60 | $this->tracker->updateVisit( |
| 486 | 60 | $visit_id, |
| 487 | 60 | $event_id, |
| 488 | 60 | $request_time, |
| 489 | 60 | $uid, |
| 490 | 60 | $page_view, |
| 491 | 60 | ); |
| 492 | } | |
| 493 | ||
| 494 | /** | |
| 495 | * Reads the hits to store for this beacon. | |
| 496 | * | |
| 497 | * A body without a requests key is one query-string hit. A bulk body | |
| 498 | * that is too large, not JSON, or over the hit cap is rejected so it | |
| 499 | * cannot be stored as a blank page view. | |
| 500 | * | |
| 501 | * @param \Symfony\Component\HttpFoundation\Request $request | |
| 502 | * The tracking request. | |
| 503 | * | |
| 504 | * @return array | |
| 505 | * rejected: TRUE when nothing should be stored. send_image: the | |
| 506 | * raw value for the beacon. queries: parameter arrays. | |
| 507 | */ | |
| 508 | protected function bulkTracking(Request $request): array { | |
| 509 | 75 | $content = $request->getContent(); |
| 510 | 75 | if (!is_string($content) || !$this->isBulkBody($content)) { |
| 511 | 72 | $query = $request->query->all(); |
| 512 | 72 | return [ |
| 513 | 72 | 'rejected' => FALSE, |
| 514 | 72 | 'send_image' => $query['send_image'] ?? FALSE, |
| 515 | 72 | 'queries' => [$query], |
| 516 | 72 | ]; |
| 517 | } | |
| 518 | ||
| 519 | 3 | if (strlen($content) > self::BULK_BODY_LIMIT) { |
| 520 | 0 | return [ |
| 521 | 0 | 'rejected' => TRUE, |
| 522 | 0 | 'send_image' => FALSE, |
| 523 | 0 | 'queries' => [], |
| 524 | 0 | ]; |
| 525 | } | |
| 526 | ||
| 527 | 3 | $json = json_decode($content, TRUE); |
| 528 | 3 | $requests = is_array($json) ? ($json['requests'] ?? NULL) : NULL; |
| 529 | 3 | if (!is_array($requests)) { |
| 530 | 1 | return [ |
| 531 | 1 | 'rejected' => TRUE, |
| 532 | 1 | 'send_image' => FALSE, |
| 533 | 1 | 'queries' => [], |
| 534 | 1 | ]; |
| 535 | } | |
| 536 | ||
| 537 | 2 | $send_image = $json['send_image'] ?? FALSE; |
| 538 | 2 | if (count($requests) > self::BULK_REQUEST_LIMIT) { |
| 539 | 1 | return [ |
| 540 | 1 | 'rejected' => TRUE, |
| 541 | 1 | 'send_image' => $send_image, |
| 542 | 1 | 'queries' => [], |
| 543 | 1 | ]; |
| 544 | } | |
| 545 | ||
| 546 | 1 | $queries = []; |
| 547 | 1 | foreach ($requests as $entry) { |
| 548 | 1 | $query = $this->bulkQuery($entry); |
| 549 | 1 | if ($query !== NULL) { |
| 550 | 1 | $queries[] = $query; |
| 551 | } | |
| 552 | } | |
| 553 | ||
| 554 | 1 | return [ |
| 555 | 1 | 'rejected' => FALSE, |
| 556 | 1 | 'send_image' => $send_image, |
| 557 | 1 | 'queries' => $queries, |
| 558 | 1 | ]; |
| 559 | } | |
| 560 | ||
| 561 | /** | |
| 562 | * Whether the raw body is a Matomo bulk tracking request. | |
| 563 | * | |
| 564 | * The tracker posts JSON with a form content type, so the body is | |
| 565 | * not in the query or request bags. | |
| 566 | * | |
| 567 | * @param string $content | |
| 568 | * The raw request body. | |
| 569 | * | |
| 570 | * @return bool | |
| 571 | * TRUE when the body contains a requests key. | |
| 572 | */ | |
| 573 | protected function isBulkBody(string $content): bool { | |
| 574 | 75 | return str_contains($content, '"requests"') |
| 575 | 75 | || str_contains($content, "'requests'"); |
| 576 | } | |
| 577 | ||
| 578 | /** | |
| 579 | * Reads one bulk entry into tracker parameters. | |
| 580 | * | |
| 581 | * A string is a query string, with or without a leading path. An | |
| 582 | * array is a parameter object. Anything else is skipped. | |
| 583 | * | |
| 584 | * @param mixed $entry | |
| 585 | * One requests entry. | |
| 586 | * | |
| 587 | * @return array|null | |
| 588 | * The parameters, or NULL when the entry has none. | |
| 589 | */ | |
| 590 | protected function bulkQuery(mixed $entry): ?array { | |
| 591 | 1 | if (is_string($entry)) { |
| 592 | 1 | $url = parse_url($entry); |
| 593 | if ( | |
| 594 | 1 | !is_array($url) |
| 595 | 1 | || empty($url['query']) |
| 596 | 0 | || !is_string($url['query']) |
| 597 | ) { | |
| 598 | 1 | return NULL; |
| 599 | } | |
| 600 | 0 | $params = []; |
| 601 | 0 | parse_str($url['query'], $params); |
| 602 | 0 | return $params; |
| 603 | } | |
| 604 | 1 | if (!is_array($entry)) { |
| 605 | 0 | return NULL; |
| 606 | } | |
| 607 | 1 | return $entry; |
| 608 | } | |
| 609 | ||
| 610 | /** | |
| 611 | * Resolves the user id stored on the visit and the event. | |
| 612 | * | |
| 613 | * Tracking off, an anonymous id, and an empty id are stored as NULL. | |
| 614 | * A plain id is stored as a string. A pseudonym is 32 hex characters. | |
| 615 | * | |
| 616 | * @param mixed $uid | |
| 617 | * The user id from the tracking request. | |
| 618 | * | |
| 619 | * @return string|null | |
| 620 | * The value to store, or NULL when the user is not tracked. | |
| 621 | */ | |
| 622 | protected function resolveUserId(mixed $uid): ?string { | |
| 623 | 62 | $track = $this->settings->get('track.userid') !== FALSE; |
| 624 | 62 | $anonymize = (bool) $this->settings->get('track.userid_anonymize'); |
| 625 | 62 | $anonymous = $uid === NULL || $uid === '' || $uid === 0 || $uid === '0'; |
| 626 | 62 | if (!$track || $anonymous) { |
| 627 | 60 | return NULL; |
| 628 | } | |
| 629 | ||
| 630 | 3 | $uid = (string) $uid; |
| 631 | 3 | if ($anonymize) { |
| 632 | 1 | return UserIdAnonymizer::apply($uid, $this->userIdSalt()); |
| 633 | } | |
| 634 | ||
| 635 | 2 | return substr($uid, 0, 32); |
| 636 | } | |
| 637 | ||
| 638 | /** | |
| 639 | * Returns the site salt used for user id pseudonyms. | |
| 640 | * | |
| 641 | * The salt is created once and kept in state so it is not exported | |
| 642 | * with configuration. | |
| 643 | * | |
| 644 | * @return string | |
| 645 | * The salt. | |
| 646 | */ | |
| 647 | protected function userIdSalt(): string { | |
| 648 | 1 | $salt = $this->state->get('visitors.userid_salt'); |
| 649 | 1 | if (is_string($salt) && $salt !== '') { |
| 650 | 1 | return $salt; |
| 651 | } | |
| 652 | ||
| 653 | 0 | $salt = bin2hex(random_bytes(20)); |
| 654 | 0 | $this->state->set('visitors.userid_salt', $salt); |
| 655 | 0 | return $salt; |
| 656 | } | |
| 657 | ||
| 658 | /** | |
| 659 | * Get the response. | |
| 660 | * | |
| 661 | * @param bool $send_image | |
| 662 | * Whether to send the image. | |
| 663 | * | |
| 664 | * @return \Symfony\Component\HttpFoundation\Response | |
| 665 | * The response. | |
| 666 | */ | |
| 667 | protected function getResponse(bool $send_image): Response { | |
| 668 | 75 | $headers = [ |
| 669 | 75 | 'Cache-Control' => 'no-cache, no-store, must-revalidate', |
| 670 | 75 | 'Pragma' => 'no-cache', |
| 671 | 75 | 'Expires' => '0', |
| 672 | 75 | ]; |
| 673 | 75 | $content = ''; |
| 674 | 75 | if ($send_image) { |
| 675 | 2 | $content = $this->getImageContent(); |
| 676 | 2 | $headers['Content-Type'] = 'image/gif'; |
| 677 | 2 | $headers['Content-Length'] = strlen($content); |
| 678 | } | |
| 679 | ||
| 680 | 75 | $response = new Response( |
| 681 | 75 | $content, |
| 682 | 75 | ($send_image) ? Response::HTTP_OK : Response::HTTP_NO_CONTENT, |
| 683 | 75 | $headers, |
| 684 | 75 | ); |
| 685 | ||
| 686 | 75 | return $response; |
| 687 | } | |
| 688 | ||
| 689 | /** | |
| 690 | * Whether this beacon is a heartbeat. | |
| 691 | * | |
| 692 | * @param array $query | |
| 693 | * The tracker query parameters. | |
| 694 | * | |
| 695 | * @return bool | |
| 696 | * TRUE when the tracker sent ping=1. | |
| 697 | */ | |
| 698 | protected function isPing(array $query): bool { | |
| 699 | 67 | return (string) ($query['ping'] ?? '') === '1'; |
| 700 | } | |
| 701 | ||
| 702 | /** | |
| 703 | * Extends the open visit without storing an event. | |
| 704 | * | |
| 705 | * A ping that arrives after the session window is ignored. It does | |
| 706 | * not open a new visit, bump the counter, or add an event. | |
| 707 | * | |
| 708 | * @param \Symfony\Component\HttpFoundation\Request $request | |
| 709 | * The tracking request. | |
| 710 | * @param array $query | |
| 711 | * The tracker query parameters. | |
| 712 | */ | |
| 713 | protected function trackPing(Request $request, array $query): void { | |
| 714 | 4 | $request_time = $this->time->getRequestTime(); |
| 715 | 4 | $visit = $this->getDefaultFields(); |
| 716 | 4 | $ip = $request->getClientIp(); |
| 717 | 4 | if (is_string($ip) && $this->settings->get('ip_anonymize')) { |
| 718 | 0 | $mask_length = (int) ($this->settings->get('ip_mask_length') ?? 1); |
| 719 | 0 | $ip = IpAnonymizer::apply($ip, $mask_length); |
| 720 | } | |
| 721 | 4 | $visit['location_ip'] = $ip; |
| 722 | 4 | $this->doDeviceDetect($visit, $request->server); |
| 723 | 4 | $bot_retention_log = $this->settings->get('bot_retention_log'); |
| 724 | 4 | if ($bot_retention_log == -1 && !empty($visit['bot'])) { |
| 725 | 0 | return; |
| 726 | } | |
| 727 | ||
| 728 | 4 | $this->doVisitorId($visit, $query); |
| 729 | 4 | $this->doConfig($visit, $query); |
| 730 | 4 | $languages = $request->getLanguages() ?? []; |
| 731 | 4 | $this->doLanguage($visit, $languages); |
| 732 | ||
| 733 | 4 | $visit_id = $this->tracker->findOpenVisitId($visit, $request_time); |
| 734 | 4 | if ($visit_id === NULL) { |
| 735 | 2 | return; |
| 736 | } | |
| 737 | ||
| 738 | 2 | $page_view = $query['pv_id'] ?? NULL; |
| 739 | 2 | $this->tracker->creditTime( |
| 740 | 2 | $visit_id, |
| 741 | 2 | $request_time, |
| 742 | 2 | is_string($page_view) && $page_view !== '' ? $page_view : NULL, |
| 743 | 2 | NULL, |
| 744 | 2 | ); |
| 745 | 2 | $this->tracker->touchVisit( |
| 746 | 2 | $visit_id, |
| 747 | 2 | $request_time, |
| 748 | 2 | $this->resolveUserId($query['uid'] ?? NULL), |
| 749 | 2 | ); |
| 750 | } | |
| 751 | ||
| 752 | /** | |
| 753 | * Whether this beacon should be dropped for Do Not Track. | |
| 754 | * | |
| 755 | * @param \Symfony\Component\HttpFoundation\Request $request | |
| 756 | * The tracking request. | |
| 757 | * | |
| 758 | * @return bool | |
| 759 | * TRUE when the setting is on and a Do Not Track header is set. | |
| 760 | */ | |
| 761 | protected function isDoNotTrack(Request $request): bool { | |
| 762 | 73 | if (!$this->settings->get('honor_do_not_track')) { |
| 763 | 67 | return FALSE; |
| 764 | } | |
| 765 | ||
| 766 | 6 | foreach (['DNT', 'X-Do-Not-Track'] as $header) { |
| 767 | 6 | $value = $request->headers->get($header); |
| 768 | 6 | if (!is_string($value)) { |
| 769 | continue; | |
| 770 | } | |
| 771 | 6 | $normalized = strtolower(trim($value)); |
| 772 | 6 | if ($normalized === '1' || $normalized === 'yes') { |
| 773 | 6 | return TRUE; |
| 774 | } | |
| 775 | } | |
| 776 | ||
| 777 | 0 | return FALSE; |
| 778 | } | |
| 779 | ||
| 780 | /** | |
| 781 | * Get the image content. | |
| 782 | * | |
| 783 | * @return string | |
| 784 | * The image content. | |
| 785 | */ | |
| 786 | protected function getImageContent(): string { | |
| 787 | 3 | return hex2bin('47494638396101000100800000000000FFFFFF21F9040100000000002C00000000010001000002024401003B'); |
| 788 | } | |
| 789 | ||
| 790 | /** | |
| 791 | * Get the default fields. | |
| 792 | * | |
| 793 | * @return array | |
| 794 | * The default fields. | |
| 795 | */ | |
| 796 | protected function getDefaultFields(): array { | |
| 797 | 63 | $fields = [ |
| 798 | 63 | 'bot' => 0, |
| 799 | 63 | 'ai_agent' => 0, |
| 800 | 63 | ]; |
| 801 | ||
| 802 | 63 | return $fields; |
| 803 | } | |
| 804 | ||
| 805 | /** | |
| 806 | * Detects the visitor url. | |
| 807 | * | |
| 808 | * @param string[] $fields | |
| 809 | * The fields array. | |
| 810 | * @param string[] $query | |
| 811 | * The query array. | |
| 812 | */ | |
| 813 | protected function doUrl(array &$fields, array $query) { | |
| 814 | 62 | $url = $query['url'] ?? ''; |
| 815 | 62 | if (!is_string($url)) { |
| 816 | 0 | $url = ''; |
| 817 | } | |
| 818 | // Attribution reads the raw URL. The stored page drops campaign keys | |
| 819 | // so each ad click is not a distinct page. | |
| 820 | 62 | $url = $this->campaignService->strip($url); |
| 821 | ||
| 822 | 62 | $visitors_url = new VisitorsUrl($url); |
| 823 | ||
| 824 | 62 | $fields['url'] = $visitors_url->getUrl(); |
| 825 | 62 | $fields['url_prefix'] = $visitors_url->getPrefix(); |
| 826 | } | |
| 827 | ||
| 828 | /** | |
| 829 | * Detects the visitor id. | |
| 830 | * | |
| 831 | * @param string[] $fields | |
| 832 | * The fields array. | |
| 833 | * @param string[] $query | |
| 834 | * The query array. | |
| 835 | */ | |
| 836 | protected function doVisitorId(array &$fields, array $query) { | |
| 837 | 63 | $visitor_id = $query['_id'] ?? $this->cookie->getId(); |
| 838 | 63 | $fields['visitor_id'] = $visitor_id; |
| 839 | } | |
| 840 | ||
| 841 | /** | |
| 842 | * Detects the referrer. | |
| 843 | * | |
| 844 | * @param string[] $fields | |
| 845 | * The fields array. | |
| 846 | * @param string[] $query | |
| 847 | * The query array. | |
| 848 | */ | |
| 849 | protected function doReferrer(array &$fields, array $query) { | |
| 850 | 62 | $referrer = $query['urlref'] ?? ''; |
| 851 | 62 | $page = $query['url'] ?? ''; |
| 852 | 62 | $page_host = $this->referrer->hostOf(is_string($page) ? $page : ''); |
| 853 | 62 | $fields += $this->referrer->storageFields(is_string($referrer) ? $referrer : '', $page_host); |
| 854 | } | |
| 855 | ||
| 856 | /** | |
| 857 | * Detects the device. | |
| 858 | * | |
| 859 | * @param string[] $fields | |
| 860 | * The fields array. | |
| 861 | * @param \Symfony\Component\HttpFoundation\ServerBag $server | |
| 862 | * The server array. | |
| 863 | */ | |
| 864 | protected function doDeviceDetect(array &$fields, ServerBag $server) { | |
| 865 | 63 | if (!$this->device->hasLibrary()) { |
| 866 | 2 | return NULL; |
| 867 | } | |
| 868 | ||
| 869 | 63 | $user_agent = $server->get('HTTP_USER_AGENT', ''); |
| 870 | 63 | $this->device->doDeviceFields($fields, $user_agent, $server->all()); |
| 871 | ||
| 872 | } | |
| 873 | ||
| 874 | /** | |
| 875 | * Set the fields with data in the custom variable. | |
| 876 | */ | |
| 877 | protected function doCustom(array &$fields, $cvar = NULL) { | |
| 878 | 62 | if (!is_null($cvar)) { |
| 879 | 6 | $custom = json_decode($cvar) ?? []; |
| 880 | 6 | foreach ($custom as $c) { |
| 881 | 6 | switch ($c[0]) { |
| 882 | 6 | case 'path': |
| 883 | 6 | $fields['path'] = $c[1]; |
| 884 | 6 | break; |
| 885 | ||
| 886 | 6 | case 'route': |
| 887 | 6 | $fields['route'] = $c[1]; |
| 888 | 6 | break; |
| 889 | ||
| 890 | 5 | case 'server': |
| 891 | 5 | $fields['server'] = $c[1]; |
| 892 | break; | |
| 893 | ||
| 894 | } | |
| 895 | } | |
| 896 | } | |
| 897 | } | |
| 898 | ||
| 899 | /** | |
| 900 | * Reads a Matomo download URL. | |
| 901 | * | |
| 902 | * The tracker sends the file address in download. The query string is | |
| 903 | * kept, matching Matomo, which does not strip parameters from downloads. | |
| 904 | * | |
| 905 | * @param array $query | |
| 906 | * The tracker query parameters. | |
| 907 | * | |
| 908 | * @return string|null | |
| 909 | * The file URL, or NULL when this hit is not a download. | |
| 910 | */ | |
| 911 | protected function downloadUrl(array $query): ?string { | |
| 912 | 64 | $download = $query['download'] ?? NULL; |
| 913 | 64 | if (!is_scalar($download)) { |
| 914 | 62 | return NULL; |
| 915 | } | |
| 916 | 4 | $download = trim((string) $download); |
| 917 | 4 | if ($download === '') { |
| 918 | 1 | return NULL; |
| 919 | } | |
| 920 | 3 | return $download; |
| 921 | } | |
| 922 | ||
| 923 | /** | |
| 924 | * Whether the beacon is an outbound link click. | |
| 925 | * | |
| 926 | * Matomo sends those clicks as link. Downloads use download instead. | |
| 927 | * | |
| 928 | * @param array $query | |
| 929 | * The tracker query parameters. | |
| 930 | * | |
| 931 | * @return bool | |
| 932 | * TRUE when link is present and not blank. | |
| 933 | */ | |
| 934 | protected function isOutlink(array $query): bool { | |
| 935 | 63 | $link = $query['link'] ?? NULL; |
| 936 | 63 | if (!is_scalar($link)) { |
| 937 | 60 | return FALSE; |
| 938 | } | |
| 939 | 4 | return trim((string) $link) !== ''; |
| 940 | } | |
| 941 | ||
| 942 | /** | |
| 943 | * Reads a Matomo outbound link URL. | |
| 944 | * | |
| 945 | * The tracker sends the address in link. The query string is kept, | |
| 946 | * matching Matomo, which does not strip parameters from outlinks. | |
| 947 | * | |
| 948 | * @param array $query | |
| 949 | * The tracker query parameters. | |
| 950 | * | |
| 951 | * @return string|null | |
| 952 | * The link URL, or NULL when this hit is not an outlink. | |
| 953 | */ | |
| 954 | protected function outlinkUrl(array $query): ?string { | |
| 955 | 63 | if (!$this->isOutlink($query)) { |
| 956 | 61 | return NULL; |
| 957 | } | |
| 958 | 3 | return trim((string) $query['link']); |
| 959 | } | |
| 960 | ||
| 961 | /** | |
| 962 | * Whether a tracking checkbox is on. | |
| 963 | * | |
| 964 | * A missing value is on, matching a site that has not saved the form. | |
| 965 | * | |
| 966 | * @param string $key | |
| 967 | * The visitors.settings key. | |
| 968 | * | |
| 969 | * @return bool | |
| 970 | * TRUE when the setting is on. | |
| 971 | */ | |
| 972 | protected function isTrackingEnabled(string $key): bool { | |
| 973 | 9 | return (bool) ($this->settings->get($key) ?? TRUE); |
| 974 | } | |
| 975 | ||
| 976 | /** | |
| 977 | * Whether a JavaScript error hit should be stored. | |
| 978 | * | |
| 979 | * Off drops the beacon. Drupal log writes the message, source location, | |
| 980 | * and page URL, then drops the hit. Activity log stores it as an event. | |
| 981 | * | |
| 982 | * @param array $track_event | |
| 983 | * The event columns for a JavaScript Errors hit. | |
| 984 | * @param array $query | |
| 985 | * The tracker query parameters. | |
| 986 | * | |
| 987 | * @return bool | |
| 988 | * TRUE when the hit should be stored. | |
| 989 | */ | |
| 990 | protected function recordJavaScriptError( | |
| 991 | array $track_event, | |
| 992 | array $query, | |
| 993 | ): bool { | |
| 994 | 3 | $mode = $this->settings->get('javascript_errors'); |
| 995 | 3 | if ($mode === 'activity') { |
| 996 | 1 | return TRUE; |
| 997 | } | |
| 998 | 2 | if ($mode === 'drupal') { |
| 999 | 1 | $url = $query['url'] ?? ''; |
| 1000 | 1 | $this->logger->error('JavaScript error @message at @location on @url', [ |
| 1001 | 1 | '@message' => (string) ($track_event['event_name'] ?? ''), |
| 1002 | 1 | '@location' => (string) ($track_event['event_action'] ?? ''), |
| 1003 | 1 | '@url' => is_scalar($url) ? (string) $url : '', |
| 1004 | 1 | ]); |
| 1005 | } | |
| 1006 | 2 | return FALSE; |
| 1007 | } | |
| 1008 | ||
| 1009 | /** | |
| 1010 | * Whether a trackEvent is a media interaction. | |
| 1011 | * | |
| 1012 | * @param array|null $track_event | |
| 1013 | * The event columns, or NULL when this hit is not a trackEvent. | |
| 1014 | * | |
| 1015 | * @return bool | |
| 1016 | * TRUE for MediaVideo and MediaAudio. | |
| 1017 | */ | |
| 1018 | protected function isMediaCategory(?array $track_event): bool { | |
| 1019 | 65 | if ($track_event === NULL) { |
| 1020 | 58 | return FALSE; |
| 1021 | } | |
| 1022 | 10 | return in_array($track_event['event_category'], ['MediaVideo', 'MediaAudio'], TRUE); |
| 1023 | } | |
| 1024 | ||
| 1025 | /** | |
| 1026 | * Reads Matomo media parameters. | |
| 1027 | * | |
| 1028 | * Ma_id, ma_re, and ma_mt are required. A play or finish on the same | |
| 1029 | * beacon increments the matching counter. | |
| 1030 | * | |
| 1031 | * @param array $query | |
| 1032 | * The tracker query parameters. | |
| 1033 | * @param array|null $track_event | |
| 1034 | * The event columns when this beacon is also a trackEvent. | |
| 1035 | * | |
| 1036 | * @return array|null | |
| 1037 | * The media columns, or NULL when this hit is not media. | |
| 1038 | */ | |
| 1039 | protected function mediaFields(array $query, ?array $track_event): ?array { | |
| 1040 | 65 | $media_id = $this->truncateMediaString($query['ma_id'] ?? NULL, 32); |
| 1041 | 65 | $resource = $this->truncateMediaString($query['ma_re'] ?? NULL, 2000); |
| 1042 | 65 | $type = strtolower($this->truncateMediaString($query['ma_mt'] ?? NULL, 16)); |
| 1043 | 65 | if ($media_id === '' || $resource === '' || !in_array($type, ['video', 'audio'], TRUE)) { |
| 1044 | 62 | return NULL; |
| 1045 | } | |
| 1046 | ||
| 1047 | 4 | $action = ''; |
| 1048 | 4 | if ($this->isMediaCategory($track_event)) { |
| 1049 | 2 | $action = strtolower((string) $track_event['event_action']); |
| 1050 | } | |
| 1051 | 4 | $player = $this->truncateMediaString($query['ma_pn'] ?? NULL, 32); |
| 1052 | 4 | $segments = $this->truncateMediaString($query['ma_se'] ?? NULL, 10000); |
| 1053 | ||
| 1054 | 4 | return [ |
| 1055 | 4 | 'media_id' => $media_id, |
| 1056 | 4 | 'resource' => $resource, |
| 1057 | 4 | 'media_type' => $type, |
| 1058 | 4 | 'title' => $this->truncateMediaString($query['ma_ti'] ?? NULL, 255), |
| 1059 | 4 | 'player' => $player === '' ? 'html5' : $player, |
| 1060 | 4 | 'spent_time' => $this->mediaInt($query['ma_st'] ?? NULL) ?? 0, |
| 1061 | 4 | 'length' => $this->mediaInt($query['ma_le'] ?? NULL), |
| 1062 | 4 | 'progress' => $this->mediaInt($query['ma_ps'] ?? NULL), |
| 1063 | 4 | 'time_to_play' => $this->mediaInt($query['ma_ttp'] ?? NULL), |
| 1064 | 4 | 'width' => $this->mediaInt($query['ma_w'] ?? NULL), |
| 1065 | 4 | 'height' => $this->mediaInt($query['ma_h'] ?? NULL), |
| 1066 | 4 | 'fullscreen' => ($this->mediaInt($query['ma_fs'] ?? NULL) ?? 0) === 1 ? 1 : 0, |
| 1067 | 4 | 'segments' => $segments === '' ? NULL : $segments, |
| 1068 | 4 | 'play_count' => $action === 'play' ? 1 : 0, |
| 1069 | 4 | 'finish_count' => $action === 'finish' ? 1 : 0, |
| 1070 | 4 | ]; |
| 1071 | } | |
| 1072 | ||
| 1073 | /** | |
| 1074 | * Trims and truncates a media string. | |
| 1075 | * | |
| 1076 | * @param mixed $value | |
| 1077 | * The query value. | |
| 1078 | * @param int $length | |
| 1079 | * The maximum stored length. | |
| 1080 | * | |
| 1081 | * @return string | |
| 1082 | * The stored string, or an empty string when the value is blank. | |
| 1083 | */ | |
| 1084 | protected function truncateMediaString(mixed $value, int $length): string { | |
| 1085 | 65 | if (!is_scalar($value)) { |
| 1086 | 65 | return ''; |
| 1087 | } | |
| 1088 | 4 | $value = trim((string) $value); |
| 1089 | 4 | if ($value === '') { |
| 1090 | 0 | return ''; |
| 1091 | } | |
| 1092 | 4 | return Unicode::truncate($value, $length, FALSE, FALSE); |
| 1093 | } | |
| 1094 | ||
| 1095 | /** | |
| 1096 | * Reads a non-negative media number. | |
| 1097 | * | |
| 1098 | * @param mixed $value | |
| 1099 | * The query value. | |
| 1100 | * | |
| 1101 | * @return int|null | |
| 1102 | * The rounded number, or NULL when it is missing or negative. | |
| 1103 | */ | |
| 1104 | protected function mediaInt(mixed $value): ?int { | |
| 1105 | 4 | if (!is_scalar($value)) { |
| 1106 | 4 | return NULL; |
| 1107 | } | |
| 1108 | 4 | $value = trim((string) $value); |
| 1109 | 4 | if ($value === '' || !is_numeric($value)) { |
| 1110 | 0 | return NULL; |
| 1111 | } | |
| 1112 | 4 | $number = (int) round((float) $value); |
| 1113 | 4 | if ($number < 0) { |
| 1114 | 0 | return NULL; |
| 1115 | } | |
| 1116 | 4 | return $number; |
| 1117 | } | |
| 1118 | ||
| 1119 | /** | |
| 1120 | * Reads Matomo trackEvent parameters. | |
| 1121 | * | |
| 1122 | * Category and action are required, matching Matomo. Name and value are | |
| 1123 | * optional. A missing or non-numeric value is stored as NULL. | |
| 1124 | * | |
| 1125 | * @param array $query | |
| 1126 | * The tracker query parameters. | |
| 1127 | * | |
| 1128 | * @return array|null | |
| 1129 | * The event columns, or NULL when this hit is not a trackEvent. | |
| 1130 | */ | |
| 1131 | protected function trackEventFields(array $query): ?array { | |
| 1132 | 66 | $category = $this->truncateEventString($query['e_c'] ?? NULL); |
| 1133 | 66 | $action = $this->truncateEventString($query['e_a'] ?? NULL); |
| 1134 | 66 | if ($category === '' || $action === '') { |
| 1135 | 58 | return NULL; |
| 1136 | } | |
| 1137 | ||
| 1138 | 11 | $name = $this->truncateEventString($query['e_n'] ?? NULL); |
| 1139 | ||
| 1140 | 11 | return [ |
| 1141 | 11 | 'event_category' => $category, |
| 1142 | 11 | 'event_action' => $action, |
| 1143 | 11 | 'event_name' => ($name === '') ? NULL : $name, |
| 1144 | 11 | 'event_value' => $this->eventValue($query['e_v'] ?? NULL), |
| 1145 | 11 | ]; |
| 1146 | } | |
| 1147 | ||
| 1148 | /** | |
| 1149 | * Whether the beacon is a Matomo custom action. | |
| 1150 | * | |
| 1151 | * A ca value of 0 is a page view. Any other integer is a custom action, | |
| 1152 | * matching Matomo, so the hit is not recorded as a page view. | |
| 1153 | * | |
| 1154 | * @param array $query | |
| 1155 | * The tracker query parameters. | |
| 1156 | * | |
| 1157 | * @return bool | |
| 1158 | * TRUE when ca is present and not 0. | |
| 1159 | */ | |
| 1160 | protected function isCustomAction(array $query): bool { | |
| 1161 | 53 | $ca = $query['ca'] ?? 0; |
| 1162 | 53 | return is_scalar($ca) && (int) $ca !== 0; |
| 1163 | } | |
| 1164 | ||
| 1165 | /** | |
| 1166 | * Trims and truncates a Matomo event string to the column length. | |
| 1167 | * | |
| 1168 | * @param mixed $value | |
| 1169 | * The query value. | |
| 1170 | * | |
| 1171 | * @return string | |
| 1172 | * The stored string, or an empty string when the value is blank. | |
| 1173 | */ | |
| 1174 | protected function truncateEventString(mixed $value): string { | |
| 1175 | 66 | if (!is_scalar($value)) { |
| 1176 | 61 | return ''; |
| 1177 | } | |
| 1178 | 13 | $value = trim((string) $value); |
| 1179 | 13 | if ($value === '') { |
| 1180 | 2 | return ''; |
| 1181 | } | |
| 1182 | 13 | return Unicode::truncate($value, 255, FALSE, FALSE); |
| 1183 | } | |
| 1184 | ||
| 1185 | /** | |
| 1186 | * Reads an optional Matomo event value. | |
| 1187 | * | |
| 1188 | * @param mixed $value | |
| 1189 | * The query value. | |
| 1190 | * | |
| 1191 | * @return float|null | |
| 1192 | * The value, or NULL when it is missing or not numeric. | |
| 1193 | */ | |
| 1194 | protected function eventValue(mixed $value): ?float { | |
| 1195 | 11 | if (!is_scalar($value)) { |
| 1196 | 8 | return NULL; |
| 1197 | } | |
| 1198 | 4 | $value = trim((string) $value); |
| 1199 | 4 | if ($value === '' || !is_numeric($value)) { |
| 1200 | 1 | return NULL; |
| 1201 | } | |
| 1202 | 4 | return (float) $value; |
| 1203 | } | |
| 1204 | ||
| 1205 | /** | |
| 1206 | * Records a site search on a page view. | |
| 1207 | * | |
| 1208 | * Search columns replace plugin columns already set for this hit. A page | |
| 1209 | * that is not a search is left unchanged. | |
| 1210 | * | |
| 1211 | * @param array $fields | |
| 1212 | * The event fields. | |
| 1213 | * @param array $query | |
| 1214 | * The tracking query. | |
| 1215 | */ | |
| 1216 | protected function doSiteSearch(array &$fields, array $query): void { | |
| 1217 | 52 | $url = is_string($query['url'] ?? NULL) ? $query['url'] : ''; |
| 1218 | 52 | $referrer = is_string($query['urlref'] ?? NULL) ? $query['urlref'] : ''; |
| 1219 | 52 | $search = $this->siteSearch->eventFields( |
| 1220 | 52 | (string) ($fields['route'] ?? ''), |
| 1221 | 52 | (string) ($fields['path'] ?? ''), |
| 1222 | 52 | $url, |
| 1223 | 52 | (string) ($fields['referrer_route'] ?? ''), |
| 1224 | 52 | (string) ($fields['referrer_path'] ?? ''), |
| 1225 | 52 | $referrer, |
| 1226 | 52 | $query, |
| 1227 | 52 | ); |
| 1228 | 52 | if ($search === NULL) { |
| 1229 | 52 | return; |
| 1230 | } | |
| 1231 | 5 | $fields = $search + $fields; |
| 1232 | } | |
| 1233 | ||
| 1234 | /** | |
| 1235 | * Set the fields with data in the custom variable. | |
| 1236 | */ | |
| 1237 | protected function doEvent(array &$fields, $cvar = NULL) { | |
| 1238 | 62 | if (!is_null($cvar)) { |
| 1239 | 3 | $custom = json_decode($cvar); |
| 1240 | 3 | foreach ($custom as $c) { |
| 1241 | 3 | switch ($c[0]) { |
| 1242 | 3 | case 'plugin': |
| 1243 | 3 | $fields['plugin'] = $c[1]; |
| 1244 | 3 | break; |
| 1245 | ||
| 1246 | 3 | case 'event': |
| 1247 | 3 | $fields['event'] = $c[1]; |
| 1248 | 3 | break; |
| 1249 | ||
| 1250 | 3 | case 'plugin_int_1': |
| 1251 | 3 | $fields['plugin_int_1'] = $c[1]; |
| 1252 | 3 | if ($fields['plugin_int_1'] === 'null') { |
| 1253 | 1 | $fields['plugin_int_1'] = NULL; |
| 1254 | } | |
| 1255 | 1 | break; |
| 1256 | ||
| 1257 | 3 | case 'plugin_int_2': |
| 1258 | 2 | $fields['plugin_int_2'] = $c[1]; |
| 1259 | 2 | if ($fields['plugin_int_2'] === 'null') { |
| 1260 | 1 | $fields['plugin_int_2'] = NULL; |
| 1261 | } | |
| 1262 | 1 | break; |
| 1263 | ||
| 1264 | 3 | case 'plugin_var_1': |
| 1265 | 3 | $fields['plugin_var_1'] = $c[1]; |
| 1266 | 3 | break; |
| 1267 | ||
| 1268 | 2 | case 'plugin_var_2': |
| 1269 | 2 | $fields['plugin_var_2'] = $c[1]; |
| 1270 | 2 | break; |
| 1271 | ||
| 1272 | 1 | case 'plugin_var_3': |
| 1273 | 1 | $fields['plugin_var_3'] = $c[1]; |
| 1274 | 1 | break; |
| 1275 | ||
| 1276 | 1 | case 'plugin_var_4': |
| 1277 | 1 | $fields['plugin_var_4'] = $c[1]; |
| 1278 | break; | |
| 1279 | } | |
| 1280 | } | |
| 1281 | } | |
| 1282 | } | |
| 1283 | ||
| 1284 | /** | |
| 1285 | * Record the view of the entity. | |
| 1286 | */ | |
| 1287 | protected function doCounter($cvar = NULL) { | |
| 1288 | ||
| 1289 | 52 | $viewed = NULL; |
| 1290 | 52 | if (!is_null($cvar)) { |
| 1291 | 6 | $custom = json_decode($cvar); |
| 1292 | 6 | foreach ($custom as $c) { |
| 1293 | 6 | if ($c[0] == 'viewed') { |
| 1294 | 5 | $viewed = $c[1]; |
| 1295 | } | |
| 1296 | } | |
| 1297 | } | |
| 1298 | ||
| 1299 | 52 | if (!is_null($viewed)) { |
| 1300 | 5 | [$type, $id] = explode(':', $viewed); |
| 1301 | 5 | $this->counter->recordView($type, $id); |
| 1302 | } | |
| 1303 | } | |
| 1304 | ||
| 1305 | /** | |
| 1306 | * Set the configuration fields. | |
| 1307 | */ | |
| 1308 | protected function doConfig(array &$fields, array $query) { | |
| 1309 | ||
| 1310 | 63 | $fields['config_resolution'] = $query['res'] ?? NULL; |
| 1311 | 63 | $fields['config_pdf'] = $query['pdf'] ?? NULL; |
| 1312 | 63 | $fields['config_flash'] = $query['fla'] ?? NULL; |
| 1313 | 63 | $fields['config_java'] = $query['java'] ?? NULL; |
| 1314 | 63 | $fields['config_quicktime'] = $query['qt'] ?? NULL; |
| 1315 | 63 | $fields['config_realplayer'] = $query['realp'] ?? NULL; |
| 1316 | 63 | $fields['config_windowsmedia'] = $query['wma'] ?? NULL; |
| 1317 | 63 | $fields['config_silverlight'] = $query['ag'] ?? NULL; |
| 1318 | 63 | $fields['config_cookie'] = $query['cookie'] ?? NULL; |
| 1319 | } | |
| 1320 | ||
| 1321 | /** | |
| 1322 | * Set the performance fields. | |
| 1323 | */ | |
| 1324 | protected function doPerformance(array &$fields, array $query) { | |
| 1325 | 62 | $fields['pf_network'] = $query['pf_net'] ?? NULL; |
| 1326 | 62 | $fields['pf_server'] = $query['pf_srv'] ?? NULL; |
| 1327 | 62 | $fields['pf_transfer'] = $query['pf_tfr'] ?? NULL; |
| 1328 | 62 | $fields['pf_dom_processing'] = $query['pf_dm1'] ?? NULL; |
| 1329 | 62 | $fields['pf_dom_complete'] = $query['pf_dm2'] ?? NULL; |
| 1330 | 62 | $fields['pf_on_load'] = $query['pf_onl'] ?? NULL; |
| 1331 | ||
| 1332 | 62 | $fields['pf_total'] = ($fields['pf_network'] ?? 0) |
| 1333 | 62 | + ($fields['pf_server'] ?? 0) |
| 1334 | 62 | + ($fields['pf_transfer'] ?? 0) |
| 1335 | 62 | + ($fields['pf_dom_processing'] ?? 0) |
| 1336 | 62 | + ($fields['pf_dom_complete'] ?? 0) |
| 1337 | 62 | + ($fields['pf_on_load'] ?? 0); |
| 1338 | } | |
| 1339 | ||
| 1340 | /** | |
| 1341 | * Set the visitor's local time field. | |
| 1342 | */ | |
| 1343 | protected function doLocalTime(array &$fields, array $query) { | |
| 1344 | 62 | $hours = $query['h'] ?? NULL; |
| 1345 | 62 | $minutes = $query['m'] ?? NULL; |
| 1346 | 62 | $seconds = $query['s'] ?? NULL; |
| 1347 | ||
| 1348 | 62 | $has_null = is_null($hours) || is_null($minutes) || is_null($seconds); |
| 1349 | 62 | if ($has_null) { |
| 1350 | 61 | return NULL; |
| 1351 | } | |
| 1352 | ||
| 1353 | 4 | $time = $hours * 3600 + $minutes * 60 + $seconds; |
| 1354 | ||
| 1355 | 4 | $fields['localtime'] = $time; |
| 1356 | } | |
| 1357 | ||
| 1358 | /** | |
| 1359 | * Set the language fields. | |
| 1360 | * | |
| 1361 | * Accepts only well-formed ISO 639-1 (2-letter) and ISO 639-2 (3-letter) | |
| 1362 | * primary tag. Malformed or single-letter tokens are discarded so | |
| 1363 | * that bad Accept-Language values never reach the database. | |
| 1364 | */ | |
| 1365 | protected function doLanguage(array &$fields, array $languages) { | |
| 1366 | 63 | if (empty($languages)) { |
| 1367 | 2 | return NULL; |
| 1368 | } | |
| 1369 | ||
| 1370 | 63 | $language = $languages[0] ?? ''; |
| 1371 | 63 | $lang = explode('_', $language); |
| 1372 | 63 | $language_code = strtolower((string) ($lang[0] ?? '')); |
| 1373 | ||
| 1374 | 63 | if (!preg_match('/^[a-z]{2,3}$/', $language_code)) { |
| 1375 | 2 | return NULL; |
| 1376 | } | |
| 1377 | ||
| 1378 | 63 | $fields['location_browser_lang'] = $language_code; |
| 1379 | ||
| 1380 | // $fields['location_language'] = $language; | |
| 1381 | } | |
| 1382 | ||
| 1383 | /** | |
| 1384 | * Set the location fields from the selected location plugin. | |
| 1385 | * | |
| 1386 | * The IP is the address stored on the visit. It may already be masked. | |
| 1387 | */ | |
| 1388 | protected function doLocation(array &$fields, $ip_address, array $languages): void { | |
| 1389 | 62 | $ip = is_string($ip_address) ? $ip_address : ''; |
| 1390 | 62 | $place = $this->locationResolver->resolve($ip, $languages); |
| 1391 | 62 | if ($place === NULL) { |
| 1392 | 5 | return; |
| 1393 | } | |
| 1394 | 62 | $values = [ |
| 1395 | 62 | 'location_continent' => $place->continent, |
| 1396 | 62 | 'location_country' => $place->country, |
| 1397 | 62 | 'location_region' => $place->region, |
| 1398 | 62 | 'location_city' => $place->city, |
| 1399 | 62 | 'location_latitude' => $place->latitude, |
| 1400 | 62 | 'location_longitude' => $place->longitude, |
| 1401 | 62 | ]; |
| 1402 | 62 | foreach ($values as $field => $value) { |
| 1403 | 62 | if ($value !== NULL) { |
| 1404 | 62 | $fields[$field] = $value; |
| 1405 | } | |
| 1406 | } | |
| 1407 | } | |
| 1408 | ||
| 1409 | /** | |
| 1410 | * Sets the visit referrer fields. | |
| 1411 | * | |
| 1412 | * Campaign parameters are read from the landing page, then from the | |
| 1413 | * tracker's _rcn and _rck values. A campaign on the referrer does not | |
| 1414 | * win. A referrer whose host is the viewed page is direct entry. | |
| 1415 | */ | |
| 1416 | protected function doRefererFields(array &$fields, array $query) { | |
| 1417 | ||
| 1418 | $referer = []; | |
| 1419 | 62 | $fields['referer_url'] = $query['urlref'] ?? ''; |
| 1420 | 62 | $url_ref_parts = parse_url(is_string($fields['referer_url']) ? $fields['referer_url'] : ''); |
| 1421 | 62 | $fields['referer_name'] = $url_ref_parts['host'] ?? ''; |
| 1422 | 62 | $url_parts = parse_url(is_string($query['url'] ?? NULL) ? $query['url'] : ''); |
| 1423 | 62 | $page_host = $url_parts['host'] ?? ''; |
| 1424 | 62 | $same_host = $page_host !== '' |
| 1425 | 62 | && strcasecmp($page_host, (string) $fields['referer_name']) === 0; |
| 1426 | ||
| 1427 | 62 | $campaign = $this->campaignFromRequest($query); |
| 1428 | 62 | if (is_array($campaign) && $this->hasCampaignAttribution($campaign)) { |
| 1429 | 6 | $referer['referer_type'] = 'campaign'; |
| 1430 | 6 | $referer['referer_name'] = $campaign['campaign'] ?? ''; |
| 1431 | 6 | $referer['referer_keyword'] = $campaign['keyword'] ?? ''; |
| 1432 | $dimensions = [ | |
| 1433 | 'source' => 'campaign_source', | |
| 1434 | 'medium' => 'campaign_medium', | |
| 1435 | 'content' => 'campaign_content', | |
| 1436 | 'campaign_id' => 'campaign_id', | |
| 1437 | 'group' => 'campaign_group', | |
| 1438 | 'placement' => 'campaign_placement', | |
| 1439 | ]; | |
| 1440 | 6 | foreach ($dimensions as $key => $column) { |
| 1441 | 6 | $value = $campaign[$key] ?? NULL; |
| 1442 | 6 | $referer[$column] = is_string($value) && $value !== '' ? $value : NULL; |
| 1443 | } | |
| 1444 | } | |
| 1445 | 59 | elseif ($this->spamService->match($fields['referer_name'])) { |
| 1446 | 1 | $referer['referer_type'] = 'spam'; |
| 1447 | } | |
| 1448 | 59 | elseif ($fields['referer_url'] === '' || $same_host) { |
| 1449 | $referer['referer_type'] = 'direct'; | |
| 1450 | $referer['referer_name'] = ''; | |
| 1451 | 47 | $referer['referer_keyword'] = ''; |
| 1452 | } | |
| 1453 | 14 | elseif ($referer_name = $this->socialNetworksService->match($fields['referer_name'])) { |
| 1454 | 5 | $referer['referer_type'] = 'social_network'; |
| 1455 | 5 | $referer['referer_name'] = $referer_name; |
| 1456 | } | |
| 1457 | 11 | elseif ($referer_name = $this->aiAssistantsService->match($fields['referer_name'])) { |
| 1458 | 2 | $referer['referer_type'] = 'ai_assistant'; |
| 1459 | 2 | $referer['referer_name'] = $referer_name; |
| 1460 | } | |
| 1461 | 9 | elseif ($data = $this->searchEngine->match($fields['referer_url'])) { |
| 1462 | 3 | $referer['referer_type'] = 'search_engine'; |
| 1463 | 3 | $referer['referer_name'] = $data['name']; |
| 1464 | 3 | $referer['referer_keyword'] = $data['keyword']; |
| 1465 | } | |
| 1466 | else { | |
| 1467 | 6 | $referer['referer_type'] = 'website'; |
| 1468 | } | |
| 1469 | ||
| 1470 | 62 | $fields = array_merge($fields, $referer); |
| 1471 | ||
| 1472 | } | |
| 1473 | ||
| 1474 | /** | |
| 1475 | * Reads the campaign from the landing URL, then from tracker parameters. | |
| 1476 | * | |
| 1477 | * @param array $query | |
| 1478 | * The tracking query. | |
| 1479 | * | |
| 1480 | * @return array|null | |
| 1481 | * Parsed campaign values, or NULL when none are present. | |
| 1482 | */ | |
| 1483 | protected function campaignFromRequest(array $query): ?array { | |
| 1484 | 62 | $landing = $query['url'] ?? NULL; |
| 1485 | 62 | $campaign = $this->campaignService->parse(is_string($landing) ? $landing : NULL); |
| 1486 | 62 | if ($this->hasCampaignAttribution($campaign)) { |
| 1487 | 5 | return $campaign; |
| 1488 | } | |
| 1489 | ||
| 1490 | 60 | $tracker = []; |
| 1491 | 60 | foreach (['_rcn', '_rck'] as $key) { |
| 1492 | 60 | $value = $query[$key] ?? NULL; |
| 1493 | 60 | if (is_string($value) && $value !== '') { |
| 1494 | 1 | $tracker[$key] = $value; |
| 1495 | } | |
| 1496 | } | |
| 1497 | 60 | if ($tracker === []) { |
| 1498 | 59 | return $campaign; |
| 1499 | } | |
| 1500 | ||
| 1501 | 1 | return $this->campaignService->parse(http_build_query($tracker)); |
| 1502 | } | |
| 1503 | ||
| 1504 | /** | |
| 1505 | * Whether parsed campaign data includes a name or source. | |
| 1506 | * | |
| 1507 | * @param array|null $campaign | |
| 1508 | * Parsed campaign values. | |
| 1509 | * | |
| 1510 | * @return bool | |
| 1511 | * TRUE when the name or source is a non-empty string. | |
| 1512 | */ | |
| 1513 | protected function hasCampaignAttribution(?array $campaign): bool { | |
| 1514 | 62 | if (!is_array($campaign)) { |
| 1515 | 60 | return FALSE; |
| 1516 | } | |
| 1517 | 6 | foreach (['campaign', 'source'] as $key) { |
| 1518 | if ( | |
| 1519 | 6 | isset($campaign[$key]) |
| 1520 | 6 | && is_string($campaign[$key]) |
| 1521 | 6 | && $campaign[$key] !== '' |
| 1522 | ) { | |
| 1523 | 6 | return TRUE; |
| 1524 | } | |
| 1525 | } | |
| 1526 | 0 | return FALSE; |
| 1527 | } | |
| 1528 | ||
| 1529 | } |