Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super Cache: fix how the plugin uses apache_request_headers #39951

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions projects/plugins/super-cache/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
// PhanTypeMismatchReturn : 2 occurrences
// PhanCommentParamWithoutRealParam : 1 occurrence
// PhanPluginDuplicateIfCondition : 1 occurrence
// PhanRedefineFunctionInternal : 1 occurrence
// PhanRedundantConditionInLoop : 1 occurrence
// PhanTypeArraySuspicious : 1 occurrence
// PhanTypeConversionFromArray : 1 occurrence
Expand Down Expand Up @@ -80,7 +79,7 @@
'tests/e2e/tools/mu-test-helpers.php' => ['PhanTypeMismatchArgument'],
'wp-cache-base.php' => ['PhanTypeMismatchArgumentNullableInternal'],
'wp-cache-phase1.php' => ['PhanRedundantConditionInGlobalScope', 'PhanTypeNonVarPassByRef'],
'wp-cache-phase2.php' => ['PhanImpossibleCondition', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginDuplicateIfCondition', 'PhanPluginRedundantAssignment', 'PhanPluginSimplifyExpressionBool', 'PhanPluginUnreachableCode', 'PhanPossiblyUndeclaredVariable', 'PhanRedefineFunctionInternal', 'PhanRedundantCondition', 'PhanSuspiciousValueComparison', 'PhanTypeArraySuspicious', 'PhanTypeArraySuspiciousNull', 'PhanTypeArraySuspiciousNullable', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentInternalProbablyReal', 'PhanTypeMismatchArgumentNullable', 'PhanTypeMismatchArgumentNullableInternal', 'PhanTypeNonVarPassByRef', 'PhanTypePossiblyInvalidDimOffset', 'PhanTypeSuspiciousNonTraversableForeach', 'PhanTypeSuspiciousStringExpression', 'PhanUndeclaredConstant', 'PhanUndeclaredVariableDim'],
'wp-cache-phase2.php' => ['PhanImpossibleCondition', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginDuplicateIfCondition', 'PhanPluginRedundantAssignment', 'PhanPluginSimplifyExpressionBool', 'PhanPluginUnreachableCode', 'PhanPossiblyUndeclaredVariable', 'PhanRedundantCondition', 'PhanSuspiciousValueComparison', 'PhanTypeArraySuspicious', 'PhanTypeArraySuspiciousNull', 'PhanTypeArraySuspiciousNullable', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentInternalProbablyReal', 'PhanTypeMismatchArgumentNullable', 'PhanTypeMismatchArgumentNullableInternal', 'PhanTypeNonVarPassByRef', 'PhanTypePossiblyInvalidDimOffset', 'PhanTypeSuspiciousNonTraversableForeach', 'PhanTypeSuspiciousStringExpression', 'PhanUndeclaredConstant', 'PhanUndeclaredVariableDim'],
'wp-cache.php' => ['PhanImpossibleCondition', 'PhanPluginDuplicateAdjacentStatement', 'PhanPluginDuplicateExpressionAssignmentOperation', 'PhanPluginNeverReturnFunction', 'PhanPluginSimplifyExpressionBool', 'PhanPossiblyUndeclaredVariable', 'PhanRedundantCondition', 'PhanRedundantConditionInLoop', 'PhanSuspiciousValueComparison', 'PhanTypeArraySuspiciousNullable', 'PhanTypeInvalidDimOffset', 'PhanTypeInvalidLeftOperandOfBitwiseOp', 'PhanTypeInvalidLeftOperandOfNumericOp', 'PhanTypeInvalidRightOperandOfAdd', 'PhanTypeInvalidRightOperandOfBitwiseOp', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentInternalProbablyReal', 'PhanTypeMismatchArgumentInternalReal', 'PhanTypeMismatchArgumentNullableInternal', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeNonVarPassByRef', 'PhanTypePossiblyInvalidDimOffset', 'PhanTypeSuspiciousNonTraversableForeach', 'PhanTypeSuspiciousStringExpression', 'PhanUndeclaredConstant', 'PhanUndeclaredFunction', 'PhanUndeclaredVariable', 'PhanUndeclaredVariableDim'],
],
// 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Super Cache: Fixed the apache_request_headers fallback so it works when that command is disabled
28 changes: 15 additions & 13 deletions projects/plugins/super-cache/wp-cache-phase2.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ function wp_cache_serve_cache_file() {
// don't try to match modified dates if using dynamic code.
if ( $wp_cache_mfunc_enabled == 0 && $wp_supercache_304 ) {
wp_cache_debug( 'wp_cache_serve_cache_file: checking age of cached vs served files.' );
$headers = apache_request_headers();
$headers = wpsc_apache_request_headers();
$remote_mod_time = isset( $headers['If-Modified-Since'] ) ? $headers['If-Modified-Since'] : null;

if ( $remote_mod_time === null && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
Expand Down Expand Up @@ -1751,7 +1751,7 @@ function wp_cache_user_agent_is_rejected() {
return false;
}

$headers = apache_request_headers();
$headers = wpsc_apache_request_headers();
if ( empty( $headers['User-Agent'] ) ) {
return false;
}
Expand Down Expand Up @@ -3582,23 +3582,25 @@ function wpsc_is_get_query() {
return $is_get_query;
}

if ( ! function_exists( 'apache_request_headers' ) ) {
/**
* A fallback for get request headers.
* Based on comments from http://php.net/manual/en/function.apache-request-headers.php
*
* @return array List of request headers
*/
function apache_request_headers() {
$headers = array();
/**
* A fallback for get request headers.
* Based on comments from http://php.net/manual/en/function.apache-request-headers.php
*
* @return array List of request headers
*/
function wpsc_apache_request_headers() {

if ( ! function_exists( 'apache_request_headers' ) || ! is_callable( 'apache_request_headers' ) ) {
$headers = array();
foreach ( array_keys( $_SERVER ) as $skey ) {
if ( str_starts_with( $skey, 'HTTP_' ) ) {
$header = implode( '-', array_map( 'ucfirst', array_slice( explode( '_', strtolower( $skey ) ), 1 ) ) );
$headers[ $header ] = $_SERVER[ $skey ];
}
}

return $headers;
} else {
$headers = apache_request_headers();
}

return $headers;
}
6 changes: 0 additions & 6 deletions projects/plugins/super-cache/wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -1676,8 +1676,6 @@ function wp_cache_sanitize_value($text, & $array) {
function wp_cache_update_rejected_ua() {
global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;

if ( !function_exists( 'apache_request_headers' ) ) return;

if ( isset( $_POST[ 'wp_rejected_user_agent' ] ) && $valid_nonce ) {
$_POST[ 'wp_rejected_user_agent' ] = str_replace( ' ', '___', $_POST[ 'wp_rejected_user_agent' ] );
$text = str_replace( '___', ' ', wp_cache_sanitize_value( $_POST[ 'wp_rejected_user_agent' ], $cache_rejected_user_agent ) );
Expand All @@ -1692,10 +1690,6 @@ function wp_cache_update_rejected_ua() {
function wpsc_edit_rejected_ua() {
global $cache_rejected_user_agent;

if ( ! function_exists( 'apache_request_headers' ) ) {
return;
}

$admin_url = admin_url( 'options-general.php?page=wpsupercache' );
wp_cache_update_rejected_ua();
wpsc_render_partial(
Expand Down
Loading