7use \Bitrix\Main\ErrorCollection;
9use \Bitrix\Main\Application;
10use \Bitrix\Main\Context;
14use \Bitrix\Main\Localization\Loc;
15use \Bitrix\Main\Web\Json;
16use Bitrix\Tasks\AccessDeniedException;
43 private $collectDebugInfo = 1;
59 $this->errorCollection->add(array(
new Error($e->getMessage())));
62 if (!$this->errorCollection->isEmpty())
64 $this->sendJsonErrorResponse();
81 protected function end()
86 \CMain::finalActions();
102 if ($this->
prepareParams() && $this->errorCollection->isEmpty() && $this->processBeforeAction($this->getAction()) ===
true)
110 $this->errorCollection->add(array(
new Error($e->getMessage())));
113 if (!$this->errorCollection->isEmpty())
115 $this->sendJsonErrorResponse();
163 if(!defined(
'PUBLIC_AJAX_MODE'))
165 define(
'PUBLIC_AJAX_MODE',
true);
169 $APPLICATION->restartBuffer();
171 if(!empty($params[
'http_status']) && $params[
'http_status'] == 403)
173 header(
'HTTP/1.0 403 Forbidden',
true, 403);
175 if(!empty($params[
'http_status']) && $params[
'http_status'] == 500)
177 header(
'HTTP/1.0 500 Internal Server Error',
true, 500);
179 if(!empty($params[
'http_status']) && $params[
'http_status'] == 510)
181 header(
'HTTP/1.0 510 Not Extended',
true, 510);
184 header(
'Content-Type:application/json; charset=UTF-8');
185 echo Json::encode($response);
194 protected function sendJsonErrorResponse()
201 'message' => $error->getMessage(),
202 'code' => $error->getCode(),
206 $this->sendJsonResponse(array(
207 'status' => self::STATUS_ERROR,
220 'status' => self::STATUS_DENIED,
221 'message' => $message,
232 $response[
'status'] = self::STATUS_SUCCESS;
247 $APPLICATION->restartBuffer();
260 return $this->errorCollection->toArray();
271 return $this->errorCollection->getErrorByCode($code);
281 $listOfActions = array_change_key_case($this->
listActions(), CASE_LOWER);
282 $action = mb_strtolower($this->action);
284 if(!isset($listOfActions[
$action]))
286 'VOTE_CONTROLLER_ERROR_UNKNOWN_ACTION',
287 array(
'#ACTION#' => $this->sanitizeActionName($action))
290 $this->realActionName = $action;
293 $this->
setAction($description[
'name'], $description);
298 private function sanitizeActionName($actionName)
300 if(!preg_match(
'/^[a-zA-Z0-9]+$/i', $actionName))
325 $description = array_merge(
327 'method' => array(
'GET'),
328 'name' => (is_string($description) && $description <>
'' ? $description :
$action),
330 'check_sessid' =>
true,
331 'redirect_on_auth' =>
true
333 (is_array($description) ? $description : array())
335 $description[
"method"] = array_intersect(is_array($description[
"method"]) ? $description[
"method"] : array($description[
"method"]), array(
"GET",
"POST"));
348 if ($description[
"need_auth"] && (!$this->
getUser() || !$this->
getUser()->getId()))
350 if ($description[
"redirect_on_auth"])
352 LocalRedirect(SITE_DIR .
'auth/?backurl=' . urlencode(
Application::getInstance()->getContext()->getRequest()->getRequestUri()));
360 if (!in_array($this->request->getRequestMethod(), $description[
'method']))
363 if ($description[
'check_sessid'] && !check_bitrix_sessid())
394 return $this->action;
403 return $this->actionDescription;
415 $this->actionDescription = $description;
456 if (! method_exists($this,
'processAction' . $this->
getAction()))
458 return call_user_func(array($this,
'processAction' . $this->
getAction()));
481 foreach ($required as $item)
483 if(!isset($inputParams[$item]) || (!$inputParams[$item] && !(is_string($inputParams[$item]) && mb_strlen($inputParams[$item]))))
485 $this->errorCollection->add(array(
new Error(
Loc::getMessage(
'VOTE_CONTROLLER_ERROR_REQUIRED_PARAMETER', array(
'#PARAM#' => $item)), self::ERROR_REQUIRED_PARAMETER)));
502 foreach($required as $item)
504 $params[$item] = $this->request->getPost($item);
520 foreach($required as $item)
522 $params[$item] = $this->request->getQuery($item);
538 foreach($required as $item)
540 $params[$item] = $this->request->getFile($item);
553 return $this->request->isAjaxRequest();
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
sendJsonSuccessResponse(array $response=array())
const EVENT_ON_BEFORE_ACTION
const ERROR_REQUIRED_PARAMETER
processBeforeAction($actionName)
checkRequiredFilesParams(array $required)
setAction($action, array $description)
const ERROR_UNKNOWN_ACTION
sendJsonResponse($response, $params=null)
normalizeActionDescription($action, $description)
checkRequiredPostParams(array $required)
checkRequiredGetParams(array $required)
checkRequiredInputParams(array $inputParams, array $required)
sendJsonAccessDeniedResponse($message='')