diff options
-rw-r--r-- | _test/cases/inc/init_getbaseurl.test.php | 69 | ||||
-rw-r--r-- | inc/init.php | 18 |
2 files changed, 76 insertions, 11 deletions
diff --git a/_test/cases/inc/init_getbaseurl.test.php b/_test/cases/inc/init_getbaseurl.test.php index 5dec4a954..edd370d61 100644 --- a/_test/cases/inc/init_getbaseurl.test.php +++ b/_test/cases/inc/init_getbaseurl.test.php @@ -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 : diff --git a/inc/init.php b/inc/init.php index 9563af296..ee82bbe0e 100644 --- a/inc/init.php +++ b/inc/init.php @@ -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']); |