Update GetBaseURL() to avoid multiple consecutive slashes in absolute return values (incl. test cases)

darcs-hash:20080418101946-f07c6-615691fab5d4b5714134634b38567ca8422a0aa0.gz
This commit is contained in:
Chris Smith 2008-04-18 12:19:46 +02:00
parent bde4e34172
commit 46c73e019a
2 changed files with 76 additions and 11 deletions

View File

@ -32,7 +32,7 @@ class init_getBaseURL_test extends UnitTestCase {
*
* data provided by Hilko Bengen <bengen@hilluzination.de>
*/
function test2(){
function test2(){
global $conf;
$conf['basedir'] = '';
$conf['baseurl'] = '';
@ -40,7 +40,7 @@ class init_getBaseURL_test extends UnitTestCase {
$_SERVER['DOCUMENT_ROOT'] = '/var/www/localhost';
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['SCRIPT_FILENAME'] = '/usr/lib/cgi-bin/php4';
$_SERVER['SCRIPT_FILENAME'] = '/usr/lib/cgi-bin/php4';
$_SERVER['REQUEST_URI'] = '/~bengen/dokuwiki/doku.php?do=debug';
$_SERVER['SCRIPT_NAME'] = '/cgi-bin/php4';
$_SERVER['PATH_INFO'] = '/~bengen/dokuwiki/doku.php';
@ -210,6 +210,71 @@ class init_getBaseURL_test extends UnitTestCase {
$this->assertEqual(getBaseURL(),'/dokuwiki/');
}
/**
* Possible user settings of $conf['baseurl'] & absolute baseURL required
*
* data provided by Andreas Gohr <andi@splitbrain.org>
*/
function test10(){
// values for $conf['baseurl'] and expected results
$tests = array(
'http://www.mysite.com' => 'http://www.mysite.com/dokuwiki/',
'http://www.mysite.com/' => 'http://www.mysite.com/dokuwiki/',
'http://www.mysite.com/path/to/wiki' => 'http://www.mysite.com/path/to/wiki/dokuwiki/',
'http://www.mysite.com/path/to/wiki/' => 'http://www.mysite.com/path/to/wiki/dokuwiki/',
);
global $conf;
$conf['basedir'] = '';
$conf['baseurl'] = '';
$_SERVER['DOCUMENT_ROOT'] = '/var/www/';
$_SERVER['HTTP_HOST'] = 'xerxes.my.home';
$_SERVER['SCRIPT_FILENAME'] = '/var/www/dokuwiki/doku.php';
$_SERVER['REQUEST_URI'] = '/dokuwiki/wiki/syntax?do=debug';
$_SERVER['SCRIPT_NAME'] = '/dokuwiki/doku.php';
$_SERVER['PATH_INFO'] = null;
$_SERVER['PATH_TRANSLATED'] = '/var/www/dokuwiki/doku.php';
$_SERVER['PHP_SELF'] = '/dokuwiki/doku.php';
foreach ($tests as $test => $correct_result) {
$conf['baseurl'] = $test;
$this->assertEqual(getBaseURL(true),$correct_result);
}
}
/**
* Possible user settings of $conf['baseurl'] & absolute baseURL required
*
* data provided by Andreas Gohr <andi@splitbrain.org>
*/
function test11(){
// values for $conf['baseurl'] and expected results
$tests = array(
'http://www.mysite.com' => 'http://www.mysite.com/dokuwiki/',
'http://www.mysite.com/' => 'http://www.mysite.com/dokuwiki/',
'http://www.mysite.com/path/to/wiki' => 'http://www.mysite.com/path/to/wiki/dokuwiki/',
'http://www.mysite.com/path/to/wiki/' => 'http://www.mysite.com/path/to/wiki/dokuwiki/',
);
global $conf;
$conf['basedir'] = '/dokuwiki';
$conf['baseurl'] = '';
$_SERVER['DOCUMENT_ROOT'] = '/var/www/';
$_SERVER['HTTP_HOST'] = 'xerxes.my.home';
$_SERVER['SCRIPT_FILENAME'] = '/var/www/dokuwiki/doku.php';
$_SERVER['REQUEST_URI'] = '/dokuwiki/wiki/syntax?do=debug';
$_SERVER['SCRIPT_NAME'] = '/dokuwiki/doku.php';
$_SERVER['PATH_INFO'] = null;
$_SERVER['PATH_TRANSLATED'] = '/var/www/dokuwiki/doku.php';
$_SERVER['PHP_SELF'] = '/dokuwiki/doku.php';
foreach ($tests as $test => $correct_result) {
$conf['baseurl'] = $test;
$this->assertEqual(getBaseURL(true),$correct_result);
}
}
}
//Setup VIM: ex: et ts=2 enc=utf-8 :

View File

@ -298,21 +298,21 @@ function getBaseURL($abs=null){
if(is_null($abs)) $abs = $conf['canonical'];
if($conf['basedir']){
$dir = $conf['basedir'].'/';
$dir = $conf['basedir'];
}elseif(substr($_SERVER['SCRIPT_NAME'],-4) == '.php'){
$dir = dirname($_SERVER['SCRIPT_NAME']).'/';
$dir = dirname($_SERVER['SCRIPT_NAME']);
}elseif(substr($_SERVER['PHP_SELF'],-4) == '.php'){
$dir = dirname($_SERVER['PHP_SELF']).'/';
$dir = dirname($_SERVER['PHP_SELF']);
}elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
$dir = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
$_SERVER['SCRIPT_FILENAME']);
$dir = dirname('/'.$dir).'/';
$dir = dirname('/'.$dir);
}else{
$dir = './'; //probably wrong
$dir = '.'; //probably wrong
}
$dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
$dir = preg_replace('#//+#','/',$dir);
$dir = str_replace('\\','/',$dir); // bugfix for weird WIN behaviour
$dir = preg_replace('#//+#','/',"/$dir/"); // ensure leading and trailing slashes
//handle script in lib/exe dir
$dir = preg_replace('!lib/exe/$!','',$dir);
@ -323,8 +323,8 @@ function getBaseURL($abs=null){
//finish here for relative URLs
if(!$abs) return $dir;
//use config option if available
if($conf['baseurl']) return $conf['baseurl'].$dir;
//use config option if available, trim any slash from end of baseurl to avoid multiple consecutive slashes in the path
if($conf['baseurl']) return rtrim($conf['baseurl'],'/').$dir;
//split hostheader into host and port
list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);