'GET', 'timeout' => 10, 'max' => 2000000, 'redirects' => 5, 'ua' => '', 'headers' => []]; $out = ['ok' => false, 'code' => 0, 'headers' => [], 'body' => '', 'url' => $url, 'ms' => 0, 'error' => '', 'chain' => []]; $ua = $a['ua'] ?: 'Mozilla/5.0 (compatible; AIOT-Site-Audit/' . AIOTSA_VERSION . '; +' . home_url('/') . ')'; for ($i = 0; $i <= $a['redirects']; $i++) { $url = aiotsa_norm_url($url); if ($url === '') { $out['error'] = 'bad_url'; return $out; } $p = wp_parse_url($url); $host = strtolower($p['host']); $own = aiotsa_is_own($url); $pin = null; if (!$own) { $ips = aiotsa_resolve($host); if (!$ips) { $out['error'] = 'private_or_unresolved'; return $out; } if (!filter_var($host, FILTER_VALIDATE_IP)) { $port = ($p['scheme'] === 'https') ? 443 : 80; $pin = [$host . ':' . $port . ':' . $ips[0]]; } } $GLOBALS['aiotsa_pin'] = $pin; $t = microtime(true); $r = wp_remote_request($url, ['method' => $a['method'], 'timeout' => $a['timeout'], 'redirection' => 0, 'user-agent' => $ua, 'limit_response_size' => $a['max'], 'headers' => $a['headers'] + ['Accept' => 'text/html,application/xhtml+xml,*/*;q=0.8'], 'sslverify' => !$own || !empty($a['verify']), 'reject_unsafe_urls' => false]); $GLOBALS['aiotsa_pin'] = null; $ms = (int) round((microtime(true) - $t) * 1000); if (is_wp_error($r)) { $out['error'] = $r->get_error_message(); $out['url'] = $url; $out['ms'] = $ms; return $out; } $code = (int) wp_remote_retrieve_response_code($r); $h = wp_remote_retrieve_headers($r); $h = is_object($h) ? $h->getAll() : (array) $h; $out['chain'][] = ['url' => $url, 'code' => $code]; if ($code >= 300 && $code < 400 && !empty($h['location'])) { $loc = is_array($h['location']) ? end($h['location']) : $h['location']; $url = WP_Http::make_absolute_url($loc, $url); continue; } $out = ['ok' => $code >= 200 && $code < 400, 'code' => $code, 'headers' => $h, 'body' => (string) wp_remote_retrieve_body($r), 'url' => $url, 'ms' => $ms, 'error' => '', 'chain' => $out['chain']]; return $out; } $out['error'] = 'too_many_redirects'; return $out; } function aiotsa_hdr($res, $name) { $v = $res['headers'][strtolower($name)] ?? ''; return is_array($v) ? implode(', ', $v) : (string) $v; } /** HEAD first, GET when the server refuses HEAD. Used for link checks. */ function aiotsa_status($url) { $r = aiotsa_fetch($url, ['method' => 'HEAD', 'timeout' => 7, 'max' => 2000]); if ($r['error'] === '' && in_array($r['code'], [403, 405, 400, 501, 0], true)) $r = aiotsa_fetch($url, ['timeout' => 7, 'max' => 20000, 'ua' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36']); return $r; } function aiotsa_dns($host, $type) { if (!function_exists('dns_get_record')) return null; $r = apply_filters('aiotsa_dns', null, $host, $type); if (is_array($r)) return $r; $r = @dns_get_record($host, $type); // phpcs:ignore WordPress.PHP.NoSilencedErrors return is_array($r) ? $r : null; } /** Registrable domain, good enough for common suffixes. */ function aiotsa_root_domain($host) { $host = preg_replace('/^www\./', '', strtolower($host)); $p = explode('.', $host); $n = count($p); if ($n <= 2) return $host; $two = $p[$n - 2] . '.' . $p[$n - 1]; if (preg_match('/^(co|com|net|org|gov|ac|edu|gen|firm|ind|nic|res)\.[a-z]{2}$/', $two)) return $p[$n - 3] . '.' . $two; return $two; }