request->get($key, TYPE_STR, $default); } /** * Is submit action request * @return bool */ public function isSubmitAction() { return $this->request->isPOST() && empty($this->getRequestAction()); } /** * Handle action request. * @param string|null $action * @return mixed|null */ public function handleActionRequest(?string $action = null) { $action = $action ?: $this->getRequestAction(); if (empty($action)) { return null; } $method = 'on' . Str::studly($action) . 'Action'; if (method_exists($this, $method)) { return $this->$method(); } return null; } /** * Get response data as Response * @param array $response * @return \bff\http\Response */ public function getActionResponse(array $response = []) { if ($this->iframeResponse) { return $this->iframeResponseForm( array_merge($this->actionResponse, $response) ); } return $this->ajaxResponseForm( array_merge($this->actionResponse, $response) ); } /** * Set/get response data * @param array|string $key * @param mixed $value * @param bool $extend * @return mixed */ public function respond($key, $value = null, $extend = true) { # Get data value by key if (is_string($key) && is_null($value)) { return $this->actionResponse[$key] ?? null; } # Set data if (! is_array($key)) { $key = [$key => $value]; } if ($extend) { foreach ($key as $k => $v) { if (is_string($k)) { $this->actionResponse[$k] = $v; } } } else { $this->actionResponse = $key; } return $this->actionResponse; } /** * Проверка запроса на валидность * @param array $opts доп. параметры: referer, userId * @return bool true - запрос корректный */ public function isRequestValid(array $opts = []) { # Request $request = $opts['request'] ?? $this->getRequest(); # Referer $referer = $opts['referer'] ?? $request->referer(); if ( $referer !== false && ! bff::security()->validateReferer($referer) ) { return false; } # CSRF token $user = $opts['userId'] ?? $request->userId(); if ( $user && ! bff::security()->validateToken() ) { return false; } return true; } /** * Проверка на частотность выполняемого действия * @param string $actionKey ключ выполняемого действия * @param array|int $opts доп. параметры или таймаут в секундах * @return bool true - частота отправки превышает допустимый лимит, false - все ок */ public function tooManyRequests($actionKey, $opts = []) { # Request $request = $opts['request'] ?? $this->getRequest(); if (! is_array($opts)) { $opts = ['timeout' => intval($opts)]; } $opts = array_merge([ 'silent' => false, # устанавливать ошибку 'timeout' => 20, # допустимая частота выполнения действия, в секундах 'limit' => 0, # допустимое кол-во повторов (0 без ограничений) 'userId' => $request->userId(), # ID текущего пользователя 'ip' => $request->remoteAddress(), # IP адрес запроса ], $opts); if ($opts['limit'] > 0) { return Site::i()->preventSpamCounter($actionKey, $opts['limit'], $opts['timeout'], !$opts['silent'], $opts); } return Site::i()->preventSpam($actionKey, $opts['timeout'], !$opts['silent'], $opts); } /** * Проверка корректности введенной капчи * @param string $actionKey ключ выполняемого действия * @param array $opts [value, inputKey, message, expire, request] * @return bool */ public function isCaptchaValid(string $actionKey, array $opts = []) { $opts['request'] = $opts['request'] ?? $this->getRequest(); return Captcha::valid($actionKey, $opts); } }