| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PiedWeb\CMSBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PiedWeb\CMSBundle\Entity\PageInterface as Page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PiedWeb\CMSBundle\Repository\PageRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PiedWeb\CMSBundle\Service\AppConfigHelper as App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PiedWeb\CMSBundle\Service\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class PageController extends AbstractController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var ParameterBagInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var App | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ParameterBagInterface $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->params = $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->app = App::load(null, $this->params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function show(?string $slug, ?string $host, Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $page = $this->getPage($slug, $host, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if ((null === $host || $host == $request->getHost()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             && false !== $redirect = $this->checkIfUriIsCanonical($request, $page)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             return $this->redirect($redirect[0], $redirect[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // Maybe the page is a redirection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if ($page->getRedirection()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return $this->redirect($page->getRedirection(), $page->getRedirectionCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $template = $this->app->getDefaultTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $this->render($template, array_merge(['page' => $page], $this->app->getParamsForRendering())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function preview(?string $slug, ?string $host, Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $page = $this->getPage($slug, $host, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $page->setMainContent($request->request->get('plaintext')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         // todo update all fields to avoid errors with autosave | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // (getting them via json?!) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         // And not getPage but create a new Page !!! (else, error on unexisting Page) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             '@PiedWebCMS/page/preview.html.twig', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             array_merge(['page' => $page], $this->app->getParamsForRendering()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function showFeed(?string $slug, ?string $host, Request $request) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $page = $this->getPage($slug, $host); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ('homepage' == $slug) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return $this->redirect($this->generateUrl('piedweb_cms_page_feed', ['slug' => 'index']), 301); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $response = new Response(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $response->headers->set('Content-Type', 'text/xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             '@PiedWebCMS/page/rss.xml.twig', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             array_merge(['page' => $page], $this->app->getParamsForRendering()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Show Last created page in an XML Feed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function showMainFeed(?string $host, Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->app->switchCurrentApp($host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $locale = $request->getLocale() ? rtrim($request->getLocale(), '/') : $this->params->get('locale'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $LocaleHomepage = $this->getPage($locale, $host, $request, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $slug = 'homepage'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $page = $LocaleHomepage ? $LocaleHomepage : $this->getPage($slug, $host, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'pages' => $this->getPages(5, $request), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'page' => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'feedUri' => ($this->params->get('locale') == $locale ? '' : $locale.'/').'feed.xml', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $this->render('@PiedWebCMS/page/rss.xml.twig', array_merge($params, $this->app->getParamsForRendering())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function showSitemap($_format, ?string $host, Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->app->switchCurrentApp($host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $pages = $this->getPages(null, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if (!$pages) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             throw $this->createNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $this->render('@PiedWebCMS/page/sitemap.'.$_format.'.twig', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'pages' => $pages, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             'app_base_url' => $this->app->getBaseUrl(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     protected function getPages(?int $limit = null, Request $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         $requestedLocale = rtrim($request->getLocale(), '/'); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         //var_dump($requestedLocale);exit; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         $pages = $this->getPageRepository()->getIndexablePages( | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |                 $this->app->getHost(), | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |                 $this->app->isFirstApp(), | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |                 $requestedLocale, | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |                 $this->params->get('locale'), | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                 $limit | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |             )->getQuery()->getResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |         //foreach ($pages as $page) echo $page->getMetaRobots().' '.$page->getTitle().'<br>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         //exit('feed updated'); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         return $pages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return PageRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     protected function getPageRepository() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return Repository::getPageRepository($this->getDoctrine(), $this->params->get('pwc.entity_page')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     protected function getPage(?string &$slug, ?string $host = null, ?Request $request, $throwException = true): ?Page | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->app->switchCurrentApp($host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $slug = $this->noramlizeSlug($slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $page = $this->getPageRepository()->getPage($slug, $this->app->getHost(), $this->app->isFirstApp()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         // Check if page exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         if (null === $page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             if ($throwException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 throw $this->createNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         if (!$page->getLocale()) { // avoid bc break | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             $page->setLocale($this->params->get('pwc.locale')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         //if (null !== $request) { $request->setLocale($page->getLocale()); } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->get('translator')->setLocale($page->getLocale()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         // Check if page is public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_EDITOR')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             if ($throwException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 throw $this->createNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         return $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     protected function noramlizeSlug($slug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         return (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     protected function checkIfUriIsCanonical(Request $request, Page $page) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         $real = $request->getRequestUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $expected = 'homepage' == $page->getSlug() ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             $this->get('piedweb.page_canonical')->generatePathForHomepage() : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         if ($real != $expected) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             return [$request->getBasePath().$expected, 301]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.