1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
controller.php
См. документацию.
1<?php
2
4
13
14Loc::loadMessages(__FILE__);
15
16abstract class Controller
17{
18 const EVENT_ON_BEFORE_ACTION = 'onBeforeAction';
19
20 const ERROR_REQUIRED_PARAMETER = 'REPORT_CONTROLLER_22001';
21 const ERROR_UNKNOWN_ACTION = 'REPORT_CONTROLLER_22002';
22
23 const STATUS_SUCCESS = 'success';
24 const STATUS_DENIED = 'denied';
25 const STATUS_ERROR = 'error';
26 const STATUS_NEED_AUTH = 'need_auth';
27 const STATUS_INVALID_SIGN = 'invalid_sign';
28 const STATUS_RESTRICTION = 'restriction';
29
31 protected $action;
35 protected $realActionName;
39 protected $request;
40
44 public function __construct()
45 {
46 $this->errorCollection = new ErrorCollection;
47 $this->request = Context::getCurrent()->getRequest();
48
49 $this->init();
50 }
51
57 protected function init()
58 {}
59
65 protected function end()
66 {
68 \CMain::finalActions();
69 die;
70 }
71
77 public function exec()
78 {
79 try
80 {
81 $this->resolveAction();
82 $this->checkAction();
83
84 $this->checkRequiredModules();
85
86 if(!$this->prepareParams())
87 {
88 $this->sendJsonErrorResponse();
89 }
90
91 $action = $this->getAction();
92 if(
93 $this->processBeforeAction($action) === true &&
94 $this->triggerOnBeforeAction($action) === true
95 )
96 {
97 $this->runAction();
98 }
99 }
100 catch(\Exception $e)
101 {
102 $this->runProcessingException($e);
103 }
104 }
105
114 protected function triggerOnBeforeAction($action)
115 {
116 $event = new Event('report', static::EVENT_ON_BEFORE_ACTION . $action, array(
117 'action' => $action,
118 'controller' => $this,
119 ));
120 $event->send($this);
121
122 if($event->getResults())
123 {
124 foreach($event->getResults() as $eventResult)
125 {
126 if($eventResult->getType() != EventResult::SUCCESS)
127 {
128 return false;
129 }
130 }
131 }
132
133 return true;
134 }
135
140 protected function getUser()
141 {
142 global $USER;
143
144 return $USER;
145 }
146
153 protected function sendJsonResponse($response, $params = null)
154 {
155 if(!defined('PUBLIC_AJAX_MODE'))
156 {
157 define('PUBLIC_AJAX_MODE', true);
158 }
159
160 global $APPLICATION;
161 $APPLICATION->restartBuffer();
162
163 if(!empty($params['http_status']) && $params['http_status'] == 403)
164 {
165 header('HTTP/1.0 403 Forbidden', true, 403);
166 }
167 if(!empty($params['http_status']) && $params['http_status'] == 500)
168 {
169 header('HTTP/1.0 500 Internal Server Error', true, 500);
170 }
171 if(!empty($params['http_status']) && $params['http_status'] == 510)
172 {
173 header('HTTP/1.0 510 Not Extended', true, 510);
174 }
175
176 header('Content-Type:application/json; charset=UTF-8');
177 echo Json::encode($response);
178
179 $this->end();
180 }
181
186 protected function sendJsonErrorResponse()
187 {
188 $errors = array();
189 foreach($this->getErrors() as $error)
190 {
192 $errors[] = array(
193 'message' => $error->getMessage(),
194 'code' => $error->getCode(),
195 );
196 }
197 unset($error);
198 $this->sendJsonResponse(array(
199 'status' => self::STATUS_ERROR,
200 'errors' => $errors,
201 ));
202 }
203
209 protected function sendJsonAccessDeniedResponse($message = '')
210 {
211 $this->sendJsonResponse(array(
212 'status' => self::STATUS_DENIED,
213 'message' => $message,
214 ));
215 }
216
222 protected function sendJsonInvalidSignResponse($message = '')
223 {
224 $this->sendJsonResponse(array(
225 'status' => self::STATUS_INVALID_SIGN,
226 'message' => $message,
227 ));
228 }
229
236 {
237 $response['status'] = self::STATUS_SUCCESS;
239 }
240
247 protected function sendResponse($response)
248 {
249 global $APPLICATION;
250 $APPLICATION->restartBuffer();
251
252 echo $response;
253
254 $this->end();
255 }
256
261 public function getErrors()
262 {
263 return $this->errorCollection->toArray();
264 }
265
271 public function getErrorByCode($code)
272 {
273 return $this->errorCollection->getErrorByCode($code);
274 }
275
281 protected function resolveAction()
282 {
283 $listOfActions = $this->normalizeListOfAction($this->listActions());
284 $action = mb_strtolower($this->action);
285
286 if(!isset($listOfActions[$action]))
287 {
288 $this->errorCollection->add(array(new Error(Loc::getMessage('REPORT_CONTROLLER_ERROR_UNKNOWN_ACTION',
289 array('#ACTION#' => $action)), self::ERROR_UNKNOWN_ACTION)));
290 return $this;
291 }
292
293 $this->realActionName = $action;
294 $description = $listOfActions[$this->realActionName];
295 $this->setAction($description['name'], $description);
296
297 return $this;
298 }
299
300 //todo refactor BaseComponent + Controller normalizeListOfAction, resolveAction.
301 //you can use composition in BaseComponent
307 protected function normalizeListOfAction(array $listOfActions)
308 {
309 $normalized = array();
310 foreach($listOfActions as $action => $description)
311 {
312 if(!is_string($action))
313 {
315 }
316 else
317 {
319 }
320 }
321 unset($action, $description);
322
323 return array_change_key_case($normalized, CASE_LOWER);
324 }
325
343 {
344 if(!is_array($description))
345 {
347 'method' => array('GET'),
348 'name' => $description,
349 'check_csrf_token' => false,
350 'redirect_on_auth' => true,
351 'close_session' => false,
352 );
353 }
354 if(empty($description['name']))
355 {
356 $description['name'] = $action;
357 }
358 if(!isset($description['redirect_on_auth']))
359 {
360 $description['redirect_on_auth'] = false;
361 }
362 if(!isset($description['close_session']))
363 {
364 $description['close_session'] = false;
365 }
366
367 return $description;
368 }
369
375 protected function checkAction()
376 {
377 if($this->errorCollection->count())
378 {
379 $this->sendJsonErrorResponse();
380 }
382
383 if(!$this->getUser() || !$this->getUser()->getId())
384 {
385 if($description['redirect_on_auth'])
386 {
387 LocalRedirect(SITE_DIR . 'auth/?backurl=' .
388 urlencode(Application::getInstance()->getContext()->getRequest()->getRequestUri()));
389 }
390 else
391 {
393 }
394 }
395
396 //if does not exist check_csrf_token we have to check csrf for only POST method.
397 if(
398 (isset($description['check_csrf_token']) && $description['check_csrf_token'] === true) ||
399 ($this->request->isPost() && !isset($description['check_csrf_token'])))
400 {
401 //in BDisk we have token_sid
402 if(!check_bitrix_sessid() && !check_bitrix_sessid('token_sid'))
403 {
405 }
406 }
407
408 if(!in_array($this->request->getRequestMethod(), $description['method']))
409 {
410 $this->sendJsonAccessDeniedResponse('Wrong method for current action');
411 }
412 }
413
446 protected function listActions()
447 {
448 return array();
449 }
450
455 public function getAction()
456 {
457 return $this->action;
458 }
459
464 public function getActionDescription()
465 {
466 return $this->actionDescription;
467 }
468
476 {
477 $this->action = $action;
478 $this->actionDescription = $description;
479
480 return $this;
481 }
482
488 public function setActionName($action)
489 {
490 $this->action = $action;
491 return $this;
492 }
493
498 protected function checkRequiredModules()
499 {}
500
505 protected function prepareParams()
506 {
507 return true;
508 }
509
515 protected function processBeforeAction($actionName)
516 {
517 return true;
518 }
519
520 protected function runAction()
521 {
523 if($description['close_session'] === true)
524 {
525 //todo be careful by using this features.
526 session_write_close();
527 }
528 $actionMethod = 'processAction' . $this->getAction();
529
530 return $this->$actionMethod();
531 }
532
538 protected function runProcessingException(\Exception $e)
539 {
540// throw $e;
541 $this->errorCollection->add(array(new Error($e->getMessage())));
542 $this->sendJsonErrorResponse();
543 }
544
550 {
552 }
553
559 {
560 $this->sendJsonAccessDeniedResponse('Wrong csrf token');
561 }
562
567 protected function getApplication()
568 {
569 global $APPLICATION;
570 return $APPLICATION;
571 }
572
580 protected function checkRequiredInputParams(array $inputParams, array $required)
581 {
582 foreach ($required as $item)
583 {
584 if(!isset($inputParams[$item]) || (!$inputParams[$item] &&
585 !(is_string($inputParams[$item]) && mb_strlen($inputParams[$item]))))
586 {
587 $this->errorCollection->add(array(new Error(
588 Loc::getMessage('REPORT_CONTROLLER_ERROR_REQUIRED_PARAMETER',
589 array('#PARAM#' => $item)), self::ERROR_REQUIRED_PARAMETER)));
590 return false;
591 }
592 }
593
594 return true;
595 }
596
603 protected function checkRequiredPostParams(array $required)
604 {
605 $params = array();
606 foreach($required as $item)
607 {
608 $params[$item] = $this->request->getPost($item);
609 }
610 unset($item);
611
612 return $this->checkRequiredInputParams($params, $required);
613 }
614
621 protected function checkRequiredGetParams(array $required)
622 {
623 $params = array();
624 foreach($required as $item)
625 {
626 $params[$item] = $this->request->getQuery($item);
627 }
628 unset($item);
629
630 return $this->checkRequiredInputParams($params, $required);
631 }
632
639 protected function checkRequiredFilesParams(array $required)
640 {
641 $params = array();
642 foreach($required as $item)
643 {
644 $params[$item] = $this->request->getFile($item);
645 }
646 unset($item);
647
648 return $this->checkRequiredInputParams($params, $required);
649 }
650
655 protected function isAjaxRequest()
656 {
657 return $this->request->isAjaxRequest();
658 }
659}
global $APPLICATION
Определения include.php:80
static getInstance()
Определения application.php:98
Определения error.php:15
Определения event.php:5
Определения json.php:9
sendJsonSuccessResponse(array $response=array())
Определения controller.php:235
const STATUS_INVALID_SIGN
Определения controller.php:27
runProcessingIfUserNotAuthorized()
Определения controller.php:549
const EVENT_ON_BEFORE_ACTION
Определения controller.php:18
sendJsonInvalidSignResponse($message='')
Определения controller.php:222
triggerOnBeforeAction($action)
Определения controller.php:114
const ERROR_REQUIRED_PARAMETER
Определения controller.php:20
processBeforeAction($actionName)
Определения controller.php:515
checkRequiredFilesParams(array $required)
Определения controller.php:639
setAction($action, array $description)
Определения controller.php:475
sendResponse($response)
Определения controller.php:247
setActionName($action)
Определения controller.php:488
const ERROR_UNKNOWN_ACTION
Определения controller.php:21
runProcessingException(\Exception $e)
Определения controller.php:538
sendJsonResponse($response, $params=null)
Определения controller.php:153
normalizeActionDescription($action, $description)
Определения controller.php:342
checkRequiredPostParams(array $required)
Определения controller.php:603
checkRequiredGetParams(array $required)
Определения controller.php:621
runProcessingIfInvalidCsrfToken()
Определения controller.php:558
normalizeListOfAction(array $listOfActions)
Определения controller.php:307
checkRequiredInputParams(array $inputParams, array $required)
Определения controller.php:580
sendJsonAccessDeniedResponse($message='')
Определения controller.php:209
const STATUS_RESTRICTION
Определения controller.php:28
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if(Loader::includeModule( 'bitrix24')) elseif(Loader::includeModule('intranet') &&CIntranetUtils::getPortalZone() !=='ru') $description
Определения .description.php:24
$errors
Определения iblock_catalog_edit.php:74
global $USER
Определения csv_new_run.php:40
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
const SITE_DIR(!defined('LANG'))
Определения include.php:72
check_bitrix_sessid($varname='sessid')
Определения tools.php:4686
LocalRedirect($url, $skip_security_check=false, $status="302 Found")
Определения tools.php:4005
Определения culture.php:9
getErrors()
Определения errorableimplementation.php:34
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
die
Определения quickway.php:367
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$response
Определения result.php:21
$error
Определения subscription_card_product.php:20
$action
Определения file_dialog.php:21