injectableFactory = $injectableFactory; $this->entryPointManager = $entryPointManager; $this->entityManager = $entityManager; $this->clientManager = $clientManager; $this->applicationUser = $applicationUser; $this->authTokenManager = $authTokenManager; $this->params = $params ?? (object) []; } public function run() { $entryPoint = $this->params->entryPoint ?? $_GET['entryPoint']; $final = $this->params->final ?? false; $data = $this->params->data ?? null; if (!$entryPoint) { throw new Error(); } $authRequired = $this->entryPointManager->checkAuthRequired($entryPoint); $authNotStrict = $this->entryPointManager->checkNotStrictAuth($entryPoint); if ($authRequired && !$authNotStrict && !$final) { $portalId = $this->detectPortalId(); if ($portalId) { $this->runThroughPortal($portalId, $entryPoint, $data); return; } } $request = (ServerRequestCreatorFactory::create())->createServerRequestFromGlobals(); if ($request->getMethod() !== 'GET') { throw new Error("Only GET request allowed for entry points."); } $requestWrapped = new RequestWrapper($request, Route::detectBasePath()); $responseWrapped = new ResponseWrapper(new Response()); $this->processRequest($entryPoint, $requestWrapped, $responseWrapped, $data, $authRequired, $authNotStrict); (new ResponseEmitter())->emit($responseWrapped->getResponse()); } protected function processRequest( string $entryPoint, RequestWrapper $requestWrapped, ResponseWrapper $responseWrapped, ?StdClass $data, bool $authRequired, bool $authNotStrict ) { try { $authentication = $this->injectableFactory->createWith(Authentication::class, [ 'allowAnyAccess' => $authNotStrict, ]); $apiAuth = ApiAuth::getBuilder() ->setAuthentication($authentication) ->setAuthRequired($authRequired) ->forEntryPoint() ->build(); $apiAuth->process($requestWrapped, $responseWrapped); if (!$apiAuth->isResolved()) { return; } if ($apiAuth->isResolvedUseNoAuth()) { $this->applicationUser->setupSystemUser(); } ob_start(); $this->entryPointManager->run($entryPoint, $requestWrapped, $responseWrapped, $data); $contents = ob_get_clean(); if ($contents) { $responseWrapped->writeBody($contents); } } catch (Exception $e) { (new ApiErrorOutput($requestWrapped))->process($responseWrapped, $e, true); } } protected function detectPortalId() : ?string { if (!empty($_GET['portalId'])) { return $_GET['portalId']; } if (empty($_COOKIE['auth-token'])) { return null; } $authToken = $this->authTokenManager->get($_COOKIE['auth-token']); if ($authToken) { return $authToken->getPortalId(); } return null; } protected function runThroughPortal(string $portalId, string $entryPoint, ?StdClass $data) { $app = new PortalApplication($portalId); $app->setClientBasePath($this->clientManager->getBasePath()); $app->run(EntryPoint::class, (object) [ 'entryPoint' => $entryPoint, 'data' => $data, 'final' => true, ]); } }