• Модуль: imopenlines
  • Путь к файлу: ~/bitrix/modules/imopenlines/lib/rest.php
  • Класс: BitrixImOpenLinesRest
  • Вызов: Rest::widgetUserRegister
static function widgetUserRegister($params, $n, CRestServer $server)
{
	if ($server->getAuthType() != WidgetAuth::AUTH_TYPE)
	{
		throw new RestException('Access for this method allowed only by livechat authorization.', 'WRONG_AUTH_TYPE', CRestServer::STATUS_FORBIDDEN);
	}

	$params = array_change_key_case($params, CASE_UPPER);

	$params['CONFIG_ID'] = (int)$params['CONFIG_ID'];
	if ($params['CONFIG_ID'] <= 0)
	{
		throw new RestException('Config id is not specified.', 'CONFIG_ID_EMPTY', CRestServer::STATUS_WRONG_REQUEST);
	}

	$config = ModelConfigTable::getById($params['CONFIG_ID'])->fetch();
	if (!$config)
	{
		throw new RestException('Config is not found.', 'CONFIG_NOT_FOUND', CRestServer::STATUS_WRONG_REQUEST);
	}

	if (
		$_SESSION['LIVECHAT']['REGISTER']
		&& !(
			isset($params['USER_HASH']) && trim($params['USER_HASH']) && preg_match("/^[a-fA-F0-9]{32}$/i", $params['USER_HASH'])
		)
	)
	{
		$params['USER_HASH'] = $_SESSION['LIVECHAT']['REGISTER']['hash'];
	}

	$userDataFields = [
		'NAME' => $params['NAME'],
		'LAST_NAME' => $params['LAST_NAME'],
		'AVATAR' => $params['AVATAR'],
		'EMAIL' => $params['EMAIL'],
		'PERSONAL_WWW' => $params['WWW'],
		'PERSONAL_GENDER' => $params['GENDER'],
		'WORK_POSITION' => $params['POSITION'],
		'USER_HASH' => $params['USER_HASH'],
	];
	$userData = WidgetUser::register($userDataFields);
	if (!$userData)
	{
		throw new RestException(
			WidgetUser::getError()->msg,
			WidgetUser::getError()->code,
			CRestServer::STATUS_WRONG_REQUEST
		);
	}

	$dialogData = WidgetDialog::register($userData['ID'], $config['ID']);
	if (!$dialogData)
	{
		throw new RestException(
			WidgetDialog::getError()->msg,
			WidgetDialog::getError()->code,
			CRestServer::STATUS_WRONG_REQUEST
		);
	}

	WidgetAuth::authorizeById($userData['ID'], true, true);

	$result = [
		'id' => (int)$userData['ID'],
		'hash' => $userData['HASH'],
		'chatId' => (int)$dialogData['CHAT_ID'],
		'dialogId' => 'chat'.$dialogData['CHAT_ID'],
		'userConsent' => false,
	];

	$_SESSION['LIVECHAT']['REGISTER'] = $result;

	self::checkWelcomeFormNeeded($params, (int)$dialogData['CHAT_ID']);

	WidgetCache::set($userData['ID'], [
		'TRACE_DATA' => (string)$params['TRACE_DATA'],
 		'CUSTOM_DATA' => (string)$params['CUSTOM_DATA'],
	]);

	return $result;
}