vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Routing/ContentRouteProvider.php line 154

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\WebsiteBundle\Routing;
  11. use PHPCR\RepositoryException;
  12. use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
  13. use Sulu\Bundle\PageBundle\Document\PageDocument;
  14. use Sulu\Component\Content\Compat\Structure\PageBridge;
  15. use Sulu\Component\Content\Compat\StructureManagerInterface;
  16. use Sulu\Component\Content\Document\Behavior\ExtensionBehavior;
  17. use Sulu\Component\Content\Document\Behavior\ResourceSegmentBehavior;
  18. use Sulu\Component\Content\Document\Behavior\WebspaceBehavior;
  19. use Sulu\Component\Content\Document\RedirectType;
  20. use Sulu\Component\Content\Exception\ResourceLocatorMovedException;
  21. use Sulu\Component\Content\Exception\ResourceLocatorNotFoundException;
  22. use Sulu\Component\Content\Types\ResourceLocator\Strategy\ResourceLocatorStrategyPoolInterface;
  23. use Sulu\Component\DocumentManager\DocumentManagerInterface;
  24. use Sulu\Component\Security\Authorization\PermissionTypes;
  25. use Sulu\Component\Security\Authorization\SecurityCheckerInterface;
  26. use Sulu\Component\Security\Authorization\SecurityCondition;
  27. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  28. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  29. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  30. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  33. use Symfony\Component\Routing\Route;
  34. use Symfony\Component\Routing\RouteCollection;
  35. /**
  36.  * The PortalRouteProvider should load the dynamic routes created by Sulu.
  37.  */
  38. class ContentRouteProvider implements RouteProviderInterface
  39. {
  40.     /**
  41.      * @var DocumentManagerInterface
  42.      */
  43.     private $documentManager;
  44.     /**
  45.      * @var DocumentInspector
  46.      */
  47.     private $documentInspector;
  48.     /**
  49.      * @var ResourceLocatorStrategyPoolInterface
  50.      */
  51.     private $resourceLocatorStrategyPool;
  52.     /**
  53.      * @var StructureManagerInterface
  54.      */
  55.     private $structureManager;
  56.     /**
  57.      * @var WebspaceManagerInterface
  58.      */
  59.     private $webspaceManager;
  60.     /**
  61.      * @var RequestAnalyzerInterface
  62.      */
  63.     private $requestAnalyzer;
  64.     /**
  65.      * @var SecurityCheckerInterface|null
  66.      */
  67.     private $securityChecker;
  68.     /**
  69.      * @var array
  70.      */
  71.     private $defaultOptions;
  72.     public function __construct(
  73.         DocumentManagerInterface $documentManager,
  74.         DocumentInspector $documentInspector,
  75.         ResourceLocatorStrategyPoolInterface $resourceLocatorStrategyPool,
  76.         StructureManagerInterface $structureManager,
  77.         WebspaceManagerInterface $webspaceManager,
  78.         RequestAnalyzerInterface $requestAnalyzer,
  79.         SecurityCheckerInterface $securityChecker null,
  80.         array $defaultOptions = []
  81.     ) {
  82.         $this->documentManager $documentManager;
  83.         $this->documentInspector $documentInspector;
  84.         $this->resourceLocatorStrategyPool $resourceLocatorStrategyPool;
  85.         $this->structureManager $structureManager;
  86.         $this->webspaceManager $webspaceManager;
  87.         $this->requestAnalyzer $requestAnalyzer;
  88.         $this->securityChecker $securityChecker;
  89.         $this->defaultOptions $defaultOptions;
  90.     }
  91.     public function getRouteCollectionForRequest(Request $request): RouteCollection
  92.     {
  93.         $collection = new RouteCollection();
  94.         if ('' === $request->getRequestFormat()) {
  95.             return $collection;
  96.         }
  97.         /** @var RequestAttributes $attributes */
  98.         $attributes $request->attributes->get('_sulu');
  99.         if (!$attributes) {
  100.             return $collection;
  101.         }
  102.         $matchType $attributes->getAttribute('matchType');
  103.         // no portal information without localization supported
  104.         if (null === $attributes->getAttribute('localization')
  105.             && RequestAnalyzerInterface::MATCH_TYPE_PARTIAL !== $matchType
  106.             && RequestAnalyzerInterface::MATCH_TYPE_REDIRECT !== $matchType
  107.         ) {
  108.             return $collection;
  109.         }
  110.         $resourceLocator $this->decodePathInfo($attributes->getAttribute('resourceLocator'));
  111.         $prefix $attributes->getAttribute('resourceLocatorPrefix');
  112.         $pathInfo $this->decodePathInfo($request->getPathInfo());
  113.         $htmlRedirect $pathInfo !== $prefix $resourceLocator
  114.                         && \in_array($request->getRequestFormat(), ['htm''html']);
  115.         if ($htmlRedirect
  116.             || RequestAnalyzerInterface::MATCH_TYPE_REDIRECT == $matchType
  117.             || RequestAnalyzerInterface::MATCH_TYPE_PARTIAL == $matchType
  118.         ) {
  119.             return $collection;
  120.         }
  121.         // just show the page
  122.         $portal $attributes->getAttribute('portal');
  123.         $locale $attributes->getAttribute('localization')->getLocale();
  124.         $resourceLocatorStrategy $this->resourceLocatorStrategyPool->getStrategyByWebspaceKey(
  125.             $portal->getWebspace()->getKey()
  126.         );
  127.         try {
  128.             // load content by url ignore ending trailing slash
  129.             /** @var PageDocument $document */
  130.             $document $this->documentManager->find(
  131.                 $resourceLocatorStrategy->loadByResourceLocator(
  132.                     \rtrim($resourceLocator'/'),
  133.                     $portal->getWebspace()->getKey(),
  134.                     $locale
  135.                 ),
  136.                 $locale,
  137.                 [
  138.                     'load_ghost_content' => false,
  139.                 ]
  140.             );
  141.             if (!$document->getTitle()) {
  142.                 // If the title is empty the document does not exist in this locale
  143.                 // Necessary because of https://github.com/sulu/sulu/issues/2724, otherwise locale could be checked
  144.                 return $collection;
  145.             }
  146.             if ($this->securityChecker && $portal->getWebspace()->hasWebsiteSecurity()) {
  147.                 $this->securityChecker->checkPermission(
  148.                     new SecurityCondition(
  149.                         'sulu.webspaces.' $document->getWebspaceName(),
  150.                         $document->getLocale(),
  151.                         \get_class($document),
  152.                         $document->getUuid()
  153.                     ),
  154.                     PermissionTypes::VIEW
  155.                 );
  156.             }
  157.             if (\preg_match('/\/$/'$resourceLocator) && ('/' !== $resourceLocator || $prefix)) {
  158.                 // redirect page to page without slash at the end
  159.                 $url $prefix \rtrim($resourceLocator'/');
  160.                 if ($request->getQueryString()) {
  161.                     $url .= '?' $request->getQueryString();
  162.                 }
  163.                 $collection->add('redirect_' \uniqid(), $this->getRedirectRoute($request$url));
  164.             } elseif (RedirectType::INTERNAL === $document->getRedirectType()) {
  165.                 $redirectTarget $document->getRedirectTarget();
  166.                 if (!$redirectTarget instanceof ResourceSegmentBehavior || !$redirectTarget instanceof WebspaceBehavior) {
  167.                     return $collection;
  168.                 }
  169.                 $redirectUrl $this->webspaceManager->findUrlByResourceLocator(
  170.                     $redirectTarget->getResourceSegment(),
  171.                     null,
  172.                     $document->getLocale(),
  173.                     $redirectTarget->getWebspaceName()
  174.                 );
  175.                 if ($request->getQueryString()) {
  176.                     $redirectUrl .= '?' $request->getQueryString();
  177.                 }
  178.                 $collection->add(
  179.                     $document->getStructureType() . '_' $document->getUuid(),
  180.                     $this->getRedirectRoute($request$redirectUrl)
  181.                 );
  182.             } elseif (RedirectType::EXTERNAL === $document->getRedirectType()) {
  183.                 $collection->add(
  184.                     $document->getStructureType() . '_' $document->getUuid(),
  185.                     $this->getRedirectRoute($request$document->getRedirectExternal())
  186.                 );
  187.             } elseif (!$this->checkResourceLocator($resourceLocator$prefix)) {
  188.                 return $collection;
  189.             } else {
  190.                 if ($document instanceof ExtensionBehavior) {
  191.                     $documentSegments $document->getExtensionsData()['excerpt']['segments'] ?? [];
  192.                     $documentSegmentKey $documentSegments[$portal->getWebspace()->getKey()] ?? null;
  193.                     $segment $this->requestAnalyzer->getSegment();
  194.                     if ($segment && $documentSegmentKey && $segment->getKey() !== $documentSegmentKey) {
  195.                         $this->requestAnalyzer->changeSegment($documentSegmentKey);
  196.                     }
  197.                 }
  198.                 // convert the page to a StructureBridge because of BC
  199.                 $metadata $this->documentInspector->getStructureMetadata($document);
  200.                 if (!$metadata) {
  201.                     return $collection;
  202.                 }
  203.                 /** @var PageBridge $structure */
  204.                 $structure $this->structureManager->wrapStructure(
  205.                     $this->documentInspector->getMetadata($document)->getAlias(),
  206.                     $metadata
  207.                 );
  208.                 $structure->setDocument($document);
  209.                 // show the page
  210.                 $collection->add(
  211.                     $document->getStructureType() . '_' $document->getUuid(),
  212.                     $this->getStructureRoute($request$structure)
  213.                 );
  214.             }
  215.         } catch (ResourceLocatorNotFoundException $exc) {
  216.             // just do not add any routes to the collection
  217.         } catch (ResourceLocatorMovedException $exc) {
  218.             // old url resource was moved
  219.             $collection->add(
  220.                 $exc->getNewResourceLocatorUuid() . '_' \uniqid(),
  221.                 $this->getRedirectRoute($request$prefix $exc->getNewResourceLocator())
  222.             );
  223.         } catch (RepositoryException $exc) {
  224.             // just do not add any routes to the collection
  225.         }
  226.         return $collection;
  227.     }
  228.     /**
  229.      * @param string $name
  230.      */
  231.     public function getRouteByName($name): Route
  232.     {
  233.         throw new RouteNotFoundException();
  234.     }
  235.     public function getRoutesByNames($names null): iterable
  236.     {
  237.         return [];
  238.     }
  239.     /**
  240.      * Checks if the resource locator is valid.
  241.      * A resource locator with a slash only is not allowed, the only exception is when it is a single language
  242.      * website, where the browser automatically adds the slash.
  243.      *
  244.      * @param string $resourceLocator
  245.      * @param string $resourceLocatorPrefix
  246.      *
  247.      * @return bool
  248.      */
  249.     private function checkResourceLocator($resourceLocator$resourceLocatorPrefix)
  250.     {
  251.         return !('/' === $resourceLocator && $resourceLocatorPrefix);
  252.     }
  253.     /**
  254.      * @param string $url
  255.      *
  256.      * @return Route
  257.      */
  258.     protected function getRedirectRoute(Request $request$url)
  259.     {
  260.         $requestFormat $request->getRequestFormat(null);
  261.         $formatSuffix $requestFormat '.' $requestFormat '';
  262.         // redirect to linked page
  263.         return new Route(
  264.             $this->decodePathInfo($request->getPathInfo()),
  265.             [
  266.                 '_controller' => 'sulu_website.redirect_controller::redirectAction',
  267.                 'url' => $url $formatSuffix,
  268.             ],
  269.             [],
  270.             $this->defaultOptions
  271.         );
  272.     }
  273.     /**
  274.      * @return Route
  275.      */
  276.     protected function getStructureRoute(Request $requestPageBridge $content)
  277.     {
  278.         return new Route(
  279.             $this->decodePathInfo($request->getPathInfo()),
  280.             [
  281.                 '_controller' => $content->getController(),
  282.                 'structure' => $content,
  283.                 'partial' => 'true' === $request->get('partial''false'),
  284.             ],
  285.             [],
  286.             $this->defaultOptions
  287.         );
  288.     }
  289.     /**
  290.      * Server encodes the url and symfony does not encode it
  291.      * Symfony decodes this data here https://github.com/symfony/symfony/blob/3.3/src/Symfony/Component/Routing/Matcher/UrlMatcher.php#L91.
  292.      *
  293.      * @param string $pathInfo
  294.      *
  295.      * @return string
  296.      */
  297.     private function decodePathInfo($pathInfo)
  298.     {
  299.         if (null === $pathInfo || '' === $pathInfo) {
  300.             return '';
  301.         }
  302.         return '/' \ltrim(\rawurldecode($pathInfo), '/');
  303.     }
  304. }