8Main\Localization\Loc::loadMessages(__FILE__);
19 public static function create($fields)
24 'LOGIN' => $fields[
"EMAIL"],
25 'EMAIL' => $fields[
"EMAIL"],
26 'NAME' => (!empty($fields[
"NAME"]) ? $fields[
"NAME"] :
''),
27 'LAST_NAME' => (!empty($fields[
"LAST_NAME"]) ? $fields[
"LAST_NAME"] :
''),
28 'PERSONAL_PHOTO' => (!empty($fields[
"PERSONAL_PHOTO_ID"]) ? \CFile::makeFileArray($fields[
'PERSONAL_PHOTO_ID']) :
false),
29 'EXTERNAL_AUTH_ID' =>
'email',
34 $userFields[
'UF_DEPARTMENT'] = array();
39 && is_array($fields[
'UF'])
42 foreach($fields[
'UF'] as $key => $value)
46 $userFields[$key] = $value;
51 $mailGroup = self::getMailUserGroup();
52 if (!empty($mailGroup))
54 $userFields[
"GROUP_ID"] = $mailGroup;
57 $result = $user->add($userFields);
69 $eventManager = Main\EventManager::getInstance();
70 $handler = $eventManager->addEventHandlerCompatible(
'main',
'OnUserLoginExternal', array(
'\Bitrix\Mail\User',
'onLoginExternal'));
73 $USER->login(
null,
null,
'Y');
75 $eventManager->removeEventHandler(
'main',
'OnUserLoginExternal', $handler);
86 $context = Main\Application::getInstance()->getContext();
87 $request = $context->getRequest();
89 if ($token = $request->get(
'token') ?: $request->getCookie(
'MAIL_AUTH_TOKEN'))
91 $userRelation = UserRelationsTable::getList(array(
92 'select' => array(
'USER_ID'),
95 '=USER.EXTERNAL_AUTH_ID' =>
'email',
102 $context->getResponse()->addCookie(
new Main\Web\
Cookie(
'MAIL_AUTH_TOKEN', $token));
104 return $userRelation[
'USER_ID'];
122 public static function getReplyTo($siteId, $userId, $entityType, $entityId, $entityLink =
null, $backurl =
null)
125 '=SITE_ID' => $siteId,
126 '=USER_ID' => $userId,
127 '=ENTITY_TYPE' => $entityType,
128 '=ENTITY_ID' => $entityId
130 $userRelation = UserRelationsTable::getList(array(
'filter' => $filter))->fetch();
132 if (empty($userRelation))
134 $filter[
'=SITE_ID'] =
null;
135 $userRelation = UserRelationsTable::getList(array(
'filter' => $filter))->fetch();
138 if (empty($userRelation))
140 if (empty($entityLink))
143 $userRelation = array(
144 'SITE_ID' => $siteId,
145 'TOKEN' => base_convert(md5(time().Main\Security\Random::getBytes(6)), 16, 36),
146 'USER_ID' => $userId,
147 'ENTITY_TYPE' => $entityType,
148 'ENTITY_ID' => $entityId,
149 'ENTITY_LINK' => $entityLink,
150 'BACKURL' => $backurl
153 if (!UserRelationsTable::add($userRelation)->isSuccess())
157 $site = Main\SiteTable::getByPrimary($siteId)->fetch();
158 $context = Main\Application::getInstance()->getContext();
160 $scheme = $context->getRequest()->isHttps() ?
'https' :
'http';
161 $domain = $site[
'SERVER_NAME'] ?: \COption::getOptionString(
'main',
'server_name',
'');
163 if (preg_match(
'/^(?<domain>.+):(?<port>\d+)$/', $domain, $matches))
165 $domain = $matches[
'domain'];
166 $port = $matches[
'port'];
170 $port = $context->getServer()->getServerPort();
173 $port = in_array($port, array(80, 443)) ?
'' :
':'.$port;
174 $path = ltrim(trim($site[
'DIR'],
'/') .
'/pub/entry.php',
'/');
176 $replyTo = sprintf(
'rpl%s@%s', $userRelation[
'TOKEN'], $domain);
177 $backUrl = sprintf(
'%s://%s%s/%s#%s', $scheme, $domain, $port, $path, $userRelation[
'TOKEN']);
179 return array($replyTo, $backUrl);
192 $cache = new \CPHPCache();
194 $cacheKey = sprintf(
'%s_%s', $userId, $entityType);
195 $cacheDir = sprintf(
'/mail/user/forward/%s', bin2hex($siteId));
197 if ($cache->initCache(365*24*3600, $cacheKey, $cacheDir))
199 $forwardTo = $cache->getVars();
203 $userRelation = UserRelationsTable::getList(array(
205 '=SITE_ID' => $siteId,
206 '=USER_ID' => $userId,
207 '=ENTITY_TYPE' => $entityType,
212 if (empty($userRelation))
214 $userRelation = array(
215 'SITE_ID' => $siteId,
216 'TOKEN' => base_convert(md5(time().Main\Security\Random::getBytes(6)), 16, 36),
217 'USER_ID' => $userId,
218 'ENTITY_TYPE' => $entityType
221 if (!UserRelationsTable::add($userRelation)->isSuccess())
226 $user->update($userId, array());
229 $site = Main\SiteTable::getByPrimary($siteId)->fetch();
230 $domain = $site[
'SERVER_NAME'] ?: \COption::getOptionString(
'main',
'server_name',
'');
232 if (preg_match(
'/^(?<domain>.+):(?<port>\d+)$/', $domain, $matches))
233 $domain = $matches[
'domain'];
235 $forwardTo = sprintf(
'fwd%s@%s', $userRelation[
'TOKEN'], $domain);
237 $cache->startDataCache();
238 $cache->endDataCache($forwardTo);
241 return array($forwardTo);
246 if (!preg_match(
'/^(?<type>rpl|fwd)(?<token>[a-z0-9]+)@(?<domain>.+)/i', $to, $matches))
255 $userRelation = UserRelationsTable::getList(array(
267 return $userRelation;
278 public static function onEmailReceived($to, $message, $recipient, $userRelation, &$error)
280 $type = $recipient[
'type'];
281 $token = $recipient[
'token'];
283 $message[
'secret'] = $token;
295 if (empty($content) && empty($message[
'files']))
297 $error = sprintf(
'Empty message (rcpt: %s)', $to);
301 $attachments = array_filter(
303 array_column((array) $message[
'files'],
'name'),
304 array_column((array) $message[
'files'],
'tmp_name')
308 $addResult = User\MessageTable::add(array(
310 'SITE_ID' => $userRelation[
'SITE_ID'],
311 'ENTITY_TYPE' => $userRelation[
'ENTITY_TYPE'],
312 'ENTITY_ID' => $userRelation[
'ENTITY_ID'],
313 'USER_ID' => $userRelation[
'USER_ID'],
314 'SUBJECT' => $message[
'subject'],
315 'CONTENT' => $content,
316 'ATTACHMENTS' => serialize($attachments),
319 if ($addResult->isSuccess())
322 "\\Bitrix\\Mail\\User::sendEventAgent(".$addResult->getId().
");",
338 $messageId = intval($messageId);
344 $res = User\MessageTable::getList(array(
349 if ($messageFields = $res->fetch())
351 if (intval($cnt) > 10)
353 if (Main\Loader::includeModule(
'im'))
355 $title = trim($messageFields[
'SUBJECT']);
358 $title = trim($messageFields[
'CONTENT']);
359 $title = preg_replace(
"/\[ATTACHMENT\s*=\s*[^\]]*\]/is".BX_UTF_PCRE_MODIFIER,
"", $title);
361 $CBXSanitizer = new \CBXSanitizer;
362 $CBXSanitizer->delAllTags();
363 $title = $CBXSanitizer->sanitizeHtml($title);
366 \CIMNotify::add(array(
367 "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
368 "NOTIFY_TYPE" => IM_NOTIFY_SYSTEM,
369 "NOTIFY_MODULE" =>
"mail",
370 "NOTIFY_EVENT" =>
"user_message_failed",
371 "TO_USER_ID" => $messageFields[
'USER_ID'],
377 User\MessageTable::delete($messageId);
381 switch ($messageFields[
'TYPE'])
384 $eventId = sprintf(
'onReplyReceived%s', $messageFields[
'ENTITY_TYPE']);
387 $eventId = sprintf(
'onForwardReceived%s', $messageFields[
'ENTITY_TYPE']);
391 if (!empty($eventId))
393 $attachments = array();
394 if (!empty($messageFields[
'ATTACHMENTS']))
396 $tmpAttachments = unserialize($messageFields[
'ATTACHMENTS'], [
'allowed_classes' =>
false]);
397 if (is_array($tmpAttachments))
399 foreach($tmpAttachments as $key => $uploadFile)
401 $file = \CFile::makeFileArray($uploadFile);
407 $file[
'name'] = $key;
408 $attachments[$key] = $file;
417 'site_id' => $messageFields[
'SITE_ID'],
418 'entity_id' => $messageFields[
'ENTITY_ID'],
419 'from' => $messageFields[
'USER_ID'],
420 'subject' => $messageFields[
'SUBJECT'],
421 'content' => $messageFields[
'CONTENT'],
422 'attachments' => $attachments
427 foreach ($event->getResults() as $eventResult)
429 if ($eventResult->getType() == \
Bitrix\Main\EventResult::ERROR)
434 $pPERIOD = 10 + (60 * $cnt);
435 return "\\Bitrix\\Mail\\User::sendEventAgent(".$messageId.
", ".$cnt.
");";
439 User\MessageTable::delete($messageId);
454 $mailInvitedGroup = Main\Config\Option::get(
"mail",
"mail_invited_group",
false);
455 if ($mailInvitedGroup)
457 $res[] = intval($mailInvitedGroup);
466 if(preg_match(
"/\\.bitrix24\\.([a-z]+|com\\.br)$/i", BX24_HOST_NAME))
468 $domain = BX24_HOST_NAME;
472 $domain = str_replace(
".",
"-", BX24_HOST_NAME).
".bitrix24.com";
475 $defaultEmailFrom =
"info@".$domain;
479 $defaultEmailFrom = Main\Config\Option::get(
'main',
'email_from',
'');
480 if ($defaultEmailFrom ==
'')
482 $defaultEmailFrom =
"info@".($serverName ?: Main\Config\Option::get(
'main',
'server_name',
$GLOBALS[
"SERVER_NAME"]));
486 return $defaultEmailFrom;
504 "=EXTERNAL_AUTH_ID" =>
'email'
508 \IsModuleInstalled(
'intranet')
509 || Main\
Config\Option::get(
"main",
"new_user_registration_email_confirmation",
"N") ==
"Y"
512 $filter[
"CONFIRM_CODE"] =
false;
515 $res = \Bitrix\Main\UserTable::getList(array(
518 'select' => array(
"ID",
"EMAIL",
"NAME",
"LAST_NAME",
"SECOND_NAME",
"LOGIN")
521 while ($user = $res->fetch())
523 $result[$user[
"ID"]] = array(
524 "NAME_FORMATTED" => (
525 !empty($user[
"NAME"])
526 || !empty($user[
"LAST_NAME"])
527 ? \CUser::formatName($nameTemplate, $user)
530 "EMAIL" => $user[
"EMAIL"]
539 if (array_key_exists(
'SERVER_NAME', $fields))
541 static::clearTokensCache();
547 static::clearTokensCache();
552 $cache = new \CPHPCache();
553 $cache->cleanDir(
'/mail/user/forward');
static parseReply(array &$message)
static parseForward(array &$message)
static onLoginExternal(&$params)
static getReplyTo($siteId, $userId, $entityType, $entityId, $entityLink=null, $backurl=null)
static getUserRelation($token)
static onEmailReceived($to, $message, $recipient, $userRelation, &$error)
static clearTokensCache()
static getUserData($userList, $nameTemplate)
static getMailUserGroup()
static parseEmailRecipient($to)
static handleServerNameUpdate()
static getForwardTo($siteId, $userId, $entityType)
static sendEventAgent($messageId=0, $cnt=0)
static getDefaultEmailFrom($serverName=false)
static handleSiteUpdate($fields)
static getMessage($code, $replace=null, $language=null)
static isModuleInstalled($moduleName)
$GLOBALS['____1444769544']