From ecc67abd8b4c1c2043c516ba2a6d57ab3271432f Mon Sep 17 00:00:00 2001 From: Olav Morken <olav.morken@uninett.no> Date: Tue, 4 Mar 2008 13:16:50 +0000 Subject: [PATCH] Configuration: Extract the path resolution code from getPathValue into a resolvePath-function, for use elsewhere. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@345 44740490-163a-0410-bde0-09ae8108e29a --- lib/SimpleSAML/Configuration.php | 48 ++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php index 374bb76a8..a05325549 100644 --- a/lib/SimpleSAML/Configuration.php +++ b/lib/SimpleSAML/Configuration.php @@ -70,6 +70,38 @@ class SimpleSAML_Configuration { } + /** + * This function resolves a path which may be relative to the + * simpleSAMLphp base directory. + * + * The path will never end with a '/'. + * + * @param $path The path we should resolve. This option may be NULL. + * @return $path if $path is an absolute path, or $path prepended with + * the base directory of this simpleSAMLphp installation. We + * will return NULL if $path is NULL. + */ + public function resolvePath($path) { + if($path === NULL) { + return NULL; + } + + assert('is_string($path)'); + + /* Prepend path with basedir if it doesn't start with + * a slash. We assume getBaseDir ends with a slash. + */ + if ($path[0] !== '/') $path = $this->getBaseDir() . $path; + + /* Remove trailing slashes. */ + while (substr($path, -1) === '/') { + $path = substr($path, 0, -1); + } + + return $path; + } + + /* Retrieve a path configuration option set in config.php. * The function will always return an absolute path unless the * option is not set. It will then return the default value. @@ -93,20 +125,12 @@ class SimpleSAML_Configuration { /* Return the default value if the option is unset. */ if (!array_key_exists($name, $this->configuration)) { - return $default; + $path = $default; + } else { + $path = $this->configuration[$name]; } - $path = $this->configuration[$name]; - - /* Prepend path with basedir if it doesn't start with - * a slash. We assume getBaseDir ends with a slash. - */ - if ($path[0] !== '/') $path = $this->getBaseDir() . $path; - - /* Add trailing slash if it is missing to be consistent with getBaseDir */ - if (substr($path, -1) !== '/') $path .= '/'; - - return $path; + return $this->resolvePath($path) . '/'; } -- GitLab