X-Real-IP > X-Forwarded-For > REMOTE_ADDR */ function argon_ai_query_get_client_ip() { $ip = ''; // Cloudflare if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) { $ip = $_SERVER['HTTP_CF_CONNECTING_IP']; } // Nginx proxy_pass 或其他反向代理 elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) { $ip = $_SERVER['HTTP_X_REAL_IP']; } // 通过代理转发(取第一个 IP) elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]; } // 直连 IP else { $ip = $_SERVER['REMOTE_ADDR']; } $ip = trim($ip); // 验证 IP 格式 if (!filter_var($ip, FILTER_VALIDATE_IP)) { return ''; } // 如果是内网 IP 或 CDN IP,尝试从其他头获取 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) { // 内网 IP,尝试从 X-Forwarded-For 获取真实公网 IP if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = array_map('trim', explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])); foreach ($ips as $forwarded_ip) { if (filter_var($forwarded_ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { return $forwarded_ip; } } } } return $ip; } /** * 检查 IP 访问频率限制 * @return bool|string true 表示允许访问,字符串表示错误信息 */ function argon_ai_query_check_rate_limit() { $client_ip = argon_ai_query_get_client_ip(); if (empty($client_ip)) { return __('无法获取客户端 IP', 'argon'); } $transient_key = 'ai_query_lock_' . md5($client_ip); $rate_limit_key = 'ai_query_rate_' . md5($client_ip); // 检查是否有正在进行的查询(单线程限制) if (get_transient($transient_key)) { return __('请等待上一次查询完成', 'argon'); } // 检查访问频率(60秒内最多10次) $access_count = get_transient($rate_limit_key); if ($access_count === false) { set_transient($rate_limit_key, 1, 60); } else { if ($access_count >= 10) { return __('访问过于频繁,请稍后再试', 'argon'); } set_transient($rate_limit_key, $access_count + 1, 60); } // 设置查询锁(3秒超时) set_transient($transient_key, 1, 3); return true; } // 执行访问限制检查 $rate_limit_check = argon_ai_query_check_rate_limit(); if ($rate_limit_check !== true) { // 访问受限,显示错误页面 get_header(); ?>

get_var($wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_argon_ai_summary_code' AND meta_value = %s", $query_code )); if ($post_id) { $post = get_post($post_id); if ($post && $post->post_status === 'publish') { $result = [ 'post_id' => $post_id, 'post_title' => get_the_title($post_id), 'post_url' => get_permalink($post_id), 'post_date' => get_the_date('Y-m-d H:i:s', $post_id), 'post_modified' => get_the_modified_date('Y-m-d H:i:s', $post_id), 'post_author' => get_the_author_meta('display_name', $post->post_author), 'summary' => get_post_meta($post_id, '_argon_ai_summary', true), 'model' => get_post_meta($post_id, '_argon_ai_summary_model', true), 'provider' => get_post_meta($post_id, '_argon_ai_summary_provider', true), 'generated_time' => get_post_meta($post_id, '_argon_ai_summary_time', true), 'code' => $query_code ]; $provider_names = [ 'openai' => 'OpenAI', 'anthropic' => 'Anthropic', 'deepseek' => 'DeepSeek', 'qianwen' => '通义千问', 'wenxin' => '文心一言', 'doubao' => '豆包', 'kimi' => 'Kimi', 'zhipu' => '智谱', 'siliconflow' => 'SiliconFlow' ]; $result['provider_display'] = isset($provider_names[$result['provider']]) ? $provider_names[$result['provider']] : $result['provider']; // 缓存结果(1小时) set_transient($cache_key, $result, 3600); } else { $error = __('文章不存在或未发布', 'argon'); } } else { $error = __('未找到对应的 AI 生成内容记录', 'argon'); } } } } // 释放查询锁 $client_ip = argon_ai_query_get_client_ip(); if (!empty($client_ip)) { delete_transient('ai_query_lock_' . md5($client_ip)); } get_header(); ?>