• Модуль: mail
  • Путь к файлу: ~/bitrix/modules/mail/lib/user.php
  • Класс: BitrixMailUser
  • Вызов: User::getForwardTo
static function getForwardTo($siteId, $userId, $entityType)
{
	$cache = new CPHPCache();

	$cacheKey = sprintf('%s_%s', $userId, $entityType);
	$cacheDir = sprintf('/mail/user/forward/%s', bin2hex($siteId));

	if ($cache->initCache(365*24*3600, $cacheKey, $cacheDir))
	{
		$forwardTo = $cache->getVars();
	}
	else
	{
		$userRelation = UserRelationsTable::getList(array(
			'filter' => array(
				'=SITE_ID'     => $siteId,
				'=USER_ID'     => $userId,
				'=ENTITY_TYPE' => $entityType,
				'=ENTITY_ID'   => null
			)
		))->fetch();

		if (empty($userRelation))
		{
			$userRelation = array(
				'SITE_ID'     => $siteId,
				'TOKEN'       => base_convert(md5(time().MainSecurityRandom::getBytes(6)), 16, 36),
				'USER_ID'     => $userId,
				'ENTITY_TYPE' => $entityType
			);

			if (!UserRelationsTable::add($userRelation)->isSuccess())
				return false;

			// for dav addressbook modification label
			$user = new CUser;
			$user->update($userId, array());
		}

		$site   = MainSiteTable::getByPrimary($siteId)->fetch();
		$domain = $site['SERVER_NAME'] ?: COption::getOptionString('main', 'server_name', '');

		if (preg_match('/^(?.+):(?d+)$/', $domain, $matches))
			$domain = $matches['domain'];

		$forwardTo = sprintf('fwd%s@%s', $userRelation['TOKEN'], $domain);

		$cache->startDataCache();
		$cache->endDataCache($forwardTo);
	}

	return array($forwardTo);
}