• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_rest.php
  • Класс: CIMRestService
  • Вызов: CIMRestService::chatUpdateAvatar
static function chatUpdateAvatar($arParams, $n, CRestServer $server)
{
	global $USER;

	$arParams = array_change_key_case($arParams, CASE_UPPER);

	if (isset($arParams['DIALOG_ID']))
	{
		if (BitrixImCommon::isChatId($arParams['DIALOG_ID']))
		{
			$arParams['CHAT_ID'] = BitrixImDialog::getChatId($arParams['DIALOG_ID']);
		}
		else
		{
			throw new BitrixRestRestException("Dialog ID can't be empty", "DIALOG_ID_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
		}
	}

	$arParams['CHAT_ID'] = intval($arParams['CHAT_ID']);

	if ($arParams['CHAT_ID'] <= 0)
	{
		throw new BitrixRestRestException("Chat ID can't be empty", "CHAT_ID_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
	}

	$userId = $USER->GetId();
	if ($server->getMethod() == mb_strtolower("imbot.chat.updateAvatar"))
	{
		$userId = self::getBotId($arParams, $server);
	}

	if (CIMChat::GetGeneralChatId() == $arParams['CHAT_ID'] && !CIMChat::CanSendMessageToGeneralChat($userId))
	{
		throw new BitrixRestRestException("Action unavailable", "ACCESS_ERROR", CRestServer::STATUS_FORBIDDEN);
	}

	if (!Chat::isActionAllowed('chat' . $arParams['CHAT_ID'], 'AVATAR'))
	{
		throw new BitrixRestRestException('The avatar of this chat cannot be changed', 'ACCESS_ERROR', CRestServer::STATUS_FORBIDDEN);
	}

	$arParams['AVATAR'] = CRestUtil::saveFile($arParams['AVATAR']);
	if (!$arParams['AVATAR'] || mb_strpos($arParams['AVATAR']['type'], "image/") !== 0)
	{
		throw new BitrixRestRestException("Avatar incorrect type", "AVATAR_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	$imageCheck = (new BitrixMainFileImage($arParams['AVATAR']["tmp_name"]))->getInfo();
	if(
		!$imageCheck
		|| !$imageCheck->getWidth()
		|| $imageCheck->getWidth() > 5000
		|| !$imageCheck->getHeight()
		|| $imageCheck->getHeight() > 5000
	)
	{
		throw new BitrixRestRestException("Avatar incorrect size (max 5000x5000)", "AVATAR_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	$arParams['AVATAR'] = CFile::saveFile($arParams['AVATAR'], 'im');

	$result = CIMDisk::UpdateAvatarId($arParams['CHAT_ID'], $arParams['AVATAR'], $userId);
	if (!$result)
	{
		throw new BitrixRestRestException("Chat isn't exists", "WRONG_REQUEST", CRestServer::STATUS_WRONG_REQUEST);
	}

	return true;
}