7if(!\
Bitrix\Main\Loader::includeModule(
'rest'))
13class Rest extends \IRestService
19 'pull.application.config.get' => array(
'callback' => array(__CLASS__,
'applicationConfigGet')),
20 'pull.application.event.add' => array(
'callback' => array(__CLASS__,
'applicationEventAdd')),
21 'pull.application.push.add' => array(
'callback' => array(__CLASS__,
'applicationPushAdd')),
22 'pull.watch.extend' => array(
'callback' => array(__CLASS__,
'watchExtend'),
'options' => array()),
24 'pull_channel' => array(
25 'pull.config.get' => array(
'callback' => array(__CLASS__,
'configGet'),
'options' => array()),
26 'pull.channel.public.get' => array(
'callback' => array(__CLASS__,
'channelPublicGet'),
'options' => array()),
27 'pull.channel.public.list' => array(
'callback' => array(__CLASS__,
'channelPublicList'),
'options' => array()),
30 'mobile.counter.types.get' => array(
'callback' => array(__CLASS__,
'counterTypesGet'),
'options' => array()),
31 'mobile.counter.get' => array(
'callback' => array(__CLASS__,
'counterGet'),
'options' => array()),
32 'mobile.counter.config.get' => array(
'callback' => array(__CLASS__,
'counterConfigGet'),
'options' => array()),
33 'mobile.counter.config.set' => array(
'callback' => array(__CLASS__,
'counterConfigSet'),
'options' => array()),
35 'mobile.push.types.get' => array(
'callback' => array(__CLASS__,
'pushTypesGet'),
'options' => array()),
36 'mobile.push.config.get' => array(
'callback' => array(__CLASS__,
'pushConfigGet'),
'options' => array()),
37 'mobile.push.config.set' => array(
'callback' => array(__CLASS__,
'pushConfigSet'),
'options' => array()),
38 'mobile.push.status.get' => array(
'callback' => array(__CLASS__,
'pushStatusGet'),
'options' => array()),
39 'mobile.push.status.set' => array(
'callback' => array(__CLASS__,
'pushStatusSet'),
'options' => array()),
40 'mobile.push.smartfilter.status.get' => array(
'callback' => array(__CLASS__,
'pushSmartfilterStatusGet'),
'options' => array()),
41 'mobile.push.smartfilter.status.set' => array(
'callback' => array(__CLASS__,
'pushSmartfilterStatusSet'),
'options' => array()),
48 $params = array_change_key_case($params, CASE_UPPER);
50 $type = \CPullChannel::TYPE_PRIVATE;
51 if ($params[
'APPLICATION'] ===
'Y')
53 $clientId = $server->getClientId();
56 throw new \Bitrix\Rest\RestException(
"Get application public channel available only for application authorization.",
"WRONG_AUTH_TYPE", \CRestServer::STATUS_WRONG_REQUEST);
61 $userId = (int)$params[
'USER_ID'];
63 $configParams = Array();
64 $configParams[
'TYPE'] = $type;
65 $configParams[
'USER_ID'] = $userId;
66 $configParams[
'JSON'] =
true;
68 $config = \Bitrix\Pull\Channel::getPublicId($configParams);
71 throw new \Bitrix\Rest\RestException(
"Push & Pull server is not configured",
"SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
79 $params = array_change_key_case($params, CASE_UPPER);
81 $type = \CPullChannel::TYPE_PRIVATE;
82 if ($params[
'APPLICATION'] ===
'Y')
84 $clientId = $server->getClientId();
87 throw new \Bitrix\Rest\RestException(
"Get application public channel available only for application authorization.",
"WRONG_AUTH_TYPE", \CRestServer::STATUS_WRONG_REQUEST);
93 if (is_string($params[
'USERS']))
95 $params[
'USERS'] = \CUtil::JsObjectToPhp($params[
'USERS']);
97 if (is_array($params[
'USERS']))
99 foreach ($params[
'USERS'] as $userId)
101 $userId = (int)$userId;
104 $users[$userId] = $userId;
111 throw new \Bitrix\Rest\RestException(
"A wrong format for the USERS field is passed",
"INVALID_FORMAT", \CRestServer::STATUS_WRONG_REQUEST);
114 $configParams = Array();
115 $configParams[
'TYPE'] = $type;
116 $configParams[
'USERS'] = $users;
117 $configParams[
'JSON'] =
true;
119 $config = \Bitrix\Pull\Channel::getPublicIds($configParams);
122 throw new \Bitrix\Rest\RestException(
"Push & Pull server is not configured",
"SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
130 $params = array_change_key_case($params, CASE_UPPER);
132 $clientId = $server->getClientId();
135 throw new \Bitrix\Rest\RestException(
"Get access to application config available only for application authorization.",
"WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
138 $configParams = Array();
139 $configParams[
'CACHE'] = $params[
'CACHE'] !==
'N';
140 $configParams[
'REOPEN'] = $params[
'REOPEN'] !==
'N';
141 $configParams[
'CUSTOM_TYPE'] = $clientId;
142 $configParams[
'JSON'] =
true;
144 $config = \Bitrix\Pull\Config::get($configParams);
147 throw new \Bitrix\Rest\RestException(
"Push & Pull server is not configured",
"SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
155 $params = array_change_key_case($params, CASE_UPPER);
157 $clientId = $server->getClientId();
160 throw new \Bitrix\Rest\RestException(
"Get access to application config available only for application authorization.",
"WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
168 if (is_string($params[
'USER_ID']))
170 $params[
'USER_ID'] = \CUtil::JsObjectToPhp($params[
'USER_ID']);
173 if (!isset($params[
'USER_ID']))
175 $users = [Event::SHARED_CHANNEL];
177 else if (is_array($params[
'USER_ID']))
179 foreach ($params[
'USER_ID'] as $userId)
181 $userId = intval($userId);
184 $users[$userId] = $userId;
187 $users = array_values($users);
191 $users = (int)$params[
'USER_ID'];
196 if (isset($params[
'USER_ID']))
198 if ($params[
'USER_ID'] === $USER->GetId())
200 $users = $USER->GetId();
204 throw new \Bitrix\Rest\RestException(
"Only admin can send notifications to other channels",
"USER_ID_ACCESS_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
209 $users = [Event::SHARED_CHANNEL];
213 if (isset($params[
'MODULE_ID']))
215 $moduleId = $params[
'MODULE_ID'];
216 if (preg_match(
"/[^a-z0-9._]/", $moduleId))
218 throw new \Bitrix\Rest\RestException(
"Module ID format error",
"MODULE_ID_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
223 $moduleId =
'application';
226 $command = $params[
'COMMAND'];
227 if (!preg_match(
"/^[\d\w:_|\.\-]+$/", $command))
229 throw new \Bitrix\Rest\RestException(
"Command format error",
"COMMAND_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
232 if (isset($params[
'PARAMS']))
234 if (is_string($params[
'PARAMS']))
236 $params[
'PARAMS'] = \CUtil::JsObjectToPhp($params[
'PARAMS']);
239 $eventParams = $params[
'PARAMS'];
240 if (!is_array($eventParams))
242 throw new \Bitrix\Rest\RestException(
"Params format error",
"PARAMS_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
250 Event::add($users, array(
251 'module_id' => $moduleId,
252 'command' => $command,
253 'params' => $eventParams,
261 $params = array_change_key_case($params, CASE_UPPER);
263 $clientId = $server->getClientId();
266 throw new \Bitrix\Rest\RestException(
"Send push notifications available only for application authorization.",
"WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
269 if (!self::isAdmin())
271 throw new \Bitrix\Rest\RestException(
"You do not have access to send push notifications",
"ACCESS_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
275 if (is_string($params[
'USER_ID']))
277 $params[
'USER_ID'] = \CUtil::JsObjectToPhp($params[
'USER_ID']);
280 if (is_array($params[
'USER_ID']))
282 foreach ($params[
'USER_ID'] as $userId)
284 $userId = intval($userId);
287 $users[$userId] = $userId;
290 $users = array_values($users);
294 $users = (int)$params[
'USER_ID'];
297 if (!$params[
'TEXT'] || $params[
'TEXT'] ==
'')
299 throw new \Bitrix\Rest\RestException(
"Text can't be empty",
"TEXT_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
302 $result = \Bitrix\Rest\AppTable::getList(
305 '=CLIENT_ID' => $clientId
310 'APP_NAME_DEFAULT' =>
'LANG_DEFAULT.MENU_NAME',
314 if (empty($result[
'APP_NAME']))
316 throw new \Bitrix\Rest\RestException(
"For send push-notification application name can't be empty",
"EMPTY_APP_NAME", \CRestServer::STATUS_WRONG_REQUEST);
319 $appName = $result[
'APP_NAME'];
321 $text = $params[
'TEXT'];
324 if ($params[
'AVATAR'])
327 $params[
'AVATAR'] = $parsedUrl->getUri();
328 if ($params[
'AVATAR'])
330 $avatar = $params[
'AVATAR'];
343 "avatarUrl"=> $avatar,
344 "senderName" => $appName,
345 "senderMessage" => $text,
353 public static function configGet($params, $n, \CRestServer $server)
355 $params = array_change_key_case($params, CASE_UPPER);
357 if ($server->getAuthType() === \
Bitrix\
Rest\OAuth\Auth::AUTH_TYPE)
359 throw new \Bitrix\Rest\RestException(
"Method not available for OAuth authorization.",
"WRONG_AUTH_TYPE", \CRestServer::STATUS_FORBIDDEN);
363 $guestMode = defined(
"PULL_USER_ID") && (int)PULL_USER_ID != 0;
364 if($server->getAuthType() === GuestAuth::AUTH_TYPE && $guestMode)
366 $userId = (int)PULL_USER_ID;
368 else if ($USER->IsAuthorized())
370 $userId = $USER->getId();
374 throw new \Bitrix\Rest\RestException(
"Method not available for guest session at the moment.",
"AUTHORIZE_ERROR", \CRestServer::STATUS_FORBIDDEN);
377 $configParams = Array();
378 $configParams[
'USER_ID'] = $userId;
379 $configParams[
'CACHE'] = !isset($params[
'CACHE']) || $params[
'CACHE'] !==
'N';
380 $configParams[
'REOPEN'] = !isset($params[
'REOPEN']) || $params[
'REOPEN'] !==
'N';
381 $configParams[
'JSON'] =
true;
383 $config = \Bitrix\Pull\Config::get($configParams);
386 throw new \Bitrix\Rest\RestException(
"Push & Pull server is not configured",
"SERVER_ERROR", \CRestServer::STATUS_INTERNAL);
389 $config[
'serverTime'] = date(
'c', time());
394 public static function watchExtend($params, $n, \CRestServer $server)
396 $params = array_change_key_case($params, CASE_UPPER);
398 if(is_string($params[
'TAGS']))
400 $params[
'TAGS'] = \CUtil::JsObjectToPhp($params[
'TAGS']);
404 $userId = $USER->GetID();
406 return \CPullWatch::Extend($userId, array_values($params[
'TAGS']));
411 $types = \Bitrix\Pull\MobileCounter::getTypes();
413 if (isset($params[
'USER_VALUES']) && $params[
'USER_VALUES'] ===
'Y')
415 $config = \Bitrix\Pull\MobileCounter::getConfig();
416 foreach ($types as $type => $value)
418 $types[$type][
'VALUE'] = $config[$type];
423 foreach ($types as $type)
425 $result[] = array_change_key_case($type, CASE_LOWER);
431 public static function counterGet($params, $n, \CRestServer $server)
433 return \Bitrix\Pull\MobileCounter::get();
439 $config = \Bitrix\Pull\MobileCounter::getConfig();
440 foreach ($config as $type => $value)
453 $params = array_change_key_case($params, CASE_UPPER);
455 if(is_string($params[
'CONFIG']))
457 $params[
'CONFIG'] = \CUtil::JsObjectToPhp($params[
'CONFIG']);
460 if (!is_array($params[
'CONFIG']) || empty($params[
'CONFIG']))
462 throw new \Bitrix\Rest\RestException(
"New config is not specified",
"CONFIG_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
465 \Bitrix\Pull\MobileCounter::setConfig($params[
'CONFIG']);
473 $params = array_change_key_case($params, CASE_UPPER);
475 $userConfig = Array();
476 $config = \Bitrix\Pull\Push::getTypes();
478 $withUserValues =
false;
479 if (isset($params[
'USER_VALUES']) && $params[
'USER_VALUES'] ===
'Y')
481 $withUserValues =
true;
482 $userConfig = \Bitrix\Pull\Push::getConfig();
486 foreach ($config as $moduleId => $module)
490 foreach ($module[
'TYPES'] as $typeId => $typeConfig)
494 $typeConfig[
'VALUE'] = $userConfig[$moduleId][$typeId];
496 $types[] = array_change_key_case($typeConfig, CASE_LOWER);
498 $module[
'TYPES'] = $types;
500 $result[] = array_change_key_case($module, CASE_LOWER);
503 \Bitrix\Main\Type\Collection::sortByColumn($result, array(
'module_id' => array(SORT_STRING, SORT_ASC)));
511 $config = \Bitrix\Pull\Push::getConfig();
517 foreach ($config as $moduleId => $module)
519 foreach ($module as $typeId => $typeValue)
522 'module_id' => $moduleId,
524 'active' => $typeValue
533 $params = array_change_key_case($params, CASE_UPPER);
535 if(is_string($params[
'CONFIG']))
537 $params[
'CONFIG'] = \CUtil::JsObjectToPhp($params[
'CONFIG']);
540 if (!is_array($params[
'CONFIG']) || empty($params[
'CONFIG']))
542 throw new \Bitrix\Rest\RestException(
"New config is not specified",
"CONFIG_ERROR", \CRestServer::STATUS_WRONG_REQUEST);
545 $newConfig = Array();
546 foreach ($params[
'CONFIG'] as $config)
549 !isset($config[
'module_id']) || empty($config[
'module_id'])
550 || !isset($config[
'type']) || empty($config[
'type'])
551 || !isset($config[
'active'])
557 $newConfig[$config[
'module_id']][$config[
'type']] = (bool)$config[
'active'];
560 \Bitrix\Pull\Push::setConfig($newConfig);
567 return \Bitrix\Pull\Push::getStatus();
572 $params = array_change_key_case($params, CASE_UPPER);
574 $status = (bool)$params[
'ACTIVE'];
575 \Bitrix\Pull\Push::setStatus($status);
582 return \Bitrix\Pull\PushSmartfilter::getStatus();
587 $params = array_change_key_case($params, CASE_UPPER);
589 $status = (bool)$params[
'ACTIVE'];
591 \Bitrix\Pull\PushSmartfilter::setStatus($status);
598 throw new \Bitrix\Rest\RestException(
"Method isn't implemented yet",
"NOT_IMPLEMENTED", \CRestServer::STATUS_NOT_FOUND);
601 private static function isAdmin()
static includeModule($moduleName)
static onRestServiceBuildDescription()
static counterConfigSet($params, $n, \CRestServer $server)
static pushConfigGet($params, $n, \CRestServer $server)
static pushConfigSet($params, $n, \CRestServer $server)
static channelPublicList($params, $n, \CRestServer $server)
static channelPublicGet($params, $n, \CRestServer $server)
static counterGet($params, $n, \CRestServer $server)
static applicationEventAdd($params, $n, \CRestServer $server)
static pushSmartfilterStatusGet($params, $n, \CRestServer $server)
static notImplemented($params, $n, \CRestServer $server)
static pushSmartfilterStatusSet($params, $n, \CRestServer $server)
static pushStatusGet($params, $n, \CRestServer $server)
static configGet($params, $n, \CRestServer $server)
static pushStatusSet($params, $n, \CRestServer $server)
static pushTypesGet($params, $n, \CRestServer $server)
static counterTypesGet($params, $n, \CRestServer $server)
static watchExtend($params, $n, \CRestServer $server)
static applicationPushAdd($params, $n, \CRestServer $server)
static counterConfigGet($params, $n, \CRestServer $server)
static applicationConfigGet($params, $n, \CRestServer $server)