query('c', $request->input('c', '')))); $clientUid = trim((string) $request->query('u', $request->input('u', ''))); $path = trim((string) $request->query('p', $request->input('p', ''))); if (! preg_match('/^[0-9a-f]{32}$/', $channelId)) { return $this->pixel(); } if (! preg_match('/^[0-9a-fA-F-]{8,64}$/', $clientUid) && ! preg_match('/^tmp_[0-9a-f]+$/i', $clientUid)) { return $this->pixel(); } if (strlen($path) > 255) { $path = substr($path, 0, 255); } $debounceKey = 'page_hit:'.$channelId.':'.$clientUid; if (! Cache::add($debounceKey, 1, now()->addSeconds(8))) { return $this->pixel(); } $ua = substr((string) $request->userAgent(), 0, 512); $parsed = UserAgentParser::parse($ua); $referer = $this->referer($request); PageVisit::query()->create([ 'channel_id' => $channelId, 'client_uid' => substr($clientUid, 0, 64), 'user_agent' => $ua !== '' ? $ua : null, 'os' => $parsed['os'], 'os_version' => $parsed['os_version'] !== '' ? $parsed['os_version'] : null, 'browser' => $parsed['browser'], 'browser_version' => $parsed['browser_version'] !== '' ? $parsed['browser_version'] : null, 'ip' => $request->ip(), 'path' => $path !== '' ? $path : null, 'referer' => $referer, 'created_at' => now(), ]); return $this->pixel(); } private function referer(Request $request): ?string { $referer = trim((string) $request->query('r', $request->input('r', ''))); if ($referer === '') { $referer = trim((string) $request->headers->get('referer', '')); } if ($referer === '') { return null; } return substr($referer, 0, 512); } private function pixel(): Response { return response(self::GIF_1X1, 200, [ 'Content-Type' => 'image/gif', 'Content-Length' => (string) strlen(self::GIF_1X1), 'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0', 'Pragma' => 'no-cache', ]); } }