From 44c20a11cd049b8f08938b855e94d2ca4f8bf2e1 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Mon, 18 Apr 2016 16:05:58 +0200
Subject: [PATCH] Add a method that tries to guess the base URI path.

---
 lib/SimpleSAML/Utils/HTTP.php | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php
index 39ecec069..6292de74f 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/
-- 
GitLab