Created
September 1, 2015 16:13
-
-
Save ukautz/0b430aafc7cc996fc946 to your computer and use it in GitHub Desktop.
Decrypt previously encrypted environment variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$secEnv = []; | |
function bootstrapSecEnv($key) { | |
global $secEnv; | |
foreach ($_SERVER as $name => $value) { | |
if (is_string($value) && strpos($value, 'ENC:') === 0) { | |
$secEnv[$name] = decryptEnv($key, substr($value, 4)); | |
} | |
} | |
} | |
function secEnv($name, $fallback = '') { | |
global $secEnv; | |
if (isset($secEnv[$name])) { | |
return $secEnv[$name]; | |
} elseif (isset($_SERVER[$name])) { | |
return $_SERVER[$name]; | |
} else { | |
return $fallback; | |
} | |
} | |
function decryptEnv($key, $encoded) { | |
list($iv, $value) = unserialize(base64_decode($encoded)); | |
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $value, MCRYPT_MODE_CBC, $iv); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment