Skip to content

hegelmax/php-env-secured

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ EnvSecured β€” Encrypted Configuration Manager for PHP

EnvSecured is a lightweight, secure, and self-contained PHP module for storing sensitive configuration values (API keys, database credentials, tokens, secrets) in an encrypted file and provides a clean interface to access them in runtime.


⭐ Key Features

  • πŸ”’ Encrypted config file (config.enc)
  • 🌐 Browser-based UI for editing settings
  • πŸ“€ JSON export (download)
  • πŸ“₯ JSON import (load file into form)
  • πŸ”‘ Automatic key generation (keys/*.key)
  • 🧬 Server-bound encryption (fingerprint-based)
  • 🧩 Zero global functions β€” everything wrapped in PHP classes
  • πŸš€ Drop-in integration into any project
  • βš™οΈ Can be used:
    • with Composer
    • without Composer

πŸ—‚οΈ Project Structure

env_secured/
β”œβ”€β”€ _init.php                    β†’ Bootloader (entry point)
β”œβ”€β”€ libs/
β”‚   β”œβ”€β”€ EnvSecured.php           β†’ Main config manager
β”‚   β”œβ”€β”€ EnvSecuredCrypto.php     β†’ Encryption engine
β”‚   └── html/
β”‚       β”œβ”€β”€ page_form.php        β†’ UI template: config editor
β”‚       β”œβ”€β”€ page_success.php     β†’ UI template: success page
β”‚       └── page_error.php       β†’ UI template: error page
β”œβ”€β”€ configs/                     β†’ Encrypted config files (auto-created)
β”‚   └── config.enc               β†’ Main encrypted config (auto-created)
└── keys/                        β†’ Key files (auto-created)
    β”œβ”€β”€ sodium.key               β†’ Internal crypto key
    └── secret.key               β†’ Master secret key

Both configs/ and keys/ directories are created automatically on first use if they do not exist.


πŸ“¦ Installation

Option A β€” Composer (recommended)

composer require hegelmax/env-secured

Option B β€” No Composer

Download the directory:

env_secured/

and place it anywhere in your project.


πŸš€ Quick Start (Composer version)

require __DIR__ . '/vendor/autoload.php';

use EnvSecured\EnvSecured;

$envRoot = __DIR__ . '/env'; // Directory for configs/ and keys/

$env = new EnvSecured($envRoot);
$env->run();

// Retrieve configuration
$config = EnvSecured::get();          // full array
$dbHost = EnvSecured::get('DB_HOST'); // single value

πŸš€ Quick Start (No Composer)

require __DIR__ . '/env_secured/init.php';

Then read configuration via:

$env = EnvSecured::get();  // array
echo EnvSecured::get('API_URL'); 

πŸ–₯️ First Run β€” Creating Config

When no encrypted config exists, opening your init script in a browser shows the Config Editor UI:

/env_secured/init.php

UI allows:

βœ” Editing KEY=value rows

βœ” Saving encrypted config (config.enc)

βœ” Downloading JSON

βœ” Loading JSON into form

Folders created automatically:

env/
  configs/
    config.enc
  keys/
    sodium.key
    secret.key

πŸ”’ Encryption Model

EnvSecured uses:

  • 256-bit sodium.key
  • 256-bit secret.key
  • machine + project fingerprint
  • XSalsa20-Poly1305 (libsodium)
  • unique nonce per encryption
  • atomic writes to prevent corruption

Conceptually:

fingerprint = HASH( hostname | projectRoot | secret.key )
finalKey    = HASH( fingerprint | sodium.key )
cipher      = base64( nonce | secretbox(plaintext, nonce, finalKey) )

πŸ›‘οΈ Why It's Safe

  • Keys stored outside web root (in env_secured/keys/)
  • Config stored encrypted (env_secured/configs/config.enc)
  • No plaintext config on server
  • No global functions β†’ no name collisions
  • Atomic writes for safe file operations
  • Encryption relies on libsodium (modern & secure)

βš™οΈ Configuration in Code

Once EnvSecured loads the config:

1️⃣ Array access

$config = EnvSecured::get();
echo $config['DB_HOST'];

2️⃣ Single value

echo EnvSecured::get('API_TOKEN');

3️⃣ Global constants

If constant autodefine is enabled:

echo API_TOKEN;

Enable via:

const ENV_SECURED_CONFIG_DEFINE_CONST = true;

πŸ› οΈ Optional Constants

Place them before calling EnvSecured.

const ENV_SECURED_CONFIG_SCHEMA       = 'prod';
const ENV_SECURED_CONFIG_ALLOW_EDIT   = false;
const ENV_SECURED_CONFIG_ALLOW_SESSION = true;
const ENV_SECURED_CONFIG_DEFINE_CONST = true;

const ENV_SECURED_DEFAULTS = [
    ['key' => 'DB_HOST', 'value' => 'localhost'],
    ['key' => 'API_URL', 'value' => 'https://localhost/api'],
];

πŸ”§ Requirements

  • PHP 8.1+
  • ext-sodium enabled
  • Writable directory for:
    • configs/
    • keys/

πŸ’» JSON Import / Export

EnvSecured supports configuration migration via JSON file, that can be useful for:

  • migrations
  • backups
  • moving configs between servers
  • Dev β†’ Prod workflows

Export (Download JSON)

Downloads a readable .json file containing all config values.

Import (Load JSON)

Loads a .json file directly in the browser and fills the config form.

No data is sent to the server until Save (encrypted) is pressed.


πŸ“€ Migrating Between Servers

  1. On old server β†’ open UI β†’ Download JSON
  2. Transfer the downloaded file to the new server
  3. On new server β†’ open UI β†’ Load JSON
  4. Click Save (encrypted)

A new encrypted config is generated automatically for the new environment; secret keys remain private.


πŸ§ͺ Self-Test (Optional)

Temporary snippet:

require_once __DIR__ . '/env_secured/_init.php';

$cipher = (new EnvSecuredCrypto(__DIR__ . '/env_secured'))->encrypt("test");
var_dump($cipher);

Then ensure:

(new EnvSecuredCrypto(__DIR__ . '/env_secured'))->decrypt($cipher) === "test";

πŸ“„ License

MIT License. Free for commercial use.


Β© 2025 Maxim Hegel

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published