| 1 |
<?php |
| 2 |
|
| 3 |
class berqDetectCrawler { |
| 4 |
public static function is_crawler() { |
| 5 |
// Load the JSON data |
| 6 |
$json_file = optifer_PATH . '/inc/crawler/crawler-user-agents.json'; |
| 7 |
$user_agent = $_SERVER['HTTP_USER_AGENT']; |
| 8 |
$crawler_data = json_decode(file_get_contents($json_file), true); |
| 9 |
|
| 10 |
if (!$crawler_data) { |
| 11 |
error_log('Failed to load or decode the JSON file.'); |
| 12 |
return false; // Return false if the JSON file couldn't be loaded or decoded |
| 13 |
} |
| 14 |
|
| 15 |
// Loop through each crawler pattern in the JSON file |
| 16 |
foreach ($crawler_data as $crawler) { |
| 17 |
if (isset($crawler['pattern'])) { |
| 18 |
// Escape backslashes and forward slashes for accurate pattern matching |
| 19 |
$pattern = str_replace(['\\/', '/'], ['/', '\/'], $crawler['pattern']); |
| 20 |
|
| 21 |
// Check if the user agent matches the pattern |
| 22 |
if (preg_match('/' . $pattern . '/i', $user_agent)) { |
| 23 |
return true; // User agent matches a crawler pattern |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return false; // No match found, user agent is not a crawler |
| 29 |
} |
| 30 |
} |