Skip to content

Internal: Add default error page for undefined URL in multi-URL - refs #6297 #6316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/CoreBundle/Controller/ExceptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

namespace Chamilo\CoreBundle\Controller;

use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ExceptionController extends AbstractController
{
Expand Down Expand Up @@ -91,4 +93,33 @@ public function error(Request $request): Response
'exception' => $exception,
]);
}

#[Route(path: '/error/undefined-url', name: 'undefined_url_error')]
public function undefinedUrlError(
Request $request,
UrlGeneratorInterface $urlGenerator,
AccessUrlHelper $accessUrlHelper
): Response {
$host = $request->getHost();

$accessUrl = $accessUrlHelper->getFirstAccessUrl();
$themeHost = rtrim($accessUrl?->getUrl() ?? '', '/');
$themeName = 'chamilo';

$cssUrl = $themeHost . $urlGenerator->generate('theme_asset', [
'name' => $themeName,
'path' => 'colors.css',
], UrlGeneratorInterface::ABSOLUTE_PATH);

$logoUrl = $themeHost . $urlGenerator->generate('theme_asset', [
'name' => $themeName,
'path' => 'images/header-logo.svg',
], UrlGeneratorInterface::ABSOLUTE_PATH);

return $this->render('@ChamiloCore/Exception/undefined_url.html.twig', [
'host' => $host,
'cssUrl' => $cssUrl,
'logoUrl' => $logoUrl,
]);
}
}
78 changes: 78 additions & 0 deletions src/CoreBundle/EventListener/AccessUrlValidationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\EventListener;

use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\RouterInterface;

/**
* This event listener checks whether the current request is using a valid, registered AccessUrl.
* If not, it redirects to a custom error page (/error/undefined-url).
*/
class AccessUrlValidationListener
{
public function __construct(
private readonly AccessUrlHelper $accessUrlHelper,
private readonly RouterInterface $router
) {}

public function onKernelRequest(RequestEvent $event): void
{
// Only handle the main request, not subrequests.
if (!$event->isMainRequest()) {
return;
}

$request = $event->getRequest();
$path = $request->getPathInfo();

// Prevent infinite redirection loop to the same error page
if (str_starts_with($path, '/error/undefined-url')) {
return;
}

// Skip validation for static assets and special files
$excludedPrefixes = [
'/build/', // Webpack build assets
'/theme/', // Legacy theme assets
'/favicon.ico', // Favicon
'/robots.txt', // Robots file
'/apple-touch-icon.png', // Mobile shortcut icon
];

foreach ($excludedPrefixes as $prefix) {
if (str_starts_with($path, $prefix)) {
return;
}
}

// Skip validation if multi-URL is not enabled
if (!$this->accessUrlHelper->isMultiple()) {
return;
}

try {
// Try to get the current AccessUrl from the request
$accessUrl = $this->accessUrlHelper->getCurrent();

// If it's null, throw an exception to trigger redirection
if (null === $accessUrl) {
throw new \RuntimeException('AccessUrl not defined');
}
} catch (\Throwable $e) {
// Log host and URI for debugging
$host = $request->getHost();
$uri = $request->getUri();

// Redirect to custom error page
$url = $this->router->generate('undefined_url_error');
$event->setResponse(new RedirectResponse($url));
}
}
}
4 changes: 4 additions & 0 deletions src/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,7 @@ services:
$parameterBag: '@parameter_bag'
$pluginRepo: '@Chamilo\CoreBundle\Repository\AccessUrlRelPluginRepository'
$accessUrlHelper: '@Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper'

Chamilo\CoreBundle\EventListener\AccessUrlValidationListener:
tags:
- { name: kernel.event_listener, event: kernel.request, priority: 255 }
42 changes: 42 additions & 0 deletions src/CoreBundle/Resources/views/Exception/undefined_url.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends '@ChamiloCore/Layout/layout_one_col.html.twig' %}

{% block chamilo_head %}
{{ parent() }}
<link rel="stylesheet" href="{{ cssUrl }}">
{% endblock %}

{% block content %}
<div class="text-center my-10">
{% if logoUrl %}
<img
src="{{ logoUrl }}"
alt="Chamilo Logo"
class="mx-auto h-20 mb-6"
/>
{% endif %}

<div style="background-color: rgb(var(--color-danger-base)); color: rgb(var(--color-danger-button-text));"
class="mx-auto p-6 rounded-lg shadow-md md:w-3/4 lg:w-1/2">
<p style="font-size: 1.5rem; font-weight: bold;" class="mb-4">
{{ 'Oops! URL not registered.'|trans }}
</p>

<p style="font-size: 1.2rem;" class="mb-4">
{{ 'The address "%host%" is not configured in this Chamilo instance.'|trans({'%host%': host}) }}
</p>

<p style="font-size: 1rem;" class="mb-6">
{{ 'If the problem persists, please contact support.'|trans }}
</p>

<a href="/"
class="inline-block px-6 py-2 rounded"
style="
background-color: rgb(var(--color-primary-base));
color: rgb(var(--color-primary-button-alternative-text));
">
{{ 'Go to home page'|trans }}
</a>
</div>
</div>
{% endblock %}
6 changes: 3 additions & 3 deletions src/CoreBundle/ServiceHelper/AuthenticationConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public function getEnabledOAuthProviders(?AccessUrl $url = null): array

public function getAuthSources(?AccessUrl $url)
{
$urlId = $url ?: $this->urlHelper->getCurrent();
$accessUrl = $url ?: $this->urlHelper->getCurrent();

$authentication = $this->parameterBag->has('authentication')
? $this->parameterBag->get('authentication')
: [];

if (isset($authentication[$urlId->getId()])) {
return $authentication[$urlId->getId()];
if ($accessUrl instanceof AccessUrl && isset($authentication[$accessUrl->getId()])) {
return $authentication[$accessUrl->getId()];
}

if (isset($authentication['default'])) {
Expand Down
6 changes: 5 additions & 1 deletion src/CoreBundle/ServiceHelper/ThemeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Chamilo\CoreBundle\ServiceHelper;

use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Settings\SettingsManager;
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
use League\Flysystem\FilesystemException;
Expand Down Expand Up @@ -46,9 +47,12 @@ public function getVisualTheme(): string
return $visualTheme;
}

$visualTheme = null;
$accessUrl = $this->accessUrlHelper->getCurrent();

$visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug();
if ($accessUrl instanceof AccessUrl) {
$visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug();
}

if ('true' == $this->settingsManager->getSetting('profile.user_selected_theme')) {
$visualTheme = $this->userHelper->getCurrent()?->getTheme();
Expand Down
Loading