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