From 0c485f5fd3800af835dd2e5d07f2e7bbd8d9bebb Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Fri, 14 Dec 2007 08:43:25 +0000
Subject: [PATCH] Configuration: added getBaseDir() function.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@105 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Configuration.php | 45 ++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php
index 1f979e491..e8ee79155 100644
--- a/lib/SimpleSAML/Configuration.php
+++ b/lib/SimpleSAML/Configuration.php
@@ -56,6 +56,51 @@ class SimpleSAML_Configuration {
 		return $this->configuration[$name];
 	}
 
+
+	/* Retrieve the base directory for this simpleSAMLphp installation.
+	 * This function first checks the 'basedir' configuration option. If
+	 * this option is undefined or NULL, then we fall back to looking at
+	 * the current filename.
+	 *
+	 * Returns:
+	 *  The absolute path to the base directory for this simpleSAMLphp
+	 *  installation. This path will always end with a slash.
+	 */
+	public function getBaseDir() {
+		/* Check if a directory is configured in the configuration
+		 * file.
+		 */
+		$dir = $this->getValue('basedir');
+		if($dir !== NULL) {
+			/* Add trailing slash if it is missing. */
+			if(substr($dir, -1) !== '/') {
+				$dir .= '/';
+			}
+
+			return $dir;
+		}
+
+		/* The directory wasn't set in the configuration file. Our
+		 * path is <base directory>/lib/SimpleSAML/Configuration.php
+		 */
+
+		$dir = __FILE__;
+		assert('basename($dir) === "Configuration.php"');
+
+		$dir = dirname($dir);
+		assert('basename($dir) === "SimpleSAML"');
+
+		$dir = dirname($dir);
+		assert('basename($dir) === "lib"');
+
+		$dir = dirname($dir);
+
+		/* Add trailing slash. */
+		$dir .= '/';
+
+		return $dir;
+	}
+
 }
 
 ?>
\ No newline at end of file
-- 
GitLab