self::MAX_FILE_SIZE ) { return new WP_Error( 'file_too_large', /* translators: %s: max size in MB */ sprintf( __( 'File exceeds the maximum allowed size of %d MB.', 'crowbridge' ), 5 ) ); } $content = file_get_contents( $real_target ); if ( false === $content ) { return new WP_Error( 'file_unreadable', __( 'Unable to read the local file.', 'crowbridge' ) ); } $ext = strtolower( pathinfo( $real_target, PATHINFO_EXTENSION ) ); $type = ( 'pdf' === $ext ) ? 'pdf' : 'html'; return array( 'content' => $content, 'type' => $type, 'filename' => pathinfo( $real_target, PATHINFO_FILENAME ), ); } /** * Fetches a remote HTTPS resource safely. * * @param string $url * @return array|WP_Error */ private static function fetch_remote( $url ) { // Strict HTTPS check per Question 23. if ( 0 !== stripos( $url, 'https://' ) ) { return new WP_Error( 'https_required', __( 'Only secure HTTPS URLs are permitted.', 'crowbridge' ) ); } // Validate URL and prevent SSRF to private/internal IPs. if ( ! wp_http_validate_url( $url ) ) { return new WP_Error( 'invalid_url', __( 'The provided URL is invalid or targets a private/restricted address.', 'crowbridge' ) ); } // Pre-flight HEAD request to check content length and type if available. $head_response = wp_safe_remote_head( $url, array( 'timeout' => 8, 'redirection' => 5, 'user-agent' => 'CrowBridge/' . CROWBRIDGE_VERSION . '; ' . home_url(), ) ); if ( ! is_wp_error( $head_response ) ) { $headers = wp_remote_retrieve_headers( $head_response ); if ( isset( $headers['content-length'] ) ) { $length = (int) $headers['content-length']; if ( $length > self::MAX_FILE_SIZE ) { return new WP_Error( 'file_too_large', sprintf( /* translators: %s: file size in bytes */ __( 'Remote file exceeds the 5 MB limit (%s bytes).', 'crowbridge' ), number_format( $length ) ) ); } } } // GET request. $response = wp_safe_remote_get( $url, array( 'timeout' => self::TIMEOUT_SECONDS, 'redirection' => 5, 'user-agent' => 'CrowBridge/' . CROWBRIDGE_VERSION . '; ' . home_url(), 'limit_response_size' => self::MAX_FILE_SIZE + 1024, ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'remote_fetch_failed', /* translators: %s: error message */ sprintf( __( 'Failed to fetch remote URL: %s', 'crowbridge' ), $response->get_error_message() ) ); } $status_code = wp_remote_retrieve_response_code( $response ); if ( 200 !== $status_code ) { return new WP_Error( 'http_status_error', /* translators: %d: HTTP status code */ sprintf( __( 'Remote server returned HTTP status code %d.', 'crowbridge' ), $status_code ) ); } $body = wp_remote_retrieve_body( $response ); if ( strlen( $body ) > self::MAX_FILE_SIZE ) { return new WP_Error( 'file_too_large', __( 'Downloaded content exceeds the 5 MB limit.', 'crowbridge' ) ); } // Determine type from Content-Type or path extension. $content_type = wp_remote_retrieve_header( $response, 'content-type' ); $parsed_path = wp_parse_url( $url, PHP_URL_PATH ); $ext = strtolower( pathinfo( (string) $parsed_path, PATHINFO_EXTENSION ) ); if ( false !== stripos( (string) $content_type, 'application/pdf' ) || 'pdf' === $ext ) { $type = 'pdf'; } else { $type = 'html'; } $filename = pathinfo( (string) $parsed_path, PATHINFO_FILENAME ); if ( empty( $filename ) ) { $filename = 'source'; } return array( 'content' => $body, 'type' => $type, 'filename' => $filename, ); } }