ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  var $u_password = "";
21 
27  var $user = "";
28 
34  var $target = "";
35 
36  private $lang_variables_as_fallback = false;
37 
38  private $attachments = array();
39 
44  public function __construct()
45  {
46  }
47 
48  public function useLangVariablesAsFallback($a_status)
49  {
50  $this->lang_variables_as_fallback = $a_status;
51  }
52 
54  {
56  }
57 
64  function setUserPassword($a_pwd)
65  {
66  $this->u_password = $a_pwd;
67  }
68 
75  function getUserPassword()
76  {
77  return $this->u_password;
78  }
79 
87  function setUser(&$a_user)
88  {
89  $this->user =& $a_user;
90  }
91 
98  function &getUser()
99  {
100  return $this->user;
101  }
102 
109  function setTarget($a_target)
110  {
111  $this->u_target = $a_target;
112  }
113 
120  function getTarget()
121  {
122  return $this->target;
123  }
124 
128  function reset()
129  {
130  unset($this->u_password);
131  unset($this->user);
132  unset($this->target);
133  }
134 
138  function readAccountMail($a_lang)
139  {
140  if (!is_array($this->amail[$a_lang]))
141  {
142  include_once('./Services/User/classes/class.ilObjUserFolder.php');
143  $this->amail[$a_lang] = ilObjUserFolder::_lookupNewAccountMail($a_lang);
144  $amail["body"] = trim($this->amail[$a_lang]["body"]);
145  $amail["subject"] = trim($this->amail[$a_lang]["subject"]);
146  }
147 
148  return $this->amail[$a_lang];
149  }
150 
159  function send()
160  {
161  global $ilSetting;
162 
163  $user =& $this->getUser();
164 
165  if (!$user->getEmail())
166  {
167  return false;
168  }
169 
170  // determine language and get account mail data
171  // fall back to default language if acccount mail data is not given for user language.
172  $amail = $this->readAccountMail($user->getLanguage());
173  if ($amail['body'] == '' || $amail['subject'] == '')
174  {
175  $amail = $this->readAccountMail($ilSetting->get('language'));
176  $lang = $ilSetting->get('language');
177  }
178  else
179  {
180  $lang = $user->getLanguage();
181  }
182 
183  // fallback if mail data is still not given
184  if($this->areLangVariablesUsedAsFallback() &&
185  ($amail['body'] == '' || $amail['subject'] == ''))
186  {
187  $lang = $user->getLanguage();
188  $tmp_lang = new ilLanguage($lang);
189 
190  // mail subject
191  $mail_subject = $tmp_lang->txt('reg_mail_subject');
192 
193  // mail body
194  $mail_body = $tmp_lang->txt('reg_mail_body_salutation').' '.$user->getFullname().",\n\n".
195  $tmp_lang->txt('reg_mail_body_text1')."\n\n".
196  $tmp_lang->txt('reg_mail_body_text2')."\n".
197  ILIAS_HTTP_PATH.'/login.php?client_id='.CLIENT_ID."\n";
198  $mail_body .= $tmp_lang->txt('login').': '.$user->getLogin()."\n";
199  $mail_body.= $tmp_lang->txt('passwd').': '.$this->u_password."\n";
200  $mail_body.= "\n";
201  $mail_body .= $tmp_lang->txt('reg_mail_body_text3')."\n\r";
202  $mail_body .= $user->getProfileAsString($tmp_lang);
203  }
204  else
205  {
206  // replace placeholders
207  $mail_subject = $this->replacePlaceholders($amail['subject'], $user, $amail, $lang);
208  $mail_body = $this->replacePlaceholders($amail['body'], $user, $amail, $lang);
209  }
210 
211  // send the mail
212  include_once 'Services/Mail/classes/class.ilMimeMail.php';
213  $mmail = new ilMimeMail();
214  $mmail->autoCheck(false);
215  $mmail->From($ilSetting->get('admin_email'));
216  $mmail->Subject($mail_subject);
217  $mmail->To($user->getEmail());
218  $mmail->Body($mail_body);
219 
220  foreach($this->attachments as $filename => $display_name)
221  {
222  $mmail->Attach($filename, "", "attachment", $display_name);
223  }
224 /*
225 echo "<br><br><b>From</b>:".$ilSetting->get("admin_email");
226 echo "<br><br><b>To</b>:".$user->getEmail();
227 echo "<br><br><b>Subject</b>:".$mail_subject;
228 echo "<br><br><b>Body</b>:".$mail_body;
229 return true;*/
230  $mmail->Send();
231 
232  return true;
233  }
234 
235  function replacePlaceholders($a_string, &$a_user, $a_amail, $a_lang)
236  {
237  global $ilSetting, $tree;
238 
239  // determine salutation
240  switch ($a_user->getGender())
241  {
242  case "f" : $gender_salut = $a_amail["sal_f"];
243  break;
244  case "m" : $gender_salut = $a_amail["sal_m"];
245  break;
246  default : $gender_salut = $a_amail["sal_g"];
247  }
248  $gender_salut = trim($gender_salut);
249 
250  $a_string = str_replace("[MAIL_SALUTATION]", $gender_salut, $a_string);
251  $a_string = str_replace("[LOGIN]", $a_user->getLogin(), $a_string);
252  $a_string = str_replace("[FIRST_NAME]", $a_user->getFirstname(), $a_string);
253  $a_string = str_replace("[LAST_NAME]", $a_user->getLastname(), $a_string);
254  // BEGIN Mail Include E-Mail Address in account mail
255  $a_string = str_replace("[EMAIL]", $a_user->getEmail(), $a_string);
256  // END Mail Include E-Mail Address in account mail
257  $a_string = str_replace("[PASSWORD]", $this->getUserPassword(), $a_string);
258  $a_string = str_replace("[ILIAS_URL]",
259  ILIAS_HTTP_PATH."/login.php?client_id=".CLIENT_ID, $a_string);
260  $a_string = str_replace("[CLIENT_NAME]", CLIENT_NAME, $a_string);
261  $a_string = str_replace("[ADMIN_MAIL]", $ilSetting->get("admin_email"),
262  $a_string);
263 
264  // (no) password sections
265  if ($this->getUserPassword() == "")
266  {
267  // #12232
268  $a_string = preg_replace("/\[IF_PASSWORD\].*\[\/IF_PASSWORD\]/imsU", "", $a_string);
269  $a_string = preg_replace("/\[IF_NO_PASSWORD\](.*)\[\/IF_NO_PASSWORD\]/imsU", "$1", $a_string);
270  }
271  else
272  {
273  $a_string = preg_replace("/\[IF_NO_PASSWORD\].*\[\/IF_NO_PASSWORD\]/imsU", "", $a_string);
274  $a_string = preg_replace("/\[IF_PASSWORD\](.*)\[\/IF_PASSWORD\]/imsU", "$1", $a_string);
275  }
276 
277  // target
278  $tar = false;
279  if ($_GET["target"] != "")
280  {
281  $tarr = explode("_", $_GET["target"]);
282  if ($tree->isInTree($tarr[1]))
283  {
284  $obj_id = ilObject::_lookupObjId($tarr[1]);
285  $type = ilObject::_lookupType($obj_id);
286  if ($type == $tarr[0])
287  {
288  $a_string = str_replace("[TARGET_TITLE]", ilObject::_lookupTitle($obj_id),
289  $a_string);
290  $a_string = str_replace("[TARGET]",
291  ILIAS_HTTP_PATH."/goto.php?client_id=".CLIENT_ID."&target=".$_GET["target"],
292  $a_string);
293 
294  // this looks complicated, but we may have no initilised $lng object here
295  // if mail is send during user creation in authentication
296  include_once("./Services/Language/classes/class.ilLanguage.php");
297  $a_string = str_replace("[TARGET_TYPE]",
298  ilLanguage::_lookupEntry($a_lang, "common", "obj_".$tarr[0]),
299  $a_string);
300 
301  $tar = true;
302  }
303  }
304  }
305 
306  // (no) target section
307  if (!$tar)
308  {
309  $a_string = preg_replace("/\[IF_TARGET\].*\[\/IF_TARGET\]/imsU", "", $a_string);
310  }
311  else
312  {
313  $a_string = preg_replace("/\[IF_TARGET\](.*)\[\/IF_TARGET\]/imsU", "$1", $a_string);
314  }
315 
316  return $a_string;
317  }
318 
319  function addAttachment($a_filename, $a_display_name)
320  {
321  $this->attachments[$a_filename] = $a_display_name;
322  }
323 }
324 ?>