Skip to content

Commit

Permalink
Fix #11534: Incorrect path/short_path detection
Browse files Browse the repository at this point in the history
Rather than attempting to try and decipher the path and short_path by
comparing the file's location on the filesytem with the document_root,
MantisBT should be looking at the PHP_SELF server var provided by PHP.
This variable is based on the current URL used to access the file, and
is not confused by symlinks or the document_root pointing directly to
the installation.  It is also much simpler than the old method.

Tested on Linux and Windows with the following setups:
 - installed as subdomain, eg http://mantis.host/foo.php
 - installed in subdirectory, eg http://host/mantisbt/foo.php
 - accessed by symlink (Linux), eg http://host/mantis/foo.php
   where /var/www/mantis -> /usr/share/mantisbt
  • Loading branch information
amyreese committed Apr 19, 2010
1 parent 4ec992b commit 5ac1fdf
Showing 1 changed file with 5 additions and 35 deletions.
40 changes: 5 additions & 35 deletions config_defaults_inc.php
Expand Up @@ -123,42 +123,12 @@
} else if ( isset( $_SERVER['SERVER_ADDR'] ) ) {
$t_host = $_SERVER['SERVER_ADDR'] . $t_port;
} else {
$t_host = 'www.example.com';
$t_host = 'localhost';
}

// Get server root to compare with path to this file
if( !isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
if( isset( $_SERVER['SCRIPT_FILENAME'] ) ) {
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr( $_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen( $_SERVER['SCRIPT_NAME'] ) ) );
}
}

if( !isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
if( isset( $_SERVER['PATH_TRANSLATED'] ) ) {
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr( str_replace( '\\\\', '\\', $_SERVER['PATH_TRANSLATED'] ), 0, 0 - strlen( $_SERVER['SCRIPT_NAME'] ) ) );
}
}

$t_docroot = rtrim( $_SERVER['DOCUMENT_ROOT'], '/' );
$t_file_path = str_replace( DIRECTORY_SEPARATOR, '/', __FILE__ );

// Extract the unique directory path of this file relative to the server's documunt root
if ( preg_match( '@' . preg_quote( $t_docroot ) . '(.*)@', $t_file_path, $t_matches ) ) {
$t_path = dirname( strip_tags( $t_matches[1] ) );
} else {
$t_path = dirname( strip_tags( $_SERVER['SCRIPT_NAME'] ) );
if ( '/' == $t_path || '\\' == $t_path ) {
$t_path = '';
}
}
$t_path = rtrim($t_path, '/');

$inc_files = get_included_files();

$t_relative_path = str_replace( dirname($t_file_path), '', str_replace( DIRECTORY_SEPARATOR, '/', dirname($inc_files[0])));
$t_path = str_replace( $t_relative_path, '', $t_path );
$t_path = str_replace( basename( $_SERVER['PHP_SELF'] ), '', $_SERVER['PHP_SELF'] );
$t_url = $t_protocol . '://' . $t_host . $t_path;

$t_url = $t_protocol . '://' . $t_host . $t_path.'/';
} else {
$t_path = '';
$t_host = '';
Expand All @@ -170,7 +140,7 @@
* requires trailing /
* @global string $g_path
*/
$g_path = isset( $t_url ) ? $t_url : 'http://www.example.com/mantisbt/';
$g_path = isset( $t_url ) ? $t_url : 'http://localhost/mantisbt/';

/**
* path to your images directory (for icons)
Expand All @@ -184,7 +154,7 @@
* requires trailing /
* @global string $g_short_path
*/
$g_short_path = $t_path . '/';
$g_short_path = $t_path;

/**
* absolute path to your installation. Requires trailing / or \
Expand Down

0 comments on commit 5ac1fdf

Please sign in to comment.