• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_rest.php
  • Класс: CIMRestService
  • Вызов: CIMRestService::appRegister
static function appRegister($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);

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

	if (!isset($arParams['CODE']) || empty($arParams['CODE']))
	{
		throw new BitrixRestRestException("App code isn't specified", "CODE_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	$iframe = '';
	$iframeWidth = 0;
	$iframeHeight = 0;
	$iframePopup = 'N';
	$hash = '';

	$js = '';
	if (
		isset($arParams['JS_METHOD']) && in_array($arParams['JS_METHOD'], Array('PUT', 'SEND', 'CALL', 'SUPPORT')) &&
		isset($arParams['JS_PARAM']) && !empty($arParams['JS_PARAM'])
	)
	{
		if ($arParams['JS_METHOD'] == 'SEND')
		{
			if (preg_match('//([a-zA-Z0-9-_+]+)((s)?([a-zA-Z0-9-_+]+))+/im', $arParams['JS_PARAM'], $matches))
			{
				$js = "BXIM.sendMessage('".$matches[0]."');";
			}
		}
		else if ($arParams['JS_METHOD'] == 'PUT')
		{
			if (preg_match('//([a-zA-Z0-9-_+]+)((s)?([a-zA-Z0-9-_+]+))+/im', $arParams['JS_PARAM'], $matches))
			{
				$js = "BXIM.putMessage('".$matches[0]."');";
			}
		}
		else if ($arParams['JS_METHOD'] == 'CALL')
		{
			if (preg_match('/+?[ -d+()#]+$/im', $arParams['JS_PARAM'], $matches))
			{
				$js = "BXIM.phoneTo('".$matches[0]."');";
			}
		}
		else if ($arParams['JS_METHOD'] == 'SUPPORT')
		{
			if (preg_match('/[a-f0-9]{32}$/im', $arParams['JS_PARAM'], $matches))
			{
				$js = "BXIM.openMessenger('networkLines".$matches[0]."');";
			}
		}
	}
	else if (isset($arParams['IFRAME']) && !empty($arParams['IFRAME']))
	{
		$check = parse_url($arParams['IFRAME']);
		if (!isset($check['scheme']) && !isset($check['host']))
		{
			throw new BitrixRestRestException("Iframe params must be HTTPS site", "IFRAME_HTTPS", CRestServer::STATUS_WRONG_REQUEST);
		}
		else if ($check['scheme'] != 'https' || empty($check['host']))
		{
			throw new BitrixRestRestException("Iframe params must be HTTPS site", "IFRAME_HTTPS", CRestServer::STATUS_WRONG_REQUEST);
		}
		$iframe = $arParams['IFRAME'];
		$iframeWidth = 320;
		if (isset($arParams['IFRAME_WIDTH']))
		{
			$iframeWidth = intval($arParams['IFRAME_WIDTH']) > 250? $arParams['IFRAME_WIDTH']: 250;
		}
		$iframeHeight = 250;
		if (isset($arParams['IFRAME_HEIGHT']))
		{
			$iframeHeight = intval($arParams['IFRAME_HEIGHT']) > 50? $arParams['IFRAME_HEIGHT']: 50;
		}
		$iframePopup = isset($arParams['IFRAME_POPUP']) && $arParams['IFRAME_POPUP'] == 'Y'? 'Y': 'N';

		if (!isset($arParams['HASH']) || empty($arParams['HASH']))
		{
			throw new BitrixRestRestException("Hash can't be empty", "HASH_ERROR", CRestServer::STATUS_WRONG_REQUEST);
		}
		$hash = mb_substr($arParams['HASH'], 0, 32);
	}

	if (!$iframe && !$js)
	{
		throw new BitrixRestRestException("Iframe or JS method isn't specified", "PARAMS_ERROR", CRestServer::STATUS_WRONG_REQUEST);
	}

	$iconId = 0;
	if (isset($arParams['ICON_FILE']) && $arParams['ICON_FILE'])
	{
		$iconFile = CRestUtil::saveFile($arParams['ICON_FILE']);
		$imageCheck = (new BitrixMainFileImage($iconFile["tmp_name"]))->getInfo();
		if(
			!$imageCheck
			|| !$imageCheck->getWidth()
			|| $imageCheck->getWidth() > 5000
			|| !$imageCheck->getHeight()
			|| $imageCheck->getHeight() > 5000
		)
		{
			$iconFile = null;
		}
		if ($iconFile && mb_strpos($iconFile['type'], "image/") === 0)
		{
			$iconFile['MODULE_ID'] = 'imbot';
			$iconId = CFile::saveFile($iconFile, 'imbot');
		}
	}


	$context = isset($arParams['CONTEXT'])? $arParams['CONTEXT']: 'ALL';
	$hidden = isset($arParams['HIDDEN']) && $arParams['HIDDEN'] == 'Y'? 'Y': 'N';
	$extranetSupport = isset($arParams['EXTRANET_SUPPORT']) && $arParams['EXTRANET_SUPPORT'] == 'Y'? 'Y': 'N';
	$livechatSupport = isset($arParams['LIVECHAT_SUPPORT']) && $arParams['LIVECHAT_SUPPORT'] == 'Y'? 'Y': 'N';

	$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);
	}

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

	$id = BitrixImApp::register(Array(
									   'APP_ID' => $clientId,
									   'BOT_ID' => $arParams['BOT_ID'],
									   'CODE' => $arParams['CODE'],
									   'ICON_ID' => $iconId,
									   'HASH' => $hash,
									   'CONTEXT' => $context,
									   'HIDDEN' => $hidden,
									   'REGISTERED' => 'Y',
									   'JS' => $js,
									   'IFRAME' => $iframe,
									   'IFRAME_HEIGHT' => $iframeHeight,
									   'IFRAME_WIDTH' => $iframeWidth,
									   'IFRAME_POPUP' => $iframePopup,
									   'EXTRANET_SUPPORT' => $extranetSupport,
									   'LIVECHAT_SUPPORT' => $livechatSupport,
									   'MODULE_ID' => 'rest',
									   'LANG' => $arParams['LANG'],
								   ));
	if (!$id)
	{
		throw new BitrixRestRestException("App can't be registered".var_export(Array(
																					  'APP_ID' => $clientId,
																					  'BOT_ID' => $arParams['BOT_ID'],
																					  'CODE' => $arParams['CODE'],
																					  'ICON_ID' => $iconId,
																					  'HASH' => $hash,
																					  'CONTEXT' => $context,
																					  'HIDDEN' => $hidden,
																					  'JS' => $js,
																					  'IFRAME' => $iframe,
																					  'IFRAME_HEIGHT' => $iframeHeight,
																					  'IFRAME_WIDTH' => $iframeWidth,
																					  'IFRAME_POPUP' => $iframePopup,
																					  'EXTRANET_SUPPORT' => $extranetSupport,
																					  'LIVECHAT_SUPPORT' => $livechatSupport,
																					  'MODULE_ID' => 'rest',
																					  'LANG' => $arParams['LANG'],
																				  ),1), "WRONG_REQUEST", CRestServer::STATUS_WRONG_REQUEST);
	}

	return $id;
}