Files
KantSites/src/Command/HttpsCommand.php
T
kantarciandClaude Opus 5.5 8b212d5cc9 Force HTTPS from the admin, with a probe before and after
System > HTTPS and cms:https switch the redirect lines in public/.htaccess
only when the site answers over HTTPS, and take them back on a loop. HSTS
is a separate second switch. The redirect block moves before the front
controller rules, where it never reached pages, and .well-known is no
longer denied, so the origin probe works on Apache. The Git updater keeps
switched-on lines like the archive update.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:22:56 +02:00

48 lines
1.9 KiB
PHP

<?php
namespace App\Command;
use App\Ops\HttpsRedirect;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(name: 'cms:https', description: 'Shows or switches the HTTPS redirect and HSTS in public/.htaccess, with a probe before and after (ADR 120).')]
final class HttpsCommand extends Command
{
public function __construct(private HttpsRedirect $https, private string $siteOrigin)
{
parent::__construct();
}
protected function configure(): void
{
$this->addArgument('aktion', InputArgument::OPTIONAL, 'an, aus, hsts-an oder hsts-aus; ohne Angabe der Stand.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$action = (string) $input->getArgument('aktion');
if ('' === $action) {
$state = $this->https->state();
$io->writeln(sprintf('Umleitung auf HTTPS: %s · HSTS: %s%s', $state['redirect'] ? 'an' : 'aus', $state['hsts'] ? 'an' : 'aus', $state['found'] ? '' : ' · Zeilen fehlen in public/.htaccess'));
return Command::SUCCESS;
}
$result = match ($action) {
'an' => $this->https->enableRedirect($this->siteOrigin),
'aus' => $this->https->disableRedirect(),
'hsts-an' => $this->https->switchHsts(true, $this->siteOrigin),
'hsts-aus' => $this->https->switchHsts(false, $this->siteOrigin),
default => ['ok' => false, 'message' => 'Erlaubt sind an, aus, hsts-an und hsts-aus.'],
};
$result['ok'] ? $io->success($result['message']) : $io->error($result['message']);
return $result['ok'] ? Command::SUCCESS : Command::FAILURE;
}
}