diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index 39ecec0692c35667f0c8c01104b323f34a30879a..6292de74f4480f2a8739a6fee3b1f4803c9b459c 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -501,6 +501,36 @@ class HTTP } + /** + * Try to guess the base SimpleSAMLphp path from the current request. + * + * This method offers just a guess, so don't rely on it. + * + * @return string The guessed base path that should correspond to the root installation of SimpleSAMLphp. + */ + public static function guessBasePath() + { + // get the name of the current script + $path = explode('/', $_SERVER['SCRIPT_FILENAME']); + $script = array_pop($path); + + // get the portion of the URI up to the script, i.e.: /simplesaml/some/directory/script.php + preg_match('#^/(?:[^/]+/)*'.$script.'#', $_SERVER['REQUEST_URI'], $matches); + + $uri_s = explode('/', $matches[0]); + $file_s = explode('/', $_SERVER['SCRIPT_FILENAME']); + + // compare both arrays from the end, popping elements matching out of them + while ($uri_s[count($uri_s) - 1] === $file_s[count($file_s) - 1]) { + array_pop($uri_s); + array_pop($file_s); + } + + // we are now left with the minimum part of the URI that does not match anything in the file system, use it + return join('/', $uri_s).'/'; + } + + /** * Retrieve the base URL of the SimpleSAMLphp installation. The URL will always end with a '/'. For example: * https://idp.example.org/simplesaml/