2020-12-23 20:13:04 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* HTTPS detection functions.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 5.7.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the website is using HTTPS.
|
|
|
|
*
|
2021-01-29 20:10:58 +01:00
|
|
|
* This is based on whether both the home and site URL are using HTTPS.
|
2020-12-23 20:13:04 +01:00
|
|
|
*
|
|
|
|
* @since 5.7.0
|
2021-01-29 20:10:58 +01:00
|
|
|
* @see wp_is_home_url_using_https()
|
|
|
|
* @see wp_is_site_url_using_https()
|
2020-12-23 20:13:04 +01:00
|
|
|
*
|
|
|
|
* @return bool True if using HTTPS, false otherwise.
|
|
|
|
*/
|
|
|
|
function wp_is_using_https() {
|
2021-01-29 20:10:58 +01:00
|
|
|
if ( ! wp_is_home_url_using_https() ) {
|
2020-12-23 20:13:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:10:58 +01:00
|
|
|
return wp_is_site_url_using_https();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the current site URL is using HTTPS.
|
|
|
|
*
|
|
|
|
* @since 5.7.0
|
|
|
|
* @see home_url()
|
|
|
|
*
|
|
|
|
* @return bool True if using HTTPS, false otherwise.
|
|
|
|
*/
|
|
|
|
function wp_is_home_url_using_https() {
|
|
|
|
return 'https' === wp_parse_url( home_url(), PHP_URL_SCHEME );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the current site's URL where WordPress is stored is using HTTPS.
|
|
|
|
*
|
2021-05-15 19:38:05 +02:00
|
|
|
* This checks the URL where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder)
|
|
|
|
* are accessible.
|
2021-01-29 20:10:58 +01:00
|
|
|
*
|
|
|
|
* @since 5.7.0
|
|
|
|
* @see site_url()
|
|
|
|
*
|
|
|
|
* @return bool True if using HTTPS, false otherwise.
|
|
|
|
*/
|
|
|
|
function wp_is_site_url_using_https() {
|
2023-07-11 00:38:25 +02:00
|
|
|
/*
|
|
|
|
* Use direct option access for 'siteurl' and manually run the 'site_url'
|
|
|
|
* filter because `site_url()` will adjust the scheme based on what the
|
|
|
|
* current request is using.
|
|
|
|
*/
|
2020-12-23 20:13:04 +01:00
|
|
|
/** This filter is documented in wp-includes/link-template.php */
|
|
|
|
$site_url = apply_filters( 'site_url', get_option( 'siteurl' ), '', null, null );
|
|
|
|
|
2021-01-29 20:10:58 +01:00
|
|
|
return 'https' === wp_parse_url( $site_url, PHP_URL_SCHEME );
|
2020-12-23 20:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether HTTPS is supported for the server and domain.
|
|
|
|
*
|
|
|
|
* @since 5.7.0
|
|
|
|
*
|
|
|
|
* @return bool True if HTTPS is supported, false otherwise.
|
|
|
|
*/
|
|
|
|
function wp_is_https_supported() {
|
|
|
|
$https_detection_errors = get_option( 'https_detection_errors' );
|
|
|
|
|
|
|
|
// If option has never been set by the Cron hook before, run it on-the-fly as fallback.
|
|
|
|
if ( false === $https_detection_errors ) {
|
|
|
|
wp_update_https_detection_errors();
|
|
|
|
|
|
|
|
$https_detection_errors = get_option( 'https_detection_errors' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no detection errors, HTTPS is supported.
|
|
|
|
return empty( $https_detection_errors );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors.
|
|
|
|
*
|
|
|
|
* This internal function is called by a regular Cron hook to ensure HTTPS support is detected and maintained.
|
|
|
|
*
|
2023-09-22 21:08:19 +02:00
|
|
|
* @since 6.4.0
|
2020-12-23 20:13:04 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
2023-09-22 21:08:19 +02:00
|
|
|
function wp_get_https_detection_errors() {
|
2021-01-29 21:00:02 +01:00
|
|
|
/**
|
|
|
|
* Short-circuits the process of detecting errors related to HTTPS support.
|
|
|
|
*
|
|
|
|
* Returning a `WP_Error` from the filter will effectively short-circuit the default logic of trying a remote
|
|
|
|
* request to the site over HTTPS, storing the errors array from the returned `WP_Error` instead.
|
|
|
|
*
|
2023-09-22 21:08:19 +02:00
|
|
|
* @since 6.4.0
|
2021-01-29 21:00:02 +01:00
|
|
|
*
|
|
|
|
* @param null|WP_Error $pre Error object to short-circuit detection,
|
|
|
|
* or null to continue with the default behavior.
|
2023-09-22 21:08:19 +02:00
|
|
|
* @return null|WP_Error Error object if HTTPS detection errors are found, null otherwise.
|
2021-01-29 21:00:02 +01:00
|
|
|
*/
|
2023-09-22 21:08:19 +02:00
|
|
|
$support_errors = apply_filters( 'pre_wp_get_https_detection_errors', null );
|
2021-01-29 21:00:02 +01:00
|
|
|
if ( is_wp_error( $support_errors ) ) {
|
2023-09-22 21:08:19 +02:00
|
|
|
return $support_errors->errors;
|
2021-01-29 21:00:02 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 20:13:04 +01:00
|
|
|
$support_errors = new WP_Error();
|
|
|
|
|
|
|
|
$response = wp_remote_request(
|
|
|
|
home_url( '/', 'https' ),
|
|
|
|
array(
|
|
|
|
'headers' => array(
|
|
|
|
'Cache-Control' => 'no-cache',
|
|
|
|
),
|
|
|
|
'sslverify' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
$unverified_response = wp_remote_request(
|
|
|
|
home_url( '/', 'https' ),
|
|
|
|
array(
|
|
|
|
'headers' => array(
|
|
|
|
'Cache-Control' => 'no-cache',
|
|
|
|
),
|
|
|
|
'sslverify' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( is_wp_error( $unverified_response ) ) {
|
|
|
|
$support_errors->add(
|
2021-03-02 16:08:09 +01:00
|
|
|
'https_request_failed',
|
|
|
|
__( 'HTTPS request failed.' )
|
2020-12-23 20:13:04 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$support_errors->add(
|
|
|
|
'ssl_verification_failed',
|
2021-03-02 16:08:09 +01:00
|
|
|
__( 'SSL verification failed.' )
|
2020-12-23 20:13:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $unverified_response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $response ) ) {
|
|
|
|
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
|
|
|
|
$support_errors->add( 'bad_response_code', wp_remote_retrieve_response_message( $response ) );
|
2021-01-29 20:10:58 +01:00
|
|
|
} elseif ( false === wp_is_local_html_output( wp_remote_retrieve_body( $response ) ) ) {
|
2020-12-23 20:13:04 +01:00
|
|
|
$support_errors->add( 'bad_response_source', __( 'It looks like the response did not come from this site.' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 21:08:19 +02:00
|
|
|
return $support_errors->errors;
|
2020-12-23 20:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a given HTML string is likely an output from this WordPress site.
|
|
|
|
*
|
|
|
|
* This function attempts to check for various common WordPress patterns whether they are included in the HTML string.
|
|
|
|
* Since any of these actions may be disabled through third-party code, this function may also return null to indicate
|
|
|
|
* that it was not possible to determine ownership.
|
|
|
|
*
|
|
|
|
* @since 5.7.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param string $html Full HTML output string, e.g. from a HTTP response.
|
|
|
|
* @return bool|null True/false for whether HTML was generated by this site, null if unable to determine.
|
|
|
|
*/
|
2021-01-29 20:10:58 +01:00
|
|
|
function wp_is_local_html_output( $html ) {
|
2020-12-23 20:13:04 +01:00
|
|
|
// 1. Check if HTML includes the site's Really Simple Discovery link.
|
|
|
|
if ( has_action( 'wp_head', 'rsd_link' ) ) {
|
2021-02-19 22:13:08 +01:00
|
|
|
$pattern = preg_replace( '#^https?:(?=//)#', '', esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ) ); // See rsd_link().
|
Code Modernization: Replace usage of `strpos()` with `str_contains()`.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].
Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.
Built from https://develop.svn.wordpress.org/trunk@55988
git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 16:36:26 +02:00
|
|
|
return str_contains( $html, $pattern );
|
2020-12-23 20:13:04 +01:00
|
|
|
}
|
|
|
|
|
2023-04-04 16:56:22 +02:00
|
|
|
// 2. Check if HTML includes the site's REST API link.
|
2020-12-23 20:13:04 +01:00
|
|
|
if ( has_action( 'wp_head', 'rest_output_link_wp_head' ) ) {
|
|
|
|
// Try both HTTPS and HTTP since the URL depends on context.
|
2021-02-19 22:13:08 +01:00
|
|
|
$pattern = preg_replace( '#^https?:(?=//)#', '', esc_url( get_rest_url() ) ); // See rest_output_link_wp_head().
|
Code Modernization: Replace usage of `strpos()` with `str_contains()`.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].
Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.
Built from https://develop.svn.wordpress.org/trunk@55988
git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 16:36:26 +02:00
|
|
|
return str_contains( $html, $pattern );
|
2020-12-23 20:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise the result cannot be determined.
|
|
|
|
return null;
|
|
|
|
}
|