Skip to content
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
174 changes: 90 additions & 84 deletions components/DataLiberation/URL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,35 @@
* then it would be nice to re-encode that block markup also without the space character. This is similar
* to how the tag processor avoids changing parts of the tag it doesn't need to change.
*/
function wp_rewrite_urls( $options ) {
if ( empty( $options['base_url'] ) ) {
// Use first from-url as base_url if not specified.
$from_urls = array_keys( $options['url-mapping'] );
$options['base_url'] = $from_urls[0];
}
if ( ! function_exists( __NAMESPACE__ . '\\wp_rewrite_urls' ) ) {
function wp_rewrite_urls( $options ) {
if ( empty( $options['base_url'] ) ) {
// Use first from-url as base_url if not specified.
$from_urls = array_keys( $options['url-mapping'] );
$options['base_url'] = $from_urls[0];
}

$url_mapping = array();
foreach ( $options['url-mapping'] as $from_url_string => $to_url_string ) {
$url_mapping[] = array(
'from_url' => WPURL::parse( $from_url_string ),
'to_url' => WPURL::parse( $to_url_string ),
);
}
$url_mapping = array();
foreach ( $options['url-mapping'] as $from_url_string => $to_url_string ) {
$url_mapping[] = array(
'from_url' => WPURL::parse( $from_url_string ),
'to_url' => WPURL::parse( $to_url_string ),
);
}

$p = new BlockMarkupUrlProcessor( $options['block_markup'], $options['base_url'] );
while ( $p->next_url() ) {
$parsed_url = $p->get_parsed_url();
foreach ( $url_mapping as $mapping ) {
if ( is_child_url_of( $parsed_url, $mapping['from_url'] ) ) {
$p->replace_base_url( $mapping['to_url'] );
break;
$p = new BlockMarkupUrlProcessor( $options['block_markup'], $options['base_url'] );
while ( $p->next_url() ) {
$parsed_url = $p->get_parsed_url();
foreach ( $url_mapping as $mapping ) {
if ( is_child_url_of( $parsed_url, $mapping['from_url'] ) ) {
$p->replace_base_url( $mapping['to_url'] );
break;
}
}
}
}

return $p->get_updated_html();
return $p->get_updated_html();
}
}

/**
Expand All @@ -73,32 +75,34 @@ function wp_rewrite_urls( $options ) {
*
* @return bool Whether the URL matches the current site URL.
*/
function is_child_url_of( $child, $parent_url ) {
$parent_url = is_string( $parent_url ) ? WPURL::parse( $parent_url ) : $parent_url;
$child = is_string( $child ) ? WPURL::parse( $child ) : $child;
$child_pathname_no_trailing_slash = rtrim( urldecode( $child->pathname ), '/' );

if ( false === $child || false === $parent_url ) {
return false;
}
if ( ! function_exists( __NAMESPACE__ . '\\is_child_url_of' ) ) {
function is_child_url_of( $child, $parent_url ) {
$parent_url = is_string( $parent_url ) ? WPURL::parse( $parent_url ) : $parent_url;
$child = is_string( $child ) ? WPURL::parse( $child ) : $child;
$child_pathname_no_trailing_slash = rtrim( urldecode( $child->pathname ), '/' );

if ( false === $child || false === $parent_url ) {
return false;
}

if ( $parent_url->hostname !== $child->hostname ) {
return false;
}
if ( $parent_url->hostname !== $child->hostname ) {
return false;
}

if ( $parent_url->protocol !== $child->protocol ) {
return false;
}
if ( $parent_url->protocol !== $child->protocol ) {
return false;
}

$parent_pathname = urldecode( $parent_url->pathname );
$parent_pathname = urldecode( $parent_url->pathname );

return (
// Direct match.
$parent_pathname === $child_pathname_no_trailing_slash ||
$parent_pathname === $child_pathname_no_trailing_slash . '/' ||
// Path prefix.
0 === strncmp( $child_pathname_no_trailing_slash . '/', $parent_pathname, strlen( $parent_pathname ) )
);
return (
// Direct match.
$parent_pathname === $child_pathname_no_trailing_slash ||
$parent_pathname === $child_pathname_no_trailing_slash . '/' ||
// Path prefix.
0 === strncmp( $child_pathname_no_trailing_slash . '/', $parent_pathname, strlen( $parent_pathname ) )
);
}
}

/**
Expand All @@ -112,53 +116,55 @@ function is_child_url_of( $child, $parent_url ) {
*
* @return string The decoded string.
*/
function urldecode_n( $input, $decode_n ) {
// Fast paths: nothing to do.
if ( $decode_n <= 0 || false === strpos( $input, '%' ) ) {
if ( ! function_exists( __NAMESPACE__ . '\\urldecode_n' ) ) {
function urldecode_n( $input, $decode_n ) {
// Fast paths: nothing to do.
if ( $decode_n <= 0 || false === strpos( $input, '%' ) ) {
return $input;
}

$result = '';
$at = 0;
while ( true ) {
if ( $at + 3 > strlen( $input ) ) {
break;
}

$last_at = $at;
$at += strcspn( $input, '%', $at );
// Consume bytes except for the percent sign.
$result .= substr( $input, $last_at, $at - $last_at );
$result = '';
$at = 0;
while ( true ) {
if ( $at + 3 > strlen( $input ) ) {
break;
}

// If we've already decoded the requested number of bytes, stop.
if ( strlen( $result ) >= $decode_n ) {
break;
}
$last_at = $at;
$at += strcspn( $input, '%', $at );
// Consume bytes except for the percent sign.
$result .= substr( $input, $last_at, $at - $last_at );

++$at;
if ( $at > strlen( $input ) ) {
break;
}
// If we've already decoded the requested number of bytes, stop.
if ( strlen( $result ) >= $decode_n ) {
break;
}

$decodable_length = strspn(
$input,
'0123456789ABCDEFabcdef',
$at,
2
);
++$at;
if ( $at > strlen( $input ) ) {
break;
}

if ( 2 === $decodable_length ) {
// Decodes the urlencoded hex sequence from URL.
// Note: This decodes bytes, not characters. It will recover the original byte sequence,
// not necessarily any valid UTF-8 characters.
$result .= chr( hexdec( $input[ $at ] . $input[ $at + 1 ] ) );
$at += 2;
} else {
// Consume the next byte and move on.
$result .= '%';
$decodable_length = strspn(
$input,
'0123456789ABCDEFabcdef',
$at,
2
);

if ( 2 === $decodable_length ) {
// Decodes the urlencoded hex sequence from URL.
// Note: This decodes bytes, not characters. It will recover the original byte sequence,
// not necessarily any valid UTF-8 characters.
$result .= chr( hexdec( $input[ $at ] . $input[ $at + 1 ] ) );
$at += 2;
} else {
// Consume the next byte and move on.
$result .= '%';
}
}
}
$result .= substr( $input, $at );
$result .= substr( $input, $at );

return $result;
return $result;
}
}
Loading
Loading