From 087fcd847fecc57151f7bdc5577d8ee87f8bb9de Mon Sep 17 00:00:00 2001
From: Tom Johnell <tjohnell@gmail.com>
Date: Wed, 10 Jun 2015 11:48:35 -0600
Subject: [PATCH] Moved logic pertaining to finding configdir to central
 location, added ability to set via environment variable

---
 bin/memcacheSync.php                    |  2 +-
 lib/SimpleSAML/Configuration.php        |  4 ++--
 lib/SimpleSAML/Utils/Config.php         | 30 ++++++++++++++++++++++++-
 modules/metarefresh/bin/metarefresh.php |  3 ++-
 modules/statistics/bin/loganalyzer.php  |  3 ++-
 modules/statistics/bin/logcleaner.php   |  3 ++-
 www/_include.php                        |  2 +-
 7 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/bin/memcacheSync.php b/bin/memcacheSync.php
index 0090f9625..13dc7cc5c 100755
--- a/bin/memcacheSync.php
+++ b/bin/memcacheSync.php
@@ -24,7 +24,7 @@ $baseDir = dirname(dirname(__FILE__));
 require_once($baseDir . '/lib/_autoload.php');
 
 /* Initialize the configuration. */
-$configdir = $baseDir . '/config';
+$configdir = SimpleSAML\Utils\Config::getConfigDir();
 SimpleSAML_Configuration::setConfigDir($configdir);
 
 /* Things we should warn the user about. */
diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php
index e3425bace..517e6d124 100644
--- a/lib/SimpleSAML/Configuration.php
+++ b/lib/SimpleSAML/Configuration.php
@@ -144,7 +144,7 @@ class SimpleSAML_Configuration {
 			if ($configSet !== 'simplesaml') {
 				throw new Exception('Configuration set \'' . $configSet . '\' not initialized.');
 			} else {
-				self::$configDirs['simplesaml'] = dirname(dirname(dirname(__FILE__))) . '/config';
+				self::$configDirs['simplesaml'] = SimpleSAML\Utils\Config::getConfigDir();
 			}
 		}
 
@@ -171,7 +171,7 @@ class SimpleSAML_Configuration {
 			if ($configSet !== 'simplesaml') {
 				throw new Exception('Configuration set \'' . $configSet . '\' not initialized.');
 			} else {
-				self::$configDirs['simplesaml'] = dirname(dirname(dirname(__FILE__))) . '/config';
+				self::$configDirs['simplesaml'] = SimpleSAML\Utils\Config::getConfigDir();
 			}
 		}
 
diff --git a/lib/SimpleSAML/Utils/Config.php b/lib/SimpleSAML/Utils/Config.php
index e0c3f57fd..afbc2088c 100644
--- a/lib/SimpleSAML/Utils/Config.php
+++ b/lib/SimpleSAML/Utils/Config.php
@@ -55,4 +55,32 @@ class Config
 
         return $secretSalt;
     }
-}
\ No newline at end of file
+
+    /**
+     * Returns the path to the config dir
+     *
+     * If the SIMPLSAMLPHP_CONFIG_DIR environment variable has been set, it takes precedence
+     * over the default $simplesamldir/config directory.
+     *
+     * @return string
+     */
+    public static function getConfigDir()
+    {
+        $configDir    = dirname(dirname(dirname(__DIR__))) . '/config';
+        $configDirEnv = getenv('SIMPLESAMLPHP_CONFIG_DIR');
+        if ($configDirEnv !== false) {
+            if (!is_dir($configDirEnv)) {
+                throw new \InvalidArgumentException(
+                    sprintf(
+                        'Config directory specified by environment variable SIMPLESAMLPHP_CONFIG_DIR is not a ' .
+                        'directory.  Given: "%s"',
+                        $configDirEnv
+                    )
+                );
+            }
+            $configDir = $configDirEnv;
+        }
+
+        return $configDir;
+    }
+}
diff --git a/modules/metarefresh/bin/metarefresh.php b/modules/metarefresh/bin/metarefresh.php
index 2e351f357..b31fff56a 100755
--- a/modules/metarefresh/bin/metarefresh.php
+++ b/modules/metarefresh/bin/metarefresh.php
@@ -23,7 +23,8 @@ if(!SimpleSAML_Module::isModuleEnabled('metarefresh')) {
 }
 
 /* Initialize the configuration. */
-SimpleSAML_Configuration::setConfigDir($baseDir . '/config');
+$configdir = SimpleSAML\Utils\Config::getConfigDir();
+SimpleSAML_Configuration::setConfigDir($configdir);
 
 /* $outputDir contains the directory we will store the generated metadata in. */
 $outputDir = $baseDir . '/metadata-generated';
diff --git a/modules/statistics/bin/loganalyzer.php b/modules/statistics/bin/loganalyzer.php
index 7186890f4..f64fa99cd 100755
--- a/modules/statistics/bin/loganalyzer.php
+++ b/modules/statistics/bin/loganalyzer.php
@@ -9,7 +9,8 @@ $baseDir = dirname(dirname(dirname(dirname(__FILE__))));
 require_once($baseDir . '/lib/_autoload.php');
 
 /* Initialize the configuration. */
-SimpleSAML_Configuration::setConfigDir($baseDir . '/config');
+$configdir = SimpleSAML\Utils\Config::getConfigDir();
+SimpleSAML_Configuration::setConfigDir($configdir);
 
 SimpleSAML\Utils\Time::initTimezone();
 
diff --git a/modules/statistics/bin/logcleaner.php b/modules/statistics/bin/logcleaner.php
index ffa1ecded..4bdad961e 100755
--- a/modules/statistics/bin/logcleaner.php
+++ b/modules/statistics/bin/logcleaner.php
@@ -9,7 +9,8 @@ $baseDir = dirname(dirname(dirname(dirname(__FILE__))));
 require_once($baseDir . '/lib/_autoload.php');
 
 /* Initialize the configuration. */
-SimpleSAML_Configuration::setConfigDir($baseDir . '/config');
+$configdir = SimpleSAML\Utils\Config::getConfigDir();
+SimpleSAML_Configuration::setConfigDir($configdir);
 
 
 
diff --git a/www/_include.php b/www/_include.php
index 37f0c883f..8b0ee6e10 100644
--- a/www/_include.php
+++ b/www/_include.php
@@ -97,7 +97,7 @@ class SimpleSAML_IncPrefixWarn {
 $SIMPLESAML_INCPREFIX = new SimpleSAML_IncPrefixWarn();
 
 
-$configdir = dirname(dirname(__FILE__)) . '/config';
+$configdir = SimpleSAML\Utils\Config::getConfigDir();
 if (!file_exists($configdir . '/config.php')) {
 	header('Content-Type: text/plain');
 	echo("You have not yet created the simpleSAMLphp configuration files.\n");
-- 
GitLab