ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilAccountMail.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
14{
20 public $u_password = "";
21
27 public $user = "";
28
34 public $target = "";
35
37
38 private $attachments = array();
39
41 private $attachConfiguredFiles = false;
42
47 public function __construct()
48 {
49 }
50
51 public function useLangVariablesAsFallback($a_status)
52 {
53 $this->lang_variables_as_fallback = $a_status;
54 }
55
57 {
59 }
60
64 public function shouldAttachConfiguredFiles() : bool
65 {
67 }
68
73 {
74 $this->attachConfiguredFiles = $attachConfiguredFiles;
75 }
76
83 public function setUserPassword($a_pwd)
84 {
85 $this->u_password = $a_pwd;
86 }
87
94 public function getUserPassword()
95 {
96 return $this->u_password;
97 }
98
106 public function setUser(&$a_user)
107 {
108 if (
109 $this->user instanceof ilObjUser &&
110 $a_user instanceof ilObjUser &&
111 $a_user->getId() != $this->user->getId()
112 ) {
113 $this->attachments = [];
114 }
115
116 $this->user = &$a_user;
117 }
118
125 public function &getUser()
126 {
127 return $this->user;
128 }
129
136 public function setTarget($a_target)
137 {
138 $this->u_target = $a_target;
139 }
140
147 public function getTarget()
148 {
149 return $this->target;
150 }
151
155 public function reset()
156 {
157 unset($this->u_password);
158 unset($this->user);
159 unset($this->target);
160 }
161
165 public function readAccountMail($a_lang)
166 {
167 if (!is_array($this->amail[$a_lang])) {
168 include_once('./Services/User/classes/class.ilObjUserFolder.php');
169 $this->amail[$a_lang] = ilObjUserFolder::_lookupNewAccountMail($a_lang);
170 $amail["body"] = trim($amail["body"]);
171 $amail["subject"] = trim($amail["subject"]);
172 }
173
174 return $this->amail[$a_lang];
175 }
176
177 /***
178 * @param $mailData
179 */
180 private function addAttachments($mailData)
181 {
182 if ($this->shouldAttachConfiguredFiles() && isset($mailData['att_file'])) {
184 $fs->create();
185
186 $pathToFile = '/' . implode('/', array_map(function ($pathPart) {
187 return trim($pathPart, '/');
188 }, [
189 $fs->getAbsolutePath(),
190 $mailData['lang'],
191 ]));
192
193 $this->addAttachment($pathToFile, $mailData['att_file']);
194 }
195 }
196
205 public function send()
206 {
207 global $ilSetting;
208
209 $user = &$this->getUser();
210
211 if (!$user->getEmail()) {
212 return false;
213 }
214
215 // determine language and get account mail data
216 // fall back to default language if acccount mail data is not given for user language.
217 $amail = $this->readAccountMail($user->getLanguage());
218 if ($amail['body'] == '' || $amail['subject'] == '') {
219 $amail = $this->readAccountMail($ilSetting->get('language'));
220 $lang = $ilSetting->get('language');
221 } else {
222 $lang = $user->getLanguage();
223 }
224
225 // fallback if mail data is still not given
226 if ($this->areLangVariablesUsedAsFallback() &&
227 ($amail['body'] == '' || $amail['subject'] == '')) {
228 $lang = $user->getLanguage();
229 $tmp_lang = new ilLanguage($lang);
230
231 // mail subject
232 $mail_subject = $tmp_lang->txt('reg_mail_subject');
233
234 $timelimit = "";
235 if (!$user->checkTimeLimit()) {
236 $tmp_lang->loadLanguageModule("registration");
237
238 // #6098
239 $timelimit_from = new ilDateTime($user->getTimeLimitFrom(), IL_CAL_UNIX);
240 $timelimit_until = new ilDateTime($user->getTimeLimitUntil(), IL_CAL_UNIX);
241 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
242 $timelimit = "\n" . sprintf($tmp_lang->txt('reg_mail_body_timelimit'), $timelimit) . "\n\n";
243 }
244
245 // mail body
246 $mail_body = $tmp_lang->txt('reg_mail_body_salutation') . ' ' . $user->getFullname() . ",\n\n" .
247 $tmp_lang->txt('reg_mail_body_text1') . "\n\n" .
248 $tmp_lang->txt('reg_mail_body_text2') . "\n" .
249 ILIAS_HTTP_PATH . '/login.php?client_id=' . CLIENT_ID . "\n";
250 $mail_body .= $tmp_lang->txt('login') . ': ' . $user->getLogin() . "\n";
251 $mail_body .= $tmp_lang->txt('passwd') . ': ' . $this->u_password . "\n";
252 $mail_body .= "\n" . $timelimit;
253 $mail_body .= $tmp_lang->txt('reg_mail_body_text3') . "\n\r";
254 $mail_body .= $user->getProfileAsString($tmp_lang);
255 } else {
256 $this->addAttachments($amail);
257
258 // replace placeholders
259 $mail_subject = $this->replacePlaceholders($amail['subject'], $user, $amail, $lang);
260 $mail_body = $this->replacePlaceholders($amail['body'], $user, $amail, $lang);
261 }
262
264 $senderFactory = $GLOBALS["DIC"]["mail.mime.sender.factory"];
265
266 // send the mail
267 include_once 'Services/Mail/classes/class.ilMimeMail.php';
268 $mmail = new ilMimeMail();
269 $mmail->From($senderFactory->system());
270 $mmail->Subject($mail_subject, true);
271 $mmail->To($user->getEmail());
272 $mmail->Body($mail_body);
273
274 foreach ($this->attachments as $filename => $display_name) {
275 $mmail->Attach($filename, "", "attachment", $display_name);
276 }
277 /*
278 echo "<br><br><b>From</b>:".$ilSetting->get("admin_email");
279 echo "<br><br><b>To</b>:".$user->getEmail();
280 echo "<br><br><b>Subject</b>:".$mail_subject;
281 echo "<br><br><b>Body</b>:".$mail_body;
282 return true;*/
283 $mmail->Send();
284
285 return true;
286 }
287
288 public function replacePlaceholders($a_string, &$a_user, $a_amail, $a_lang)
289 {
290 global $ilSetting, $tree;
291
292 // determine salutation
293 switch ($a_user->getGender()) {
294 case "f": $gender_salut = $a_amail["sal_f"];
295 break;
296 case "m": $gender_salut = $a_amail["sal_m"];
297 break;
298 default: $gender_salut = $a_amail["sal_g"];
299 }
300 $gender_salut = trim($gender_salut);
301
302 $a_string = str_replace("[MAIL_SALUTATION]", $gender_salut, $a_string);
303 $a_string = str_replace("[LOGIN]", $a_user->getLogin(), $a_string);
304 $a_string = str_replace("[FIRST_NAME]", $a_user->getFirstname(), $a_string);
305 $a_string = str_replace("[LAST_NAME]", $a_user->getLastname(), $a_string);
306 // BEGIN Mail Include E-Mail Address in account mail
307 $a_string = str_replace("[EMAIL]", $a_user->getEmail(), $a_string);
308 // END Mail Include E-Mail Address in account mail
309 $a_string = str_replace("[PASSWORD]", $this->getUserPassword(), $a_string);
310 $a_string = str_replace(
311 "[ILIAS_URL]",
312 ILIAS_HTTP_PATH . "/login.php?client_id=" . CLIENT_ID,
313 $a_string
314 );
315 $a_string = str_replace("[CLIENT_NAME]", CLIENT_NAME, $a_string);
316 $a_string = str_replace(
317 "[ADMIN_MAIL]",
318 $ilSetting->get("admin_email"),
319 $a_string
320 );
321
322 // (no) password sections
323 if ($this->getUserPassword() == "") {
324 // #12232
325 $a_string = preg_replace("/\[IF_PASSWORD\].*\[\/IF_PASSWORD\]/imsU", "", $a_string);
326 $a_string = preg_replace("/\[IF_NO_PASSWORD\](.*)\[\/IF_NO_PASSWORD\]/imsU", "$1", $a_string);
327 } else {
328 $a_string = preg_replace("/\[IF_NO_PASSWORD\].*\[\/IF_NO_PASSWORD\]/imsU", "", $a_string);
329 $a_string = preg_replace("/\[IF_PASSWORD\](.*)\[\/IF_PASSWORD\]/imsU", "$1", $a_string);
330 }
331
332 // #13346
333 if (!$a_user->getTimeLimitUnlimited()) {
334 // #6098
335 $a_string = preg_replace("/\[IF_TIMELIMIT\](.*)\[\/IF_TIMELIMIT\]/imsU", "$1", $a_string);
336 $timelimit_from = new ilDateTime($a_user->getTimeLimitFrom(), IL_CAL_UNIX);
337 $timelimit_until = new ilDateTime($a_user->getTimeLimitUntil(), IL_CAL_UNIX);
338 $timelimit = ilDatePresentation::formatPeriod($timelimit_from, $timelimit_until);
339 $a_string = str_replace("[TIMELIMIT]", $timelimit, $a_string);
340 } else {
341 $a_string = preg_replace("/\[IF_TIMELIMIT\](.*)\[\/IF_TIMELIMIT\]/imsU", "", $a_string);
342 }
343
344 // target
345 $tar = false;
346 if ($_GET["target"] != "") {
347 $tarr = explode("_", $_GET["target"]);
348 if ($tree->isInTree($tarr[1])) {
349 $obj_id = ilObject::_lookupObjId($tarr[1]);
350 $type = ilObject::_lookupType($obj_id);
351 if ($type == $tarr[0]) {
352 $a_string = str_replace(
353 "[TARGET_TITLE]",
354 ilObject::_lookupTitle($obj_id),
355 $a_string
356 );
357 $a_string = str_replace(
358 "[TARGET]",
359 ILIAS_HTTP_PATH . "/goto.php?client_id=" . CLIENT_ID . "&target=" . $_GET["target"],
360 $a_string
361 );
362
363 // this looks complicated, but we may have no initilised $lng object here
364 // if mail is send during user creation in authentication
365 include_once("./Services/Language/classes/class.ilLanguage.php");
366 $a_string = str_replace(
367 "[TARGET_TYPE]",
368 ilLanguage::_lookupEntry($a_lang, "common", "obj_" . $tarr[0]),
369 $a_string
370 );
371
372 $tar = true;
373 }
374 }
375 }
376
377 // (no) target section
378 if (!$tar) {
379 $a_string = preg_replace("/\[IF_TARGET\].*\[\/IF_TARGET\]/imsU", "", $a_string);
380 } else {
381 $a_string = preg_replace("/\[IF_TARGET\](.*)\[\/IF_TARGET\]/imsU", "$1", $a_string);
382 }
383
384 return $a_string;
385 }
386
387 public function addAttachment($a_filename, $a_display_name)
388 {
389 $this->attachments[$a_filename] = $a_display_name;
390 }
391}
user()
Definition: user.php:4
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
$filename
Definition: buildRTE.php:89
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const USER_FOLDER_ID
Class ilObjUserFolder.
Class ilAccountMail.
replacePlaceholders($a_string, &$a_user, $a_amail, $a_lang)
setUser(&$a_user)
Set user.
addAttachment($a_filename, $a_display_name)
& getUser()
get user object
useLangVariablesAsFallback($a_status)
addAttachments($mailData)
getUserPassword()
get user password
readAccountMail($a_lang)
get new account mail array (including subject and message body)
reset()
reset all values
setUserPassword($a_pwd)
set user password
__construct()
constructor @access public
setTarget($a_target)
set repository item target
setAttachConfiguredFiles(bool $attachConfiguredFiles)
static formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day=false)
Format a period of two date Shows: 14.
@classDescription Date and time handling
language handling
static _lookupEntry($a_lang_key, $a_mod, $a_id)
Class ilMimeMail.
static _lookupNewAccountMail($a_lang)
static _lookupObjId($a_id)
static _lookupTitle($a_id)
lookup object title
getId()
get object id @access public
static _lookupType($a_id, $a_reference=false)
lookup object type
global $ilSetting
Definition: privfeed.php:17
$type
$lang
Definition: xapiexit.php:8
catch(ilCmiXapiException $e) send($response)
Definition: xapitoken.php:92