Skip to content

Commit

Permalink
fix replace lowercasing
Browse files Browse the repository at this point in the history
  • Loading branch information
francescolaffi committed Sep 7, 2014
1 parent 1e152f9 commit 8cc4daf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
26 changes: 18 additions & 8 deletions safe-redirect-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,17 @@ public function action_parse_request() {
// Allow redirects to be filtered
$redirects = apply_filters( 'srm_registered_redirects', $redirects, $unslashed_requested_path );


$case_insensitive = apply_filters( 'srm_case_insensitive_redirects', true );

$regex_flag = '';
$original_unslashed_request_path = $requested_path;
if ( $case_insensitive ) {
$regex_flag = 'i';
$unslashed_requested_path = strtolower( $unslashed_requested_path );
}


foreach ( (array)$redirects as $redirect ) {

$redirect_from = untrailingslashit( $redirect['redirect_from'] );
Expand All @@ -827,15 +838,14 @@ public function action_parse_request() {
$status_code = $redirect['status_code'];
$enable_regex = ( isset( $redirect['enable_regex'] ) ) ? $redirect['enable_regex'] : false;

if ( apply_filters( 'srm_case_insensitive_redirects', true ) ) {
$unslashed_requested_path = strtolower( $unslashed_requested_path );
$redirect_from = strtolower( $redirect_from );
}

// check if requested path is the same as the redirect from path
if ( $enable_regex ) {
$matched_path = preg_match( '@' . $redirect_from . '@', $unslashed_requested_path );
$matched_path = preg_match( '@' . $redirect_from . '@' . $regex_flag, $original_unslashed_request_path );
} else {
if ( $case_insensitive ) {
$redirect_from = strtolower( $redirect_from );
}

$matched_path = ( $unslashed_requested_path == $redirect_from );

// check if the redirect_from ends in a wildcard
Expand All @@ -845,7 +855,7 @@ public function action_parse_request() {
// mark as match if requested path matches the base of the redirect from
$matched_path = (substr( $unslashed_requested_path, 0, strlen( $wildcard_base ) ) == $wildcard_base);
if ( (strrpos( $redirect_to, '*' ) == strlen( $redirect_to ) - 1 ) ) {
$redirect_to = rtrim( $redirect_to, '*' ) . ltrim( substr( $unslashed_requested_path, strlen( $wildcard_base ) ), '/' );
$redirect_to = rtrim( $redirect_to, '*' ) . ltrim( substr( $original_unslashed_request_path, strlen( $wildcard_base ) ), '/' );
}
}
}
Expand All @@ -860,7 +870,7 @@ public function action_parse_request() {

// Allow for regex replacement in $redirect_to
if ( $enable_regex ) {
$redirect_to = preg_replace( '@' . $redirect_from . '@', $redirect_to, $unslashed_requested_path );
$redirect_to = preg_replace( '@' . $redirect_from . '@' . $regex_flag, $redirect_to, $original_unslashed_request_path );
}

$sanitized_redirect_to = esc_url_raw( $redirect_to );
Expand Down
41 changes: 41 additions & 0 deletions tests/test-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,45 @@ public function testSimplePathRegex() {

$this->assertTrue( ! $redirected );
}

/**
* Test that replace (both wildcard and regex) doesn't change the casing on the matched part
*
* @since 1.7.5
*/
public function testReplaceCasing() {
global $safe_redirect_manager;

// with wildcard
$_SERVER['REQUEST_URI'] = '/myfiles1/FooBar.JPEG';
$redirected = false;
$redirect_to = '/images1/*';
$safe_redirect_manager->create_redirect( '/myfiles1/*', $redirect_to );

add_action( 'srm_do_redirect', function( $requested_path, $redirected_to, $status_code ) use ( &$redirect_to, &$redirected ) {
if ( $redirected_to === '/images1/FooBar.JPEG' ) {
$redirected = true;
}
}, 10, 3 );

$safe_redirect_manager->action_parse_request();

$this->assertTrue( $redirected );

// with regex
$_SERVER['REQUEST_URI'] = '/myfiles2/FooBar.JPEG';
$redirected = false;
$redirect_to = '/images2/$1';
$safe_redirect_manager->create_redirect( '/myfiles2/(.*\.jpe?g)', $redirect_to, 301, true );

add_action( 'srm_do_redirect', function( $requested_path, $redirected_to, $status_code ) use ( &$redirect_to, &$redirected ) {
if ( $redirected_to === '/images2/FooBar.JPEG' ) {
$redirected = true;
}
}, 10, 3 );

$safe_redirect_manager->action_parse_request();

$this->assertTrue( $redirected );
}
}

0 comments on commit 8cc4daf

Please sign in to comment.