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

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