← Back
Editing: class-cal-aiv-detector.php
<?php if (!defined('ABSPATH')) { exit; } class CAL_AIV_Detector { private static $matched_bot = null; public static function run() { if (is_admin() || (defined('DOING_CRON') && DOING_CRON)) { return; } $bot_list = CAL_AIV_Bot_List::get_list(); if (empty($bot_list)) { return; } $user_agent = cal_aiv_server_value('HTTP_USER_AGENT'); if ($user_agent === null) { return; } $ua_lower = strtolower($user_agent); $matched_name = null; $matched_length = 0; // Longest match wins: Applebot-Extended must not be reported as Applebot, since /collect keys bot IP ranges by name. foreach ($bot_list as $signature => $name) { $signature = (string) $signature; if (strlen($signature) > $matched_length && strpos($ua_lower, strtolower($signature)) !== false) { // A list saved in another shape (e.g. {"GPTBot": {}}) falls back to the signature as the name. $matched_name = is_string($name) && $name !== '' ? $name : $signature; $matched_length = strlen($signature); } } if ($matched_name === null) { return; } // /collect requires url's host to equal domain, so both come from the same normalised host. $host = cal_aiv_request_host(); if ($host === '') { return; } self::$matched_bot = array( 'url' => self::get_full_url($host), 'method' => isset($_SERVER['REQUEST_METHOD']) ? strtoupper(sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD']))) : 'GET', 'user_agent' => $user_agent, 'referer' => self::strip_query(cal_aiv_server_value('HTTP_REFERER')), 'origin' => cal_aiv_server_value('HTTP_ORIGIN'), 'client_ip' => self::get_client_ip(), 'hostname' => $host, 'bot_name' => $matched_name, ); } public static function get_matched() { return self::$matched_bot; } private static function get_full_url($host) { $scheme = is_ssl() ? 'https' : 'http'; $uri = self::strip_query(cal_aiv_server_value('REQUEST_URI')); // Apache passes an absolute-form request target ("GET https://host/path") through as-is. if ($uri !== null && preg_match('#^https?://#i', $uri)) { $path = wp_parse_url($uri, PHP_URL_PATH); $uri = $path ? $path : '/'; } return $scheme . '://' . $host . ($uri === null || $uri === '' ? '/' : $uri); } // Query strings can carry emails or tokens, and /collect drops them anyway, so they never leave the site. private static function strip_query($url) { if ($url === null) { return null; } $parts = preg_split('/[?#]/', $url, 2); return $parts[0]; } private static function get_client_ip() { $remote = (string) cal_aiv_server_value('REMOTE_ADDR'); if (filter_var($remote, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { return $remote; } // Forwarded headers are trusted only behind a local proxy; from the internet anyone can set them. foreach (array('HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR') as $header) { $value = cal_aiv_server_value($header); if ($value === null) { continue; } $parts = explode(',', $value); $ip = trim($parts[0]); if (filter_var($ip, FILTER_VALIDATE_IP)) { return $ip; } } return filter_var($remote, FILTER_VALIDATE_IP) ? $remote : ''; } }
Save File
Cancel