Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventmessagecompiler.php
1<?php
8namespace Bitrix\Main\Mail;
9
13
15{
16 protected $mailTo;
17 protected $mailHeaders;
18 protected $mailSubject;
19 protected $mailBody;
20 protected $mailCharset;
21 protected $mailId;
23 protected $mailAttachment;
24
25 protected $eventMessageId = null;
26 protected $event = array();
28 protected $eventFields;
29 protected $siteFields;
30 protected $siteId;
31 protected $languageId;
33
34 /*
35 * 'MESSAGE' = array(
36 'BODY_TYPE' => 'html',
37 'SUBJECT' => '',
38 'EMAIL_TO' => '',
39 'EMAIL_FROM' => '',
40 'MESSAGE' => '',
41 'ID' => '',
42 'DATE_INSERT' => '',
43 'SITE_TEMPLATE_ID' => '',
44 'BCC' => '',
45 'CC' => '',
46 'REPLY_TO' => '',
47 'IN_REPLY_TO' => '',
48 'PRIORITY' => '',
49 'ADDITIONAL_FIELD' => array(),
50 'FILE' => array(),
51 )
52 */
53 public function __construct(array $arMessageParams)
54 {
55 if(!array_key_exists('FIELDS', $arMessageParams))
56 throw new \Bitrix\Main\ArgumentTypeException("FIELDS");
57 if(!array_key_exists('MESSAGE', $arMessageParams))
58 throw new \Bitrix\Main\ArgumentTypeException("MESSAGE");
59 if(!array_key_exists('SITE', $arMessageParams))
60 throw new \Bitrix\Main\ArgumentTypeException("SITE");
61 if(!array_key_exists('CHARSET', $arMessageParams))
62 throw new \Bitrix\Main\ArgumentTypeException("CHARSET");
63
64
65 $this->eventFields = $arMessageParams['FIELDS'];
66 if(array_key_exists('EVENT', $arMessageParams))
67 $this->event = $arMessageParams['EVENT'];
68
69 $this->eventMessageFields = $arMessageParams['MESSAGE'];
70 if(array_key_exists('ID', $arMessageParams['MESSAGE']))
71 $this->eventMessageId = $arMessageParams['MESSAGE']['ID'];
72
73 $this->siteFields = $this->getSiteFieldsArray(
74 is_array($arMessageParams['SITE'])
75 ? $arMessageParams['SITE']
76 : [$arMessageParams['SITE']]
77 );
78 $this->eventSiteFields = array_merge($this->siteFields, $this->eventFields);
79 foreach($this->eventSiteFields as $k => $v) $this->eventSiteFields[$k] = static::getFieldFlatValue($v);
80 $this->setMailCharset($arMessageParams['CHARSET']);
81 }
82
86 public static function createInstance(array $arMessageParams)
87 {
88 return new static($arMessageParams);
89 }
90
94 public function compile()
95 {
96 $this->setMailHeaders();
97
98 $this->setMailId();
99 $this->setMailContentType();
100
101 $this->setMailTo();
102 $this->setMailSubject();
103 $this->setMailBody();
104 $this->setMailAttachment();
105 }
109 protected function setMailBody()
110 {
111 $isHtml = $this->eventMessageFields["BODY_TYPE"] == "html";
112
113 // replace placeholders in body
114 $message = $this->eventMessageFields["MESSAGE_PHP"];
115 if(empty($message) && !empty($this->eventMessageFields["MESSAGE"]))
116 {
117 $message = MailInternal\EventMessageTable::replaceTemplateToPhp($this->eventMessageFields["MESSAGE"]);
118 if($this->eventMessageFields["ID"]>0)
119 MailInternal\EventMessageTable::update($this->eventMessageFields["ID"], array('MESSAGE_PHP' => $message));
120 }
121
122 if(!empty($this->eventMessageFields['SITE_TEMPLATE_ID']))
123 $siteTemplateId = $this->eventMessageFields['SITE_TEMPLATE_ID'];
124 else
125 $siteTemplateId = null;
126
127 $themeCompiler = EventMessageThemeCompiler::createInstance($siteTemplateId, $message, $isHtml);
128
129 if(empty($siteTemplateId))
130 $siteTemplateId = ".default";
131
132 // set context variables for components
133 $themeCompiler->setSiteTemplateId($siteTemplateId);
134 $themeCompiler->setSiteId($this->siteId);
135 $themeCompiler->setLanguageId($this->languageId);
136 Loc::setCurrentLang($themeCompiler->getLanguageId());
137
138 // set values of $arParams, used by components
140 if($isHtml)
141 {
142 foreach ($this->eventSiteFields as $fieldKey => $fieldValue)
143 {
144 $eventSiteFields["HTML_".$fieldKey] = nl2br(htmlspecialcharsbx($fieldValue, ENT_COMPAT, false));
145
146 if (strpos($fieldValue, "<") === false)
147 {
148 $eventSiteFields[$fieldKey] = nl2br($fieldValue);
149 }
150 }
151 }
152 $eventSiteFields['MAIL_EVENTS_UNSUBSCRIBE_LINK'] = Tracking::getLinkUnsub(
153 'main',
154 [
155 'CODE' => mb_strtolower(trim(explode(',', $this->getMailTo())[0])),
156 'EVENT_NAME' => $this->eventMessageFields["EVENT_NAME"]
157 ]
158 );
159 $themeCompiler->setParams($eventSiteFields);
160 // eval site template and body
161 $themeCompiler->execute();
162 // get eval result of site template and body
163 $message = $themeCompiler->getResult();
164
165 $this->mailBody = $message;
166
169 }
170
174 public function getMailBody()
175 {
176 return $this->mailBody;
177 }
178
182 protected function setMailCharset($mailCharset)
183 {
184 $this->mailCharset = $mailCharset;
185 }
186
190 public function getMailCharset()
191 {
192 return $this->mailCharset;
193 }
194
198 protected function setMailContentType()
199 {
200 $this->mailContentType = $this->eventMessageFields["BODY_TYPE"];
201 }
202
206 public function getMailContentType()
207 {
209 }
210
211 protected function setMailAttachment()
212 {
213 $eventMessageAttachment = [];
214 $eventFilesContent = [];
215
216 // Attach files from message template
217 if(array_key_exists('FILE', $this->eventMessageFields))
218 {
219 $eventMessageAttachment = $this->eventMessageFields["FILE"];
220 }
221
222 if(array_key_exists('FILES_CONTENT', $this->event))
223 {
224 $eventFilesContent = $this->event["FILES_CONTENT"];
225 }
226
227 // Attach files from event
228 if(isset($this->event["FILE"]) && is_array($this->event["FILE"]))
229 {
230 $eventFileList = array();
231 foreach($this->event["FILE"] as $fileId)
232 {
233 if(is_numeric($fileId))
234 {
235 $eventFileList[] = $fileId;
236 }
237 }
238
239 $eventMessageAttachment = array_merge($eventMessageAttachment, $eventFileList);
240 }
241
242
243 if(!empty($eventMessageAttachment))
244 {
245 $attachFileList = array();
246 $eventMessageAttachment = array_unique($eventMessageAttachment);
247
248 $strId = implode(',', $eventMessageAttachment);
249 $conn = \Bitrix\Main\Application::getConnection();
250 $strSql = "SELECT * FROM b_file WHERE ID IN(".$strId.")";
251 $resultDb = $conn->query($strSql);
252 while($file = $resultDb->fetch())
253 {
254 $tempFile = \CFile::MakeFileArray($file["ID"]);
255 $attachFileList[] = array(
256 'PATH' => $tempFile['tmp_name'],
257 'ID' => $file['ID'],
258 'CONTENT_TYPE' => $file['CONTENT_TYPE'],
259 'NAME' => ($file['ORIGINAL_NAME'] <> "" ? $file['ORIGINAL_NAME'] : $file['FILE_NAME']),
260 );
261 }
262
263 $this->mailAttachment = $attachFileList;
264 }
265
266 if (!empty($eventFilesContent))
267 {
268 foreach ($eventFilesContent as $item)
269 {
270 $this->mailAttachment[] = [
271 'CONTENT_TYPE' => $item['CONTENT_TYPE'],
272 'NAME' => $item['NAME'],
273 'CONTENT' => $item['CONTENT'],
274 'ID' => $item['ID'],
275 'CHARSET' => $item['CHARSET'],
276 'METHOD' => $item['METHOD'],
277 ];
278 }
279 }
280 }
281
285 public function getMailAttachment()
286 {
288 }
289
293 protected function setMailHeaders()
294 {
295 $arMailFields = array();
296 $messageFields = $this->eventMessageFields;
297 $arFields = $this->eventFields + $this->siteFields;
298
299
300 $arMailFields["From"] = $this->replaceTemplate($messageFields["EMAIL_FROM"], $arFields);
301
302 if(isset($messageFields["BCC"]) && $messageFields["BCC"]!='')
303 {
304 $bcc = $this->replaceTemplate($messageFields["BCC"], $arFields);
305 if(strpos($bcc, "@") !== false)
306 $arMailFields["BCC"] = $bcc;
307 }
308
309 if(isset($messageFields["CC"]) && $messageFields["CC"]!='')
310 $arMailFields["CC"] = $this->replaceTemplate($messageFields["CC"], $arFields);
311
312 if(isset($messageFields["REPLY_TO"]) && $messageFields["REPLY_TO"]!='')
313 $arMailFields["Reply-To"] = $this->replaceTemplate($messageFields["REPLY_TO"], $arFields);
314 else
315 $arMailFields["Reply-To"] = preg_replace("/(.*)\\<(.*)\\>/i", '$2', $arMailFields["From"]);
316
317 if(isset($messageFields["IN_REPLY_TO"]) && $messageFields["IN_REPLY_TO"]!='')
318 $arMailFields["In-Reply-To"] = $this->replaceTemplate($messageFields["IN_REPLY_TO"], $arFields);
319
320 if(isset($messageFields["ADDITIONAL_FIELD"]) && is_array($messageFields['ADDITIONAL_FIELD']))
321 {
322 foreach($messageFields['ADDITIONAL_FIELD'] as $additionalField)
323 $arMailFields[$additionalField['NAME']] = static::replaceTemplate($additionalField['VALUE'], $arFields);
324 }
325
326 if(isset($messageFields["PRIORITY"]) && $messageFields["PRIORITY"]!='')
327 $arMailFields["X-Priority"] = $this->replaceTemplate($messageFields["PRIORITY"], $arFields);
328
329 foreach($arFields as $f=>$v)
330 {
331 if(mb_substr($f, 0, 1) == "=")
332 $arMailFields[mb_substr($f, 1)] = $v;
333 }
334
335 foreach($arMailFields as $k=>$v)
336 $arMailFields[$k] = trim($v, "\r\n");
337
338 //add those who want to receive all emails
339 if(isset($this->event["DUPLICATE"]) && $this->event["DUPLICATE"]=="Y")
340 {
341 $all_bcc = Config\Option::get("main", "all_bcc");
342 if(strpos($all_bcc, "@") !== false)
343 $arMailFields["BCC"] .= ($all_bcc <> ''?($arMailFields["BCC"] <> ''?",":"").$all_bcc:"");
344 }
345
346 if(isset($this->event["EVENT_NAME"]))
347 $arMailFields['X-EVENT_NAME'] = $this->event["EVENT_NAME"];
348
349
350 $this->mailHeaders = $arMailFields;
351 }
352
356 public function getMailHeaders()
357 {
358 return $this->mailHeaders;
359 }
360
364 protected function setMailId()
365 {
366 if(isset($this->event['ID']) && isset($this->eventMessageFields["ID"]))
367 $this->mailId = $this->event['ID'].".".$this->eventMessageFields["ID"]." (".$this->event["DATE_INSERT"].")";
368 else
369 $this->mailId = '';
370 }
371
375 public function getMailId()
376 {
377 return $this->mailId;
378 }
379
383 protected function setMailSubject()
384 {
385 $this->mailSubject = $this->replaceTemplate($this->eventMessageFields["SUBJECT"], $this->eventSiteFields);
386 }
387
391 public function getMailSubject()
392 {
393 return $this->mailSubject;
394 }
395
399 protected function setMailTo()
400 {
401 $this->mailTo = $this->replaceTemplate($this->eventMessageFields["EMAIL_TO"], $this->eventSiteFields);
402 }
403
407 public function getMailTo()
408 {
409 return $this->mailTo;
410 }
411
412
419 protected function replaceTemplate($str, $ar, $bNewLineToBreak=false)
420 {
421 $str = str_replace("%", "%2", $str);
422
423 foreach ($ar as $key => $val)
424 {
425 if (is_array($val))
426 {
427 $val = implode(', ', $val);
428 }
429
430 if ($bNewLineToBreak && strpos($val, "<") === false)
431 {
432 $val = nl2br($val);
433 }
434
435 $val = str_replace("%", "%2", $val);
436 $val = str_replace("#", "%1", $val);
437 $str = str_replace("#".$key."#", $val, $str);
438 }
439
440 $str = str_replace("%1", "#", $str);
441 $str = str_replace("%2", "%", $str);
442
443 return $str;
444 }
445
452 protected function getSiteFieldsArray($sites)
453 {
454 $site_id = $sites[0];
455
456 if(!empty($this->eventMessageId))
457 {
458 $messageSiteDb = MailInternal\EventMessageSiteTable::getList(array(
459 'select' => array('SITE_ID'),
460 'filter' => array(
461 '=EVENT_MESSAGE_ID' => $this->eventMessageId,
462 '=SITE_ID' => $sites
463 )
464 ));
465 if ($arMessageSite = $messageSiteDb->Fetch())
466 $site_id = $arMessageSite['SITE_ID'];
467 }
468
469 $globalName = $GLOBALS["SERVER_NAME"] ?? '';
470 $SITE_NAME = Config\Option::get("main", "site_name", $globalName);
471 $SERVER_NAME = Config\Option::get("main", "server_name", $globalName);
472 $DEFAULT_EMAIL_FROM = Config\Option::get("main", "email_from", "admin@" . $globalName);
473
474 if($site_id <> '')
475 {
476 $result = \Bitrix\Main\SiteTable::getById($site_id);
477 if($arSite = $result->fetch())
478 {
479 $this->siteId = $arSite['LID'];
480 $this->languageId = $arSite['LANGUAGE_ID'];
481
482 \CEvent::$EVENT_SITE_PARAMS[$site_id] = array(
483 "SITE_NAME" => ($arSite["SITE_NAME"]<>''? $arSite["SITE_NAME"] : $SITE_NAME),
484 "SERVER_NAME" => ($arSite["SERVER_NAME"]<>''? $arSite["SERVER_NAME"] : $SERVER_NAME),
485 "DEFAULT_EMAIL_FROM" => ($arSite["EMAIL"]<>''? $arSite["EMAIL"] : $DEFAULT_EMAIL_FROM),
486 "LANGUAGE_ID" => $arSite['LANGUAGE_ID'],
487 "SITE_ID" => $arSite['LID'],
488 "SITE_DIR" => $arSite['DIR'],
489 );
490 return \CEvent::$EVENT_SITE_PARAMS[$site_id];
491 }
492 }
493
494 return array(
495 "SITE_NAME" => $SITE_NAME,
496 "SERVER_NAME" => $SERVER_NAME,
497 "DEFAULT_EMAIL_FROM" => $DEFAULT_EMAIL_FROM
498 );
499 }
500
505 protected static function getFieldFlatValue($value)
506 {
507 $flatValue = '';
508 if(is_array($value))
509 {
510 foreach($value as $v)
511 $flatValue .= ($flatValue <> ''? ', ':'').static::getFieldFlatValue($v);
512 }
513 else
514 $flatValue = $value;
515
516 return $flatValue;
517 }
518
522 public function toString()
523 {
524 $result = '';
525 $delimeter = str_repeat('-',5);
526
527 $result .= $delimeter."CHARSET".$delimeter."\n".$this->getMailCharset()."\n\n";
528 $result .= $delimeter."CONTENT_TYPE".$delimeter."\n".$this->getMailContentType()."\n\n";
529 $result .= $delimeter."MESSAGE_ID".$delimeter."\n".$this->getMailId()."\n\n";
530 $result .= $delimeter."TO".$delimeter."\n".$this->getMailTo()."\n\n";
531 $result .= $delimeter."SUBJECT".$delimeter."\n".$this->getMailSubject()."\n\n";
532 $result .= $delimeter."HEADERS".$delimeter."\n".print_r($this->getMailHeaders(), true)."\n\n";
533 $result .= $delimeter."BODY".$delimeter."\n".$this->getMailBody()."\n\n";
534 $result .= $delimeter."ATTACHMENT".$delimeter."\n".print_r($this->getMailAttachment(), true)."\n\n";
535
536 return $result;
537 }
538}
static setCurrentLang($language)
Definition loc.php:95
static createInstance(array $arMessageParams)
replaceTemplate($str, $ar, $bNewLineToBreak=false)
static createInstance($siteTemplateId=null, $body, $isHtml=true)
static getLinkUnsub($moduleId, $fields, $urlPage=null)
Definition tracking.php:148
$GLOBALS['____1444769544']
Definition license.php:1