From dc535a0d2a9231e3632f0774fe9e6ac5c95c76f2 Mon Sep 17 00:00:00 2001 From: Thomas Lehmann Date: Thu, 13 Jun 2024 17:53:23 +0200 Subject: [PATCH] [POC] (C11N) lib/base: add "redirect" config WIP: Tests need to be written The "redirects" config allows configuring redirects from paths to routes. Example config: "redirects" => { "^/path/regexp" => "an.apps.controller.route.locator" } Use case: * An administrator can configure another app as target for certain paths. Differentiation from Apache redirects: * this allows configuring redirects in one place and avoids spreading the configuration in multiple places. * the target route locator is more expressive/easier to trace than a path --- config/config.redirects-sample.php | 13 +++++++++++++ lib/base.php | 22 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 config/config.redirects-sample.php diff --git a/config/config.redirects-sample.php b/config/config.redirects-sample.php new file mode 100644 index 0000000000000..a6ac4bd668419 --- /dev/null +++ b/config/config.redirects-sample.php @@ -0,0 +1,13 @@ + [ + // Request path without /index.php/ maps to a controller path in the form + // ... + + // - For a FooController.php the controller name is "foo" (lowercase) + // - A handler would be a method in FooController that was annotated with + // - either #[FrontpageRoute] attribute + // - or configured in routes.php + '^\/settings' => 'acmesettings.page.index' + ], +]; diff --git a/lib/base.php b/lib/base.php index 1443726705202..47080e3752ad4 100644 --- a/lib/base.php +++ b/lib/base.php @@ -1047,7 +1047,27 @@ public static function handleRequest(): void { OC_App::loadApps(['filesystem', 'logging']); OC_App::loadApps(); } - Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo()); + $requestPath = $request->getRawPathInfo(); + $redirects = $systemConfig->getValue('redirects', []); + + if ($redirects) { + foreach ($redirects as $fromPattern => $toLocator) { + if (!preg_match('/' . $fromPattern . '/', $requestPath)) { + continue; + } + + try { + $targetLocation = Server::get(IURLGenerator::class)->linkToRouteAbsolute($toLocator); + header('Location: ' . $targetLocation); + return; + } catch (\Exception) { + // In case of container exceptions or + // route not found exceptions we proceed as usual. + } + } + } + + Server::get(\OC\Route\Router::class)->match($requestPath); return; } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { //header('HTTP/1.0 404 Not Found');