diff --git a/.env.develop b/.env.develop index 968fd4b..f6a431d 100644 --- a/.env.develop +++ b/.env.develop @@ -3,21 +3,23 @@ # composer: Set cache directory COMPOSER_CACHE_DIR=./.composer -# MariaDB container +# Environment variables for MariaDB container MYSQL_RANDOM_ROOT_PASSWORD=yes MYSQL_DATABASE=mailusers MYSQL_USER=mailaccountadmin MYSQL_PASSWORD=mailaccountadmin # App settings +APP_TITLE="MailAccountAdmin [dev]" +APP_ENV=development APP_DEBUG=true +APP_TIMEZONE=Europe/Berlin -# - Disable Twig cache +# Disable Twig cache TWIG_CACHE_DIR= -TWIG_STRICT=true -# - Database credentials +# Database credentials DB_HOST=db DB_DATABASE=mailusers -DB_USER=mailaccountadmin +DB_USERNAME=mailaccountadmin DB_PASSWORD=mailaccountadmin diff --git a/.gitignore b/.gitignore index 646c542..37cecc0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,9 @@ # PHP /.composer /.phpunit.cache +/.twig.cache /coverage /vendor + +# Production settings +/config/app.yml diff --git a/Dockerfile b/Dockerfile index 695e69b..89effb8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,10 @@ FROM php:7.4-apache AS base WORKDIR /var/www RUN apt-get update && \ - apt-get install -y libzip-dev unzip git && \ - docker-php-ext-install pdo pdo_mysql zip + apt-get install -y git unzip libyaml-dev libzip-dev && \ + docker-php-ext-install pdo pdo_mysql zip && \ + pecl install yaml && \ + docker-php-ext-enable yaml RUN a2enmod rewrite && \ sed -ri -e 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/*.conf @@ -18,8 +20,6 @@ RUN cp $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini && \ pecl install xdebug && \ docker-php-ext-enable xdebug -ENV APP_ENV=development - # TODO: production image untested #FROM base AS production # diff --git a/TODO.md b/TODO.md index 2226cf1..4697f74 100644 --- a/TODO.md +++ b/TODO.md @@ -4,7 +4,6 @@ ## General -- Settings from a config file - Database migrations - Documentation - App deployment @@ -12,6 +11,7 @@ - Refactor auth and session handling ## Admin user management + - Create/edit/delete admins - Change own password diff --git a/composer.json b/composer.json index 93dcd53..aa59362 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "require": { "php": "^7.4", "ext-pdo": "*", + "ext-yaml": "*", "slim/slim": "^4.8", "slim/psr7": "^1.3", "php-di/php-di": "^6.3", diff --git a/config/app.develop.yml b/config/app.develop.yml new file mode 100644 index 0000000..923ceb4 --- /dev/null +++ b/config/app.develop.yml @@ -0,0 +1,19 @@ +# Config file for local development (same settings as .env.develop) + +# -- App settings +appTitle: "MailAccountAdmin [dev]" +environment: development +debug: true +timezone: Europe/Berlin + +# -- Twig settings +twig: + cacheDir: + +# -- Database settings +database: + host: db + port: 3306 + name: mailusers + username: mailaccountadmin + password: mailaccountadmin diff --git a/config/app.example.yml b/config/app.example.yml new file mode 100644 index 0000000..2683c29 --- /dev/null +++ b/config/app.example.yml @@ -0,0 +1,23 @@ +# This is an example config file for MailAccountAdmin. +# Copy this file to /config/app.yml and change as needed. +# Settings that are commented out represent the default values. + +# -- App settings +# appTitle: MailAccountAdmin +# environment: production +# debug: false +# timezone: UTC +# dateTimeFormat: r + +# -- Twig settings +twig: + # Cache directory for Twig templates (default: unset, which means no cache is used) + cacheDir: .twig.cache + +# -- Database settings +database: + host: localhost + port: 3306 + name: mailusers + username: mailaccountadmin + password: very_secret_password diff --git a/public/index.php b/public/index.php index 37641db..c918330 100644 --- a/public/index.php +++ b/public/index.php @@ -3,19 +3,25 @@ declare(strict_types=1); require_once __DIR__ . '/../vendor/autoload.php'; +use MailAccountAdmin\Config\Loaders\AutoConfigLoader; use MailAccountAdmin\Dependencies; use MailAccountAdmin\Middlewares; use MailAccountAdmin\Routes; -use MailAccountAdmin\Settings; use Slim\Factory\AppFactory; +const ROOT_DIR = __DIR__ . '/..'; + session_start(); -$settings = new Settings(); -$container = Dependencies::createContainer($settings); +// Load application config (from config file or environment variables) +$configLoader = new AutoConfigLoader(); +$config = $configLoader->loadConfig(); + +// Create application +$container = Dependencies::createContainer($config); $app = AppFactory::createFromContainer($container); -Middlewares::setMiddlewares($app, $settings); +Middlewares::setMiddlewares($app, $config); Routes::setRoutes($app); $app->run(); diff --git a/src/Config/AppConfig.php b/src/Config/AppConfig.php new file mode 100644 index 0000000..ce94e39 --- /dev/null +++ b/src/Config/AppConfig.php @@ -0,0 +1,105 @@ +timezone = ini_get('date.timezone') ?: 'UTC'; + } + + public static function createFromArray(array $configArray): self + { + $config = new self(); + + foreach ($configArray as $key => $value) { + assert(property_exists($config, $key)); + + if ($value !== null) { + $config->$key = $value; + } + } + + return $config; + } + + public function getAppTitle(): string + { + return $this->appTitle; + } + + public function getAppEnvironment(): string + { + return $this->environment; + } + + public function isDebugMode(): bool + { + return $this->debug; + } + + public function getTimezone(): string + { + return $this->timezone; + } + + public function getDateTimeFormat(): string + { + // Specify datetime format (https://www.php.net/manual/en/datetime.format.php) + // Default value "r": RFC 2822 formatted date (Thu, 21 Dec 2000 16:01:07 +0200) + return $this->dateTimeFormat; + } + + public function getTwigCacheDir(): ?string + { + if (empty($this->twigCacheDir)) { + return null; + } elseif (substr($this->twigCacheDir, 0, 1) === '/') { + // Absolute path + return $this->twigCacheDir; + } else { + // Relative path + return ROOT_DIR . '/' . $this->twigCacheDir; + } + } + + public function getTwigSettings(): array + { + return [ + 'cache' => $this->getTwigCacheDir() ?: false, + 'debug' => $this->isDebugMode(), + 'strict_variables' => $this->isDebugMode(), + ]; + } + + public function getDatabaseSettings(): array + { + return [ + 'host' => $this->databaseHost, + 'port' => $this->databasePort, + 'dbname' => $this->databaseName, + 'username' => $this->databaseUsername, + 'password' => $this->databasePassword, + ]; + } +} diff --git a/src/Config/Loaders/AutoConfigLoader.php b/src/Config/Loaders/AutoConfigLoader.php new file mode 100644 index 0000000..6ba336c --- /dev/null +++ b/src/Config/Loaders/AutoConfigLoader.php @@ -0,0 +1,28 @@ +configLoader = new YamlConfigLoader($yamlFilePath); + } else { + $this->configLoader = new EnvConfigLoader(); + } + } + + public function loadConfig(): AppConfig + { + return $this->configLoader->loadConfig(); + } +} diff --git a/src/Config/Loaders/ConfigLoaderInterface.php b/src/Config/Loaders/ConfigLoaderInterface.php new file mode 100644 index 0000000..ed8ebe6 --- /dev/null +++ b/src/Config/Loaders/ConfigLoaderInterface.php @@ -0,0 +1,11 @@ + getenv('APP_TITLE') ?: null, + 'environment' => getenv('APP_ENV') ?: null, + 'debug' => getenv('APP_DEBUG') === 'true' ? true : null, + 'timezone' => getenv('APP_TIMEZONE') ?: null, + 'dateTimeFormat' => getenv('APP_DATE_TIME_FORMAT') ?: null, + + // Twig settings + 'twigCacheDir' => getenv('TWIG_CACHE_DIR') ?: null, + + // Database settings + 'databaseHost' => getenv('DB_HOST') ?: null, + 'databasePort' => (int)getenv('DB_PORT') ?: null, + 'databaseName' => getenv('DB_DATABASE') ?: null, + 'databaseUsername' => getenv('DB_USERNAME') ?: null, + 'databasePassword' => getenv('DB_PASSWORD') ?: null, + ]); + } +} diff --git a/src/Config/Loaders/YamlConfigLoader.php b/src/Config/Loaders/YamlConfigLoader.php new file mode 100644 index 0000000..c4370d3 --- /dev/null +++ b/src/Config/Loaders/YamlConfigLoader.php @@ -0,0 +1,48 @@ +filePath = $filePath; + } + + public function loadConfig(): AppConfig + { + // Parse yml config file + $parsedConfig = yaml_parse_file($this->filePath); + + // Check datatypes + assert(is_array($parsedConfig)); + assert(!isset($parsedConfig['twig']) || is_array($parsedConfig['twig'])); + assert(!isset($parsedConfig['database']) || is_array($parsedConfig['database'])); + + return AppConfig::createFromArray([ + // App settings + 'appTitle' => $parsedConfig['appTitle'] ?? null, + 'environment' => $parsedConfig['environment'] ?? null, + 'debug' => (bool)$parsedConfig['debug'] ?? null, + 'timezone' => $parsedConfig['timezone'] ?? null, + 'dateTimeFormat' => $parsedConfig['dateTimeFormat'] ?? null, + + // Twig settings + 'twigCacheDir' => $parsedConfig['twig']['cacheDir'] ?? null, + + // Database settings + 'databaseHost' => $parsedConfig['database']['host'] ?? null, + 'databasePort' => (int)$parsedConfig['database']['port'] ?? null, + 'databaseName' => $parsedConfig['database']['name'] ?? null, + 'databaseUsername' => $parsedConfig['database']['username'] ?? null, + 'databasePassword' => $parsedConfig['database']['password'] ?? null, + ]); + } +} diff --git a/src/Dependencies.php b/src/Dependencies.php index 94c0d64..30dd467 100644 --- a/src/Dependencies.php +++ b/src/Dependencies.php @@ -7,6 +7,7 @@ use DI\Container; use MailAccountAdmin\Common\PasswordHelper; use MailAccountAdmin\Common\SessionHelper; use MailAccountAdmin\Common\UserHelper; +use MailAccountAdmin\Config\AppConfig; use MailAccountAdmin\Frontend\Accounts\AccountController; use MailAccountAdmin\Frontend\Accounts\AccountHandler; use MailAccountAdmin\Frontend\Domains\DomainController; @@ -23,43 +24,43 @@ use Twig\Extension\CoreExtension as TwigCoreExtension; class Dependencies { - public const SETTINGS = 'settings'; + public const CONFIG = 'config'; public const TWIG = 'view'; private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates'; public const DATABASE = 'database'; - public static function createContainer(Settings $settings): Container + public static function createContainer(AppConfig $config): Container { $container = new Container(); - // App settings - $container->set(self::SETTINGS, $settings); + // App configuration + $container->set(self::CONFIG, $config); // App information $container->set(AppInfo::class, function (ContainerInterface $c) { - /** @var Settings $settings */ - $settings = $c->get(self::SETTINGS); - $versionHelper = new VersionHelper(); + /** @var AppConfig $config */ + $config = $c->get(self::CONFIG); + $versionHelper = new VersionHelper($config); return new AppInfo( - $settings->getAppTitle(), + $config->getAppTitle(), $versionHelper->getAppVersion(), ); }); // Twig template engine $container->set(self::TWIG, function (ContainerInterface $c) { - /** @var Settings $settings */ - $settings = $c->get(self::SETTINGS); + /** @var AppConfig $config */ + $config = $c->get(self::CONFIG); // Create Twig view - $twig = Twig::create(self::TWIG_TEMPLATE_DIR, $settings->getTwigSettings()); + $twig = Twig::create(self::TWIG_TEMPLATE_DIR, $config->getTwigSettings()); // Set default date format /** @var TwigCoreExtension $coreExtension */ $coreExtension = $twig->getEnvironment()->getExtension(TwigCoreExtension::class); - $coreExtension->setDateFormat($settings->getDateFormat()); - $coreExtension->setTimezone($settings->getTimezone()); + $coreExtension->setDateFormat($config->getDateTimeFormat()); + $coreExtension->setTimezone($config->getTimezone()); // Add app information to globals $appInfo = $c->get(AppInfo::class); @@ -71,9 +72,9 @@ class Dependencies // Database connection $container->set(self::DATABASE, function (ContainerInterface $c) { - /** @var Settings $settings */ - $settings = $c->get(self::SETTINGS); - $dbSettings = $settings->getDatabaseSettings(); + /** @var AppConfig $config */ + $config = $c->get(self::CONFIG); + $dbSettings = $config->getDatabaseSettings(); return new PDO( "mysql:dbname={$dbSettings['dbname']};host={$dbSettings['host']};port={$dbSettings['port']}", @@ -108,7 +109,7 @@ class Dependencies }); // Helper classes - $container->set(SessionHelper::class, function (ContainerInterface $c) { + $container->set(SessionHelper::class, function () { return new SessionHelper(); }); @@ -120,7 +121,7 @@ class Dependencies ); }); - $container->set(PasswordHelper::class, function (ContainerInterface $c) { + $container->set(PasswordHelper::class, function () { return new PasswordHelper(); }); diff --git a/src/Middlewares.php b/src/Middlewares.php index 375e481..f60987f 100644 --- a/src/Middlewares.php +++ b/src/Middlewares.php @@ -4,14 +4,15 @@ declare(strict_types=1); namespace MailAccountAdmin; use MailAccountAdmin\Auth\AuthMiddleware; +use MailAccountAdmin\Config\AppConfig; use Slim\App; use Slim\Views\TwigMiddleware; class Middlewares { - public static function setMiddlewares(App $app, Settings $settings): void + public static function setMiddlewares(App $app, AppConfig $config): void { - $displayErrorDetails = $settings->isDebugMode(); + $displayErrorDetails = $config->isDebugMode(); $app->addErrorMiddleware($displayErrorDetails, true, true); $app->add(new AuthMiddleware()); diff --git a/src/Settings.php b/src/Settings.php deleted file mode 100644 index 4d1b989..0000000 --- a/src/Settings.php +++ /dev/null @@ -1,49 +0,0 @@ - getenv('TWIG_CACHE_DIR') ?: false, - 'debug' => $this->isDebugMode(), - 'strict_variables' => getenv('TWIG_STRICT') === 'true', - ]; - } - - public function getDatabaseSettings(): array - { - return [ - 'host' => getenv('DB_HOST') ?: 'localhost', - 'port' => getenv('DB_PORT') ?: 3306, - 'dbname' => getenv('DB_DATABASE') ?: '', - 'username' => getenv('DB_USER') ?: '', - 'password' => getenv('DB_PASSWORD') ?: '', - ]; - } -} diff --git a/src/VersionHelper.php b/src/VersionHelper.php index 4fd370a..1223bce 100644 --- a/src/VersionHelper.php +++ b/src/VersionHelper.php @@ -3,16 +3,18 @@ declare(strict_types=1); namespace MailAccountAdmin; +use MailAccountAdmin\Config\AppConfig; + class VersionHelper { private string $version; - public function __construct() + public function __construct(AppConfig $config) { $version = $this->loadFromVersionFile(); if (!empty($version)) { $this->version = $version; - } elseif ($this->inDevelopmentMode()) { + } elseif ($config->getAppEnvironment() === 'development') { $this->version = '[dev version]'; } else { $this->version = '[undefined version]'; @@ -34,9 +36,4 @@ class VersionHelper } return null; } - - private function inDevelopmentMode(): bool - { - return getenv('APP_ENV') === 'development'; - } } diff --git a/templates/base.html.twig b/templates/base.html.twig index c364b6d..bd0d091 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -12,7 +12,11 @@

{{ app_info.getTitle() }}

- {{ app_info.getVersion() }} + {% if logged_in | default() %} + {{ app_info.getVersion() }} + {% else %} + {{ app_info.getVersion() }} + {% endif %}