. */ namespace SP\Mvc\Controller; use Closure; use DI\DependencyException; use DI\NotFoundException; use SP\Bootstrap; use SP\Config\ConfigData; use SP\Core\Exceptions\SPException; use SP\Http\Json; use SP\Http\JsonResponse; use SP\Http\Request; use SP\Http\Uri; use SP\Util\Util; /** * Trait ControllerTrait * * @package SP\Mvc\Controller * @property ConfigData $configData */ trait ControllerTrait { /** * @return string */ protected function getControllerName(): string { $class = static::class; return substr($class, strrpos($class, '\\') + 1, -strlen('Controller')) ?: ''; } /** * Logout from current session * * @param Request $request * @param ConfigData $configData * @param Closure $onRedirect * * @throws DependencyException * @throws NotFoundException */ protected function sessionLogout(Request $request, ConfigData $configData, Closure $onRedirect) { if ($request->isJson()) { $jsonResponse = new JsonResponse(__u('Session not started or timed out')); $jsonResponse->setStatus(10); Json::fromDic()->returnJson($jsonResponse); } elseif ($request->isAjax()) { Util::logout(); } else { try { // Analyzes if there is any direct route within the URL // then it computes the route HMAC to build a signed URI // which would be used during logging in $route = $request->analyzeString('r'); $hash = $request->analyzeString('h'); $uri = new Uri(Bootstrap::$WEBROOT . Bootstrap::$SUBURI); $uri->addParam('_r', 'login'); if ($route && $hash) { $key = $configData->getPasswordSalt(); $request->verifySignature($key); $uri->addParam('from', $route); $onRedirect->call($this, $uri->getUriSigned($key)); } else { $onRedirect->call($this, $uri->getUri()); } } catch (SPException $e) { processException($e); } } } /** * Acción no disponible */ protected function invalidAction() { Json::fromDic()->returnJson(new JsonResponse(__u('Invalid Action'))); } /** * @param string $previousToken * @param Request $request * * @throws SPException * @deprecated */ protected function checkSecurityToken(string $previousToken, Request $request) { if ($request->analyzeString('h') !== null && $request->analyzeString('from') === null && isset($this->configData) ) { $request->verifySignature($this->configData->getPasswordSalt()); } else { $sk = $request->analyzeString('sk'); if (!$sk || $previousToken !== $sk) { throw new SPException( __u('Invalid Action'), SPException::ERROR, null, 1 ); } } } }