From d3aab2646ef24eeedea9a79dfb8de0fee3069c65 Mon Sep 17 00:00:00 2001 From: RainLoop Team Date: Sun, 22 May 2016 02:01:33 +0300 Subject: [PATCH] Add support of environment variables in the application.ini (#983) --- .../libraries/RainLoop/Config/Application.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php b/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php index bcbc9c6ca..619e3aaca 100644 --- a/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php +++ b/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php @@ -4,6 +4,8 @@ namespace RainLoop\Config; class Application extends \RainLoop\Config\AbstractConfig { + private $aReplaceEnv = null; + /** * @return void */ @@ -15,6 +17,70 @@ class Application extends \RainLoop\Config\AbstractConfig defined('APP_ADDITIONAL_CONFIGURATION_NAME') ? APP_ADDITIONAL_CONFIGURATION_NAME : ''); } + /** + * @param string $sSection + * @param string $sName + * @param mixed $mDefault = null + * @param bool $bUseEnvReplace = true + * + * @return mixed + */ + public function Load() + { + parent::Load(); + + $this->aReplaceEnv = null; + if ((isset($_ENV) && \is_array($_ENV) && 0 < \count($_ENV)) || + (isset($_SERVER) && \is_array($_SERVER) && 0 < \count($_SERVER))) + { + $sEnvNames = $this->Get('labs', 'replace_env_in_configuration', ''); + if (0 < \strlen($sEnvNames)) + { + $this->aReplaceEnv = \explode(',', $sEnvNames); + if (\is_array($this->aReplaceEnv)) + { + $this->aReplaceEnv = \array_map('trim', $this->aReplaceEnv); + $this->aReplaceEnv = \array_map('strtolower', $this->aReplaceEnv); + } + } + } + + if (!\is_array($this->aReplaceEnv) || 0 === \count($this->aReplaceEnv)) + { + $this->aReplaceEnv = null; + } + } + + /** + * @param string $sSection + * @param string $sName + * @param mixed $mDefault = null + * + * @return mixed + */ + public function Get($sSection, $sName, $mDefault = null) + { + $mResult = parent::Get($sSection, $sName, $mDefault); + if ($this->aReplaceEnv && \is_string($mResult)) + { + $sKey = \strtolower($sSection.'.'.$sName); + if (\in_array($sKey, $this->aReplaceEnv) && false !== strpos($mResult, '$')) + { + foreach ($_ENV as $sKey => $sValue) + { + $mResult = \str_replace('$'.$sKey, $sValue, $mResult); + } + + foreach ($_SERVER as $sKey => $sValue) + { + $mResult = \str_replace('$'.$sKey, $sValue, $mResult); + } + } + } + + return $mResult; + } + /** * @param string $sPassword * @@ -358,6 +424,7 @@ Enables caching in the system'), 'detect_image_exif_orientation' => array(true), 'cookie_default_path' => array(''), 'cookie_default_secure' => array(false), + 'replace_env_in_configuration' => array(''), 'startup_url' => array(''), 'emogrifier' => array(true), 'dev_email' => array(''),