ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilAccountMail.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  private readonly ilSetting $settings;
27  private readonly ilTree $repository_tree;
29  public string $u_password = '';
30  public ?ilObjUser $user = null;
31  private bool $lang_variables_as_fallback = false;
33  private array $attachments = [];
34  private bool $attach_configured_files = false;
35  private array $amail = [];
36  private ?string $permanent_link_target = null;
37 
38  public function __construct()
39  {
40  global $DIC;
41  $this->settings = $DIC->settings();
42  $this->repository_tree = $DIC->repositoryTree();
43  $this->sender_factory = $DIC->mail()->mime()->senderFactory();
44  }
45 
46  public function useLangVariablesAsFallback(bool $a_status): void
47  {
48  $this->lang_variables_as_fallback = $a_status;
49  }
50 
51  public function areLangVariablesUsedAsFallback(): bool
52  {
54  }
55 
56  public function shouldAttachConfiguredFiles(): bool
57  {
59  }
60 
61  public function setAttachConfiguredFiles(bool $attach_configured_files): void
62  {
63  $this->attach_configured_files = $attach_configured_files;
64  }
65 
66  public function setUserPassword(string $a_pwd): void
67  {
68  $this->u_password = $a_pwd;
69  }
70 
71  public function getUserPassword(): string
72  {
73  return $this->u_password;
74  }
75 
76  public function setUser(ilObjUser $a_user): void
77  {
78  if (
79  $this->user instanceof ilObjUser &&
80  $a_user->getId() !== $this->user->getId()
81  ) {
82  $this->attachments = [];
83  }
84 
85  $this->user = $a_user;
86  }
87 
88  public function setPermanentLinkTarget(?string $permanent_link_target): void
89  {
90  if ($permanent_link_target === '') {
91  throw new InvalidArgumentException(
92  'Permanent link target must not be empty'
93  );
94  }
95 
96  $this->permanent_link_target = $permanent_link_target;
97  }
98 
99  public function getUser(): ?ilObjUser
100  {
101  return $this->user;
102  }
103 
104  public function reset(): void
105  {
106  $this->user = null;
107  $this->u_password = '';
108  $this->permanent_link_target = null;
109  }
110 
115  private function ensureValidMailDataShape(array $mail_data): array
116  {
117  foreach (['lang', 'subject', 'body', 'sal_f', 'sal_g', 'sal_m', 'type'] as $key) {
118  if (!isset($mail_data[$key])) {
119  $mail_data[$key] = '';
120  }
121  }
122 
123  $mail_data['subject'] = trim($mail_data['subject']);
124  $mail_data['body'] = trim($mail_data['body']);
125 
126  return $mail_data;
127  }
128 
132  private function readAccountMail(string $a_lang): array
133  {
134  if (!isset($this->amail[$a_lang]) || !is_array($this->amail[$a_lang])) {
135  $this->amail[$a_lang] = $this->ensureValidMailDataShape(
137  );
138  }
139 
140  return $this->amail[$a_lang];
141  }
142 
146  private function addAttachments(array $mail_data): void
147  {
148  if (isset($mail_data['att_file']) && $this->shouldAttachConfiguredFiles()) {
150  $fs->create();
151 
152  $path_to_tile = '/' . implode(
153  '/',
154  array_map(
155  static fn(string $path_part): string => trim($path_part, '/'),
156  [
157  $fs->getAbsolutePath(),
158  $mail_data['lang'],
159  ]
160  )
161  );
162 
163  $this->addAttachment($path_to_tile, $mail_data['att_file']);
164  }
165  }
166 
173  public function send(): bool
174  {
175  $user = $this->getUser();
176  if (!$user instanceof ilObjUser) {
177  throw new RuntimeException('A user instance must be passed when sending emails');
178  }
179 
180  if ($user->getEmail() === '') {
181  return false;
182  }
183 
184  // determine language and get account mail data
185  // fall back to default language if acccount mail data is not given for user language.
186  $amail = $this->readAccountMail($user->getLanguage());
187  $lang = $user->getLanguage();
188  if ($amail['body'] === '' || $amail['subject'] === '') {
189  $fallback_language = 'en';
190  $amail = $this->readAccountMail($this->settings->get('language', $fallback_language));
191  $lang = $this->settings->get('language', $fallback_language);
192  }
193 
194  // fallback if mail data is still not given
195  if (($amail['body'] === '' || $amail['subject'] === '') && $this->areLangVariablesUsedAsFallback()) {
196  $lang = $user->getLanguage();
197  $tmp_lang = new ilLanguage($lang);
198 
199  $mail_subject = $tmp_lang->txt('reg_mail_subject');
200 
201  $timelimit = '';
202  if (!$user->checkTimeLimit()) {
203  $tmp_lang->loadLanguageModule('registration');
204 
205  // #6098
206  $timelimit_from = new ilDateTime($user->getTimeLimitFrom(), IL_CAL_UNIX);
207  $timelimit_until = new ilDateTime($user->getTimeLimitUntil(), IL_CAL_UNIX);
208  $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
209  $timelimit = "\n" . sprintf($tmp_lang->txt('reg_mail_body_timelimit'), $timelimit) . "\n\n";
210  }
211 
212  // mail body
213  $mail_body = $tmp_lang->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ",\n\n" .
214  $tmp_lang->txt('reg_mail_body_text1') . "\n\n" .
215  $tmp_lang->txt('reg_mail_body_text2') . "\n" .
216  ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID . "\n";
217  $mail_body .= $tmp_lang->txt('login') . ': ' . $user->getLogin() . "\n";
218  $mail_body .= $tmp_lang->txt('passwd') . ': ' . $this->u_password . "\n";
219  $mail_body .= "\n" . $timelimit;
220  $mail_body .= $tmp_lang->txt('reg_mail_body_text3') . "\n\r";
221  $mail_body .= $user->getProfileAsString($tmp_lang);
222  } else {
223  $this->addAttachments($amail);
224 
225  // replace placeholders
226  $mail_subject = $this->replacePlaceholders($amail['subject'], $user, $amail, $lang);
227  $mail_body = $this->replacePlaceholders($amail['body'], $user, $amail, $lang);
228  }
229 
230  $mmail = new ilMimeMail();
231  $mmail->From($this->sender_factory->system());
232  $mmail->Subject($mail_subject, true);
233  $mmail->To($user->getEmail());
234  $mmail->Body($mail_body);
235 
236  foreach ($this->attachments as $filename => $display_name) {
237  $mmail->Attach($filename, '', 'attachment', $display_name);
238  }
239 
240  $mmail->Send();
241 
242  return true;
243  }
244 
245  public function replacePlaceholders(string $a_string, ilObjUser $a_user, array $a_amail, string $a_lang): string
246  {
247  global $DIC;
248  $settings = $DIC->settings();
249  $mustache_factory = $DIC->mail()->mustacheFactory();
250 
251  $replacements = [];
252 
253  // determine salutation
254  $replacements['MAIL_SALUTATION'] = $mustache_factory->getBasicEngine()->render(
255  match ($a_user->getGender()) {
256  'f' => trim((string) $a_amail['sal_f']),
257  'm' => trim((string) $a_amail['sal_m']),
258  default => trim((string) $a_amail['sal_g']),
259  },
260  [
261  'FIRST_NAME' => $a_user->getFirstname(),
262  'LAST_NAME' => $a_user->getLastname(),
263  'LOGIN' => $a_user->getLogin(),
264  ]
265  );
266  $replacements['LOGIN'] = $a_user->getLogin();
267  $replacements['FIRST_NAME'] = $a_user->getFirstname();
268  $replacements['LAST_NAME'] = $a_user->getLastname();
269  // BEGIN Mail Include E-Mail Address in account mail
270  $replacements['EMAIL'] = $a_user->getEmail();
271  // END Mail Include E-Mail Address in account mail
272  $replacements['PASSWORD'] = $this->getUserPassword();
273  $replacements['ILIAS_URL'] = ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID;
274  $replacements['CLIENT_NAME'] = CLIENT_NAME;
275  $replacements['ADMIN_MAIL'] = $settings->get('admin_email');
276  $replacements['IF_PASSWORD'] = $this->getUserPassword() !== '';
277  $replacements['IF_NO_PASSWORD'] = $this->getUserPassword() === '';
278 
279  // #13346
280  if (!$a_user->getTimeLimitUnlimited()) {
281  // #6098
282  $replacements['IF_TIMELIMIT'] = !$a_user->getTimeLimitUnlimited();
283  $timelimit_from = new ilDateTime($a_user->getTimeLimitFrom(), IL_CAL_UNIX);
284  $timelimit_until = new ilDateTime($a_user->getTimeLimitUntil(), IL_CAL_UNIX);
285  $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
286  $replacements['TIMELIMIT'] = $timelimit;
287  }
288 
289  // target
290  $replacements['IF_TARGET'] = false;
291  if ($this->permanent_link_target !== null) {
292  $tarr = explode('_', $this->permanent_link_target);
293  if ($this->repository_tree->isInTree((int) $tarr[1])) {
294  $obj_id = ilObject::_lookupObjId((int) $tarr[1]);
295  $type = ilObject::_lookupType($obj_id);
296  if ($type === $tarr[0]) {
297  $replacements['TARGET_TITLE'] = ilObject::_lookupTitle($obj_id);
298  $replacements['TARGET'] = ILIAS_HTTP_PATH . '/goto.php?client_id=' . CLIENT_ID . '&target=' . $this->permanent_link_target;
299 
300  // this looks complicated, but we may have no initilised $lng object here
301  // if mail is send during user creation in authentication
302  $replacements['TARGET_TYPE'] = ilLanguage::_lookupEntry($a_lang, 'common', 'obj_' . $tarr[0]);
303  $replacements['IF_TARGET'] = true;
304  }
305  }
306  }
307 
308  return $mustache_factory->getBasicEngine()->render($a_string, $replacements);
309  }
310 
311  public function addAttachment(string $a_filename, string $a_display_name): void
312  {
313  $this->attachments[$a_filename] = $a_display_name;
314  }
315 }
send()
Sends the mail with its object properties as MimeMail It first tries to read the mail body...
const USER_FOLDER_ID
Definition: constants.php:33
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_NAME
Definition: constants.php:42
readAccountMail(string $a_lang)
ensureValidMailDataShape(array $mail_data)
setAttachConfiguredFiles(bool $attach_configured_files)
getFullname(int $a_max_strlen=0)
setPermanentLinkTarget(?string $permanent_link_target)
const IL_CAL_UNIX
static _lookupObjId(int $ref_id)
replacePlaceholders(string $a_string, ilObjUser $a_user, array $a_amail, string $a_lang)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setUserPassword(string $a_pwd)
static _lookupTitle(int $obj_id)
static _lookupNewAccountMail(string $a_lang)
readonly ilMailMimeSenderFactory $sender_factory
useLangVariablesAsFallback(bool $a_status)
addAttachment(string $a_filename, string $a_display_name)
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:26
readonly ilSetting $settings
readonly ilTree $repository_tree
$filename
Definition: buildRTE.php:78
$lang
Definition: xapiexit.php:25
setUser(ilObjUser $a_user)
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
static _lookupType(int $id, bool $reference=false)
addAttachments(array $mail_data)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.