• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_rest.php
  • Класс: CIMRestService
  • Вызов: CIMRestService::commandRegister
static function commandRegister($arParams, $n, CRestServer $server)
{
	if ($server->getAuthType() == BitrixRestSessionAuthAuth::AUTH_TYPE)
	{
		throw new BitrixRestRestException("Access for this method not allowed by session authorization.", "WRONG_AUTH_TYPE", CRestServer::STATUS_FORBIDDEN);
	}
	$arParams = array_change_key_case($arParams, CASE_UPPER);

	$customClientId = false;
	$clientId = $server->getClientId();
	if (!$clientId)
	{
		if (!empty($arParams['CLIENT_ID']))
		{
			$customClientId = true;
			$clientId = 'custom'.$arParams['CLIENT_ID'];
		}
		else
		{
			throw new BitrixRestAccessException("Client ID not specified");
		}
	}

	if ($customClientId)
	{
		$arApp = ['ID' => '', 'CLIENT_ID' => $arParams['CLIENT_ID']];
	}
	else
	{
		$dbRes = BitrixRestAppTable::getList(array('filter' => array('=CLIENT_ID' => $clientId)));
		$arApp = $dbRes->fetch();
	}

	if (isset($arParams['EVENT_COMMAND_ADD']) && !empty($arParams['EVENT_COMMAND_ADD']))
	{
		if ($customClientId)
		{
			$arParams['EVENT_COMMAND_ADD'] = $arParams['EVENT_COMMAND_ADD'].(mb_strpos($arParams['EVENT_COMMAND_ADD'], '?') === false? '?': '&').'CLIENT_ID='.$arParams['CLIENT_ID'];
		}
		try
		{
			BitrixRestHandlerHelper::checkCallback($arParams['EVENT_COMMAND_ADD'], $arApp);
		}
		catch(Exception $e)
		{
			throw new BitrixRestRestException($e->getMessage(), "EVENT_COMMAND_ADD_ERROR", CRestServer::STATUS_WRONG_REQUEST);
		}
	}
	else
	{
		throw new BitrixRestRestException("Handler for "Command add" event isn't specified", "EVENT_COMMAND_ADD_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	if (!isset($arParams['COMMAND']) || empty($arParams['COMMAND']))
	{
		throw new BitrixRestRestException("Command isn't specified", "COMMAND_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	$arParams['BOT_ID'] = intval($arParams['BOT_ID']);
	if ($arParams['BOT_ID'] > 0)
	{
		$bots = BitrixImBot::getListCache();
		if (!isset($bots[$arParams['BOT_ID']]))
		{
			throw new BitrixRestRestException("Bot not found", "BOT_ID_ERROR", CRestServer::STATUS_WRONG_REQUEST);
		}
		if ($bots[$arParams['BOT_ID']]['APP_ID'] != $clientId)
		{
			throw new BitrixRestRestException("Bot was installed by another rest application", "APP_ID_ERROR", CRestServer::STATUS_WRONG_REQUEST);
		}
	}
	else
	{
		throw new BitrixRestRestException("Bot not found", "BOT_ID_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}
	$arParams['COMMON'] = isset($arParams['COMMON']) && $arParams['COMMON'] == 'Y'? 'Y': 'N';
	$arParams['HIDDEN'] = isset($arParams['HIDDEN']) && $arParams['HIDDEN'] == 'Y'? 'Y': 'N';
	$arParams['EXTRANET_SUPPORT'] = isset($arParams['EXTRANET_SUPPORT']) && $arParams['EXTRANET_SUPPORT'] == 'Y'? 'Y': 'N';

	if (!isset($arParams['LANG']) || empty($arParams['LANG']))
	{
		throw new BitrixRestRestException("Lang set can't be empty", "LANG_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	$commandId = BitrixImCommand::register(Array(
												  'APP_ID' => $clientId,
												  'BOT_ID' => $arParams['BOT_ID'],
												  'COMMAND' => $arParams['COMMAND'],
												  'COMMON' => $arParams['COMMON'],
												  'HIDDEN' => $arParams['HIDDEN'],
												  'SONET_SUPPORT' => $arParams['SONET_SUPPORT'],
												  'EXTRANET_SUPPORT' => $arParams['EXTRANET_SUPPORT'],
												  'MODULE_ID' => 'rest',
												  'LANG' => $arParams['LANG'],
											  ));
	if ($commandId)
	{
		self::unbindEvent($arApp['ID'], $arApp['CLIENT_ID'], 'im', 'onImCommandAdd', 'onImCommandAdd', true);
		self::bindEvent($arApp['ID'], $arApp['CLIENT_ID'], 'im', 'onImCommandAdd', 'onImCommandAdd', $arParams['EVENT_COMMAND_ADD']);
	}
	else
	{
		throw new BitrixRestRestException("Command can't be created", "WRONG_REQUEST", CRestServer::STATUS_WRONG_REQUEST);
	}

	return $commandId;
}