[TASK] Migrate eID scripts to use PSR-7
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Http / EidRequestHandler.php
index a2fbef8..06d7485 100644 (file)
@@ -15,11 +15,14 @@ namespace TYPO3\CMS\Frontend\Http;
  */
 
 use TYPO3\CMS\Core\Core\Bootstrap;
+use TYPO3\CMS\Core\Exception;
+use TYPO3\CMS\Core\Http\ControllerInterface;
+use TYPO3\CMS\Core\Http\Response;
 use TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
-use TYPO3\CMS\Frontend\Utility\EidUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Core\Core\RequestHandlerInterface;
+use TYPO3\CMS\Core\Http\RequestHandlerInterface;
+use Psr\Http\Message\ServerRequestInterface;
 
 /**
  * Lightweight alternative to the regular RequestHandler used when $_GET[eID] is set.
@@ -34,7 +37,7 @@ class EidRequestHandler implements RequestHandlerInterface {
        protected $bootstrap;
 
        /**
-        * Constructor handing over the bootstrap
+        * Constructor handing over the bootstrap and the original request
         *
         * @param Bootstrap $bootstrap
         */
@@ -45,15 +48,16 @@ class EidRequestHandler implements RequestHandlerInterface {
        /**
         * Handles a frontend request based on the _GP "eID" variable.
         *
-        * @return void
+        * @param ServerRequestInterface $request
+        * @return NULL|\Psr\Http\Message\ResponseInterface
         */
-       public function handleRequest() {
+       public function handleRequest(ServerRequestInterface $request) {
                // Timetracking started
                $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']);
                if (empty($configuredCookieName)) {
                        $configuredCookieName = 'be_typo_user';
                }
-               if ($_COOKIE[$configuredCookieName]) {
+               if ($request->getCookieParams()[$configuredCookieName]) {
                        $GLOBALS['TT'] = new TimeTracker();
                } else {
                        $GLOBALS['TT'] = new NullTimeTracker();
@@ -73,18 +77,17 @@ class EidRequestHandler implements RequestHandlerInterface {
 
                // Remove any output produced until now
                $this->bootstrap->endOutputBufferingAndCleanPreviousOutput();
-               require EidUtility::getEidScriptPath();
-               $this->bootstrap->shutdown();
-               exit;
+               return $this->dispatch($request);
        }
 
        /**
         * This request handler can handle any frontend request.
         *
+        * @param ServerRequestInterface $request The request to process
         * @return bool If the request is not an eID request, TRUE otherwise FALSE
         */
-       public function canHandleRequest() {
-               return GeneralUtility::_GP('eID') ? TRUE : FALSE;
+       public function canHandleRequest(ServerRequestInterface $request) {
+               return !empty($request->getQueryParams()['eID']) || !empty($request->getParsedBody()['eID']);
        }
 
        /**
@@ -96,4 +99,38 @@ class EidRequestHandler implements RequestHandlerInterface {
        public function getPriority() {
                return 80;
        }
+
+       /**
+        * Dispatches the request to the corresponding eID class or eID script
+        *
+        * @param ServerRequestInterface $request
+        * @return NULL|\Psr\Http\Message\ResponseInterface
+        * @throws Exception
+        */
+       protected function dispatch($request) {
+               $eID = isset($request->getParsedBody()['eID'])
+                       ? $request->getParsedBody()['eID']
+                       : isset($request->getQueryParams()['eID']) ? $request->getQueryParams()['eID'] : '';
+
+               if (empty($eID) || !isset($GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID])) {
+                       return GeneralUtility::makeInstance(Response::class)->withStatus(404, 'eID not registered');
+               }
+
+               $configuration = $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$eID];
+               if (class_exists($configuration)) {
+                       $controller = GeneralUtility::makeInstance($configuration);
+                       if (!$controller instanceof ControllerInterface) {
+                               throw new Exception('The provided eID class "' . $configuration . '" does not implement "ControllerInterface".', 1436909478);
+                       }
+                       return $controller->processRequest($request);
+               }
+
+               $scriptPath = GeneralUtility::getFileAbsFileName($configuration);
+               if ($scriptPath === '') {
+                       throw new Exception('Registered eID has invalid script path.', 1416391467);
+               }
+               include $scriptPath;
+               return NULL;
+       }
+
 }