- Модуль: im
- Путь к файлу: ~/bitrix/modules/im/lib/user.php
- Класс: BitrixImUser
- Вызов: User::uploadAvatar
static function uploadAvatar($avatarUrl = '', $hash = '')
{
if (!$ar = parse_url($avatarUrl))
{
return 0;
}
if (!preg_match('#.(png|jpg|jpeg|gif|webp)$#i', $ar['path'], $matches))
{
return 0;
}
$hash = md5($hash. $avatarUrl);
$orm = BitrixImModelExternalAvatarTable::getList([
'select' => ['*', 'FILE_EXISTS' => 'FILE.ID'],
'filter' => ['=LINK_MD5' => $hash]
]);
if ($cache = $orm->fetch())
{
if ((int)$cache['FILE_EXISTS'] > 0)
{
return (int)$cache['AVATAR_ID'];
}
else
{
BitrixImModelExternalAvatarTable::delete((int)$cache['ID']);
}
}
try
{
$tempPath = CFile::GetTempName('', $hash.'.'.$matches[1]);
$http = new BitrixMainWebHttpClient();
$http
->setTimeout(10)
->setStreamTimeout(10);
if (!defined('BOT_CLIENT_URL'))
{
$http->setPrivateIp(false);
}
if ($http->download($avatarUrl, $tempPath))
{
$recordFile = CFile::MakeFileArray($tempPath);
}
else
{
return 0;
}
}
catch (BitrixMainIOIoException $exception)
{
return 0;
}
if (!CFile::IsImage($recordFile['name'], $recordFile['type']))
{
return 0;
}
if (is_array($recordFile) && $recordFile['size'] && $recordFile['size'] > 0 && $recordFile['size'] < 1000000)
{
$recordFile = array_merge($recordFile, ['MODULE_ID' => 'imbot']);
}
else
{
$recordFile = 0;
}
if ($recordFile)
{
$recordFile = CFile::SaveFile($recordFile, 'botcontroller', true);
}
if ((int)$recordFile > 0)
{
BitrixImModelExternalAvatarTable::add([
'LINK_MD5' => $hash,
'AVATAR_ID' => (int)$recordFile
]);
}
return $recordFile;
}