Skip to content
Snippets Groups Projects
Select Git revision
  • 0a4410b1e836cb0ddd4cf58441823705a2f8f503
  • main default
  • 35-cgu
  • 34-peertube-support
  • 27-add-autoplay-to-iframe
  • 33-bug-on-youtube-embed-urls
  • RC-Rekall-v1.1-fix_lpo
  • tuleap-140-go-back-to-my-capsules-page-when-i-m-on-capsule-preview-page
  • RC-Rekall-v1.2-fix10
  • RC-Rekall-v1.2-fix9
  • RC-Rekall-v1.2-fix8
  • RC-Rekall-v1.2-fix7
  • RC-Rekall-v1.2-fix6
  • RC-Rekall-v1.2-fix5
  • RC-Rekall-v1.2-fix4
  • RC-Rekall-v1.2-fix3
  • RC-Rekall-v1.2-fix2
  • RC-Rekall-v1.2-fix1
  • RC-Rekall-v1.1-fix-3
  • RC-Rekall-v1.1-fix-2
  • RC-Rekall-v1.1-fix-1
  • RC-Rekall-v1.1-delivered
  • preprod20220209-1535
23 results

FallbackController.php

Blame
  • Camille Simiand's avatar
    Camille Simiand authored and Sebastien Curt committed
    0a4410b1
    History
    FallbackController.php 4.83 KiB
    <?php
    
    namespace App\Controller;
    
    use App\Helper\LegacyHelper;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\BinaryFileResponse;
    use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
    
    class FallbackController extends AbstractController
    {
        /**
         * @Route("/", name="get_legacy_resource", priority=-2)
         * @Route("/{controller}", name="get_legacy_resource", requirements={"controller" = ".+"}, priority=-1)
         */
        public function getLegacyResourceAction(Request $request, ?string $controller = null): Response
        {
            if ($request->query->has('p') || $request->request->has('p')) {
                $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
                // TODO : the following code MUST be re-enabled with tuleap-51
    
    //            $user = $this->getUser();
    //            $link = str_replace("php/project.php", "", $controller);
    //            $link = rtrim($link, "/");
    //            $user_id = $user->getId();
    
    //          // check if capsule exists in database
    //          $cap = $this->getDoctrine()
    //                      ->getManager()
    //                      ->getRepository('AppBundle:Capsule')
    //                      ->getCapsuleByLink($link);
    //          if (!$cap instanceof Capsule) {
    //              // rediriger sur une page demandant si il est l'auteur de la capsule
    //              return $this->redirectToRoute('add_author', array('capsuleLink'=>$link, 'capsulePass'=> $pass));
    //          }
    //
    //          // if capsule exists, check if user can edit it
    //          $cap = $this->getDoctrine()
    //                      ->getManager()
    //                      ->getRepository('AppBundle:Capsule')
    //                      ->getCapsuleByLinkAndUser($link, $user_id);
    //          if (!$cap instanceof Capsule) {
    //              // rediriger sur une page lui demandant de contacter
    //              // l'administrateur de la capsule pour lui donner des droits
    //              return $this->redirectToRoute('no_edition_access');
    //          }
    //
    //          // if yes, continue
    //          $cap->setAutMaj($user);
    //          $cap->setDtMaj(new \DateTime());
    //
    //          $em = $this->getDoctrine()->getManager();
    //          $em->persist($cap);
    //          $em->flush();
            }
    
    //        if ($controller == null) {
    //            //if no controller, this is index1.php
    //            return $this->redirectToRoute('get_legacy_resource', array('controller' => 'index1.php'));
    //        }
    
            if (null === $controller) {
                return LegacyHelper::transferToLegacy(
                    $request,
                    strval($this->getParameter('app.legacy_external_prefix')),
                    strval($this->getParameter('app.legacy_url'))
                );
            }
    
            // use iframe to enhance speed but not for creation
            if (strpos($controller, 'legacy') !== 0) {
                // relative path (to app/) of the legacy code (on the same filesystem)
                //$path_to_legacy_code = $this->getParameter('kernel.root_dir').
                //$this->getParameter('legacy_root_dir');
                $originalController = $request->getPathInfo();
                $originalQueryString = $request->getQueryString();
    
                $extension = strrchr($originalController, '.');
                if ($extension != '.html' && $extension != '.php' && $extension != false) {
                    //$url = "{$path_to_legacy_code}{$originalController}";
                    $url = "{$originalController}";
                    try {
                        $mime = LegacyHelper::getMime($url, $extension);
                        $response = new BinaryFileResponse($url);
                        $response->headers->set('Content-Type', $mime ?: 'application/octet-stream');
                        return $response;
                    } catch (FileNotFoundException $e) {
                        // try normal access by url
                    }
                }
                $separator = '';
                if (!$extension) {
                    $separator = '/';
                }
    
                $app_legacy_url_external = strval($this->getParameter('app.legacy_url_external'));
    
                $url = "{$app_legacy_url_external}/" .
                    "{$originalController}{$separator}?{$originalQueryString}";
                $url = preg_replace('(^https?:\/\/[^/]+(:\d+)?)', '', $url);
    
                $pattern = '/\/+/i';
                /** @phpstan-ignore-next-line */
                $url = preg_replace($pattern, '/', $url);
    
                return $this->render("project/edit.html.twig", [
                    'url' => $url
                ]);
            }
    
            return LegacyHelper::transferToLegacy(
                $request,
                strval($this->getParameter('app.legacy_external_prefix')),
                strval($this->getParameter('app.legacy_url'))
            );
        }
    }