2016-04-15 07:58:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\Helper;
|
2016-04-15 07:58:01 +02:00
|
|
|
|
2023-12-28 21:26:10 +01:00
|
|
|
use GuzzleHttp\Psr7\Uri;
|
2022-11-14 23:11:46 +01:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2016-11-07 10:26:05 +01:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Entity\Config;
|
|
|
|
|
use Wallabag\Entity\User;
|
2016-04-15 07:58:01 +02:00
|
|
|
|
2016-04-15 09:58:29 +02:00
|
|
|
/**
|
|
|
|
|
* Manage redirections to avoid redirecting to empty routes.
|
|
|
|
|
*/
|
2016-04-15 07:58:01 +02:00
|
|
|
class Redirect
|
|
|
|
|
{
|
2025-04-05 13:54:27 +02:00
|
|
|
public function __construct(
|
2025-04-05 13:59:36 +02:00
|
|
|
private readonly UrlGeneratorInterface $router,
|
|
|
|
|
private readonly TokenStorageInterface $tokenStorage,
|
2025-04-05 13:54:27 +02:00
|
|
|
) {
|
2016-04-15 07:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-21 16:35:06 +02:00
|
|
|
* @param string $url URL to redirect
|
|
|
|
|
* @param bool $ignoreActionMarkAsRead Ignore configured action when mark as read
|
2016-04-15 07:58:01 +02:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2023-12-28 21:16:32 +01:00
|
|
|
public function to($url, $ignoreActionMarkAsRead = false)
|
2016-04-15 07:58:01 +02:00
|
|
|
{
|
2016-11-07 10:26:05 +01:00
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
|
|
|
|
|
|
2022-11-23 15:51:33 +01:00
|
|
|
if (!$user instanceof User) {
|
2023-12-28 21:26:10 +01:00
|
|
|
if (null === $url) {
|
|
|
|
|
return $this->router->generate('homepage');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Uri::isAbsolutePathReference(new Uri($url))) {
|
|
|
|
|
return $this->router->generate('homepage');
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-07 10:26:05 +01:00
|
|
|
return $url;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 19:11:01 +01:00
|
|
|
if (!$ignoreActionMarkAsRead
|
|
|
|
|
&& Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) {
|
2016-11-06 12:02:39 +01:00
|
|
|
return $this->router->generate('homepage');
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 21:26:10 +01:00
|
|
|
if (null === $url) {
|
|
|
|
|
return $this->router->generate('homepage');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Uri::isAbsolutePathReference(new Uri($url))) {
|
|
|
|
|
return $this->router->generate('homepage');
|
2016-04-15 09:58:29 +02:00
|
|
|
}
|
2016-04-15 07:58:01 +02:00
|
|
|
|
2023-12-28 21:26:10 +01:00
|
|
|
return $url;
|
2016-04-15 07:58:01 +02:00
|
|
|
}
|
|
|
|
|
}
|