From f8277e8045525a9e9416d5cfbf639da7421c125e Mon Sep 17 00:00:00 2001
From: Marien den Besten <marien@sitsat.nl>
Date: Mon, 27 Feb 2017 10:43:04 +0100
Subject: [PATCH] Apache CGI support

Option to support Apache CGI in combination with custom config directory.
---
 lib/SimpleSAML/Utils/Config.php           |  5 +++++
 tests/lib/SimpleSAML/Utils/ConfigTest.php | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/lib/SimpleSAML/Utils/Config.php b/lib/SimpleSAML/Utils/Config.php
index fd7762b6a..bbdf0e301 100644
--- a/lib/SimpleSAML/Utils/Config.php
+++ b/lib/SimpleSAML/Utils/Config.php
@@ -68,6 +68,11 @@ class Config
     {
         $configDir    = dirname(dirname(dirname(__DIR__))) . '/config';
         $configDirEnv = getenv('SIMPLESAMLPHP_CONFIG_DIR');
+        
+        if($configDirEnv === false) {
+            $configDirEnv = getenv('REDIRECT_SIMPLESAMLPHP_CONFIG_DIR');
+        }
+        
         if ($configDirEnv !== false) {
             if (!is_dir($configDirEnv)) {
                 throw new \InvalidArgumentException(
diff --git a/tests/lib/SimpleSAML/Utils/ConfigTest.php b/tests/lib/SimpleSAML/Utils/ConfigTest.php
index 5dd0fa33b..3fa74c952 100644
--- a/tests/lib/SimpleSAML/Utils/ConfigTest.php
+++ b/tests/lib/SimpleSAML/Utils/ConfigTest.php
@@ -34,6 +34,29 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($configDir, __DIR__);
     }
 
+    /**
+     * Test valid dir specified by env redirect var overrides default config dir
+     */
+    public function testEnvRedirectVariableConfigDir()
+    {
+        putenv('REDIRECT_SIMPLESAMLPHP_CONFIG_DIR=' . __DIR__);
+        $configDir = Config::getConfigDir();
+
+        $this->assertEquals($configDir, __DIR__);
+    }
+
+    /**
+     * Test which directory takes precedence
+     */
+    public function testEnvRedirectPriorityVariableConfigDir()
+    {
+        putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__));
+        putenv('REDIRECT_SIMPLESAMLPHP_CONFIG_DIR=' . __DIR__);
+        $configDir = Config::getConfigDir();
+
+        $this->assertEquals($configDir, dirname(__DIR__));
+    }
+
 
     /**
      * Test invalid dir specified by env var results in a thrown exception
-- 
GitLab