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

Fix erratic $log_file_link and $raw_log_file_link on Debug TAB on Windows #633

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

DrLightman
Copy link

On Windows backslashes in path break the replace. For example $log_file_link may become something like this:

http://example.org/D:/my_sites/example_org/wp-content/cache/view_e1b50ae15624d52670a4a76c4ce03610.php

Because ABSPATH value on Windows is D:\my_sites\example_org/ therefore it doesn't get replaced with the empty string.

Same goes with $raw_log_file_link since they are being generated in the same manner involving a string replacement.

On Windows backslashes in path break the replace.
For example $log_file_link becomes:
http://example.org/D:/my_sites/example_org/wp-content/cache/view_e1b50ae15624d52670a4a76c4ce03610.php
Because ABSPATH value on Windows is: "D:\my_sites\example_org/" therefore it doesn't get replaced with the empty string.
@stodorovic
Copy link

@DrLightman Thanks. I think that's better to we keep str_replace. Anyway, there are two variants which could work:

  • Normalization of ABSPATH:
    $abspath = functions_exists( 'wp_normalize_path' ) ? wp_normalize_path( ABSPATH ) : str_replace( '\\', '/', ABSPATH );

  • Get realpath for $cache_path before str_replace (it seems as more clear solution):
    $cache_rel_path = str_replace( ABSPATH, '', wpsc_get_realpath( $cache_path ) );
    $log_file_link = "<a href='" . site_url( "{$cache_rel_path}view_{$wp_cache_debug_log}" ) ....

I use cygwin on Windows and I can't test it at this moment. I'd like to we avoid substr(on this way) if it's possible. Could you please check is it working on Windows and update PR?

@DrLightman
Copy link
Author

Hi @stodorovic can you check now? I wasn't able to make it shorter, I had to start with:

ABSPATH = D:\Sites\example_org/
$cache_path = D:\Sites\example_org\wp-content/cache/

Also, I don't know if this kind of replacement might break when an user customizes the cache location in the Advanced tab... by moving it outside the document root, is it even possibile, theoretically?

} else {
$cache_url = str_replace( '\\', '/', $cache_path );
$cache_url = str_replace( str_replace( '\\', '/', ABSPATH ), '', $cache_url );
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found old laptop with Windows XP/PHP 5.2,5.3,5.4. I've tested following code:

Suggested change
}
// Extracts and normalizes relative path.
$cache_rel_path = str_replace( realpath( ABSPATH ), '', realpath( $cache_path ) );
$cache_rel_path = str_replace( '\\', '/', $cache_rel_path );

It seems OK by first tests and it's more readable. We need only to replace \ because $cache_rel_path doesn't contain drive letter.

$cache_url = str_replace( '\\', '/', $cache_path );
$cache_url = str_replace( str_replace( '\\', '/', ABSPATH ), '', $cache_url );
}
$cache_url = site_url( $cache_url );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$cache_url = site_url( $cache_url );
$cache_url = trailingslashit( site_url( $cache_rel_path ) );

It's possible to realpath strips last slash.

@stodorovic
Copy link

stodorovic commented Dec 7, 2018

I've checked on older PHP versions (windows) and it seems that we could reduce complexity. Could you check my suggestions? Is it working on your installations?
Also, there are other issues related to security rules (which often prevent access to the logs) and I've an idea to remove PHP code from logs. It's possible to serve part of logs via AJAX request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants