File: //home/vitanhod/www/wp-content/plugins/system-control/includes/class-sc-bot-detector.php
<?php
/**
* Bot and Search Referrer Detection
*/
class SC_Bot_Detector {
private static $google_mobile_patterns = [
'/Googlebot.*Android/i',
'/Android.*Googlebot/i',
];
private static $google_patterns = [
'/Googlebot\//i',
'/Googlebot-Mobile/i',
'/Googlebot-Image/i',
'/Googlebot-Video/i',
'/Googlebot-News/i',
'/Storebot-Google/i',
'/Google-InspectionTool/i',
'/GoogleOther/i',
'/APIs-Google/i',
'/AdsBot-Google/i',
'/Mediapartners-Google/i',
'/FeedFetcher-Google/i',
];
private static $bing_patterns = [
'/bingbot\//i',
'/msnbot/i',
'/BingPreview/i',
'/adidxbot/i',
];
private static $yandex_patterns = [
'/YandexBot/i',
'/YandexAccessibilityBot/i',
'/YandexMobileBot/i',
'/YandexDirectDyn/i',
'/YandexImages/i',
'/YandexVideo/i',
'/YandexMedia/i',
'/YandexBlogs/i',
'/YandexFavicons/i',
'/YandexWebmaster/i',
'/YandexPagechecker/i',
'/YandexImageResizer/i',
'/YandexAdNet/i',
'/YandexDirect/i',
'/YandexMetrika/i',
'/YandexNews/i',
'/YandexCatalog/i',
];
/**
* Detect bot type from user-agent
* IMPORTANT: For Google — only Google Android (mobile) bot is tracked.
* Other Google bots are ignored (not detected as bot).
* @return array ['is_bot' => bool, 'bot_type' => string|null]
*/
public static function detect($user_agent = null) {
if ($user_agent === null) {
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
}
if (empty($user_agent)) {
return ['is_bot' => false, 'bot_type' => null];
}
// Google: ONLY Android mobile bot is detected, all other Google bots are IGNORED
foreach (self::$google_mobile_patterns as $pattern) {
if (preg_match($pattern, $user_agent)) {
return ['is_bot' => true, 'bot_type' => 'google'];
}
}
// Other Google bots — intentionally NOT detected (skip $google_patterns)
foreach (self::$bing_patterns as $pattern) {
if (preg_match($pattern, $user_agent)) {
return ['is_bot' => true, 'bot_type' => 'bing'];
}
}
foreach (self::$yandex_patterns as $pattern) {
if (preg_match($pattern, $user_agent)) {
return ['is_bot' => true, 'bot_type' => 'yandex'];
}
}
return ['is_bot' => false, 'bot_type' => null];
}
/**
* Detect search engine referrer
* @return array ['is_search' => bool, 'referrer_type' => string|null]
*/
public static function detect_search_referrer($referrer = null) {
if ($referrer === null) {
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
}
if (empty($referrer)) {
return ['is_search' => false, 'referrer_type' => null];
}
$ref = strtolower($referrer);
if (preg_match('/\b(google\.com|google\.co\.|google\.ru|google\.de|google\.fr|google\.es|google\.it|google\.com\.br|google\.ca|google\.com\.au|google\.co\.uk|google\.co\.jp)\b/', $ref)) {
return ['is_search' => true, 'referrer_type' => 'google'];
}
if (preg_match('/\b(bing\.com)\b/', $ref)) {
return ['is_search' => true, 'referrer_type' => 'bing'];
}
if (preg_match('/\b(yandex\.ru|yandex\.com|yandex\.by|yandex\.kz|yandex\.ua)\b/', $ref)) {
return ['is_search' => true, 'referrer_type' => 'yandex'];
}
return ['is_search' => false, 'referrer_type' => null];
}
/**
* Detect device type from user-agent
*/
public static function detect_device($user_agent) {
$ua = strtolower($user_agent);
if (strpos($ua, 'android') !== false) return 'android';
if (strpos($ua, 'iphone') !== false || strpos($ua, 'ipad') !== false) return 'iphone';
if (strpos($ua, 'macintosh') !== false || strpos($ua, 'mac os') !== false) return 'mac';
if (strpos($ua, 'windows') !== false) return 'windows';
if (strpos($ua, 'linux') !== false) return 'linux';
return 'other';
}
/**
* Detect browser from user-agent
*/
public static function detect_browser($user_agent) {
if (preg_match('/Edg\//i', $user_agent)) return 'Edge';
if (preg_match('/OPR\//i', $user_agent) || preg_match('/Opera/i', $user_agent)) return 'Opera';
if (preg_match('/YaBrowser/i', $user_agent)) return 'Yandex Browser';
if (preg_match('/Chrome\//i', $user_agent)) return 'Chrome';
if (preg_match('/Firefox\//i', $user_agent)) return 'Firefox';
if (preg_match('/Safari\//i', $user_agent) && !preg_match('/Chrome/i', $user_agent)) return 'Safari';
if (preg_match('/MSIE|Trident/i', $user_agent)) return 'IE';
return 'Other';
}
}