ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilAccountMail.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\Refinery\Factory as Refinery;
23use ILIAS\User\Settings\NewAccountMail\Repository as NewAccountMailRepository;
24use ILIAS\User\Settings\NewAccountMail\Mail as NewAccountMail;
25use ILIAS\ResourceStorage\Services as ResourceStorage;
26
28{
29 private readonly GlobalHttpState $http;
30 private readonly ilSetting $settings;
31 private readonly Refinery $refinery;
32 private readonly ilTree $repository_tree;
34 public string $u_password = '';
35 public ?ilObjUser $user = null;
36 private bool $lang_variables_as_fallback = false;
37 private readonly ResourceStorage $irss;
38 private readonly NewAccountMailRepository $account_mail_repo;
39 private array $amail = [];
40 private ?string $permanent_link_target = null;
41
42 public function __construct()
43 {
44 global $DIC;
45 $this->http = $DIC->http();
46 $this->refinery = $DIC->refinery();
47 $this->settings = $DIC->settings();
48 $this->repository_tree = $DIC->repositoryTree();
49 $this->sender_factory = $DIC->mail()->mime()->senderFactory();
50 $this->irss = $DIC->resourceStorage();
51 $this->account_mail_repo = new NewAccountMailRepository($DIC->database());
52 }
53
54 public function useLangVariablesAsFallback(bool $a_status): void
55 {
56 $this->lang_variables_as_fallback = $a_status;
57 }
58
59 public function areLangVariablesUsedAsFallback(): bool
60 {
62 }
63
64 public function setUserPassword(string $a_pwd): void
65 {
66 $this->u_password = $a_pwd;
67 }
68
69 public function getUserPassword(): string
70 {
71 return $this->u_password;
72 }
73
74 public function setUser(ilObjUser $a_user): void
75 {
76 $this->user = $a_user;
77 }
78
79 public function setPermanentLinkTarget(?string $permanent_link_target): void
80 {
81 if ($permanent_link_target === '') {
82 throw new InvalidArgumentException(
83 'Permanent link target must not be empty'
84 );
85 }
86
87 $this->permanent_link_target = $permanent_link_target;
88 }
89
90 public function getUser(): ?ilObjUser
91 {
92 return $this->user;
93 }
94
95 public function reset(): void
96 {
97 $this->user = null;
98 $this->u_password = '';
99 $this->permanent_link_target = null;
100 }
101
105 private function readAccountMail(string $a_lang): NewAccountMail
106 {
107 if (!isset($this->amail[$a_lang]) || !($this->amail[$a_lang] instanceof NewAccountMail)) {
108 $this->amail[$a_lang] = $this->account_mail_repo->getFor($a_lang);
109 }
110
111 return $this->amail[$a_lang];
112 }
113
120 public function send(): bool
121 {
122 $user = $this->getUser();
123 if (!$user instanceof ilObjUser) {
124 throw new RuntimeException('A user instance must be passed when sending emails');
125 }
126
127 if (!$user->getEmail() === '') {
128 return false;
129 }
130
131 // determine language and get account mail data
132 // fall back to default language if acccount mail data is not given for user language.
133 $amail = $this->readAccountMail($user->getLanguage());
134 $lang = $user->getLanguage();
135 if ($amail->getBody() === '' || $amail->getSubject() === '') {
136 $fallback_language = 'en';
137 $amail = $this->readAccountMail($this->settings->get('language', $fallback_language));
138 $lang = $this->settings->get('language', $fallback_language);
139 }
140
141 $mmail = new ilMimeMail();
142
143 // fallback if mail data is still not given
144 if (($amail->getBody() === '' || $amail->getSubject() === '') && $this->areLangVariablesUsedAsFallback()) {
145 $lang = $user->getLanguage();
146 $tmp_lang = new ilLanguage($lang);
147
148 $mail_subject = $tmp_lang->txt('reg_mail_subject');
149
150 $timelimit = '';
151 if (!$user->checkTimeLimit()) {
152 $tmp_lang->loadLanguageModule('registration');
153
154 // #6098
155 $timelimit_from = new ilDateTime($user->getTimeLimitFrom(), IL_CAL_UNIX);
156 $timelimit_until = new ilDateTime($user->getTimeLimitUntil(), IL_CAL_UNIX);
157 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
158 $timelimit = "\n" . sprintf($tmp_lang->txt('reg_mail_body_timelimit'), $timelimit) . "\n\n";
159 }
160
161 // mail body
162 $mail_body = $tmp_lang->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ",\n\n" .
163 $tmp_lang->txt('reg_mail_body_text1') . "\n\n" .
164 $tmp_lang->txt('reg_mail_body_text2') . "\n" .
165 ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID . "\n";
166 $mail_body .= $tmp_lang->txt('login') . ': ' . $user->getLogin() . "\n";
167 $mail_body .= $tmp_lang->txt('passwd') . ': ' . $this->u_password . "\n";
168 $mail_body .= "\n" . $timelimit;
169 $mail_body .= $tmp_lang->txt('reg_mail_body_text3') . "\n\r";
170 $mail_body .= $user->getProfileAsString($tmp_lang);
171 } else {
172 $attachment = $amail->getAttachment($this->irss);
173 if ($attachment !== null) {
174 $mmail->Attach($attachment[0], '', 'attachment', $attachment[1]);
175 }
176
177 // replace placeholders
178 $mail_subject = $this->replacePlaceholders($amail->getSubject(), $user, $amail, $lang);
179 $mail_body = $this->replacePlaceholders($amail->getBody(), $user, $amail, $lang);
180 }
181
182 $mmail->From($this->sender_factory->system());
183 $mmail->Subject($mail_subject, true);
184 $mmail->To($user->getEmail());
185 $mmail->Body($mail_body);
186
187 $mmail->Send();
188
189 return true;
190 }
191
192 public function replacePlaceholders(string $a_string, ilObjUser $a_user, NewAccountMail $a_amail, string $a_lang): string
193 {
194 global $DIC;
195 $settings = $DIC->settings();
196 $mustache_factory = $DIC->mail()->mustacheFactory();
197
198 $replacements = [];
199
200 // determine salutation
201 $replacements['MAIL_SALUTATION'] = $mustache_factory->getBasicEngine()->render(
202 match ($a_user->getGender()) {
203 'f' => trim($a_amail->getSalutationFemale()),
204 'm' => trim($a_amail->getSalutationMale()),
205 default => trim($a_amail->getSalutationNoneSpecific()),
206 },
207 [
208 'FIRST_NAME' => $a_user->getFirstname(),
209 'LAST_NAME' => $a_user->getLastname(),
210 'LOGIN' => $a_user->getLogin(),
211 ]
212 );
213 $replacements['LOGIN'] = $a_user->getLogin();
214 $replacements['FIRST_NAME'] = $a_user->getFirstname();
215 $replacements['LAST_NAME'] = $a_user->getLastname();
216 // BEGIN Mail Include E-Mail Address in account mail
217 $replacements['EMAIL'] = $a_user->getEmail();
218 // END Mail Include E-Mail Address in account mail
219 $replacements['PASSWORD'] = $this->getUserPassword();
220 $replacements['ILIAS_URL'] = ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID;
221 $replacements['CLIENT_NAME'] = CLIENT_NAME;
222 $replacements['ADMIN_MAIL'] = $this->settings->get('admin_email');
223 $replacements['IF_PASSWORD'] = $this->getUserPassword() !== '';
224 $replacements['IF_NO_PASSWORD'] = $this->getUserPassword() === '';
225
226 // #13346
227 if (!$a_user->getTimeLimitUnlimited()) {
228 // #6098
229 $replacements['IF_TIMELIMIT'] = !$a_user->getTimeLimitUnlimited();
230 $timelimit_from = new ilDateTime($a_user->getTimeLimitFrom(), IL_CAL_UNIX);
231 $timelimit_until = new ilDateTime($a_user->getTimeLimitUntil(), IL_CAL_UNIX);
232 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
233 $replacements['TIMELIMIT'] = $timelimit;
234 }
235
236 // target
237 $replacements['IF_TARGET'] = false;
238 if ($this->permanent_link_target !== null) {
239 $tarr = explode('_', $this->permanent_link_target);
240 if ($this->repository_tree->isInTree((int) $tarr[1])) {
241 $obj_id = ilObject::_lookupObjId((int) $tarr[1]);
242 $type = ilObject::_lookupType($obj_id);
243 if ($type === $tarr[0]) {
244 $replacements['TARGET_TITLE'] = ilObject::_lookupTitle($obj_id);
245 $replacements['TARGET'] = ILIAS_HTTP_PATH . '/goto.php?client_id=' . CLIENT_ID . '&target=' . $this->permanent_link_target;
246
247 // this looks complicated, but we may have no initilised $lng object here
248 // if mail is send during user creation in authentication
249 $replacements['TARGET_TYPE'] = ilLanguage::_lookupEntry($a_lang, 'common', 'obj_' . $tarr[0]);
250 $replacements['IF_TARGET'] = true;
251 }
252 }
253 }
254
255 return $mustache_factory->getBasicEngine()->render($a_string, $replacements);
256 }
257}
Builds data types.
Definition: Factory.php:36
const IL_CAL_UNIX
useLangVariablesAsFallback(bool $a_status)
readAccountMail(string $a_lang)
readonly ilTree $repository_tree
send()
Sends the mail with its object properties as MimeMail It first tries to read the mail body,...
readonly ilSetting $settings
readonly ResourceStorage $irss
setUser(ilObjUser $a_user)
readonly ilMailMimeSenderFactory $sender_factory
setPermanentLinkTarget(?string $permanent_link_target)
setUserPassword(string $a_pwd)
readonly NewAccountMailRepository $account_mail_repo
replacePlaceholders(string $a_string, ilObjUser $a_user, NewAccountMail $a_amail, string $a_lang)
readonly Refinery $refinery
readonly GlobalHttpState $http
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.
@classDescription Date and time handling
language handling
static _lookupEntry(string $a_lang_key, string $a_mod, string $a_id)
User class.
getProfileAsString(Language $language)
Get formatted mail body text of user profile data.
getFullname(int $max_strlen=0)
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
ILIAS Setting Class.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const CLIENT_ID
Definition: constants.php:41
const CLIENT_NAME
Definition: constants.php:42
Interface GlobalHttpState.
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26