35 'messageservice.sender.add' => [__CLASS__,
'addSender'],
36 'messageservice.sender.delete' => [__CLASS__,
'deleteSender'],
37 'messageservice.sender.list' => [__CLASS__,
'getSenderList'],
39 'messageservice.message.status.update' => [__CLASS__,
'updateMessageStatus'],
40 'messageservice.message.status.get' => [__CLASS__,
'getMessageStatus'],
51 $fields = array_change_key_case($fields, CASE_UPPER);
52 if (empty($fields[
'APP_ID']))
57 if (!Loader::includeModule(
'rest'))
62 $dbRes = AppTable::getById($fields[
'APP_ID']);
63 $app = $dbRes->fetch();
70 $iterator = Internal\Entity\RestAppTable::getList([
72 'filter' => [
'=APP_ID' =>
$app[
'CLIENT_ID']]
75 while ($row = $iterator->fetch())
77 Internal\Entity\RestAppTable::delete($row[
'ID']);
87 static::onRestAppDelete($fields);
97 public static function addSender($params, $n, $server)
101 if(!$server->getClientId())
106 self::checkAdminPermissions();
107 $params = array_change_key_case($params, CASE_UPPER);
109 self::validateSender($params, $server);
111 $params[
'APP_ID'] = $server->getClientId();
113 $iterator = Internal\Entity\RestAppTable::getList([
116 '=APP_ID' => $params[
'APP_ID'],
117 '=CODE' => $params[
'CODE']
120 $result = $iterator->fetch();
123 throw new RestException(
'Sender already installed!', self::ERROR_SENDER_ALREADY_INSTALLED);
127 'NAME' => $params[
'NAME'],
128 'DESCRIPTION' => isset($params[
'DESCRIPTION']) ? $params[
'DESCRIPTION'] :
''
130 unset($params[
'NAME'], $params[
'DESCRIPTION']);
132 $params[
'AUTHOR_ID'] = $USER->getId();
133 $result = Internal\Entity\RestAppTable::add($params);
135 if ($result->getErrors())
137 throw new RestException(
'Sender save error!', self::ERROR_SENDER_ADD_FAILURE);
140 $senderLang[
'APP_ID'] = $result->getId();
141 static::addSenderLang($senderLang, $server->getClientId());
143 $app = \Bitrix\Rest\AppTable::getByClientId($params[
'APP_ID']);
148 'addProvider' . $params[
'TYPE'],
149 uniqid(
$app[
'CODE'],
true),
166 if(!$server->getClientId())
171 $params = array_change_key_case($params, CASE_UPPER);
172 self::checkAdminPermissions();
173 self::validateSenderCode($params[
'CODE']);
174 $params[
'APP_ID'] = $server->getClientId();
176 $iterator = Internal\Entity\RestAppTable::getList([
179 '=APP_ID' => $params[
'APP_ID'],
180 '=CODE' => $params[
'CODE']
183 $result = $iterator->fetch();
186 throw new RestException(
'Sender not found!', self::ERROR_SENDER_NOT_FOUND);
188 Internal\Entity\RestAppTable::delete($result[
'ID']);
189 Internal\Entity\RestAppLangTable::deleteByApp($result[
'ID']);
204 if(!$server->getClientId())
209 self::checkAdminPermissions();
210 $iterator = Internal\Entity\RestAppTable::getList([
211 'select' => [
'CODE'],
213 '=APP_ID' => $server->getClientId()
218 while ($row = $iterator->fetch())
220 $result[] = $row[
'CODE'];
235 if(!$server->getClientId())
240 $params = array_change_key_case($params, CASE_UPPER);
241 static::validateSenderCode($params[
'CODE']);
242 if (empty($params[
'MESSAGE_ID']))
244 throw new RestException(
'Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
247 $statusId = isset($params[
'STATUS']) ? Sender\Sms\Rest::resolveStatus($params[
'STATUS']) :
null;
248 if ($statusId ===
null || $statusId === MessageStatus::UNKNOWN)
250 throw new RestException(
'Message status incorrect!', self::ERROR_MESSAGE_STATUS_INCORRECT);
253 $message = Message::loadByExternalId(
255 $params[
'MESSAGE_ID'],
256 $server->getClientId().
'|'.$params[
'CODE']
260 throw new RestException(
'Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
263 if ($message->getAuthorId() !== static::getUserId())
265 static::checkAdminPermissions();
267 $message->updateStatus($statusId);
281 if (Loader::includeModule(
'intranet') && \
Bitrix\Intranet\Util::isExtranetUser(static::getUserId()))
286 $params = array_change_key_case($params, CASE_UPPER);
288 if (empty($params[
'MESSAGE_ID']) || !is_numeric($params[
'MESSAGE_ID']))
290 throw new RestException(
'Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
293 $message = Message::loadById($params[
'MESSAGE_ID']);
295 if ($message ===
null)
297 throw new RestException(
'Message not found!', self::ERROR_MESSAGE_NOT_FOUND);
300 $statusList = MessageStatus::getDescriptions(
'en');
302 return array_key_exists($message->getStatusId(), $statusList) ? $statusList[$message->getStatusId()] :
'';
305 private static function getUserId(): int
308 if (isset($USER) && $USER instanceof \CUser)
310 return (
int)$USER->getID();
315 private static function checkAdminPermissions()
320 || (!$USER->isAdmin() && !(Loader::includeModule(
'bitrix24') && \CBitrix24::isPortalAdmin($USER->getID())))
323 throw new AccessException();
327 private static function validateSender($data, $server)
329 if (!is_array($data) || empty($data))
331 throw new RestException(
'Empty data!', self::ERROR_SENDER_VALIDATION_FAILURE);
334 static::validateSenderCode($data[
'CODE']);
335 static::validateSenderHandler($data[
'HANDLER'], $server);
336 if (empty($data[
'NAME']))
338 throw new RestException(
'Empty sender NAME!', self::ERROR_SENDER_VALIDATION_FAILURE);
341 if (empty($data[
'TYPE']))
343 throw new RestException(
'Empty sender message TYPE!', self::ERROR_SENDER_VALIDATION_FAILURE);
346 if (!in_array($data[
'TYPE'], [
'SMS'],
true))
348 throw new RestException(
'Unknown sender message TYPE!', self::ERROR_SENDER_VALIDATION_FAILURE);
352 private static function validateSenderCode($code)
356 throw new RestException(
'Empty sender code!', self::ERROR_SENDER_VALIDATION_FAILURE);
358 if (!preg_match(
'#^[a-z0-9\.\-_]+$#i', $code))
360 throw new RestException(
'Wrong sender code!', self::ERROR_SENDER_VALIDATION_FAILURE);
364 private static function validateSenderHandler($handler, $server)
375 private static function getApp($server)
377 if(self::$app ==
null)
379 if (Loader::includeModule(
'rest'))
381 $result = AppTable::getList(
384 '=CLIENT_ID' => $server->getClientId()
388 self::$app = $result->fetch();
395 private static function addSenderLang($langFields, $clientId)
399 if (!is_array($langFields[
'NAME']))
402 'APP_ID' => $langFields[
'APP_ID'],
403 'LANGUAGE_ID' =>
'**',
404 'NAME' => $langFields[
'NAME'],
405 'DESCRIPTION' => is_scalar($langFields[
'DESCRIPTION']) ? (string)$langFields[
'DESCRIPTION'] : null
410 foreach ($langFields[
'NAME'] as $langId => $langName)
412 $langData[mb_strtolower($langId)] = [
413 'APP_ID' => $langFields[
'APP_ID'],
414 'LANGUAGE_ID' => mb_strtolower($langId),
415 'NAME' => $langFields[
'NAME'][$langId],
416 'DESCRIPTION' => is_array($langFields[
'DESCRIPTION']) && isset($langFields[
'DESCRIPTION'][$langId])
417 ? (string)$langFields[
'DESCRIPTION'][$langId] : null
420 if (!isset($langData[
'**']))
423 'APP_ID' => $langFields[
'APP_ID'],
424 'LANGUAGE_ID' =>
'**',
425 'NAME' => $langFields[
'NAME'][$langId],
426 'DESCRIPTION' => is_array($langFields[
'DESCRIPTION']) && isset($langFields[
'DESCRIPTION'][$langId])
427 ? (string)$langFields[
'DESCRIPTION'][$langId] : null
433 $appNames = static::getAppNames($clientId);
434 foreach ($appNames as $langId => $appName)
436 if (isset($langData[$langId]))
438 $langData[$langId][
'APP_NAME'] = $appName;
442 foreach ($langData as $toAdd)
444 Internal\Entity\RestAppLangTable::add($toAdd);
448 private static function getAppNames($clientId)
450 $iterator = \Bitrix\Rest\AppTable::getList(
453 '=CLIENT_ID' => $clientId
455 'select' => [
'ID',
'APP_NAME',
'CODE'],
458 $app = $iterator->fetch();
460 '**' =>
$app[
'APP_NAME'] ?
$app[
'APP_NAME'] :
$app[
'CODE']
463 $orm = \Bitrix\Rest\AppLangTable::getList([
465 '=APP_ID' =>
$app[
'ID']
467 'select' => [
'LANGUAGE_ID',
'MENU_NAME']
470 while ($row = $orm->fetch())
472 $result[mb_strtolower($row[
'LANGUAGE_ID'])] = $row[
'MENU_NAME'];
475 if (isset($result[LANGUAGE_ID]))
477 $result[
'**'] = $result[LANGUAGE_ID];