1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
im_mail.php
См. документацию.
1<?php
2
4
6
7class CIMMail
8{
9 public static function MailNotifyAgent(): string
10 {
11 $defSiteID = CSite::GetDefSite();
12
13 $arMark = array();
14 $arGroupNotify = array();
15 $arGroupNotifyUser = array();
16 $arUnsendNotify = CIMNotify::GetUnsendNotify();
17 $attachList = self::getAttachList(array_keys($arUnsendNotify));
18 $maxId = 0;
19
20 foreach($arUnsendNotify as $id => $arNotify)
21 {
22 $maxId = ((int)$id > $maxId) ? (int)$id : $maxId;
23 if (!isset($arMark[$arNotify["CHAT_ID"]]) || $arMark[$arNotify["CHAT_ID"]] < $arNotify["ID"])
24 $arMark[$arNotify["CHAT_ID"]] = $arNotify["ID"];
25
26 if ($arNotify['TO_EXTERNAL_AUTH_ID'] == \Bitrix\Im\Bot::EXTERNAL_AUTH_ID || $arNotify['TO_EXTERNAL_AUTH_ID'] == "network")
27 {
28 unset($arUnsendNotify[$id]);
29 continue;
30 }
31
32 if ($arNotify['TO_USER_ACTIVE'] != 'Y')
33 {
34 unset($arUnsendNotify[$id]);
35 continue;
36 }
37
38 if (isset($arNotify["NOTIFY_MODULE"]) && isset($arNotify["NOTIFY_EVENT"])
39 && !CIMSettings::GetNotifyAccess($arNotify["TO_USER_ID"], $arNotify["NOTIFY_MODULE"], $arNotify["NOTIFY_EVENT"], CIMSettings::CLIENT_MAIL))
40 {
41 unset($arUnsendNotify[$id]);
42 continue;
43 }
44
45 if ($arNotify["MESSAGE_OUT"] == IM_MAIL_SKIP)
46 {
47 unset($arUnsendNotify[$id]);
48 continue;
49 }
50
51 if (!$arNotify["TO_USER_LID"] || $arNotify["TO_USER_LID"] == '')
52 {
53 $arNotify["TO_USER_LID"] = $defSiteID;
54 if (!$arNotify["TO_USER_LID"] || $arNotify["TO_USER_LID"] == '')
55 {
56 unset($arUnsendNotify[$id]);
57 continue;
58 }
59 }
60 if ($arNotify["MESSAGE_OUT"] == '')
61 {
62 $arNotify["MESSAGE_OUT"] = self::getMessageTextWithAttach($arNotify["MESSAGE"], $attachList[$arNotify['ID']] ?? null);
63 }
64
65
66 if (!(isset($arNotify["EMAIL_TEMPLATE"]) && $arNotify["EMAIL_TEMPLATE"] <> ''))
67 $arNotify["EMAIL_TEMPLATE"] = "IM_NEW_NOTIFY";
68
70 "NAME" => $arNotify["TO_USER_NAME"],
71 "LAST_NAME" => $arNotify["TO_USER_LAST_NAME"],
72 "SECOND_NAME" => $arNotify["TO_USER_SECOND_NAME"],
73 "LOGIN" => $arNotify["TO_USER_LOGIN"],
74 "EXTERNAL_AUTH_ID" => $arNotify["TO_EXTERNAL_AUTH_ID"]
75 ));
76
77 if ($arNotify["FROM_USER_ID"] == 0)
78 {
79 $arNotify["FROM_USER"] = GetMessage('IM_MAIL_USER_SYSTEM');
80 }
81 else
82 {
84 "NAME" => $arNotify["FROM_USER_NAME"],
85 "LAST_NAME" => $arNotify["FROM_USER_LAST_NAME"],
86 "SECOND_NAME" => $arNotify["FROM_USER_SECOND_NAME"],
87 "LOGIN" => $arNotify["FROM_USER_LOGIN"],
88 "EXTERNAL_AUTH_ID" => $arNotify["FROM_EXTERNAL_AUTH_ID"]
89 ));
90 }
91
92 $arNotify['NOTIFY_TAG_MD5'] = md5($arNotify["TO_USER_ID"].'|'.$arNotify['NOTIFY_TAG']);
93 $arUnsendNotify[$id] = $arNotify;
94 if ($arNotify["EMAIL_TEMPLATE"] == "IM_NEW_NOTIFY" && $arNotify['NOTIFY_TAG'] != '')
95 {
96 if (isset($arGroupNotify[$arNotify['NOTIFY_TAG_MD5']]))
97 {
98 $arGroupNotifyUser[$arNotify['NOTIFY_TAG_MD5']][$arNotify["FROM_USER_ID"]] = $arNotify["FROM_USER"];
99 unset($arUnsendNotify[$id]);
100 }
101 else
102 {
103 $arGroupNotifyUser[$arNotify['NOTIFY_TAG_MD5']][$arNotify["FROM_USER_ID"]] = $arNotify["FROM_USER"];
104 $arGroupNotify[$arNotify['NOTIFY_TAG_MD5']] = true;
105 }
106 }
107 }
108
109 if ($maxId > 0)
110 {
111 \Bitrix\Main\Config\Option::set('im', 'last_send_mail_notification', $maxId);
112 }
113
114 $CTP = new CTextParser;
115 foreach($arUnsendNotify as $id => $arNotify)
116 {
117 $message = $CTP->convert4mail(str_replace("#BR#", "\n", strip_tags($arNotify["MESSAGE_OUT"])));
118 $messageShort = str_replace(array("<br>","<br/>","<br />", "#BR#"), Array(" ", " ", " ", " "), nl2br($CTP->convert4mail(strip_tags($arNotify["MESSAGE_OUT"]))));
120 $messageShort = $CTP->html_cut(\Bitrix\Im\Text::removeBbCodes($messageShort), 50);
121 $title = trim($arNotify["NOTIFY_TITLE"] ?? '');
122 if ($title !== '')
123 {
125 }
127 "MESSAGE_ID" => $arNotify["ID"],
128 "USER" => $arNotify["USER"],
129 "USER_ID" => $arNotify["TO_USER_ID"],
130 "USER_LOGIN" => $arNotify["TO_USER_LOGIN"],
131 "USER_NAME" => $arNotify["TO_USER_NAME"],
132 "USER_LAST_NAME" => $arNotify["TO_USER_LAST_NAME"],
133 "USER_SECOND_NAME" => $arNotify["TO_USER_SECOND_NAME"],
134 "DATE_CREATE" => FormatDate("FULL", $arNotify["DATE_CREATE"]),
135 "FROM_USER_ID" => $arNotify["FROM_USER_ID"],
136 "FROM_USER_LOGIN" => $arNotify["FROM_USER_LOGIN"],
137 "FROM_USER" => $arNotify["FROM_USER"],
138 "SENDER_ID" => $arNotify["FROM_USER_ID"], // legacy
139 "SENDER_LOGIN" => $arNotify["FROM_USER_LOGIN"], // legacy
140 "SENDER_NAME" => $arNotify["FROM_USER_NAME"], // legacy
141 "SENDER_LAST_NAME" => $arNotify["FROM_USER_LAST_NAME"], // legacy
142 "SENDER_SECOND_NAME" => $arNotify["FROM_USER_SECOND_NAME"], // legacy
143 "EMAIL_TO" => $arNotify["TO_USER_EMAIL"],
144 "TITLE" => $title,
145 "MESSAGE" => $message,
146 "MESSAGE_50" => $messageShort,
147 );
148
149 if ($arFields['TITLE'] <> '')
150 $arFields["MESSAGE_50"] = $arFields['TITLE'];
151 else
152 $arFields["TITLE"] = $arFields['MESSAGE_50'];
153
154 if (isset($arGroupNotifyUser[$arNotify['NOTIFY_TAG_MD5']]) && count($arGroupNotifyUser[$arNotify['NOTIFY_TAG_MD5']]) > 1)
155 {
156 $arNotify["EMAIL_TEMPLATE"] = "IM_NEW_NOTIFY_GROUP";
157 $arFields['FROM_USERS'] = implode(', ', $arGroupNotifyUser[$arNotify['NOTIFY_TAG_MD5']]);
158 unset($arFields['FROM_USER']);
159 }
160
161 $event = new CEvent;
162 $event->Send($arNotify["EMAIL_TEMPLATE"], $arNotify["TO_USER_LID"], $arFields, "N");
163 }
164
165 return __METHOD__. '();';
166 }
167
168 public static function MailMessageAgent(): string
169 {
170 $defSiteID = CSite::GetDefSite();
171
172 $arMark = array();
173 $arUnsendMessage = CIMMessage::GetUnsendMessage();
174 $attachList = self::getAttachList(array_keys($arUnsendMessage));
175
176 $arToUser = Array();
177 $arFromUser = Array();
178 $arDialog = Array();
179 $parser = new CTextParser();
180 $maxId = 0;
181
182 foreach($arUnsendMessage as $id => $arMessage)
183 {
184 $maxId = ((int)$id > $maxId) ? (int)$id : $maxId;
185 if (!isset($arMark[$arMessage["TO_USER_ID"]][$arMessage["CHAT_ID"]]) || $arMark[$arMessage["TO_USER_ID"]][$arMessage["CHAT_ID"]] < $arMessage["ID"])
186 $arMark[$arMessage["TO_USER_ID"]][$arMessage["CHAT_ID"]] = $arMessage["ID"];
187
188 if ($arMessage['TO_EXTERNAL_AUTH_ID'] == \Bitrix\Im\Bot::EXTERNAL_AUTH_ID || $arMessage['TO_EXTERNAL_AUTH_ID'] == "network")
189 {
190 unset($arUnsendMessage[$id]);
191 continue;
192 }
193
194 if ($arMessage['TO_USER_ACTIVE'] != 'Y')
195 {
196 unset($arUnsendMessage[$id]);
197 continue;
198 }
199
200 if (!CIMSettings::GetNotifyAccess($arMessage["TO_USER_ID"], 'im', 'message', CIMSettings::CLIENT_MAIL))
201 {
202 unset($arUnsendMessage[$id]);
203 continue;
204 }
205
206 if ($arMessage["MESSAGE_OUT"] == IM_MAIL_SKIP)
207 {
208 unset($arUnsendMessage[$id]);
209 continue;
210 }
211
212 if ($arMessage["MESSAGE_OUT"] == '')
213 {
214 $arMessage["MESSAGE_OUT"] = self::getMessageTextWithAttach($arMessage["MESSAGE"], $attachList[$arMessage['ID']] ?? null);
215 }
216
217 if (!isset($arToUser[$arMessage["TO_USER_ID"]]))
218 {
219 $siteID = $arMessage["TO_USER_LID"];
220 if ($siteID == false || $siteID == '')
221 {
222 $siteID = $defSiteID;
223 if ($siteID == false || $siteID == '')
224 continue;
225 }
226
228 "NAME" => $arMessage["TO_USER_NAME"],
229 "LAST_NAME" => $arMessage["TO_USER_LAST_NAME"],
230 "SECOND_NAME" => $arMessage["TO_USER_SECOND_NAME"] ?? null,
231 "LOGIN" => $arMessage["TO_USER_LOGIN"],
232 "EXTERNAL_AUTH_ID" => $arMessage["TO_EXTERNAL_AUTH_ID"],
233 ));
234
235 $arToUser[$arMessage["TO_USER_ID"]] = Array(
236 "USER" => $arMessage['USER'] ?? null,
237 "USER_ID" => $arMessage["TO_USER_ID"],
238 "USER_LOGIN" => $arMessage["TO_USER_LOGIN"],
239 "USER_NAME" => $arMessage["TO_USER_NAME"],
240 "USER_LAST_NAME" => $arMessage["TO_USER_LAST_NAME"],
241 "USER_SECOND_NAME" => $arMessage["TO_USER_SECOND_NAME"] ?? null,
242 "TO_USER_LID" => $siteID,
243 "EMAIL_TO" => $arMessage["TO_USER_EMAIL"],
244 );
245 }
246 if (!isset($arFromUser[$arMessage["FROM_USER_ID"]]))
247 {
248 if ($arMessage["FROM_USER_ID"] == 0)
249 {
250 $arMessage["FROM_USER"] = GetMessage('IM_MAIL_USER_SYSTEM');
251 }
252 else
253 {
254 $arMessage["FROM_USER"] = \Bitrix\Im\User::formatFullNameFromDatabase(array(
255 "NAME" => $arMessage["FROM_USER_NAME"],
256 "LAST_NAME" => $arMessage["FROM_USER_LAST_NAME"],
257 "SECOND_NAME" => $arMessage["FROM_USER_SECOND_NAME"] ?? null,
258 "LOGIN" => $arMessage["FROM_USER_LOGIN"],
259 "EXTERNAL_AUTH_ID" => $arMessage["FROM_EXTERNAL_AUTH_ID"],
260 ));
261 }
262
263 $arFromUser[$arMessage["FROM_USER_ID"]] = Array(
264 "FROM_USER" => $arMessage["FROM_USER"],
265 "FROM_USER_ID" => $arMessage["FROM_USER_ID"],
266 "FROM_USER_LOGIN" => $arMessage["FROM_USER_LOGIN"],
267 "FROM_USER_NAME" => $arMessage["FROM_USER_NAME"],
268 "FROM_USER_LAST_NAME" => $arMessage["FROM_USER_LAST_NAME"],
269 "FROM_USER_SECOND_NAME" => $arMessage["FROM_USER_SECOND_NAME"] ?? null,
270 );
271 }
272
273 $message = $parser->convert4mail(str_replace("#BR#", "\n", strip_tags($arMessage["MESSAGE_OUT"])));
275 $arDialog[$arMessage["TO_USER_ID"]][$arMessage["FROM_USER_ID"]][] = Array(
276 'DATE_CREATE' => FormatDate("FULL", $arMessage["DATE_CREATE"]),
277 'MESSAGE' => $message
278 );
279 }
280
281 if ($maxId > 0)
282 {
283 \Bitrix\Main\Config\Option::set('im', 'last_send_mail_message', $maxId);
284 }
285
286 foreach ($arToUser as $toID=> $arToInfo)
287 {
288 $message = "";
289 $messagesFromUsers = array();
290 $bHeader = false;
291 $arNames = Array();
292 $arFromId = Array();
293 $bFirstMessage = true;
294 foreach ($arDialog[$toID] as $fromID => $arMessages)
295 {
296 $fromIdUserMessages = "";
297
298 if ($bFirstMessage)
299 $bFirstMessage = false;
300 else
301 $message .= "\n";
302
303 if (count($arDialog[$toID])>1)
304 {
305 $message .= GetMessage('IM_MAIL_TEMPLATE_NEW_MESSAGE_HEADER', Array('#FROM_USER#' => $arFromUser[$fromID]['FROM_USER']))."\n";
306 $bHeader = true;
307 }
308 $arNames[] = $arFromUser[$fromID]['FROM_USER'];
309 $arFromId[] = $arFromUser[$fromID]['FROM_USER_ID'];
310 foreach ($arMessages as $arMessage)
311 {
312 $message .= GetMessage('IM_MAIL_TEMPLATE_NEW_MESSAGE_TEXT', Array('#DATE_CREATE#' => $arMessage['DATE_CREATE'], '#MESSAGE#' => $arMessage['MESSAGE']))."\n";
313 $fromIdUserMessages .= nl2br(GetMessage('IM_MAIL_TEMPLATE_NEW_MESSAGE_TEXT', Array('#DATE_CREATE#' => $arMessage['DATE_CREATE'], '#MESSAGE#' => $arMessage['MESSAGE']))."\n");
314 }
315 $messagesFromUsers[$fromID] = $fromIdUserMessages;
316 }
317 if ($bHeader)
318 $message .= "\n".GetMessage('IM_MAIL_TEMPLATE_NEW_MESSAGE_FOOTER');
319
320 $fromUserId = $arToInfo["FROM_USER_ID"] ?? null;
321
323 "USER" => $arToInfo["USER_ID"],
324 "USER_ID" => count($arNames) > 1? $arToInfo["USER_ID"]: $fromUserId,
325 "USER_LOGIN" => $arToInfo["USER_LOGIN"],
326 "USER_NAME" => $arToInfo["USER_NAME"],
327 "USER_LAST_NAME" => $arToInfo["USER_LAST_NAME"],
328 "USER_SECOND_NAME" => $arToInfo["USER_SECOND_NAME"],
329 "EMAIL_TO" => $arToInfo["EMAIL_TO"],
330 "TITLE" => $arToInfo["TITLE"] ?? null,
331 "MESSAGES" => $message,
332 "MESSAGES_FROM_USERS" => serialize($messagesFromUsers),
333 "DATE_CREATE" => null,
334 );
335 $arFields['FROM_USER_ID'] = implode(', ', $arFromId);
336 if (count($arNames) > 1)
337 {
338 $mailTemplate = "IM_NEW_MESSAGE_GROUP";
339 $arFields['FROM_USERS'] = implode(', ', $arNames);
340 }
341 else
342 {
343 $mailTemplate = "IM_NEW_MESSAGE";
344 $arFields['FROM_USER'] = implode(', ', $arNames);
345 }
346
347 $event = new CEvent;
348 $event->Send($mailTemplate, $arToInfo['TO_USER_LID'], $arFields, "N");
349 }
350
351 return __METHOD__. '();';
352 }
353
354 private static function getAttachList(array $messageIds): array
355 {
356 if (empty($messageIds))
357 {
358 return [];
359 }
360
361 $query = MessageParamTable::query()
362 ->setSelect(['MESSAGE_ID', 'PARAM_JSON'])
363 ->whereIn('MESSAGE_ID', $messageIds)
364 ->where('PARAM_NAME', 'ATTACH')
365 ->exec()
366 ;
367
368 $attachList = [];
369 while ($row = $query->fetch())
370 {
371 $attachList[(int)$row['MESSAGE_ID']] = CIMMessageParamAttach::GetAttachByJson($row['PARAM_JSON'] ?? []);
372 }
373
374 return $attachList;
375 }
376
377 private static function getMessageTextWithAttach(?string $message, ?CIMMessageParamAttach $attach): ?string
378 {
379 if ($attach === null)
380 {
381 return $message;
382 }
383
384 $description = ($attach->GetArray()['DESCRIPTION'] ?? null) ?: GetMessage('IM_MAIL_DEFAULT_ATTACH_DESCRIPTION');
385
386 if ($message === null || $message === '')
387 {
388 return $description;
389 }
390
391 return $message . "\n" . $description;
392 }
393
398 public static function IsExternalMailAvailable()
399 {
400 global $USER;
401
402 if (!is_object($USER) || !$USER->IsAuthorized())
403 return false;
404
405 if (!IsModuleInstalled('mail'))
406 return false;
407
408 if (COption::GetOptionString('intranet', 'allow_external_mail', 'Y') != 'Y')
409 return false;
410
411 if (COption::GetOptionString('extranet', 'extranet_site', '') == SITE_ID)
412 return false;
413
414 if (isset(\Bitrix\Main\Application::getInstance()->getKernelSession()['aExtranetUser_'.$USER->GetID()][SITE_ID]))
415 {
416 if (!\Bitrix\Main\Application::getInstance()->getKernelSession()['aExtranetUser_'.$USER->GetID()][SITE_ID])
417 return false;
418 }
419 else if (CModule::IncludeModule('extranet') && !CExtranet::IsIntranetUser())
420 return false;
421
422 if (!IsModuleInstalled('dav'))
423 return true;
424
425 if (COption::GetOptionString('dav', 'exchange_server', '') == '')
426 return true;
427
428 if (COption::GetOptionString('dav', 'agent_mail', 'N') != 'Y')
429 return true;
430
431 if (COption::GetOptionString('dav', 'exchange_use_login', 'Y') == 'Y')
432 return false;
433
434 if (!CUserOptions::GetOption('global', 'davex_mailbox'))
435 {
436 $arUser = CUser::GetList(
437 'ID', 'ASC',
438 array('ID_EQUAL_EXACT' => $USER->GetID()),
439 array('SELECT' => array('UF_BXDAVEX_MAILBOX'), 'FIELDS' => array('ID'))
440 )->Fetch();
441
442 CUserOptions::SetOption('global', 'davex_mailbox', empty($arUser['UF_BXDAVEX_MAILBOX']) ? 'N' : 'Y');
443 }
444
445 if (CUserOptions::GetOption('global', 'davex_mailbox') == 'Y')
446 return false;
447
448 return true;
449 }
450
451 public static function GetUserOffset(array $params): int
452 {
453 return CTimeZone::getTimezoneOffset($params['TIME_ZONE'] ?? '');
454 }
455}
static removeBbCodes($text, $withFile=false, $attachValue=false)
Определения text.php:299
static formatFullNameFromDatabase($fields)
Определения user.php:1243
static set($moduleId, $name, $value="", $siteId="")
Определения option.php:261
Определения event.php:13
Определения im_mail.php:8
static GetUserOffset(array $params)
Определения im_mail.php:451
static IsExternalMailAvailable()
Определения im_mail.php:398
static MailNotifyAgent()
Определения im_mail.php:9
static MailMessageAgent()
Определения im_mail.php:168
static GetUnsendMessage($order="ASC")
Определения im_message.php:700
static GetNotifyAccess($userId, $moduleId, $eventId, $clientId)
Определения im_settings.php:255
const CLIENT_MAIL
Определения im_settings.php:17
Определения textparser.php:21
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
if(Loader::includeModule( 'bitrix24')) elseif(Loader::includeModule('intranet') &&CIntranetUtils::getPortalZone() !=='ru') $description
Определения .description.php:24
const IM_MAIL_SKIP
Определения include.php:52
$siteID
Определения cron_frame.php:12
global $USER
Определения csv_new_run.php:40
$arNames
Определения options.php:175
FormatDate($format="", $timestamp=false, $now=false, ?string $languageId=null)
Определения tools.php:871
IsModuleInstalled($module_id)
Определения tools.php:5301
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$title
Определения pdf.php:123
const SITE_ID
Определения sonet_set_content_view.php:12