Add db secrets management
All checks were successful
Website Sync / website-sync (push) Successful in 6s
All checks were successful
Website Sync / website-sync (push) Successful in 6s
This commit is contained in:
1
includes/.gitignore
vendored
Normal file
1
includes/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.env
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
require_once "var_loading.inc.php";
|
||||||
|
loadEnvironment(".env");
|
||||||
|
|
||||||
$host = "${{ secrets.DB_HOST }}";
|
$host = "${{ secrets.DB_HOST }}";
|
||||||
$protocol = "${{ secrets.DB_PROTOCOL }}";
|
$protocol = "${{ secrets.DB_PROTOCOL }}";
|
||||||
$dbname = "${{ secrets.DB_NAME }}";
|
$dbname = "${{ secrets.DB_NAME }}";
|
||||||
|
|||||||
40
includes/var_loading.inc.php
Normal file
40
includes/var_loading.inc.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
function loadEnvironment($path) {
|
||||||
|
if (!file_exists($path)) {
|
||||||
|
throw new Exception(".env file not found at: $path");
|
||||||
|
}
|
||||||
|
|
||||||
|
# File method, parses each line as an array element
|
||||||
|
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
|
|
||||||
|
# Loops each line
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
# First removes white spaces, then searches if the first character is a comment, skips iteration line.
|
||||||
|
// Skip comments
|
||||||
|
if (strpos(trim($line), '#') === 0) continue;
|
||||||
|
|
||||||
|
# Continues if it finds an = sign somewhere
|
||||||
|
// Parse KEY=value
|
||||||
|
if (strpos($line, '=') !== false) {
|
||||||
|
# Creates a list, array alike, the items are set by splitting (explode()) each line by the equal sign.
|
||||||
|
list($key, $value) = explode('=', $line, 2);
|
||||||
|
|
||||||
|
# Removes whitespaces and quotes
|
||||||
|
$key = trim($key);
|
||||||
|
$value = trim($value, '"\''); // Remove quotes
|
||||||
|
|
||||||
|
# Stores each value in $_ENV supergglobal
|
||||||
|
$_ENV[$key] = $value;
|
||||||
|
|
||||||
|
# Not sure what this does
|
||||||
|
putenv("$key=$value");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load at application bootstrap
|
||||||
|
# Not sure either
|
||||||
|
loadEnvironment(__DIR__ . '/.env');
|
||||||
|
|
||||||
|
// First found on ttps://levelup.gitconnected.com/how-to-securely-handle-production-credentials-in-php-without-exposing-them-in-git-237f4fd5cf91,
|
||||||
|
// Own comments using #
|
||||||
Reference in New Issue
Block a user