ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilMailEventNotificationSender.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
10
11 const TYPE_POST_NEW = 60;
12
14
16
18
20
22
24
25 const PERMANENT_LINK_POST = 'PL_Post';
26
27 const PERMANENT_LINK_FORUM = 'PL_Forum';
28
32 protected $is_cronjob = false;
33
37 protected $provider;
38
42 protected $logger;
43
50 {
52 $this->provider = $provider;
53 $this->logger = $logger;
54 }
55
59 protected function initMail()
60 {
61 $mail = parent::initMail();
62 $this->logger->debug('Initialized mail service');
63
64 return $mail;
65 }
66
70 public function sendMail(array $a_rcp, $a_parse_recipients = true)
71 {
72 $this->logger->debug(sprintf(
73 'Delegating notification transport to mail service for recipient "%s" ...',
74 $a_rcp
75 ));
76 parent::sendMail($a_rcp, $a_parse_recipients);
77 $this->logger->debug('Notification transport delegated');
78 }
79
83 protected function setSubject($a_subject)
84 {
85 $value = parent::setSubject($a_subject);
86 $this->logger->debug(sprintf('Setting subject to: %s', $a_subject));
87
88 return $value;
89 }
90
95 public function send()
96 {
97 global $DIC;
98 $ilSetting = $DIC->settings();
99 $lng = $DIC->language();
100
101 if (!$ilSetting->get('forum_notification', 0)) {
102 $this->logger->debug('Forum notifications are globally disabled');
103
104 return false;
105 }
106
107 if (!$this->getRecipients()) {
108 $this->logger->debug('No notification recipients, nothing to do');
109
110 return false;
111 }
112
113 $lng->loadLanguageModule('forum');
114
117
118 $mailObjects = array();
119
120 switch ($this->getType()) {
122 foreach ($this->getRecipients() as $rcp) {
123 $this->initLanguage($rcp);
124 $customText = sprintf(
125 $this->getLanguageText('thread_deleted_by'),
126 $this->provider->getDeletedBy(),
127 $this->provider->getForumTitle()
128 );
129
130 $mailObjects[] = $this->createMailValueObjectWithoutAttachments(
131 'frm_noti_subject_del_thread',
132 (int) $rcp,
133 (string) $customText,
134 'content_deleted_thread'
135 );
136 }
137 break;
138
140 foreach ($this->getRecipients() as $rcp) {
141 $this->initLanguage($rcp);
142 $customText = sprintf(
143 $this->getLanguageText('frm_noti_new_post'),
144 $this->provider->getForumTitle()
145 );
146
147 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
148 'frm_noti_subject_new_post',
149 (int) $rcp,
150 (string) $customText,
151 'new_post'
152 );
153 }
154 break;
155
157 foreach ($this->getRecipients() as $rcp) {
158 $this->initLanguage($rcp);
159 $customText = $this->getLanguageText('forums_post_activation_mail');
160
161 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
162 'frm_noti_subject_act_post',
163 (int) $rcp,
164 (string) $customText,
165 'new_post'
166 );
167 }
168 break;
169
171 foreach ($this->getRecipients() as $rcp) {
172 $this->initLanguage($rcp);
173 $customText = $this->getLanguageText('forum_post_replied');
174
175 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
176 'frm_noti_subject_answ_post',
177 (int) $rcp,
178 (string) $customText,
179 'new_post'
180 );
181 }
182 break;
183
185 foreach ($this->getRecipients() as $rcp) {
186 $this->initLanguage($rcp);
187 $customText = sprintf(
188 $this->getLanguageText('post_updated_by'),
189 $this->provider->getPostUpdateUserName($this->getLanguage()),
190 $this->provider->getForumTitle()
191 );
192 $date = $this->provider->getPostUpdate();
193
194 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
195 'frm_noti_subject_upt_post',
196 (int) $rcp,
197 (string) $customText,
198 'content_post_updated',
199 $date
200 );
201 }
202 break;
203
205 foreach ($this->getRecipients() as $rcp) {
206 $this->initLanguage($rcp);
207 $customText = sprintf(
208 $this->getLanguageText('post_censored_by'),
209 $this->provider->getPostUpdateUserName($this->getLanguage()),
210 $this->provider->getForumTitle()
211 );
212 $date = $this->provider->getPostCensoredDate();
213
214 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
215 'frm_noti_subject_cens_post',
216 (int) $rcp,
217 (string) $customText,
218 'content_censored_post',
219 $date
220 );
221 }
222 break;
223
225 foreach ($this->getRecipients() as $rcp) {
226 $this->initLanguage($rcp);
227 $customText = sprintf(
228 $this->getLanguageText('post_uncensored_by'),
229 $this->provider->getPostUpdateUserName($this->getLanguage())
230 );
231 $date = $this->provider->getPostCensoredDate();
232
233 $mailObjects[] = $this->createMailValueObjectsWithAttachments(
234 'frm_noti_subject_uncens_post',
235 (int) $rcp,
236 (string) $customText,
237 'forums_the_post',
238 $date
239 );
240 }
241 break;
242
244 foreach ($this->getRecipients() as $rcp) {
245 $this->initLanguage($rcp);
246 $customText = sprintf(
247 $this->getLanguageText('post_deleted_by'),
248 $this->provider->getDeletedBy(),
249 $this->provider->getForumTitle()
250 );
251
252 $mailObjects[] = $this->createMailValueObjectWithoutAttachments(
253 'frm_noti_subject_del_post',
254 (int) $rcp,
255 (string) $customText,
256 'content_deleted_post'
257 );
258 }
259 break;
260 }
261
262 $contextId = \ilMailFormCall::getContextId();
263 if (null === $contextId) {
264 $contextId = '';
265 }
266
267 $contextParameters = ilMailFormCall::getContextParameters();
268 if (is_array($contextParameters)) {
269 $contextParameters = array();
270 }
271
272 $processor = new ilMassMailTaskProcessor();
273
274 $processor->run(
275 $mailObjects,
276 ANONYMOUS_USER_ID,
277 $contextId,
278 $contextParameters
279 );
280
283
284 return true;
285 }
286
290 protected function initLanguage($a_usr_id)
291 {
292 parent::initLanguage($a_usr_id);
293 $this->language->loadLanguageModule('forum');
294 }
295
299 public function isCronjob()
300 {
301 return (bool) $this->is_cronjob;
302 }
303
307 public function setIsCronjob($is_cronjob)
308 {
309 $this->is_cronjob = (bool) $is_cronjob;
310 }
311
316 private function getPermanentLink($type = self::PERMANENT_LINK_POST)
317 {
318 global $DIC;
319 $ilClientIniFile = $DIC['ilClientIniFile'];
320
321 if ($type == self::PERMANENT_LINK_FORUM) {
322 $language_text = $this->getLanguageText("forums_notification_show_frm");
323 $forum_parameters = $this->provider->getRefId();
324 } else {
325 $language_text = $this->getLanguageText("forums_notification_show_post");
326 $forum_parameters = $this->provider->getRefId() . "_" . $this->provider->getThreadId() . "_" . $this->provider->getPostId();
327 }
328
329 $this->logger->debug(sprintf(
330 'Building permanent with parameters %s',
331 $forum_parameters
332 ));
333
334 if ($this->isCronjob()) {
335 $posting_link = sprintf(
336 $language_text,
337 ilUtil::_getHttpPath() . "/goto.php?target=frm_" . $forum_parameters . '&client_id=' . CLIENT_ID
338 ) . "\n\n";
339
340 $posting_link .= sprintf(
341 $this->getLanguageText("forums_notification_intro"),
342 $ilClientIniFile->readVariable("client", "name"),
343 ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID
344 ) . "\n\n";
345 } else {
346 $posting_link = sprintf(
347 $language_text,
348 ilUtil::_getHttpPath() . "/goto.php?target=frm_" . $forum_parameters . '&client_id=' . CLIENT_ID
349 ) . "\n\n";
350
351 $posting_link .= sprintf(
352 $this->getLanguageText("forums_notification_intro"),
353 $ilClientIniFile->readVariable("client", "name"),
354 ilUtil::_getHttpPath() . '/?client_id=' . CLIENT_ID
355 ) . "\n\n";
356 }
357
358 $this->logger->debug(sprintf(
359 'Link built: %s',
360 $posting_link
361 ));
362
363 return $posting_link;
364 }
365
369 private function getSecurePostMessage()
370 {
371 $pos_message = $this->provider->getPostMessage();
372 if (strip_tags($pos_message) != $pos_message) {
373 $pos_message = preg_replace("/\n/i", "", $pos_message);
374 $pos_message = preg_replace("/<li([^>]*)>/i", "\n<li$1>", $pos_message);
375 $pos_message = preg_replace("/<\/ul([^>]*)>(?!\s*?(<p|<ul))/i", "</ul$1>\n", $pos_message);
376 $pos_message = preg_replace("/<br(\s*)(\/?)>/i", "\n", $pos_message);
377 $pos_message = preg_replace("/<p([^>]*)>/i", "\n\n", $pos_message);
378 $pos_message = preg_replace("/<\/p([^>]*)>/i", '', $pos_message);
379
380 return $pos_message;
381 }
382
383 return strip_tags($pos_message);
384 }
385
397 string $subjectLanguageId,
398 int $recipientUserId,
399 string $customText,
400 string $action,
401 string $date = ''
402 ) {
403 $subjectText = $this->createSubjectText($subjectLanguageId);
404
405 $bodyText = $this->createMailBodyText(
406 $subjectLanguageId,
407 $recipientUserId,
408 $customText,
409 $action,
410 $date
411 );
412
413 $attachmentText = $this->createAttachmentText();
414 $bodyText .= $attachmentText;
415
416 $attachmentText = $this->createAttachmentLinkText();
417 $bodyText .= $attachmentText;
418
419 $mailObject = new ilMailValueObject(
420 '',
421 ilObjUser::_lookupLogin($recipientUserId),
422 '',
423 '',
424 $subjectText,
425 $bodyText,
426 $this->provider->getAttachments(),
427 false,
428 false
429 );
430
431 return $mailObject;
432 }
433
445 string $subjectLanguageId,
446 int $recipientUserId,
447 string $customText,
448 string $action,
449 string $date = ''
450 ) {
451 $subjectText = $this->createSubjectText($subjectLanguageId);
452
453 $bodyText = $this->createMailBodyText(
454 $subjectLanguageId,
455 $recipientUserId,
456 $customText,
457 $action,
458 $date
459 );
460
461 $mailObject = new ilMailValueObject(
462 '',
463 ilObjUser::_lookupLogin($recipientUserId),
464 '',
465 '',
466 $subjectText,
467 $bodyText,
468 [],
469 false,
470 false
471 );
472
473 return $mailObject;
474 }
475
476 private function createMailBodyText(
477 string $subject,
478 int $userId,
479 string $customText,
480 string $action,
481 string $date
482 ) {
483 $date = $this->createMailDate($date);
484
485 $this->addMailSubject($subject);
486
487 $body = ilMail::getSalutation($userId, $this->getLanguage());
488
489 $body .= "\n\n";
490 $body .= $customText;
491 $body .= "\n\n";
492 $body .= $this->getLanguageText('forum') . ": " . $this->provider->getForumTitle();
493 $body .= "\n\n";
494 $body .= $this->getLanguageText('thread') . ": " . $this->provider->getThreadTitle();
495 $body .= "\n\n";
496 $body .= $this->getLanguageText($action) . ": \n------------------------------------------------------------\n";
497
498 $body .= $this->getLanguageText('author') . ": " . $this->provider->getPostUserName($this->getLanguage());
499 $body .= "\n";
500 $body .= $this->getLanguageText('date') . ": " . $date;
501 $body .= "\n";
502 $body .= $this->getLanguageText('subject') . ": " . $this->provider->getPostTitle();
503 $body .= "\n";
504 $body .= $this->getLanguageText('frm_noti_message');
505 $body .= "\n";
506
507 $message = strip_tags($this->getSecurePostMessage());
508
509 if ($this->provider->getPostCensored() == 1) {
510 $message = $this->provider->getCensorshipComment();
511 }
512
513 $body .= $message . "\n";
514 $body .= "------------------------------------------------------------\n";
515
516 return $body;
517 }
518
519 private function createAttachmentText()
520 {
521 $attachmentText = '';
522 if (count($this->provider->getAttachments()) > 0) {
523 $this->logger->debug('Adding attachments ...');
524 foreach ($this->provider->getAttachments() as $attachment) {
525 $attachmentText .= $this->getLanguageText('attachment') . ": " . $attachment . "\n";
526 }
527 $attachmentText .= "\n------------------------------------------------------------\n";
528 }
529
530 return $attachmentText;
531 }
532
533 private function createAttachmentLinkText()
534 {
535 $body = $this->getPermanentLink();
537
538 return $body;
539 }
540
545 private function addMailSubject(string $subject)
546 {
547 $this->initMail();
548
549 $this->setSubject($this->createSubjectText($subject));
550 }
551
559 private function createMailDate(string $date) : string
560 {
562
563 if ($date === '') {
564 $date = $this->provider->getPostDate();
565 }
566
568
569 return $date;
570 }
571
576 private function createSubjectText(string $subject) : string
577 {
578 return sprintf(
579 $this->getLanguageText($subject),
580 $this->provider->getForumTitle(),
581 $this->provider->getThreadTitle()
582 );
583 }
584}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATETIME
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
static setLanguage($a_lng)
set language
static setUseRelativeDates($a_status)
set use relative dates
static useRelativeDates()
check if relative dates are used
@classDescription Date and time handling
Component logger with individual log levels by component id.
createMailValueObjectsWithAttachments(string $subjectLanguageId, int $recipientUserId, string $customText, string $action, string $date='')
Add body and send mail with attachments.
createMailBodyText(string $subject, int $userId, string $customText, string $action, string $date)
sendMail(array $a_rcp, $a_parse_recipients=true)
createMailValueObjectWithoutAttachments(string $subjectLanguageId, int $recipientUserId, string $customText, string $action, string $date='')
Add body and send mail without attachments.
__construct(ilForumNotificationMailData $provider, \ilLogger $logger)
ilForumMailNotification constructor.
getPermanentLink($type=self::PERMANENT_LINK_POST)
Base class for course/group mail notifications.
getType()
Get notification type.
getRecipients()
get array of recipients
static _getInstallationSignature()
static getSalutation($a_usr_id, ilLanguage $a_language=null)
static _lookupLogin($a_user_id)
lookup login
static _getHttpPath()
Interface ilForumNotificationMailData.
language()
Definition: language.php:2
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $ilSetting
Definition: privfeed.php:17
$lng
$message
Definition: xapiexit.php:14
$DIC
Definition: xapitoken.php:46