'GET', 'headers' => array(), 'cache' => true, 'redirects' => 0, 'body' => null), $options); $cache_key = 'aowpcs_lb_' . md5((int) AOWPCS_Settings::state('loopback_generation', 0) . '|' . $options['method'] . '|' . $url . '|' . wp_json_encode($options['headers']) . '|' . $options['redirects']); if ($options['cache']) { if (isset(self::$memory[$cache_key])) { return self::$memory[$cache_key]; } $cached = get_transient($cache_key); if (is_array($cached)) { return self::$memory[$cache_key] = $cached; } } $args = array( 'method' => $options['method'], 'timeout' => 10, 'redirection' => 0, 'sslverify' => apply_filters('https_local_ssl_verify', false), 'user-agent' => 'AOWPCS-Scanner/' . AOWPCS_PLUGIN_VERSION . ' (+' . home_url('/') . ')', 'headers' => array_merge(array(self::HEADER => self::token(), 'Cache-Control' => 'no-cache'), $options['headers']), 'limit_response_size' => self::MAX_BODY, 'cookies' => array(), ); if ($options['body'] !== null) { $args['body'] = $options['body']; } $current = $url; for ($hop = 0; ; $hop++) { $hop_args = $args; if (!self::is_own_url($current)) { unset($hop_args['headers'][self::HEADER]); } $response = wp_remote_request($current, $hop_args); if (is_wp_error($response) || $hop >= (int) $options['redirects']) { break; } $code = (int) wp_remote_retrieve_response_code($response); $location = wp_remote_retrieve_header($response, 'location'); $location = is_array($location) ? reset($location) : (string) $location; if ($code < 300 || $code > 399 || $location === '') { break; } $next = WP_Http::make_absolute_url($location, $current); if (!self::is_own_url($next)) { break; } $current = $next; if ($code === 303) { $args['method'] = 'GET'; unset($args['body']); } } $result = array('url' => $current, 'status' => 0, 'headers' => array(), 'body' => '', 'error' => ''); if (is_wp_error($response)) { $result['error'] = $response->get_error_message(); } else { $result['status'] = (int) wp_remote_retrieve_response_code($response); $headers = wp_remote_retrieve_headers($response); foreach ((is_object($headers) && method_exists($headers, 'getAll') ? $headers->getAll() : (array) $headers) as $name => $value) { $result['headers'][strtolower($name)] = is_array($value) ? implode(', ', $value) : (string) $value; } $result['body'] = substr((string) wp_remote_retrieve_body($response), 0, self::MAX_BODY); } if ($options['cache']) { self::$memory[$cache_key] = $result; set_transient($cache_key, $result, 5 * MINUTE_IN_SECONDS); } return $result; } /** * Whether the site can request its own front page (staging sites behind HTTP authentication, * firewalls and some hosts block this). Checks that depend on it report "could not verify". */ public static function available() { if (self::$available !== null) { return self::$available; } $response = self::request(home_url('/')); if ($response['error'] !== '') { /* translators: %s: response error */ self::$available = new WP_Error('aowpcs_loopback', sprintf(__('The site could not request its own home page: %s', 'all-in-one-wp-content-security'), $response['error'])); } elseif ($response['status'] === 401) { self::$available = new WP_Error('aowpcs_loopback', __('The site is protected by HTTP authentication, so public exposure cannot be tested from the server.', 'all-in-one-wp-content-security')); } else { self::$available = true; } return self::$available; } /** * Whether a URL serves content to anonymous visitors (2xx without being an HTML page that * WordPress produced for a missing file). Returns true, false or null when unknown. */ public static function is_publicly_served($url, $expect_signature = '') { $response = self::request($url, array('headers' => array('Range' => 'bytes=0-4095'))); if ($response['error'] !== '') { return null; } if ($response['status'] < 200 || $response['status'] >= 300) { return false; } if ($expect_signature !== '') { return stripos($response['body'], $expect_signature) !== false; } // A soft 404 (WordPress theme page) is not the file $type = isset($response['headers']['content-type']) ? $response['headers']['content-type'] : ''; if (stripos($type, 'text/html') !== false && preg_match('/