Skip to content

Instantly share code, notes, and snippets.

@ukautz
Created September 1, 2015 16:13
Show Gist options
  • Save ukautz/0b430aafc7cc996fc946 to your computer and use it in GitHub Desktop.
Save ukautz/0b430aafc7cc996fc946 to your computer and use it in GitHub Desktop.
Decrypt previously encrypted environment variables
<?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