1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
controller.php
См. документацию.
1<?php
2
4
12
13Loc::loadMessages(__FILE__);
14
20abstract class Controller implements IErrorable
21{
22 const ERROR_REQUIRED_PARAMETER = 'LISTS_CONTROLLER_22001';
23 const ERROR_UNKNOWN_ACTION = 'LISTS_CONTROLLER_22002';
24
25 const STATUS_SUCCESS = 'success';
26 const STATUS_PROCESSING = 'processing';
27 const STATUS_COMPLETED = 'completed';
28 const STATUS_DENIED = 'denied';
29 const STATUS_ERROR = 'error';
30 const STATUS_NEED_AUTH = 'need_auth';
31 const STATUS_INVALID_SIGN = 'invalid_sign';
32
34 protected $action;
38 protected $realActionName;
42 protected $request;
43
44 public function __construct()
45 {
46 $this->errorCollection = new ErrorCollection;
47 $this->request = Context::getCurrent()->getRequest();
48 }
49
50 protected function end()
51 {
52 include($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/epilog_after.php");
53 die;
54 }
55
56 public function exec()
57 {
58 try
59 {
60 $this->resolveAction();
61 $this->checkAction();
62
63 $this->checkRequiredModules();
64
65 if(!$this->prepareParams())
66 {
67 $this->sendJsonErrorResponse();
68 }
69
70 //todo create Event!
71 if($this->processBeforeAction($this->getAction()) !== false)
72 {
73 $this->runAction();
74 }
75 }
76 catch(\Exception $e)
77 {
78 $this->runProcessingException($e);
79 }
80 }
81
85 protected function getUser()
86 {
87 global $USER;
88 return $USER;
89 }
90
91 protected function sendJsonResponse($response, $params = null)
92 {
93 if(!defined('PUBLIC_AJAX_MODE'))
94 {
95 define('PUBLIC_AJAX_MODE', true);
96 }
97
98 global $APPLICATION;
99 $APPLICATION->restartBuffer();
100
101 if(!empty($params['http_status']) && $params['http_status'] == 403)
102 {
103 header('HTTP/1.0 403 Forbidden', true, 403);
104 }
105 if(!empty($params['http_status']) && $params['http_status'] == 500)
106 {
107 header('HTTP/1.0 500 Internal Server Error', true, 500);
108 }
109
110 header('Content-Type:application/json; charset=UTF-8');
111 echo Json::encode($response);
112
113 $this->end();
114 }
115
116 protected function sendJsonErrorResponse()
117 {
118 $errors = array();
119 foreach($this->getErrors() as $error)
120 {
122 $errors[] = array(
123 'message' => $error->getMessage(),
124 'code' => $error->getCode(),
125 );
126 }
127 unset($error);
128 $this->sendJsonResponse(array(
129 'status' => self::STATUS_ERROR,
130 'errors' => $errors,
131 ));
132 }
133
134 protected function sendJsonAccessDeniedResponse($message = '')
135 {
136 $this->sendJsonResponse(array(
137 'status' => self::STATUS_DENIED,
138 'message' => $message,
139 ));
140 }
141
142 protected function sendJsonInvalidSignResponse($message = '')
143 {
144 $this->sendJsonResponse(array(
145 'status' => self::STATUS_INVALID_SIGN,
146 'message' => $message,
147 ));
148 }
149
151 {
152 $response['status'] = self::STATUS_SUCCESS;
154 }
155
157 {
158 $response['status'] = self::STATUS_PROCESSING;
160 }
161
163 {
164 $response['status'] = self::STATUS_COMPLETED;
166 }
167
168 protected function sendResponse($response)
169 {
170 global $APPLICATION;
171 $APPLICATION->restartBuffer();
172
173 echo $response;
174
175 $this->end();
176 }
177
181 public function getErrors()
182 {
183 return $this->errorCollection->toArray();
184 }
185
189 public function getErrorsByCode($code)
190 {
191 return $this->errorCollection->getErrorsByCode($code);
192 }
193
197 public function getErrorByCode($code)
198 {
199 return $this->errorCollection->getErrorByCode($code);
200 }
201
202 protected function resolveAction()
203 {
204 $listOfActions = $this->normalizeListOfAction($this->listOfActions());
205 $action = mb_strtolower($this->action);
206
207 if(!isset($listOfActions[$action]))
208 {
209 $this->errorCollection->add(array(new Error(Loc::getMessage('LISTS_CONTROLLER_ERROR_UNKNOWN_ACTION', array('#ACTION#' => $action)), self::ERROR_UNKNOWN_ACTION)));
210 return $this;
211 }
212
213 $this->realActionName = $action;
214 $description = $listOfActions[$this->realActionName];
215 $this->setAction($description['name'], $description);
216
217 return $this;
218 }
219
220 //todo refactor BaseComponent + Controller normalizeListOfAction, resolveAction.
221 //you can use composition in BaseComponent
222 protected function normalizeListOfAction(array $listOfActions)
223 {
224 $normalized = array();
225 foreach($listOfActions as $action => $description)
226 {
227 if(!is_string($action))
228 {
230 }
231 else
232 {
234 }
235 }
236 unset($action, $description);
237
238 return array_change_key_case($normalized, CASE_LOWER);
239 }
240
242 {
243 if(!is_array($description))
244 {
246 'method' => array('GET'),
247 'name' => $description,
248 'check_csrf_token' => false,
249 'redirect_on_auth' => true,
250 'close_session' => false,
251 );
252 }
253 if(empty($description['name']))
254 {
255 $description['name'] = $action;
256 }
257 if(!isset($description['redirect_on_auth']))
258 {
259 $description['redirect_on_auth'] = false;
260 }
261 if(!isset($description['close_session']))
262 {
263 $description['close_session'] = false;
264 }
265
266 return $description;
267 }
268
269 protected function checkAction()
270 {
271 if($this->errorCollection->hasErrors())
272 {
273 $this->sendJsonErrorResponse();
274 }
276
277 if(!$this->getUser() || !$this->getUser()->getId())
278 {
279 if($description['redirect_on_auth'])
280 {
281 LocalRedirect(SITE_DIR . 'auth/?backurl=' . urlencode(Application::getInstance()->getContext()->getRequest()->getRequestUri()));
282 }
283 else
284 {
286 }
287 }
288
289 //if does not exist check_csrf_token we have to check csrf for only POST method.
290 if(($description['check_csrf_token'] ?? false) === true || ($this->request->isPost() && !isset($description['check_csrf_token'])))
291 {
292 //in BDisk we have token_sid
293 if(!check_bitrix_sessid() && !check_bitrix_sessid('token_sid'))
294 {
296 }
297 }
298
299 if(!in_array($this->request->getRequestMethod(), $description['method']))
300 {
301 $this->sendJsonAccessDeniedResponse('Wrong method for current action');
302 }
303 }
304
305 protected function listOfActions()
306 {
307 return array();
308 }
309
313 public function getAction()
314 {
315 return $this->action;
316 }
317
321 public function getActionDescription()
322 {
323 return $this->actionDescription;
324 }
325
332 {
333 $this->action = $action;
334 $this->actionDescription = $description;
335
336 return $this;
337 }
338
343 public function setActionName($action)
344 {
345 $this->action = $action;
346 return $this;
347 }
348
349 protected function checkRequiredModules()
350 {}
351
352 protected function prepareParams()
353 {
354 return true;
355 }
356
362 protected function processBeforeAction($actionName)
363 {
364 return true;
365 }
366
367 protected function runAction()
368 {
370 if($description['close_session'] === true)
371 {
372 //todo be careful by using this features.
373 session_write_close();
374 }
375 $actionMethod = 'processAction' . $this->getAction();
376
377 return $this->$actionMethod();
378 }
379
380 protected function runProcessingException(\Exception $e)
381 {
382// throw $e;
383 $this->errorCollection->add(array(new Error($e->getMessage())));
384 $this->sendJsonErrorResponse();
385 }
386
388 {
390 }
391
393 {
394 $this->sendJsonAccessDeniedResponse('Wrong csrf token');
395 }
396
400 protected function getApplication()
401 {
402 global $APPLICATION;
403 return $APPLICATION;
404 }
405
411 protected function checkRequiredInputParams(array $inputParams, array $required)
412 {
413 foreach ($required as $item)
414 {
415 if(!isset($inputParams[$item]) || (!$inputParams[$item] && !(is_string($inputParams[$item]) && mb_strlen($inputParams[$item]))))
416 {
417 $this->errorCollection->add(array(new Error(Loc::getMessage('LISTS_CONTROLLER_ERROR_REQUIRED_PARAMETER', array('#PARAM#' => $item)), self::ERROR_REQUIRED_PARAMETER)));
418 return false;
419 }
420 }
421
422 return true;
423 }
424
425 protected function checkRequiredPostParams(array $required)
426 {
427 $params = array();
428 foreach($required as $item)
429 {
430 $params[$item] = $this->request->getPost($item);
431 }
432 unset($item);
433
434 return $this->checkRequiredInputParams($params, $required);
435 }
436
437 protected function checkRequiredGetParams(array $required)
438 {
439 $params = array();
440 foreach($required as $item)
441 {
442 $params[$item] = $this->request->getQuery($item);
443 }
444 unset($item);
445
446 return $this->checkRequiredInputParams($params, $required);
447 }
448
449 protected function checkRequiredFilesParams(array $required)
450 {
451 $params = array();
452 foreach($required as $item)
453 {
454 $params[$item] = $this->request->getFile($item);
455 }
456 unset($item);
457
458 return $this->checkRequiredInputParams($params, $required);
459 }
460
465 protected function isAjaxRequest()
466 {
467 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
468 }
469}
const BX_ROOT
Определения bx_root.php:3
global $APPLICATION
Определения include.php:80
sendJsonCompletedResponse(array $response=array())
Определения controller.php:162
sendJsonSuccessResponse(array $response=array())
Определения controller.php:150
const STATUS_INVALID_SIGN
Определения controller.php:31
runProcessingIfUserNotAuthorized()
Определения controller.php:387
sendJsonInvalidSignResponse($message='')
Определения controller.php:142
const ERROR_REQUIRED_PARAMETER
Определения controller.php:22
processBeforeAction($actionName)
Определения controller.php:362
const STATUS_PROCESSING
Определения controller.php:26
checkRequiredFilesParams(array $required)
Определения controller.php:449
setAction($action, array $description)
Определения controller.php:331
sendResponse($response)
Определения controller.php:168
setActionName($action)
Определения controller.php:343
const ERROR_UNKNOWN_ACTION
Определения controller.php:23
runProcessingException(\Exception $e)
Определения controller.php:380
const STATUS_COMPLETED
Определения controller.php:27
sendJsonResponse($response, $params=null)
Определения controller.php:91
sendJsonProcessingResponse(array $response=array())
Определения controller.php:156
normalizeActionDescription($action, $description)
Определения controller.php:241
checkRequiredPostParams(array $required)
Определения controller.php:425
checkRequiredGetParams(array $required)
Определения controller.php:437
runProcessingIfInvalidCsrfToken()
Определения controller.php:392
normalizeListOfAction(array $listOfActions)
Определения controller.php:222
checkRequiredInputParams(array $inputParams, array $required)
Определения controller.php:411
sendJsonAccessDeniedResponse($message='')
Определения controller.php:134
getErrorByCode($code)
Определения controller.php:197
const STATUS_NEED_AUTH
Определения controller.php:30
getErrorsByCode($code)
Определения controller.php:189
static getInstance()
Определения application.php:98
Определения error.php:15
Определения json.php:9
</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
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
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
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