WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 months ago

#15838 new defect (bug)

get_home_path() mixes PHP and server paths

Reported by: reviewmylife Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.0.3
Component: Filesystem API Keywords: has-patch
Focuses: Cc:

Description

FYI: I've looked through the other get_home_path() tickets and believe this to be a different issue.

If the home and site urls are the same then you get a PHP relative path returned. i.e. it uses ABSPATH , which is calculated using the PHP constant __FILE__
.

If the home and site urls are different you get a server relative path returned. i.e. one calculated via
$_SERVER["SCRIPT_FILENAME"] .

With web hosts who have the server and PHP paths configured consistently this is no problem.

But on 1and1 it could be if you are comparing the return value of this function against another path. If the function can either return a PHP or a server path then your code may not always work.

The server path (Apache) on 1and1 has an extra '/kunden' at the start. This means that PHP and Apache retrieve paths slightly differently.

Example.
Blog with same home and site urls

get_home_path =         /homepages/xx/dxxxxxxxx/htdocs/site1

Blog with different home and site urls

get_home_path = /kunden/homepages/xx/dxxxxxxxx/htdocs/site1

I'd suggest that as this is PHP the right thing to do is to always calculate the path using PHP relative paths - i.e. using
__FILE__ / ABSPATH . Don't use the server relative paths at all as you can't rely on them being the same as the PHP ones.

Certainly having a function returning a path that can be calculated from two different base paths is a bad idea.

For reference here's the current function:

function get_home_path() {
	$home = get_option( 'home' );
	$siteurl = get_option( 'siteurl' );
	if ( $home != '' && $home != $siteurl ) {
	        $wp_path_rel_to_home = str_replace($home, '', $siteurl); /* $siteurl - $home */
	        $pos = strpos($_SERVER["SCRIPT_FILENAME"], $wp_path_rel_to_home);
	        $home_path = substr($_SERVER["SCRIPT_FILENAME"], 0, $pos);
		$home_path = trailingslashit( $home_path );
	} else {
		$home_path = ABSPATH;
	}

	return $home_path;
}

Attachments (2)

15838.patch (1.6 KB) - added by kurtpayne 2 years ago.
Accounting for differences between ABSPATH and $_SERVERDOCUMENT_ROOT? and caching return value
1and1_server_array.php (4.7 KB) - added by kurtpayne 2 years ago.
Test data from an example 1&1 site

Download all attachments as: .zip

Change History (6)

kurtpayne2 years ago

Accounting for differences between ABSPATH and $_SERVERDOCUMENT_ROOT? and caching return value

comment:2 kurtpayne2 years ago

  • Cc kpayne@… added
  • Keywords has-patch added

I was able to recreate this, partially, on 1and1 hosting. The $_SERVER['PHP_DOCUMENT_ROOT'] variable isn't present on my test account. The $_SERVER['DOCUMENT_ROOT'] and __FILE__ paths differed as reported by reviewmylife. The DOCUMENT_ROOT was prepended with /kunden.

Patch 15838.patch should resolve the problem without affecting backwards functionality.

comment:3 follow-up: dd322 years ago

  • Keywords get_home_path 1and1 removed

Can we please get a var_export() of $_SERVER on an affected site?

(Var_export so we can dump it straight into a test environment)

kurtpayne2 years ago

Test data from an example 1&1 site

comment:4 in reply to: ↑ 3 nacin3 months ago

  • Component changed from General to Filesystem

Replying to dd32:

Can we please get a var_export() of $_SERVER on an affected site?

(Var_export so we can dump it straight into a test environment)

Looks like this got uploaded.

Note: See TracTickets for help on using tickets.