diff --git a/src/SemanticScuttle/Environment.php b/src/SemanticScuttle/Environment.php index 7ccb466..df5a81c 100644 --- a/src/SemanticScuttle/Environment.php +++ b/src/SemanticScuttle/Environment.php @@ -44,5 +44,41 @@ class SemanticScuttle_Environment //fallback when no special path after the php file is given return ''; } + + + /** + * Determines the root directory from the server environment. + * The root directory is the path that needs to be prepended + * to relative links. + * + * Returns $GLOBALS['root'] if set. + * + * @return string Base URL with trailing slash + */ + public static function getRoot() + { + if (isset($GLOBALS['root'])) { + return $GLOBALS['root']; + } + + $pieces = explode('/', $_SERVER['SCRIPT_NAME']); + $rootTmp = '/'; + foreach ($pieces as $piece) { + //we eliminate possible sscuttle subfolders (like gsearch for example) + if ($piece != '' && !strstr($piece, '.php') + && $piece != 'gsearch' && $piece != 'ajax' + ) { + $rootTmp .= $piece .'/'; + } + } + if (($rootTmp != '/') && (substr($rootTmp, -1, 1) != '/')) { + $rootTmp .= '/'; + } + + //we do not prepend http since we also want to support https connections + // "http" is not required; it's automatically determined by the browser + // depending on the current connection. + return '//'. $_SERVER['HTTP_HOST'] . $rootTmp; + } } ?> \ No newline at end of file diff --git a/src/SemanticScuttle/constants.php b/src/SemanticScuttle/constants.php index 306c32a..5d8ff2f 100644 --- a/src/SemanticScuttle/constants.php +++ b/src/SemanticScuttle/constants.php @@ -25,29 +25,7 @@ if (isset($GLOBALS['debugMode'])) { } // Determine the base URL as ROOT -if (!isset($GLOBALS['root'])) { - $pieces = explode('/', $_SERVER['SCRIPT_NAME']); - - $rootTmp = '/'; - foreach ($pieces as $piece) { - //we eliminate possible sscuttle subfolders (like gsearch for example) - if ($piece != '' && !strstr($piece, '.php') - && $piece != 'gsearch' && $piece != 'ajax' - ) { - $rootTmp .= $piece .'/'; - } - } - if (($rootTmp != '/') && (substr($rootTmp, -1, 1) != '/')) { - $rootTmp .= '/'; - } - - //we do not prepend http since we also want to support https connections - // "http" is not required; it's automatically determined by the browser - // depending on the current connection. - define('ROOT', '//'. $_SERVER['HTTP_HOST'] . $rootTmp); -} else { - define('ROOT', $GLOBALS['root']); -} +define('ROOT', SemanticScuttle_Environment::getRoot()); define('ROOT_JS', ROOT . 'js/jstree-1.0-rc2/'); // Error codes diff --git a/tests/SemanticScuttle/EnvironmentTest.php b/tests/SemanticScuttle/EnvironmentTest.php index 3baa9ed..762d350 100644 --- a/tests/SemanticScuttle/EnvironmentTest.php +++ b/tests/SemanticScuttle/EnvironmentTest.php @@ -229,6 +229,40 @@ class SemanticScuttle_EnvironmentTest extends PHPUnit_Framework_TestCase '/dummy/foo', SemanticScuttle_Environment::getServerPathInfo() ); } + + public function testGetRootInRootDir() + { + $_SERVER = array( + 'HTTP_HOST' => 'bm.bogo', + 'DOCUMENT_ROOT' => '/etc/apache2/htdocs', + 'SCRIPT_FILENAME' => '/home/cweiske/Dev/html/hosts/bm.bogo/index.php', + 'QUERY_STRING' => '', + 'REQUEST_URI' => '/', + 'SCRIPT_NAME' => '/index.php', + 'PHP_SELF' => '/index.php', + ); + $this->assertEquals( + '//bm.bogo/', + SemanticScuttle_Environment::getRoot() + ); + } + + public function testGetRootInSubdir() + { + $_SERVER = array( + 'HTTP_HOST' => 'bm-subdir.bogo', + 'DOCUMENT_ROOT' => '/etc/apache2/htdocs', + 'SCRIPT_FILENAME' => '/home/cweiske/Dev/html/hosts/bm-subdir.bogo/tools/scuttle/index.php', + 'QUERY_STRING' => '', + 'REQUEST_URI' => '/tools/scuttle/', + 'SCRIPT_NAME' => '/tools/scuttle/index.php', + 'PHP_SELF' => '/tools/scuttle/index.php', + ); + $this->assertEquals( + '//bm-subdir.bogo/tools/scuttle/', + SemanticScuttle_Environment::getRoot() + ); + } } ?> \ No newline at end of file