ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAccountMail.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
34 {
40  var $u_password = "";
41 
47  var $user = "";
48 
54  var $target = "";
55 
56  private $lang_variables_as_fallback = false;
57 
58 
63  function ilAccountMail()
64  {
65  }
66 
67  public function useLangVariablesAsFallback($a_status)
68  {
69  $this->lang_variables_as_fallback = $a_status;
70  }
71 
73  {
75  }
76 
83  function setUserPassword($a_pwd)
84  {
85  $this->u_password = $a_pwd;
86  }
87 
94  function getUserPassword()
95  {
96  return $this->u_password;
97  }
98 
106  function setUser(&$a_user)
107  {
108  $this->user =& $a_user;
109  }
110 
117  function &getUser()
118  {
119  return $this->user;
120  }
121 
128  function setTarget($a_target)
129  {
130  $this->u_target = $a_target;
131  }
132 
139  function getTarget()
140  {
141  return $this->target;
142  }
143 
147  function reset()
148  {
149  unset($this->u_password);
150  unset($this->user);
151  unset($this->target);
152  }
153 
157  function readAccountMail($a_lang)
158  {
159  if (!is_array($this->amail[$a_lang]))
160  {
161  include_once('./Services/User/classes/class.ilObjUserFolder.php');
162  $this->amail[$a_lang] = ilObjUserFolder::_lookupNewAccountMail($a_lang);
163  $amail["body"] = trim($amail["body"]);
164  $amail["subject"] = trim($amail["subject"]);
165  }
166 
167  return $this->amail[$a_lang];
168  }
169 
178  function send()
179  {
180  global $ilSetting;
181 
182  $user =& $this->getUser();
183 
184  if (!$user->getEmail())
185  {
186  return false;
187  }
188 
189  // determine language and get account mail data
190  // fall back to default language if acccount mail data is not given for user language.
191  $amail = $this->readAccountMail($user->getLanguage());
192  if ($amail['body'] == '' || $amail['subject'] == '')
193  {
194  $amail = $this->readAccountMail($ilSetting->get('language'));
195  $lang = $ilSetting->get('language');
196  }
197  else
198  {
199  $lang = $user->getLanguage();
200  }
201 
202  // fallback if mail data is still not given
203  if($this->areLangVariablesUsedAsFallback() &&
204  ($amail['body'] == '' || $amail['subject'] == ''))
205  {
206  $lang = $user->getLanguage();
207  $tmp_lang = new ilLanguage($lang);
208 
209  // mail subject
210  $mail_subject = $tmp_lang->txt('reg_mail_subject');
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";
220  $mail_body .= $tmp_lang->txt('reg_mail_body_text3')."\n\r";
221  $mail_body .= $user->getProfileAsString($tmp_lang);
222  }
223  else
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  // send the mail
231  include_once 'Services/Mail/classes/class.ilMimeMail.php';
232  $mmail = new ilMimeMail();
233  $mmail->autoCheck(false);
234  $mmail->From($ilSetting->get('admin_email'));
235  $mmail->Subject($mail_subject);
236  $mmail->To($user->getEmail());
237  $mmail->Body($mail_body);
238 /*
239 echo "<br><br><b>From</b>:".$ilSetting->get("admin_email");
240 echo "<br><br><b>To</b>:".$user->getEmail();
241 echo "<br><br><b>Subject</b>:".$mail_subject;
242 echo "<br><br><b>Body</b>:".$mail_body;
243 return true;*/
244  $mmail->Send();
245 
246  return true;
247  }
248 
249  function replacePlaceholders($a_string, &$a_user, $a_amail, $a_lang)
250  {
251  global $ilSetting, $tree;
252 
253  // determine salutation
254  switch ($a_user->getGender())
255  {
256  case "f" : $gender_salut = $a_amail["sal_f"];
257  break;
258  case "m" : $gender_salut = $a_amail["sal_m"];
259  break;
260  default : $gender_salut = $a_amail["sal_g"];
261  }
262  $gender_salut = trim($gender_salut);
263 
264  $a_string = str_replace("[MAIL_SALUTATION]", $gender_salut, $a_string);
265  $a_string = str_replace("[LOGIN]", $a_user->getLogin(), $a_string);
266  $a_string = str_replace("[FIRST_NAME]", $a_user->getFirstname(), $a_string);
267  $a_string = str_replace("[LAST_NAME]", $a_user->getLastname(), $a_string);
268  // BEGIN Mail Include E-Mail Address in account mail
269  $a_string = str_replace("[EMAIL]", $a_user->getEmail(), $a_string);
270  // END Mail Include E-Mail Address in account mail
271  $a_string = str_replace("[PASSWORD]", $this->getUserPassword(), $a_string);
272  $a_string = str_replace("[ILIAS_URL]",
273  ILIAS_HTTP_PATH."/login.php?client_id=".CLIENT_ID, $a_string);
274  $a_string = str_replace("[CLIENT_NAME]", CLIENT_NAME, $a_string);
275  $a_string = str_replace("[ADMIN_MAIL]", $ilSetting->get("admin_email"),
276  $a_string);
277 
278  // (no) password sections
279  if ($this->getUserPassword() == "")
280  {
281  $a_string = eregi_replace("\[".$ws."IF_PASSWORD".$ws."\].*\[\/".$ws."IF_PASSWORD".$ws."\]",
282  "", $a_string);
283  $a_string = eregi_replace("\[".$ws."IF_NO_PASSWORD".$ws."\](.*)\[\/".$ws."IF_NO_PASSWORD".$ws."\]",
284  "\\1", $a_string);
285  }
286  else
287  {
288  $a_string = eregi_replace("\[".$ws."IF_NO_PASSWORD".$ws."\].*\[\/".$ws."IF_NO_PASSWORD".$ws."\]",
289  "", $a_string);
290  $a_string = eregi_replace("\[".$ws."IF_PASSWORD".$ws."\](.*)\[\/".$ws."IF_PASSWORD".$ws."\]",
291  "\\1", $a_string);
292  }
293 
294  // target
295  $tar = false;
296  if ($_GET["target"] != "")
297  {
298  $tarr = explode("_", $_GET["target"]);
299  if ($tree->isInTree($tarr[1]))
300  {
301  $obj_id = ilObject::_lookupObjId($tarr[1]);
302  $type = ilObject::_lookupType($obj_id);
303  if ($type == $tarr[0])
304  {
305  $a_string = str_replace("[TARGET_TITLE]", ilObject::_lookupTitle($obj_id),
306  $a_string);
307  $a_string = str_replace("[TARGET]",
308  ILIAS_HTTP_PATH."/goto.php?client_id=".CLIENT_ID."&target=".$_GET["target"],
309  $a_string);
310 
311  // this looks complicated, but we may have no initilised $lng object here
312  // if mail is send during user creation in authentication
313  include_once("./Services/Language/classes/class.ilLanguage.php");
314  $a_string = str_replace("[TARGET_TYPE]",
315  ilLanguage::_lookupEntry($a_lang, "common", "obj_".$tarr[0]),
316  $a_string);
317 
318  $tar = true;
319  }
320  }
321  }
322 
323  // (no) target section
324  if (!$tar)
325  {
326  $a_string = eregi_replace("\[".$ws."IF_TARGET".$ws."\].*\[\/".$ws."IF_TARGET".$ws."\]",
327  "", $a_string);
328  }
329  else
330  {
331  $a_string = eregi_replace("\[".$ws."IF_TARGET".$ws."\](.*)\[\/".$ws."IF_TARGET".$ws."\]",
332  "\\1", $a_string);
333  }
334 
335  return $a_string;
336  }
337 
338 }
339 ?>