From 7fa33bbbdd3a2c824c611a923c6c3254f3a7d9f9 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Mon, 16 Nov 2026 14:52:58 +0100 Subject: [PATCH] Drop app_local.php from production and show the mail transport in polls remind app_local.php takes precedence over config/.env, so a copy made from an older template forced the file transport and reminders never left the server. Release package and Docker image now ship without it; app.php reads the database from DATABASE_URL (SQLite default). app_local.example.php keeps development defaults only. polls remind prints the transport and warns when SMTP_HOST is set but SMTP is not used. Co-Authored-By: Claude Sonnet 5.5 --- .gitea/workflows/release.yml | 4 +-- AGENTS.md | 2 +- Dockerfile | 5 ++-- config/app.php | 6 ++++ config/app_local.example.php | 46 ++++++------------------------ docs/architecture.md | 2 +- docs/configuration.md | 4 +-- docs/development.md | 2 +- docs/operations.md | 4 +-- src/Command/PollsRemindCommand.php | 40 ++++++++++++++++++++++++++ 10 files changed, 65 insertions(+), 50 deletions(-) diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 5f6d6d1..726a33e 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -85,9 +85,9 @@ jobs: index.php .htaccess composer.json composer.lock | tar -x -C build cd "build/$NAME" rm -f bin/cake.bat bin/bash_completion.sh - # --no-scripts: the installer must not bake a random salt into app_local.php (SECURITY_SALT comes from config/.env) + # --no-scripts: no app_local.php in the package; everything comes from config/.env (see app_local.example.php) composer install --no-dev --no-scripts --no-interaction --no-progress --prefer-dist --optimize-autoloader - cp config/app_local.example.php config/app_local.php + rm -f config/app_local.example.php mkdir -p tmp/cache/models tmp/cache/persistent tmp/cache/views tmp/sessions logs data printf '%s\n' "$VERSION" > VERSION cd ../.. diff --git a/AGENTS.md b/AGENTS.md index 5262fef..f4037bb 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -62,7 +62,7 @@ passende Dokument und pflegt es danach** (Details zu Verhalten, Settings, Tests ## Architektur in Kürze ``` -config/ app.php (Block 'Plenario'), app_local.php (gitignored), .env.example, +config/ app.php (Block 'Plenario'), app_local.php (nur lokal, gitignored; Produktion nur .env), .env.example, routes.php (alle Routen benannt, KEINE Fallbacks), Migrations/ (Schema = einzige Quelle der Wahrheit) src/ Controller/ AppController (Basis), Home, Admin (+ AdminAppController), Slides, Participate, Embed, Error diff --git a/Dockerfile b/Dockerfile index d684eec..943f688 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,11 +15,10 @@ FROM base AS build COPY --from=composer:2 /usr/bin/composer /usr/bin/composer WORKDIR /app COPY composer.json composer.lock ./ -# --no-scripts: the installer must not bake a random salt into app_local.php (SECURITY_SALT comes at runtime) +# --no-scripts: no app_local.php in the image; everything comes from the environment at runtime RUN composer install --no-dev --no-scripts --no-progress --prefer-dist --optimize-autoloader COPY . . -RUN composer dump-autoload --no-dev --optimize \ - && cp config/app_local.example.php config/app_local.php +RUN composer dump-autoload --no-dev --optimize FROM base WORKDIR /app diff --git a/config/app.php b/config/app.php index df0194d..538ea53 100644 --- a/config/app.php +++ b/config/app.php @@ -372,6 +372,10 @@ return [ 'default' => [ 'className' => Connection::class, 'driver' => Sqlite::class, + // Default: SQLite file in data/. For MySQL/MariaDB/PostgreSQL set DATABASE_URL, e.g. + // mysql://user:pass@localhost/plenario?encoding=utf8mb4 + 'database' => ROOT . DS . 'data' . DS . 'plenario.sqlite', + 'url' => env('DATABASE_URL', null), 'persistent' => false, 'timezone' => 'UTC', @@ -415,6 +419,8 @@ return [ 'test' => [ 'className' => Connection::class, 'driver' => Sqlite::class, + 'database' => ROOT . DS . 'tmp' . DS . 'tests.sqlite', + 'url' => env('DATABASE_TEST_URL', null), 'persistent' => false, 'timezone' => 'UTC', 'encoding' => 'utf8', diff --git a/config/app_local.example.php b/config/app_local.example.php index 44ae202..5f1b1aa 100644 --- a/config/app_local.example.php +++ b/config/app_local.example.php @@ -1,65 +1,35 @@ filter_var(env('DEBUG', true), FILTER_VALIDATE_BOOLEAN), 'App' => [ - // Absolute base URL used for links in e-mails, e.g. https://plenario.example.org 'fullBaseUrl' => env('APP_FULL_BASE_URL', 'http://localhost:8765'), ], 'Security' => [ + // Replaced by a random value during `composer install` 'salt' => env('SECURITY_SALT', '__SALT__'), ], 'Plenario' => [ - 'captcha' => [ - // 'none' | 'math' | 'recaptcha' - 'type' => env('CAPTCHA_TYPE', 'math'), - 'recaptcha' => [ - 'siteKey' => env('RECAPTCHA_SITE_KEY', ''), - 'secretKey' => env('RECAPTCHA_SECRET_KEY', ''), - ], - ], // Shows the admin link on the confirmation page. Development only! 'showAdminLinkAfterCreate' => filter_var(env('SHOW_ADMIN_LINK_AFTER_CREATE', env('DEBUG', true)), FILTER_VALIDATE_BOOLEAN), ], - 'Datasources' => [ - /* - * Default: SQLite file in data/. For MySQL/MariaDB/PostgreSQL set - * DATABASE_URL, e.g. - * mysql://user:pass@localhost/plenario?encoding=utf8mb4 - */ - 'default' => [ - 'driver' => Sqlite::class, - 'database' => ROOT . DS . 'data' . DS . 'plenario.sqlite', - 'url' => env('DATABASE_URL', null), - ], - 'test' => [ - 'driver' => Sqlite::class, - 'database' => ROOT . DS . 'tmp' . DS . 'tests.sqlite', - 'url' => env('DATABASE_TEST_URL', null), - ], - ], - 'EmailTransport' => [ - /* - * Local default: mails are written as .eml files to tmp/mails/. - * For real delivery set SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD and SMTP_TLS - * (see config/.env.example); then this override is skipped. - */ + // Mails are written as .eml files to tmp/mails/ unless an SMTP server is configured 'default' => env('SMTP_HOST') || env('EMAIL_TRANSPORT_DEFAULT_URL') ? [] : [ 'className' => \App\Mailer\Transport\FileTransport::class, ], diff --git a/docs/architecture.md b/docs/architecture.md index 2c9f723..b4a090f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -5,7 +5,7 @@ Verzeichnisstruktur, Klassen und Tabellen im Detail. ``` config/ app.php Defaults inkl. Block 'Plenario' (Captcha, Throttle, Polling) und Cache 'throttle' - app_local.example.php Vorlage für app_local.php (SQLite, FileTransport) – app_local.php ist gitignored + app_local.example.php Dev-Defaults (debug, FileTransport), von `composer install` nach app_local.php kopiert (gitignored, nur lokal, nie in Produktion) .env.example Alle Umgebungsvariablen (config/.env wird optional geladen) routes.php Alle Routen benannt, KEINE Fallback-Routen Migrations/ Schema (einzige Quelle der Wahrheit, auch für Tests) diff --git a/docs/configuration.md b/docs/configuration.md index 1b93a7c..c6793a9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -11,11 +11,11 @@ Alles andere ist optional und hat einen Standardwert. In der Spalte „Standard | Variable | Pflicht? | Standard | Bedeutung | |---|---|---|---| -| `DEBUG` | Produktion: ja | `false` (per Composer erzeugte `app_local.php`: `true`) | In Produktion auf `false` setzen, sonst werden Admin-Links direkt angezeigt | +| `DEBUG` | Produktion: ja | `false` (lokal: die von Composer erzeugte `app_local.php` setzt `true`) | In Produktion `false` lassen, sonst werden Admin-Links direkt angezeigt | | `APP_FULL_BASE_URL` | Produktion: ja | – (lokal `http://localhost:8765`) | Öffentliche URL inkl. `https://`, wird für Links in E-Mails gebraucht. In Produktion wird außerdem der Host-Header dagegen geprüft | | `SECURITY_SALT` | ja | – (lokal wird er von Composer erzeugt) | Langer Zufallswert. **Nicht nachträglich ändern** – damit werden Admin-Tokens verschlüsselt und Teilnehmer-IDs gehasht | | `DATABASE_URL` | optional | leer = SQLite in `data/` | Sonst z. B. `mysql://user:pw@host/plenario?encoding=utf8mb4` oder `postgres://user:pw@host/plenario` | -| `SMTP_HOST` | Produktion: ja | leer = Mails nur als Datei in `tmp/mails/` | Mailserver. Ohne ihn kommt der Admin-Link nie bei den Nutzenden an | +| `SMTP_HOST` | Produktion: ja | leer = Mails nur als Datei in `tmp/mails/` (per `app_local.php`), sonst PHP `mail()` | Mailserver. Ohne ihn kommt der Admin-Link nie bei den Nutzenden an. **Achtung:** Eine `config/app_local.php` hat Vorrang vor `.env`; ein `FileTransport` darin (ältere Vorlage) überschreibt SMTP, die Mails landen dann nur in `tmp/mails/`. Auf Servern gehört keine `app_local.php` hin, sie ist nur für die lokale Entwicklung. `bin/cake polls remind` zeigt den verwendeten Transport an und warnt davor | | `SMTP_PORT` | optional | `587` | Port 465 nutzt automatisch SSL | | `SMTP_USER`, `SMTP_PASSWORD` | optional | leer | Zugangsdaten, falls der Mailserver sie verlangt | | `SMTP_TLS` | optional | `true` | `true` = STARTTLS, `false` = unverschlüsselt | diff --git a/docs/development.md b/docs/development.md index c8b61f4..4a3caab 100644 --- a/docs/development.md +++ b/docs/development.md @@ -10,7 +10,7 @@ sowie [Composer](https://getcomposer.org/). ```bash git clone https://gitea.gnilebein.de/gnilebein/Plenario.git cd Plenario -composer install # legt config/app_local.php an (SQLite, Mails als Datei) +composer install # legt config/app_local.php an (nur Dev-Defaults: debug, Mails als Datei) bin/cake migrations migrate # erstellt data/plenario.sqlite bin/cake server -p 8765 # http://localhost:8765 ``` diff --git a/docs/operations.md b/docs/operations.md index a274bbe..690e622 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -53,7 +53,7 @@ Beispiel: `docker-compose.yml`. Image und App-Paket entstehen nur bei Releases ( **Releases (SemVer):** `.gitea/workflows/release.yml` läuft nur bei Tags `vMAJOR.MINOR.PATCH[-suffix]` (kein Build bei normalen Pushes). Job `test` (PHPUnit + phpcs) gatet `package` und `image`. `package` baut `plenario-.tar.gz` (git archive des Tags, -`composer install --no-dev --no-scripts`, `app_local.php` aus dem Example, leere `tmp/`/`logs/`/`data/`, Datei `VERSION`; nur per Whitelist: bin, config, plugins, resources, src, templates, webroot, index.php, .htaccess, composer-Dateien; +`composer install --no-dev --no-scripts`, **ohne** `app_local.php` (nur `.env`), leere `tmp/`/`logs/`/`data/`, Datei `VERSION`; nur per Whitelist: bin, config, plugins, resources, src, templates, webroot, index.php, .htaccess, composer-Dateien; kein tests/CI/Docker/README) + `SHA256SUMS` und hängt sie an das Gitea-Release (wird angelegt bzw. aktualisiert, Changelog = Commit-Betreffs seit dem letzten Tag, Suffix = Pre-Release). `image` pusht `:`, `:.`, `:` und `:latest` (die beiden letzten nicht bei Pre-Releases bzw. `v0.`) in die Gitea-Registry (Variablen `DOCKER_REPO`, `IMAGE_NAME`, Secret `PAT`). @@ -63,6 +63,6 @@ Wer das Paket ändert (z. B. weitere Dateien ausschließen), pflegt den Schritt Produktiv auf dem Server in `config/.env`: `DEBUG=false`, `APP_FULL_BASE_URL`, `SECURITY_SALT`, SMTP (`SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASSWORD`, `SMTP_TLS` – einzelne Variablen statt URL; `EMAIL_TRANSPORT_DEFAULT_URL` geht weiter als Altlast). -Ohne `DEBUG=false` steht die per Composer erzeugte `app_local.php` auf debug=true und zeigt Admin-Links direkt an. +Produktion kennt keine `app_local.php`: Paket und Docker-Image liefern sie nicht mit, `app.php` liest alles aus der Umgebung (Standard `DEBUG=false`). Eine `app_local.php` hätte Vorrang vor `.env` und überschreibt still einzelne Werte (z. B. den Mail-Transport); auf bestehenden Servern aus der Zeit vor diesem Stand löschen. Document-Root zeigt idealerweise auf `webroot/`; liegt die App direkt im Document-Root (z. B. `public_html`), leiten `.htaccess` + `index.php` im Projektroot alles nach `webroot/` weiter. diff --git a/src/Command/PollsRemindCommand.php b/src/Command/PollsRemindCommand.php index 3abfdaf..60033d5 100644 --- a/src/Command/PollsRemindCommand.php +++ b/src/Command/PollsRemindCommand.php @@ -9,6 +9,10 @@ use Cake\Console\Arguments; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; use Cake\I18n\DateTime; +use Cake\Mailer\Transport\SmtpTransport; +use Cake\Mailer\TransportFactory; +use ReflectionClass; +use function Cake\Core\env; /** * Mails the owners of polls that expire soon (POLL_REMINDER_DAYS before the end), once per poll. @@ -82,6 +86,10 @@ class PollsRemindCommand extends PollsCommand $due = $this->polls()->find('reminderDue')->orderBy(['Polls.id' => 'ASC'])->all()->toList(); } + if (!$dryRun && $due !== []) { + $io->out($this->describeTransport()); + } + $sent = 0; $failed = 0; foreach ($due as $poll) { @@ -116,4 +124,36 @@ class PollsRemindCommand extends PollsCommand return $failed === 0 ? static::CODE_SUCCESS : static::CODE_ERROR; } + + /** + * One line about the mail transport in use (never the password), so a misconfiguration is visible at once. + * + * @return string + */ + protected function describeTransport(): string + { + $transport = TransportFactory::get('default'); + $config = $transport->getConfig(); + $class = (new ReflectionClass($transport))->getShortName(); + if ($transport instanceof SmtpTransport) { + return __( + 'Mail-Transport: {0} {1}:{2}, Benutzer {3}, TLS {4}', + $class, + $config['host'], + $config['port'], + $config['username'] ?? __('(keiner)'), + $config['tls'] ? __('ja') : __('nein'), + ); + } + + if (trim((string)env('SMTP_HOST', '')) !== '') { + return __( + 'WARNUNG: SMTP_HOST ist gesetzt, aber der Transport ist {0}. Vermutlich überschreibt config/app_local.php ' . + 'den Transport (auf Servern config/app_local.php löschen, Konfiguration nur über .env). Es wird nichts verschickt.', + $class, + ); + } + + return __('Mail-Transport: {0} (kein SMTP, SMTP_HOST nicht gesetzt)', $class); + } }