1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
restservice.php
См. документацию.
1<?php
2namespace Bitrix\MessageService;
3
4use \Bitrix\Main\Loader;
5use \Bitrix\Rest\AppTable;
6use Bitrix\Rest\HandlerHelper;
7use \Bitrix\Rest\RestException;
8use \Bitrix\Rest\AccessException;
9use CRestServer;
10
11Loader::includeModule('rest');
12
14{
15 const SCOPE = 'messageservice';
16 protected static $app;
17
18 const ERROR_UNSUPPORTED_PROTOCOL = 'ERROR_UNSUPPORTED_PROTOCOL';
19 const ERROR_WRONG_HANDLER_URL = 'ERROR_WRONG_HANDLER_URL';
20 const ERROR_HANDLER_URL_MATCH = 'ERROR_HANDLER_URL_MATCH';
21
22 const ERROR_SENDER_ALREADY_INSTALLED = 'ERROR_SENDER_ALREADY_INSTALLED';
23 const ERROR_SENDER_ADD_FAILURE = 'ERROR_SENDER_ADD_FAILURE';
24 const ERROR_SENDER_UPDATE_FAILURE = 'ERROR_SENDER_UPDATE_FAILURE';
25 const ERROR_SENDER_VALIDATION_FAILURE = 'ERROR_SENDER_VALIDATION_FAILURE';
26 const ERROR_SENDER_NOT_FOUND = 'ERROR_SENDER_NOT_FOUND';
27 const ERROR_SENDER_CODE_REQUIRED = 'ERROR_SENDER_CODE_REQUIRED';
28 const ERROR_SENDER_OTHER_PARAMS_REQUIRED = 'ERROR_SENDER_OTHER_PARAMS_REQUIRED';
29
30 const ERROR_MESSAGE_NOT_FOUND = 'ERROR_MESSAGE_NOT_FOUND';
31 const ERROR_MESSAGE_STATUS_INCORRECT = 'ERROR_MESSAGE_STATUS_INCORRECT';
32
33 public static function onRestServiceBuildDescription()
34 {
35 return [
36 static::SCOPE => [
37 'messageservice.sender.add' => [__CLASS__, 'addSender'],
38 'messageservice.sender.update' => [__CLASS__, 'updateSender'],
39 'messageservice.sender.delete' => [__CLASS__, 'deleteSender'],
40 'messageservice.sender.list' => [__CLASS__, 'getSenderList'],
41
42 'messageservice.message.status.update' => [__CLASS__, 'updateMessageStatus'],
43 'messageservice.message.status.get' => [__CLASS__, 'getMessageStatus'],
44 ]
45 ];
46 }
47
52 public static function onRestAppDelete(array $fields)
53 {
54 $fields = array_change_key_case($fields, CASE_UPPER);
55 if (empty($fields['APP_ID']))
56 {
57 return;
58 }
59
60 if (!Loader::includeModule('rest'))
61 {
62 return;
63 }
64
65 $dbRes = AppTable::getById($fields['APP_ID']);
66 $app = $dbRes->fetch();
67
68 if (!$app)
69 {
70 return;
71 }
72
74 'select' => ['ID'],
75 'filter' => ['=APP_ID' => $app['CLIENT_ID']]
76 ]);
77
78 while ($row = $iterator->fetch())
79 {
81 }
82 }
83
88 public static function onRestAppUpdate(array $fields)
89 {
90 static::onRestAppDelete($fields);
91 }
92
100 public static function addSender($params, $n, $server)
101 {
102 global $USER;
103
104 if(!$server->getClientId())
105 {
106 throw new AccessException("Application context required");
107 }
108
109 self::checkAdminPermissions();
110 $params = array_change_key_case($params, CASE_UPPER);
111
112 self::validateSender($params, $server);
113
114 $params['APP_ID'] = $server->getClientId();
115
117 'select' => ['ID'],
118 'filter' => [
119 '=APP_ID' => $params['APP_ID'],
120 '=CODE' => $params['CODE']
121 ]
122 ]);
123 $result = $iterator->fetch();
124 if ($result)
125 {
126 throw new RestException('Sender already installed!', self::ERROR_SENDER_ALREADY_INSTALLED);
127 }
128
129 $senderLang = [
130 'NAME' => $params['NAME'],
131 'DESCRIPTION' => isset($params['DESCRIPTION']) ? $params['DESCRIPTION'] : ''
132 ];
133 unset($params['NAME'], $params['DESCRIPTION']);
134
135 $params['AUTHOR_ID'] = $USER->getId();
137
138 if ($result->getErrors())
139 {
140 throw new RestException('Sender save error!', self::ERROR_SENDER_ADD_FAILURE);
141 }
142
143 $senderLang['APP_ID'] = $result->getId();
144 static::addSenderLang($senderLang, $server->getClientId());
145
147 if ($app['CODE'])
148 {
150 'messageservice',
151 'addProvider' . $params['TYPE'],
152 uniqid($app['CODE'], true),
153 $app['CODE']
154 );
155 }
156
157 return true;
158 }
159
167 public static function updateSender($params, $n, $server)
168 {
169 global $USER;
170
171 if (!$server->getClientId())
172 {
173 throw new AccessException("Application context required");
174 }
175
176 self::checkAdminPermissions();
177 $params = array_change_key_case($params, CASE_UPPER);
178
179 if (empty($params['CODE']))
180 {
181 throw new RestException('CODE is required!', self::ERROR_SENDER_CODE_REQUIRED);
182 }
183
184 if (empty($params['NAME']) && empty($params['DESCRIPTION']) && empty($params['HANDLER']))
185 {
186 throw new RestException('At least one other parameter is required!', self::ERROR_SENDER_OTHER_PARAMS_REQUIRED);
187 }
188
189 if (!empty($params['HANDLER']))
190 {
191 self::validateSenderHandler($params['HANDLER'], $server);
192 }
193
194 unset($params['TYPE']);
195
196 $params['APP_ID'] = $server->getClientId();
197
199 'select' => ['ID'],
200 'filter' => [
201 '=APP_ID' => $params['APP_ID'],
202 '=CODE' => $params['CODE']
203 ]
204 ]);
205 $result = $iterator->fetch();
206 if (!$result)
207 {
208 throw new RestException('Sender not found!', self::ERROR_SENDER_NOT_FOUND);
209 }
210
211 if (!empty($params['NAME']) || !empty($params['DESCRIPTION']))
212 {
213 $senderLang = [
214 'APP_ID' => $result['ID'],
215 ];
216
217 if (!empty($params['NAME']))
218 {
219 $senderLang['NAME'] = $params['NAME'];
220 }
221
222 if (!empty($params['DESCRIPTION']))
223 {
224 $senderLang['DESCRIPTION'] = $params['DESCRIPTION'];
225 }
226
227 unset($params['NAME'], $params['DESCRIPTION']);
228
229 static::updateSenderLang($senderLang, $server->getClientId());
230 }
231
232 $params['AUTHOR_ID'] = $USER->getId();
233 $params = array_filter($params);
235
236 if ($updateResult->getErrors())
237 {
238 throw new RestException('Sender update error!', self::ERROR_SENDER_UPDATE_FAILURE);
239 }
240
242 if ($app['CODE'])
243 {
245 'messageservice',
246 'updateProvider' . ($params['TYPE'] ?? ''),
247 uniqid($app['CODE'], true),
248 $app['CODE']
249 );
250 }
251
252 return true;
253 }
254
262 public static function deleteSender($params, $n, $server)
263 {
264 if(!$server->getClientId())
265 {
266 throw new AccessException("Application context required");
267 }
268
269 $params = array_change_key_case($params, CASE_UPPER);
270 self::checkAdminPermissions();
271 self::validateSenderCode($params['CODE']);
272 $params['APP_ID'] = $server->getClientId();
273
275 'select' => ['ID'],
276 'filter' => [
277 '=APP_ID' => $params['APP_ID'],
278 '=CODE' => $params['CODE']
279 ]
280 ]);
281 $result = $iterator->fetch();
282 if (!$result)
283 {
284 throw new RestException('Sender not found!', self::ERROR_SENDER_NOT_FOUND);
285 }
288
289 return true;
290 }
291
300 public static function getSenderList($params, $n, $server)
301 {
302 if(!$server->getClientId())
303 {
304 throw new AccessException("Application context required");
305 }
306
307 self::checkAdminPermissions();
309 'select' => ['CODE'],
310 'filter' => [
311 '=APP_ID' => $server->getClientId()
312 ]
313 ]);
314
315 $result = [];
316 while ($row = $iterator->fetch())
317 {
318 $result[] = $row['CODE'];
319 }
320 return $result;
321 }
322
331 public static function updateMessageStatus($params, $n, $server)
332 {
333 if(!$server->getClientId())
334 {
335 throw new AccessException("Application context required");
336 }
337
338 $params = array_change_key_case($params, CASE_UPPER);
339 static::validateSenderCode($params['CODE']);
340 if (empty($params['MESSAGE_ID']))
341 {
342 throw new RestException('Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
343 }
344
345 $statusId = isset($params['STATUS']) ? Sender\Sms\Rest::resolveStatus($params['STATUS']) : null;
346 if ($statusId === null || $statusId === MessageStatus::UNKNOWN)
347 {
348 throw new RestException('Message status incorrect!', self::ERROR_MESSAGE_STATUS_INCORRECT);
349 }
350
352 'rest',
353 $params['MESSAGE_ID'],
354 $server->getClientId().'|'.$params['CODE']
355 );
356 if (!$message)
357 {
358 throw new RestException('Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
359 }
360
361 if ($message->getAuthorId() !== static::getUserId())
362 {
363 static::checkAdminPermissions();
364 }
365 $message->updateStatus($statusId);
366
367 return true;
368 }
369
377 public static function getMessageStatus(array $params, int $n, CRestServer $server)
378 {
379 if (Loader::includeModule('intranet') && \Bitrix\Intranet\Util::isExtranetUser(static::getUserId()))
380 {
381 throw new AccessException("Extranet user denied access");
382 }
383
384 $params = array_change_key_case($params, CASE_UPPER);
385
386 if (empty($params['MESSAGE_ID']) || !is_numeric($params['MESSAGE_ID']))
387 {
388 throw new RestException('Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
389 }
390
391 $message = Message::loadById($params['MESSAGE_ID']);
392
393 if ($message === null)
394 {
395 throw new RestException('Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
396 }
397
399
400 return array_key_exists($message->getStatusId(), $statusList) ? $statusList[$message->getStatusId()] : '';
401 }
402
403 private static function getUserId(): int
404 {
405 global $USER;
406 if (isset($USER) && $USER instanceof \CUser)
407 {
408 return (int)$USER->getID();
409 }
410 return 0;
411 }
412
413 private static function checkAdminPermissions()
414 {
415 global $USER;
416 if (!isset($USER)
417 || !is_object($USER)
418 || (!$USER->isAdmin() && !(Loader::includeModule('bitrix24') && \CBitrix24::isPortalAdmin($USER->getID())))
419 )
420 {
421 throw new AccessException();
422 }
423 }
424
425 private static function validateSender($data, $server)
426 {
427 if (!is_array($data) || empty($data))
428 {
429 throw new RestException('Empty data!', self::ERROR_SENDER_VALIDATION_FAILURE);
430 }
431
432 static::validateSenderCode($data['CODE']);
433 static::validateSenderHandler($data['HANDLER'], $server);
434 if (empty($data['NAME']))
435 {
436 throw new RestException('Empty sender NAME!', self::ERROR_SENDER_VALIDATION_FAILURE);
437 }
438
439 if (empty($data['TYPE']))
440 {
441 throw new RestException('Empty sender message TYPE!', self::ERROR_SENDER_VALIDATION_FAILURE);
442 }
443
444 if (!in_array($data['TYPE'], ['SMS'], true))
445 {
446 throw new RestException('Unknown sender message TYPE!', self::ERROR_SENDER_VALIDATION_FAILURE);
447 }
448 }
449
450 private static function validateSenderCode($code)
451 {
452 if (empty($code))
453 {
454 throw new RestException('Empty sender code!', self::ERROR_SENDER_VALIDATION_FAILURE);
455 }
456 if (!preg_match('#^[a-z0-9\.\-_]+$#i', $code))
457 {
458 throw new RestException('Wrong sender code!', self::ERROR_SENDER_VALIDATION_FAILURE);
459 }
460 }
461
462 private static function validateSenderHandler($handler, $server)
463 {
465 }
466
473 private static function getApp($server)
474 {
475 if(self::$app == null)
476 {
477 if (Loader::includeModule('rest'))
478 {
479 $result = AppTable::getList(
480 [
481 'filter' => [
482 '=CLIENT_ID' => $server->getClientId()
483 ]
484 ]
485 );
486 self::$app = $result->fetch();
487 }
488 }
489
490 return self::$app;
491 }
492
493 private static function addSenderLang($langFields, $clientId)
494 {
495 $langData = [];
496
497 if (!is_array($langFields['NAME']))
498 {
499 $langData['**'] = [
500 'APP_ID' => $langFields['APP_ID'],
501 'LANGUAGE_ID' => '**',
502 'NAME' => $langFields['NAME'],
503 'DESCRIPTION' => is_scalar($langFields['DESCRIPTION']) ? (string)$langFields['DESCRIPTION'] : null
504 ];
505 }
506 else
507 {
508 foreach ($langFields['NAME'] as $langId => $langName)
509 {
510 $langData[mb_strtolower($langId)] = [
511 'APP_ID' => $langFields['APP_ID'],
512 'LANGUAGE_ID' => mb_strtolower($langId),
513 'NAME' => $langFields['NAME'][$langId],
514 'DESCRIPTION' => is_array($langFields['DESCRIPTION']) && isset($langFields['DESCRIPTION'][$langId])
515 ? (string)$langFields['DESCRIPTION'][$langId] : null
516 ];
517
518 if (!isset($langData['**']))
519 {
520 $langData['**'] = [
521 'APP_ID' => $langFields['APP_ID'],
522 'LANGUAGE_ID' => '**',
523 'NAME' => $langFields['NAME'][$langId],
524 'DESCRIPTION' => is_array($langFields['DESCRIPTION']) && isset($langFields['DESCRIPTION'][$langId])
525 ? (string)$langFields['DESCRIPTION'][$langId] : null
526 ];
527 }
528 }
529 }
530
531 $appNames = static::getAppNames($clientId);
532 foreach ($appNames as $langId => $appName)
533 {
534 if (isset($langData[$langId]))
535 {
536 $langData[$langId]['APP_NAME'] = $appName;
537 }
538 }
539
540 foreach ($langData as $toAdd)
541 {
542 Internal\Entity\RestAppLangTable::add($toAdd);
543 }
544 }
545
546 private static function updateSenderLang($langFields, $clientId)
547 {
548 $fields = ['NAME', 'DESCRIPTION'];
549
550 foreach ($fields as $field)
551 {
552 if (isset($langFields[$field]) && !is_array($langFields[$field]))
553 {
554 $langData['**'][$field] = $langFields[$field];
555 }
556
557 if (is_array($langFields[$field]))
558 {
559 foreach ($langFields[$field] as $lang => $name)
560 {
561 $langData[$lang][$field] = $name;
562 }
563 }
564 }
565
566 $appNames = static::getAppNames($clientId);
567 foreach ($appNames as $langId => $appName)
568 {
569 if (isset($langData[$langId]))
570 {
571 $langData[$langId]['APP_NAME'] = $appName;
572 }
573 }
574
575 foreach ($langData as $lang => $toUpdate)
576 {
577 if (empty(array_intersect_key($toUpdate, array_flip($fields))))
578 {
579 continue;
580 }
581
582 $toUpdate['APP_ID'] = $langFields['APP_ID'];
583 $toUpdate['LANGUAGE_ID'] = $lang;
584
585 $existingLang = Internal\Entity\RestAppLangTable::getList([
586 'select' => ['ID'],
587 'filter' => [
588 '=APP_ID' => $langFields['APP_ID'],
589 '=LANGUAGE_ID' => $lang
590 ]
591 ])->fetch();
592
593 if ($existingLang)
594 {
595 Internal\Entity\RestAppLangTable::update($existingLang['ID'], $toUpdate);
596 }
597 else
598 {
599 Internal\Entity\RestAppLangTable::add($toUpdate);
600 }
601 }
602 }
603
604 private static function getAppNames($clientId)
605 {
606 $iterator = \Bitrix\Rest\AppTable::getList(
607 [
608 'filter' => [
609 '=CLIENT_ID' => $clientId
610 ],
611 'select' => ['ID', 'APP_NAME', 'CODE'],
612 ]
613 );
614 $app = $iterator->fetch();
615 $result = [
616 '**' => $app['APP_NAME'] ? $app['APP_NAME'] : $app['CODE']
617 ];
618
619 $orm = \Bitrix\Rest\AppLangTable::getList([
620 'filter' => [
621 '=APP_ID' => $app['ID']
622 ],
623 'select' => ['LANGUAGE_ID', 'MENU_NAME']
624 ]);
625
626 while ($row = $orm->fetch())
627 {
628 $result[mb_strtolower($row['LANGUAGE_ID'])] = $row['MENU_NAME'];
629 }
630
631 if (isset($result[LANGUAGE_ID]))
632 {
633 $result['**'] = $result[LANGUAGE_ID];
634 }
635
636 return $result;
637 }
638}
static getList(array $parameters=array())
Определения datamanager.php:431
static delete($primary)
Определения datamanager.php:1644
static add(array $data)
Определения datamanager.php:877
static update($primary, array $data)
Определения datamanager.php:1256
static loadById(int $id)
Определения message.php:58
static loadByExternalId(string $senderId, string $externalId, ?string $from=null)
Определения message.php:71
static getDescriptions(?string $language=null)
Определения messagestatus.php:32
static onRestAppUpdate(array $fields)
Определения restservice.php:88
static onRestServiceBuildDescription()
Определения restservice.php:33
static onRestAppDelete(array $fields)
Определения restservice.php:52
const ERROR_SENDER_ADD_FAILURE
Определения restservice.php:23
static getSenderList($params, $n, $server)
Определения restservice.php:300
const ERROR_SENDER_UPDATE_FAILURE
Определения restservice.php:24
const ERROR_SENDER_VALIDATION_FAILURE
Определения restservice.php:25
const ERROR_SENDER_NOT_FOUND
Определения restservice.php:26
static updateSender($params, $n, $server)
Определения restservice.php:167
const ERROR_HANDLER_URL_MATCH
Определения restservice.php:20
const ERROR_MESSAGE_STATUS_INCORRECT
Определения restservice.php:31
const ERROR_SENDER_ALREADY_INSTALLED
Определения restservice.php:22
const ERROR_UNSUPPORTED_PROTOCOL
Определения restservice.php:18
const ERROR_SENDER_OTHER_PARAMS_REQUIRED
Определения restservice.php:28
static getMessageStatus(array $params, int $n, CRestServer $server)
Определения restservice.php:377
const ERROR_MESSAGE_NOT_FOUND
Определения restservice.php:30
static deleteSender($params, $n, $server)
Определения restservice.php:262
const ERROR_SENDER_CODE_REQUIRED
Определения restservice.php:27
static updateMessageStatus($params, $n, $server)
Определения restservice.php:331
const ERROR_WRONG_HANDLER_URL
Определения restservice.php:19
static addSender($params, $n, $server)
Определения restservice.php:100
static resolveStatus($serviceStatus)
Определения rest.php:222
static getByClientId($clientId)
Определения app.php:967
static checkCallback($handlerUrl, $appInfo=array(), $checkInstallUrl=true)
Определения handlerhelper.php:31
Определения rest.php:896
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
global $USER
Определения csv_new_run.php:40
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
if(!defined('SITE_ID')) $lang
Определения include.php:91
AddEventToStatFile($module, $action, $tag, $label, $action_type='', $user_id=null)
Определения tools.php:3976
$name
Определения menu_edit.php:35
$message
Определения payment.php:8
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
if( $guestStatuses !=='') if(!is_array($guestStatuses)) $statusList
Определения options.php:2065
$clientId
Определения seo_client.php:18
$n
Определения update_log.php:107
$dbRes
Определения yandex_detail.php:168
$iterator
Определения yandex_run.php:610
$fields
Определения yandex_run.php:501