• Модуль: mail
  • Путь к файлу: ~/bitrix/modules/mail/classes/general/domain2.php
  • Класс: CMailDomain2
  • Вызов: CMailDomain2::getDomainUsers
static function getDomainUsers($token, $domain, &$error, $resync = false)
{
	global $DB;

	$domain = trim(mb_strtolower($domain));
	$users = array();

	if (empty($domain))
	{
		$error = self::getErrorCode('no_domain');
		return null;
	}

	if (!$resync)
	{
		$res = BitrixMailInternalsDomainEmailTable::getList(array(
			'filter' => array(
				'=DOMAIN' => $domain,
			),
		));
		if ($res !== false)
		{
			while ($item = $res->fetch())
				$users[] = $item['LOGIN'];
		}
	}

	if ($resync || empty($users))
	{
		$users = array();

		$page = 0;
		do
		{
			$result = CMailYandex2::getDomainUsers($token, $domain, $per_page = 30, ++$page, $error);

			if ($result === false)
				break;

			foreach ($result['accounts'] as $email)
			{
				list($login, $emailDomain) = explode('@', $email['login'], 2);
				if ($emailDomain == $domain)
					$users[] = trim(mb_strtolower($login));
			}
		}
		while ($result['total'] > $per_page*$page);

		$users = array_unique($users);

		if (!$error)
		{
			$cached = array();

			$res = BitrixMailInternalsDomainEmailTable::getList(array(
				'filter' => array(
					'=DOMAIN' => $domain,
				),
			));
			if ($res !== false)
			{
				while ($item = $res->fetch())
					$cached[] = $item['LOGIN'];
			}

			foreach (array_diff($cached, $users) as $login)
			{
				BitrixMailInternalsDomainEmailTable::delete(array(
					'DOMAIN' => $domain,
					'LOGIN'  => $login,
				));
			}
			foreach (array_diff($users, $cached) as $login)
			{
				BitrixMailInternalsDomainEmailTable::add(array(
					'DOMAIN' => $domain,
					'LOGIN'  => $login,
				));
			}
		}
	}

	if (empty($users) && $error)
	{
		$error = self::getErrorCode($error);
		return null;
	}
	else
	{
		sort($users);
		return $users;
	}
}