'ChatGPT', 'chat.openai.com' => 'ChatGPT', 'perplexity.ai' => 'Perplexity', 'claude.ai' => 'Claude', 'gemini.google.com' => 'Gemini', 'copilot.microsoft.com' => 'Copilot', 'chat.mistral.ai' => 'Mistral Le Chat', 'chat.deepseek.com' => 'DeepSeek', ); $host = strtolower((string) wp_parse_url($ref, PHP_URL_HOST)); foreach ($map as $domain => $name) { if ($host === $domain || substr($host, -strlen('.' . $domain)) === '.' . $domain) { return $name; } } return null; } function realbots_record($kind, $key, $verdict, $path) { global $wpdb; $kind = substr((string) $kind, 0, 8); $key = substr((string) $key, 0, 64); $verdict = substr((string) $verdict, 0, 16); $path = substr((string) $path, 0, 191); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- atomic counter upsert on our own table; caching would lose hits $wpdb->query($wpdb->prepare( 'INSERT INTO %i (day, rowhash, kind, botkey, verdict, path, hits) VALUES (%s, %s, %s, %s, %s, %s, 1) ON DUPLICATE KEY UPDATE hits = hits + 1', realbots_table(), gmdate('Y-m-d'), md5($kind . '|' . $key . '|' . $verdict . '|' . $path), $kind, $key, $verdict, $path )); } function realbots_track() { if (is_admin() || wp_doing_cron() || (defined('WP_CLI') && WP_CLI)) { return; } $ua = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : ''; $uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '/'; $path = (string) strtok($uri, '?'); if ($path === '') { $path = '/'; } // Web Bot Auth: cryptographically signed agents, recorded per agent host. $wba = realbots_wba_current(); if ($wba !== null && $wba['status'] !== 'none') { $host = $wba['agent'] ? strtolower((string) wp_parse_url($wba['agent'], PHP_URL_HOST)) : ''; if (!preg_match('/^[a-z0-9.-]{1,64}$/', $host)) { $host = 'unknown'; } realbots_record('agent', $host, $wba['status'], $wba['status'] === 'valid' ? $path : '*'); } $bot = $ua !== '' ? realbots_match_bot($ua) : null; if ($bot) { list($ip, $hidden) = realbots_client_ip(); $verdict = realbots_verdict($bot, $ip, $hidden); // Path detail only for real bots (what AI reads); fakes aggregate under '*'. realbots_record('bot', $bot['slug'], $verdict, $verdict === 'verified' ? $path : '*'); $s = get_option('realbots_settings', array()); if (!empty($s['block_fakes']) && $verdict === 'fake') { // Safe: a genuine request from this operator can't fail the IP check. status_header(403); nocache_headers(); exit('Forbidden'); } return; } $ref = isset($_SERVER['HTTP_REFERER']) ? esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER'])) : ''; if ($ref !== '') { $ai = realbots_ai_referrer($ref); if ($ai) { realbots_record('ref', $ai, 'visit', $path); } } }