From 80d860a0463acb77faf6edc082e5d1ddb2379c0c Mon Sep 17 00:00:00 2001 From: Olav Morken <olav.morken@uninett.no> Date: Mon, 9 Jun 2008 08:04:38 +0000 Subject: [PATCH] Utilities: add resolvePath function. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@617 44740490-163a-0410-bde0-09ae8108e29a --- lib/SimpleSAML/Utilities.php | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index cec167f7c..17a344591 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -932,6 +932,48 @@ class SimpleSAML_Utilities { return $ret; } + + /** + * Resolve a (possibly) relative path from the given base path. + * + * A path which starts with a '/' is assumed to be absolute, all others are assumed to be + * relative. The default base path is the root of the simpleSAMPphp installation. + * + * @param $path The path we should resolve. + * @param $base The base path, where we should search for $path from. Default value is the root + * of the simpleSAMLphp installation. + * @return An absolute path referring to $path. + */ + public static function resolvePath($path, $base = NULL) { + if($base === NULL) { + $config = SimpleSAML_Configuration::getInstance(); + $base = $config->getBaseDir(); + } + + /* Remove trailing slashes from $base. */ + while(substr($base, -1) === '/') { + $base = substr($base, 0, -1); + } + + $ret = $base; + + $path = explode('/', $path); + foreach($path as $d) { + if($d === '.') { + continue; + } elseif($d === '..') { + $ret = dirname($ret); + } else { + if(substr($ret, -1) !== '/') { + $ret .= '/'; + } + $ret .= $d; + } + } + + return $ret; + } + } ?> \ No newline at end of file -- GitLab