ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMail.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 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 
24 
128 require_once "Services/User/classes/class.ilObjUser.php";
129 
130 class ilMail
131 {
138  var $ilias;
139 
145  var $lng;
146 
152  var $mfile;
153 
155 
161  var $user_id;
162 
169 
176 
183 
190 
191 
198 
205 
212 
224 
225  var $soap_enabled = true;
226 
227  protected $mlists = null;
228 
229  protected $appendInstallationSignature = false;
230 
237  function ilMail($a_user_id)
238  {
239  require_once "classes/class.ilFileDataMail.php";
240  require_once "Services/Mail/classes/class.ilMailOptions.php";
241  require_once "Services/Contact/classes/class.ilMailingLists.php";
242 
243  global $ilias, $lng, $ilUser, $ilSetting;
244 
245  $lng->loadLanguageModule("mail");
246  // Initiate variables
247  $this->ilias =& $ilias;
248  $this->lng =& $lng;
249  $this->table_mail = 'mail';
250  $this->table_mail_saved = 'mail_saved';
251  $this->user_id = $a_user_id;
252  $this->mfile =& new ilFileDataMail($this->user_id);
253  $this->mail_options =& new ilMailOptions($a_user_id);
254  if(is_object($ilUser))
255  {
256  $this->mlists = new ilMailingLists($ilUser);
257  }
258 
259  // DEFAULT: sent mail aren't stored insentbox of user.
260  $this->setSaveInSentbox(false);
261 
262  // GET REFERENCE ID OF MAIL OBJECT
263  $this->readMailObjectReferenceId();
264 
265  }
266 
267  public function doesRecipientStillExists($a_recipient, $a_existing_recipients)
268  {
269  if(self::_usePearMail())
270  {
271  $recipients = $this->explodeRecipients($a_existing_recipients);
272  if(is_a($recipients, 'PEAR_Error'))
273  {
274  return false;
275  }
276  else
277  {
278  foreach($recipients as $rcp)
279  {
280  if (substr($rcp->mailbox, 0, 1) != '#')
281  {
282  if(trim($rcp->mailbox) == trim($a_recipient) ||
283  trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
284  {
285  return true;
286  }
287  }
288  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
289  {
290  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
291  {
292  return true;
293  }
294  }
295  else
296  {
297  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
298  {
299  return true;
300  }
301  }
302  }
303  }
304  }
305  else
306  {
307  $recipients = $this->explodeRecipients($a_existing_recipients);
308  if(count($recipients))
309  {
310  foreach($recipients as $recipient)
311  {
312  if(trim($recipient) == trim($a_recipient))
313  {
314  return true;
315  }
316  }
317  }
318  }
319 
320  return false;
321  }
322 
330  function enableSOAP($a_status)
331  {
332  $this->soap_enabled = $a_status;
333  }
334  function isSOAPEnabled()
335  {
336  global $ilSetting;
337 
338  if(!extension_loaded('curl') || !$ilSetting->get('soap_user_administration'))
339  {
340  return false;
341  }
342 
343  return (bool) $this->soap_enabled;
344  }
345 
346 
347  function setSaveInSentbox($a_save_in_sentbox)
348  {
349  $this->save_in_sentbox = $a_save_in_sentbox;
350  }
351 
352  function getSaveInSentbox()
353  {
354  return $this->save_in_sentbox;
355  }
356 
362  function setMailSendType($a_types)
363  {
364  $this->mail_send_type = $a_types;
365  }
366 
372  function setMailRcpTo($a_rcp_to)
373  {
374  $this->mail_rcp_to = $a_rcp_to;
375  }
376 
382  function setMailRcpCc($a_rcp_cc)
383  {
384  $this->mail_rcp_cc = $a_rcp_cc;
385  }
386 
392  function setMailRcpBc($a_rcp_bc)
393  {
394  $this->mail_rcp_bc = $a_rcp_bc;
395  }
396 
402  function setMailSubject($a_subject)
403  {
404  $this->mail_subject = $a_subject;
405  }
406 
412  function setMailMessage($a_message)
413  {
414  $this->mail_message = $a_message;
415  }
416 
422  {
423  global $ilDB;
424 
425  // mail settings id is set by a constant in ilias.ini. Keep the select for some time until everyone has updated his ilias.ini
426  if (!MAIL_SETTINGS_ID)
427  {
428  $res = $ilDB->queryf('
429  SELECT object_reference.ref_id FROM object_reference, tree, object_data
430  WHERE tree.parent = %s
431  AND object_data.type = %s
432  AND object_reference.ref_id = tree.child
433  AND object_reference.obj_id = object_data.obj_id',
434  array('integer', 'text'),
435  array(SYSTEM_FOLDER_ID, 'mail'));
436 
437  while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
438  {
439  $this->mail_obj_ref_id = $row["ref_id"];
440  }
441  }
442  else
443  {
444  $this->mail_obj_ref_id = MAIL_SETTINGS_ID;
445  }
446  }
447 
449  {
450  return $this->mail_obj_ref_id;
451  }
452 
454  {
455  return $this->lng->txt('mail_notification_subject');
456  }
457 
459  {
460  global $tpl, $lng;
461 
462  $tpl =& new ilTemplate('tpl.mail_notifications.html', true, true, 'Services/Mail');
463 
464  $tpl->setVariable('TXT_RECEIVED_MAILS', sprintf($this->lng->txt('mail_received_x_new_mails'), count($mail_data)));
465 
466  $counter = 0;
467  foreach ($mail_data as $mail)
468  {
469  $tpl->setCurrentBlock('mails');
470  $tpl->setVariable('NR', $counter + 1);
471  $tpl->setVariable('TXT_SENT', $this->lng->txt('sent'));
472  $tpl->setVariable('SEND_TIME', ilDatePresentation::formatDate(new ilDateTime($mail['send_time'],IL_CAL_DATETIME)));
473 
474  $tpl->setVariable('TXT_SUBJECT', $this->lng->txt('subject'));
475  $tpl->setVariable('SUBJECT', $mail['m_subject']);
476  $tpl->parseCurrentBlock();
477 
478  ++$counter;
479  }
480 
481  $message = $this->replacePlaceholders($tpl->get(), $user_id);
482 
483  return $message;
484  }
485 
496  public function formatNamesForOutput($users = '')
497  {
498  $users = trim($users);
499  if($users)
500  {
501  if(strstr($users, ','))
502  {
503  $rcp_to_array = array();
504 
505  $recipients = explode(',', $users);
506  foreach($recipients as $recipient)
507  {
508  $recipient = trim($recipient);
509  if($uid = ilObjUser::_lookupId($recipient))
510  {
511  $tmp_obj = new ilObjUser($uid);
512 
513  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
514  {
515  $rcp_to_array[] = $tmp_obj->getFullname().' ['.$recipient.']';
516  }
517  else
518  {
519  $rcp_to_array[] = $recipient;
520  }
521  unset($tmp_obj);
522  }
523  else
524  {
525  $rcp_to_array[] = $recipient;
526  }
527  }
528 
529  return trim(implode(', ', $rcp_to_array));
530  }
531  else
532  {
533  if($uid = ilObjUser::_lookupId($users))
534  {
535  $tmp_obj = new ilObjUser($uid);
536  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
537  {
538  return $tmp_obj->getFullname().' ['.$users.']';
539  }
540  else
541  {
542  unset($tmp_obj);
543  return $users;
544  }
545  }
546  else
547  {
548  return $users;
549  }
550  }
551  }
552  else
553  {
554  return $this->lng->txt('not_available');
555  }
556  }
557 
558  function getPreviousMail($a_mail_id)
559  {
560  global $ilDB;
561 
562  $ilDB->setLimit(1);
563  $res = $ilDB->queryf("
564  SELECT b.* FROM " . $this->table_mail ." a
565  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
566  AND b.user_id = a.user_id AND b.send_time > a.send_time
567  WHERE a.user_id = %s
568  AND a.mail_id = %s ORDER BY b.send_time ASC",
569  array('integer', 'integer'),
570  array($this->user_id, $a_mail_id));
571 
572  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
573 
574  return $this->mail_data;
575  }
576 
577  function getNextMail($a_mail_id)
578  {
579  global $ilDB;
580 
581  $ilDB->setLimit(1);
582  $res = $ilDB->queryf("
583  SELECT b.* FROM " . $this->table_mail ." a
584  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
585  AND b.user_id = a.user_id AND b.send_time < a.send_time
586  WHERE a.user_id = %s
587  AND a.mail_id = %s ORDER BY b.send_time DESC",
588  array('integer', 'integer'),
589  array($this->user_id, $a_mail_id));
590 
591  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
592 
593  return $this->mail_data;
594  }
595 
602  function getMailsOfFolder($a_folder_id)
603  {
604  global $ilDB;
605 
606  $this->mail_counter = array();
607  $this->mail_counter["read"] = 0;
608  $this->mail_counter["unread"] = 0;
609 
610  $res = $ilDB->queryf("
611  SELECT ".$this->table_mail.".* FROM ". $this->table_mail ."
612  LEFT JOIN object_data ON obj_id = sender_id
613  WHERE user_id = %s
614  AND folder_id = %s
615  AND ((sender_id > 0 AND sender_id IS NOT NULL AND obj_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL))
616  ORDER BY send_time DESC",
617  array('integer', 'integer'),
618  array($this->user_id, $a_folder_id));
619 
620  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
621  {
622  $tmp = $this->fetchMailData($row);
623 
624  if ($tmp["m_status"] == 'read')
625  {
626  ++$this->mail_counter["read"];
627  }
628 
629  if ($tmp["m_status"] == 'unread')
630  {
631  ++$this->mail_counter["unread"];
632  }
633 
634  $output[] = $tmp;
635  }
636 
637  $this->mail_counter["total"] = count($output);
638 
639  return $output ? $output : array();
640  }
641 
648  function countMailsOfFolder($a_folder_id)
649  {
650  global $ilDB;
651 
652  $res = $ilDB->queryf("
653  SELECT COUNT(*) FROM ". $this->table_mail ."
654  WHERE user_id = %s
655  AND folder_id = %s",
656  array('integer', 'integer'),
657  array($this->user_id, $a_folder_id));
658 
659  return $res->numRows();
660  }
661 
668  function deleteMailsOfFolder($a_folder_id)
669  {
670  if ($a_folder_id)
671  {
672  global $ilDB;
673 
674  /*$statement = $ilDB->manipulateF("
675  DELETE FROM ". $this->table_mail ."
676  WHERE user_id = %s
677  AND folder_id = %s",
678  array('integer', 'integer'),
679  array($this->user_id, $a_folder_id));*/
680  $mails = $this->getMailsOfFolder($a_folder_id);
681  foreach((array)$mails as $mail_data)
682  {
683  $this->deleteMails(array($mail_data['mail_id']));
684  }
685 
686  return true;
687  }
688 
689  return false;
690  }
691 
699  {
700  return is_array($this->mail_counter) ? $this->mail_counter : array(
701  "total" => 0,
702  "read" => 0,
703  "unread" => 0);
704  }
705 
712  function getMail($a_mail_id)
713  {
714  global $ilDB;
715 
716  $res = $ilDB->queryf("
717  SELECT * FROM ". $this->table_mail ."
718  WHERE user_id = %s
719  AND mail_id = %s",
720  array('integer', 'integer'),
721  array($this->user_id, $a_mail_id));
722 
723  $this->mail_data =$this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
724 
725  return $this->mail_data;
726  }
727 
734  function markRead($a_mail_ids)
735  {
736 
737  global $ilDB;
738 
739  $data = array();
740  $data_types = array();
741 
742  $query = "UPDATE ". $this->table_mail ."
743  SET m_status = %s
744  WHERE user_id = %s ";
745  array_push($data_types, 'text', 'integer');
746  array_push($data, 'read', $this->user_id);
747 
748  $cnt_mail_ids = count($a_mail_ids);
749 
750  if (is_array($a_mail_ids) &&
751  count($a_mail_ids) > 0)
752  {
753 
754  $in = 'mail_id IN (';
755  $counter = 0;
756  foreach($a_mail_ids as $a_mail_id)
757  {
758  array_push($data, $a_mail_id);
759  array_push($data_types, 'integer');
760 
761  if($counter > 0) $in .= ',';
762  $in .= '%s';
763  ++$counter;
764  }
765  $in .= ')';
766 
767  $query .= ' AND '.$in;
768  }
769 
770  $res = $ilDB->manipulateF($query, $data_types, $data);
771 
772  return true;
773  }
774 
781  function markUnread($a_mail_ids)
782  {
783  global $ilDB;
784 
785  $data = array();
786  $data_types = array();
787 
788  $query = "UPDATE ". $this->table_mail ."
789  SET m_status = %s
790  WHERE user_id = %s ";
791  array_push($data_types, 'text', 'integer');
792  array_push($data, 'unread', $this->user_id);
793 
794  $cnt_mail_ids = count($a_mail_ids);
795 
796  if (is_array($a_mail_ids) &&
797  count($a_mail_ids) > 0)
798  {
799 
800  $in = 'mail_id IN (';
801  $counter = 0;
802  foreach($a_mail_ids as $a_mail_id)
803  {
804  array_push($data, $a_mail_id);
805  array_push($data_types, 'integer');
806 
807  if($counter > 0) $in .= ',';
808  $in .= '%s';
809  ++$counter;
810  }
811  $in .= ')';
812 
813  $query .= ' AND '.$in;
814  }
815 
816  $statement = $ilDB->manipulateF($query, $data_types, $data);
817 
818  return true;
819  }
820 
828  function moveMailsToFolder($a_mail_ids,$a_folder_id)
829  {
830  global $ilDB;
831 
832  $data = array();
833  $data_types = array();
834 
835  $query = "UPDATE ". $this->table_mail ."
836  SET folder_id = %s
837  WHERE user_id = %s ";
838  array_push($data_types, 'text', 'integer');
839  array_push($data, $a_folder_id, $this->user_id);
840 
841  $cnt_mail_ids = count($a_mail_ids);
842 
843  if (is_array($a_mail_ids) &&
844  count($a_mail_ids) > 0)
845  {
846 
847  $in = 'mail_id IN (';
848  $counter = 0;
849  foreach($a_mail_ids as $a_mail_id)
850  {
851  array_push($data, $a_mail_id);
852  array_push($data_types, 'integer');
853 
854  if($counter > 0) $in .= ',';
855  $in .= '%s';
856  ++$counter;
857  }
858  $in .= ')';
859 
860  $query .= ' AND '.$in;
861  }
862 
863  $statement = $ilDB->manipulateF($query, $data_types, $data);
864 
865  return true;
866  }
867 
874  function deleteMails($a_mail_ids)
875  {
876  global $ilDB;
877 
878  foreach ($a_mail_ids as $id)
879  {
880  $statement = $ilDB->manipulateF("
881  DELETE FROM ". $this->table_mail ."
882  WHERE user_id = %s
883  AND mail_id = %s ",
884  array('integer', 'integer'),
885  array($this->user_id, $id));
886 
887  $this->mfile->deassignAttachmentFromDirectory($id);
888  }
889 
890  return true;
891  }
892 
899  function fetchMailData($a_row)
900  {
901  if (!$a_row) return;
902 
903  return array(
904  "mail_id" => $a_row->mail_id,
905  "user_id" => $a_row->user_id,
906  "folder_id" => $a_row->folder_id,
907  "sender_id" => $a_row->sender_id,
908  "attachments" => unserialize(stripslashes($a_row->attachments)),
909  "send_time" => $a_row->send_time,
910  "rcp_to" => $a_row->rcp_to,
911  "rcp_cc" => $a_row->rcp_cc,
912  "rcp_bcc" => $a_row->rcp_bcc,
913  "m_status" => $a_row->m_status,
914  "m_type" => unserialize(stripslashes($a_row->m_type)),
915  "m_email" => $a_row->m_email,
916  "m_subject" => $a_row->m_subject,
917  "m_message" => $a_row->m_message,
918  "import_name" => $a_row->import_name,
919  "use_placeholders"=> $a_row->use_placeholders);
920  }
921 
922  function updateDraft($a_folder_id,
923  $a_attachments,
924  $a_rcp_to,
925  $a_rcp_cc,
926  $a_rcp_bcc,
927  $a_m_type,
928  $a_m_email,
929  $a_m_subject,
930  $a_m_message,
931  $a_draft_id = 0, $a_use_placeholders = 0)
932  {
933  global $ilDB;
934 
935  $ilDB->update($this->table_mail,
936  array(
937  'folder_id' => array('integer', $a_folder_id),
938  'attachments' => array('clob', serialize($a_attachments)),
939  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
940  'rcp_to' => array('clob', $a_rcp_to),
941  'rcp_cc' => array('clob', $a_rcp_cc),
942  'rcp_bcc' => array('clob', $a_rcp_bcc),
943  'm_status' => array('text', 'read'),
944  'm_type' => array('text', serialize($a_m_type)),
945  'm_email' => array('integer', $a_m_email),
946  'm_subject' => array('text', $a_m_subject),
947  'm_message' => array('clob', $a_m_message),
948  'use_placeholders' => array('integer', $a_use_placeholders)
949  ),
950  array(
951  'mail_id' => array('integer', $a_draft_id)
952  )
953  );
954 
955  return $a_draft_id;
956  }
957 
975  function sendInternalMail($a_folder_id,
976  $a_sender_id,
977  $a_attachments,
978  $a_rcp_to,
979  $a_rcp_cc,
980  $a_rcp_bcc,
981  $a_status,
982  $a_m_type,
983  $a_m_email,
984  $a_m_subject,
985  $a_m_message,
986  $a_user_id = 0, $a_use_placeholders = 0)
987  {
988  $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
989 
990  global $ilDB, $log;
991  //$log->write('class.ilMail->sendInternalMail to user_id:'.$a_rcp_to.' '.$a_m_message);
992 
993  if ($a_use_placeholders)
994  {
995  $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
996  }
997  else
998  {
999  $a_use_placeholders = '0';
1000  }
1001 
1002 
1003  if(!$a_user_id) $a_user_id = '0';
1004  if(!$a_folder_id) $a_folder_id = '0';
1005  if(!$a_sender_id) $a_sender_id = NULL;
1006  if(!$a_attachments) $a_attachments = NULL;
1007  if(!$a_rcp_to) $a_rcp_to = NULL;
1008  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1009  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1010  if(!$a_status) $a_status = NULL;
1011  if(!$a_m_type) $a_m_type = NULL;
1012  if(!$a_m_email) $a_m_email = NULL;
1013  if(!$a_m_subject) $a_m_subject = NULL;
1014  if(!$a_m_message) $a_m_message = NULL;
1015 
1016 
1017  $next_id = $ilDB->nextId($this->table_mail);
1018 
1019  $ilDB->insert($this->table_mail, array(
1020  'mail_id' => array('integer', $next_id),
1021  'user_id' => array('integer', $a_user_id),
1022  'folder_id' => array('integer', $a_folder_id),
1023  'sender_id' => array('integer', $a_sender_id),
1024  'attachments' => array('clob', serialize($a_attachments)),
1025  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
1026  'rcp_to' => array('clob', $a_rcp_to),
1027  'rcp_cc' => array('clob', $a_rcp_cc),
1028  'rcp_bcc' => array('clob', $a_rcp_bcc),
1029  'm_status' => array('text', $a_status),
1030  'm_type' => array('text', serialize($a_m_type)),
1031  'm_email' => array('integer', $a_m_email),
1032  'm_subject' => array('text', $a_m_subject),
1033  'm_message' => array('clob', $a_m_message)
1034  ));
1035 
1036  return $next_id; //$ilDB->getLastInsertId();
1037 
1038  }
1039 
1040  function replacePlaceholders($a_message, $a_user_id)
1041  {
1042  global $lng;
1043 
1044  $user = new ilObjUser($a_user_id);
1045 
1046  // determine salutation
1047  switch ($user->getGender())
1048  {
1049  case 'f': $gender_salut = $lng->txt('salutation_f');
1050  break;
1051  case 'm': $gender_salut = $lng->txt('salutation_m');
1052  break;
1053  }
1054 
1055  $a_message = str_replace('[MAIL_SALUTATION]', $gender_salut, $a_message);
1056  $a_message = str_replace('[LOGIN]', $user->getLogin(), $a_message);
1057  $a_message = str_replace('[FIRST_NAME]', $user->getFirstname(), $a_message);
1058  $a_message = str_replace('[LAST_NAME]', $user->getLastname(), $a_message);
1059  $a_message = str_replace('[ILIAS_URL]', ILIAS_HTTP_PATH.'/login.php?client_id='.CLIENT_ID, $a_message);
1060  $a_message = str_replace('[CLIENT_NAME]', CLIENT_NAME, $a_message);
1061 
1062  unset($user);
1063 
1064  return $a_message;
1065  }
1066 
1080  function distributeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_subject,$a_message,$a_attachments,$sent_mail_id,$a_type,$a_action, $a_use_placeholders = 0)
1081  {
1082  global $log;
1083 
1084  include_once 'Services/Mail/classes/class.ilMailbox.php';
1085  include_once './Services/User/classes/class.ilObjUser.php';
1086 
1087  if (!ilMail::_usePearMail())
1088  {
1089  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1090  $a_rcp_to = $this->__substituteRecipients($a_rcp_to, 'resubstitute');
1091  $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc, 'resubstitute');
1092  $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc, 'resubstitute');
1093  }
1094 
1095  $mbox =& new ilMailbox();
1096 
1097  if (!$a_use_placeholders) # No Placeholders
1098  {
1099  $rcp_ids = $this->getUserIds(trim($a_rcp_to).','.trim($a_rcp_cc).','.trim($a_rcp_bcc));
1100 
1101  $as_email = array();
1102 
1103  foreach($rcp_ids as $id)
1104  {
1105  $tmp_mail_options =& new ilMailOptions($id);
1106 
1107  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1108  $tmp_user =& new ilObjUser($id);
1109  $tmp_user->read();
1110  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1111  $tmp_user->getActive() &&
1112  $tmp_user->checkTimeLimit();
1113 
1114  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1115  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1116  {
1117  continue;
1118  }
1119 
1120  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS/HER MAIL
1121  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1122  if (!$user_can_read_internal_mails ||
1123  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1124  {
1125  $as_email[] = $id;
1126  continue;
1127  }
1128 
1129  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1130  {
1131  $as_email[] = $id;
1132  }
1133 
1134  $mbox->setUserId($id);
1135  $inbox_id = $mbox->getInboxFolder();
1136 
1137  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1138  $a_attachments, $a_rcp_to,
1139  $a_rcp_cc, '', 'unread', $a_type,
1140  0, $a_subject, $a_message, $id, 0);
1141  if ($a_attachments)
1142  {
1143  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1144  }
1145  }
1146 
1147  // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
1148  $to = array();
1149  $bcc = array();
1150 
1151  if (count($as_email) == 1)
1152  {
1153  $to[] = ilObjUser::_lookupEmail($as_email[0]);
1154  }
1155  else
1156  {
1157  foreach ($as_email as $id)
1158  {
1159  $bcc[] = ilObjUser::_lookupEmail($id);
1160  }
1161  }
1162 
1163  if(count($to) > 0 || count($bcc) > 0)
1164  {
1165  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
1166  }
1167  }
1168  else # Use Placeholders
1169  {
1170  // to
1171  $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
1172 
1173  // cc / bcc
1174  $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
1175 
1176  $as_email = array();
1177 
1178  // to
1179  foreach($rcp_ids_replace as $id)
1180  {
1181  $tmp_mail_options =& new ilMailOptions($id);
1182 
1183  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1184  $tmp_user =& new ilObjUser($id);
1185  $tmp_user->read();
1186  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1187  $tmp_user->getActive() &&
1188  $tmp_user->checkTimeLimit();
1189 
1190  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1191  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1192  {
1193  continue;
1194  }
1195 
1196  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1197  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1198  if (!$user_can_read_internal_mails ||
1199  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1200  {
1201  $as_email[] = $id;
1202  continue;
1203  }
1204 
1205  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1206  {
1207  $as_email[] = $id;
1208  }
1209 
1210  $mbox->setUserId($id);
1211  $inbox_id = $mbox->getInboxFolder();
1212 
1213  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1214  $a_attachments, $a_rcp_to,
1215  $a_rcp_cc, '', 'unread', $a_type,
1216  0, $a_subject, $a_message, $id, 1);
1217  if ($a_attachments)
1218  {
1219  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1220  }
1221  }
1222 
1223  if (count($as_email))
1224  {
1225  foreach ($as_email as $id)
1226  {
1227  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
1228  }
1229  }
1230 
1231  $as_email = array();
1232 
1233  // cc / bcc
1234  foreach($rcp_ids_no_replace as $id)
1235  {
1236  $tmp_mail_options =& new ilMailOptions($id);
1237 
1238  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1239  $tmp_user =& new ilObjUser($id);
1240  $tmp_user->read();
1241  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement()
1242  && $tmp_user->getActive() && $tmp_user->checkTimeLimit();
1243 
1244  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1245  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1246  {
1247  continue;
1248  }
1249 
1250  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1251  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1252  if (!$user_can_read_internal_mails ||
1253  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1254  {
1255  $as_email[] = $id;
1256  continue;
1257  }
1258 
1259  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1260  {
1261  $as_email[] = $id;
1262  }
1263 
1264  $mbox->setUserId($id);
1265  $inbox_id = $mbox->getInboxFolder();
1266 
1267  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1268  $a_attachments, $a_rcp_to,
1269  $a_rcp_cc, '', 'unread', $a_type,
1270  0, $a_subject, $a_message, $id, 0);
1271  if ($a_attachments)
1272  {
1273  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1274  }
1275  }
1276 
1277  if (count($as_email))
1278  {
1279  foreach ($as_email as $id)
1280  {
1281  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $a_message, $a_attachments);
1282  }
1283  }
1284  }
1285 
1286  return true;
1287  }
1288 
1294  function getUserIds($a_recipients)
1295  {
1296  global $log, $rbacreview;
1297  $ids = array();
1298 
1299  if (ilMail::_usePearMail())
1300  {
1301  $tmp_names = $this->explodeRecipients($a_recipients);
1302  if (! is_a($tmp_names, 'PEAR_Error'))
1303  {
1304  for ($i = 0;$i < count($tmp_names); $i++)
1305  {
1306  if ( substr($tmp_names[$i]->mailbox,0,1) === '#' ||
1307  (substr($tmp_names[$i]->mailbox,0,1) === '"' &&
1308  substr($tmp_names[$i]->mailbox,1,1) === '#' ) )
1309  {
1310  $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1311  foreach($role_ids as $role_id)
1312  {
1313  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1314  {
1315  $ids[] = $usr_id;
1316  }
1317  }
1318  }
1319  else if (strtolower($tmp_names[$i]->host) == 'ilias')
1320  {
1321  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
1322  {
1323  //$log->write('class.ilMail->getUserIds() recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host.' user_id:'.$id);
1324  $ids[] = $id;
1325  }
1326  else
1327  {
1328  //$log->write('class.ilMail->getUserIds() no user account found for recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1329  }
1330  }
1331  else
1332  {
1333  // Fixed mantis bug #5875
1334  if($id = ilObjUser::_lookupId($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host))
1335  {
1336  $ids[] = $id;
1337  }
1338  else
1339  {
1340  //$log->write('class.ilMail->getUserIds() external recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1341  }
1342  }
1343  }
1344  }
1345  else
1346  {
1347  //$log->write('class.ilMail->getUserIds() illegal recipients:'.$a_recipients);
1348  }
1349  }
1350  else
1351  {
1352  $tmp_names = $this->explodeRecipients($a_recipients);
1353  for ($i = 0;$i < count($tmp_names); $i++)
1354  {
1355  if (substr($tmp_names[$i],0,1) == '#')
1356  {
1357  if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
1358  {
1359  include_once("./classes/class.ilObjectFactory.php");
1360  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1361 
1362  foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1363  {
1364  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1365  break;
1366  }
1367  // STORE MEMBER IDS IN $ids
1368  foreach ($grp_object->getGroupMemberIds() as $id)
1369  {
1370  $ids[] = $id;
1371  }
1372  }
1373  // is role: get role ids
1374  elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
1375  {
1376  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1377  {
1378  $ids[] = $usr_id;
1379  }
1380  }
1381 
1382  }
1383  else if (!empty($tmp_names[$i]))
1384  {
1385  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
1386  {
1387  $ids[] = $id;
1388  }
1389  }
1390  }
1391  }
1392  return array_unique($ids);
1393  }
1394 
1405  function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
1406  {
1407  $error_message = '';
1408 
1409  $a_m_subject = trim($a_m_subject);
1410  $a_rcp_to = trim($a_rcp_to);
1411 
1412  if (empty($a_m_subject))
1413  {
1414  $error_message .= $error_message ? "<br>" : '';
1415  $error_message .= $this->lng->txt("mail_add_subject");
1416  }
1417 
1418  if (empty($a_rcp_to))
1419  {
1420  $error_message .= $error_message ? "<br>" : '';
1421  $error_message .= $this->lng->txt("mail_add_recipient");
1422  }
1423 
1424  return $error_message;
1425  }
1426 
1433  function getEmailsOfRecipients($a_rcp)
1434  {
1435  $addresses = array();
1436 
1437  if (ilMail::_usePearMail())
1438  {
1439  $tmp_rcp = $this->explodeRecipients($a_rcp);
1440  if (! is_a($tmp_rcp, 'PEAR_Error'))
1441  {
1442  foreach ($tmp_rcp as $rcp)
1443  {
1444  // NO GROUP
1445  if (substr($rcp->mailbox,0,1) != '#')
1446  {
1447  if (strtolower($rcp->host) != 'ilias')
1448  {
1449  $addresses[] = $rcp->mailbox.'@'.$rcp->host;
1450  continue;
1451  }
1452 
1453  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
1454  {
1455  $tmp_user = new ilObjUser($id);
1456  $addresses[] = $tmp_user->getEmail();
1457  continue;
1458  }
1459  }
1460  else
1461  {
1462  // Roles
1463  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1464  foreach($role_ids as $role_id)
1465  {
1466  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1467  {
1468  $tmp_user = new ilObjUser($usr_id);
1469  $addresses[] = $tmp_user->getEmail();
1470  }
1471  }
1472  }
1473  }
1474  }
1475  }
1476  else
1477  {
1478  $tmp_rcp = $this->explodeRecipients($a_rcp);
1479 
1480  foreach ($tmp_rcp as $rcp)
1481  {
1482  // NO GROUP
1483  if (substr($rcp,0,1) != '#')
1484  {
1485  if (strpos($rcp,'@'))
1486  {
1487  $addresses[] = $rcp;
1488  continue;
1489  }
1490 
1491  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1492  {
1493  $tmp_user = new ilObjUser($id);
1494  $addresses[] = $tmp_user->getEmail();
1495  continue;
1496  }
1497  }
1498  else
1499  {
1500  // GROUP THINGS
1501  include_once("./classes/class.ilObjectFactory.php");
1502  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1503 
1504  // Fix
1505  foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1506  {
1507  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1508  break;
1509  }
1510  // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
1511  foreach ($grp_object->getGroupMemberIds() as $id)
1512  {
1513  $tmp_user = new ilObjUser($id);
1514  $addresses[] = $tmp_user->getEmail();
1515  }
1516  }
1517  }
1518  }
1519 
1520  return $addresses;
1521  }
1522 
1530  function checkRecipients($a_recipients,$a_type)
1531  {
1532  global $rbacsystem,$rbacreview;
1533  $wrong_rcps = '';
1534 
1535  if (ilMail::_usePearMail())
1536  {
1537  $tmp_rcp = $this->explodeRecipients($a_recipients);
1538  if (is_a($tmp_rcp, 'PEAR_Error'))
1539  {
1540  $colon_pos = strpos($tmp_rcp->message, ':');
1541  $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
1542  }
1543  else
1544  {
1545  foreach ($tmp_rcp as $rcp)
1546  {
1547  // NO ROLE MAIL ADDRESS
1548  if (substr($rcp->mailbox,0,1) != '#')
1549  {
1550  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1551  $user_id = ($rcp->host == 'ilias') ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
1552  if ($user_id == false && $rcp->host == 'ilias')
1553  {
1554  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
1555  continue;
1556  }
1557 
1558  // CHECK IF USER CAN RECEIVE MAIL
1559  if ($user_id)
1560  {
1561  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1562  {
1563  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1564  " (".$this->lng->txt("user_cant_receive_mail").")";
1565  continue;
1566  }
1567  }
1568  }
1569  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
1570  {
1571  if (!$this->mlists->mailingListExists($rcp->mailbox))
1572  {
1573  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1574  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1575  }
1576 
1577  continue;
1578  }
1579  else
1580  {
1581  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1582  if (count($role_ids) == 0)
1583  {
1584  $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
1585  ' ('.$this->lng->txt('mail_no_recipient_found').')';
1586  continue;
1587  }
1588  else if (count($role_ids) > 1)
1589  {
1590  $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
1591  ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
1592  }
1593  }
1594  }
1595  }
1596  }
1597  else
1598  {
1599  $tmp_rcp = $this->explodeRecipients($a_recipients);
1600 
1601  foreach ($tmp_rcp as $rcp)
1602  {
1603  if (empty($rcp))
1604  {
1605  continue;
1606  }
1607  // NO GROUP
1608  if (substr($rcp,0,1) != '#')
1609  {
1610  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1611  if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
1612  !ilUtil::is_email($rcp))
1613  {
1614  $wrong_rcps .= "<br />".htmlentities($rcp);
1615  continue;
1616  }
1617 
1618  // CHECK IF USER CAN RECEIVE MAIL
1619  if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1620  {
1621  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1622  {
1623  $wrong_rcps .= "<br />".htmlentities($rcp).
1624  " (".$this->lng->txt("user_cant_receive_mail").")";
1625  continue;
1626  }
1627  }
1628  }
1629  else if (substr($rcp, 0, 7) == '#il_ml_')
1630  {
1631  if (!$this->mlists->mailingListExists($rcp))
1632  {
1633  $wrong_rcps .= "<br />".htmlentities($rcp).
1634  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1635  }
1636 
1637  continue;
1638  }
1639  else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
1640  {
1641  continue;
1642  }
1643  else if (!$rbacreview->roleExists(addslashes(substr($rcp,1))))
1644  {
1645  $wrong_rcps .= "<br />".htmlentities($rcp).
1646  " (".$this->lng->txt("mail_no_valid_group_role").")";
1647  continue;
1648  }
1649  }
1650  }
1651  return $wrong_rcps;
1652  }
1653 
1669  function savePostData($a_user_id,
1670  $a_attachments,
1671  $a_rcp_to,
1672  $a_rcp_cc,
1673  $a_rcp_bcc,
1674  $a_m_type,
1675  $a_m_email,
1676  $a_m_subject,
1677  $a_m_message,
1678  $a_use_placeholders)
1679  {
1680  global $ilDB;
1681 
1682 
1683  if(!$a_attachments) $a_attachments = NULL;
1684  if(!$a_rcp_to) $a_rcp_to = NULL;
1685  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1686  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1687  if(!$a_m_type) $a_m_type = NULL;
1688  if(!$a_m_email) $a_m_email = NULL;
1689  if(!$a_m_message) $a_m_message = NULL;
1690  if(!$a_use_placeholders) $a_use_placeholders = '0';
1691 
1692 
1693  $statement = $ilDB->manipulateF('
1694  DELETE FROM '. $this->table_mail_saved .'
1695  WHERE user_id = %s',
1696  array('integer'), array($this->user_id));
1697 
1698  $ilDB->insert($this->table_mail_saved, array(
1699  'user_id' => array('integer', $a_user_id),
1700  'attachments' => array('clob', serialize($a_attachments)),
1701  'rcp_to' => array('clob', $a_rcp_to),
1702  'rcp_cc' => array('clob', $a_rcp_cc),
1703  'rcp_bcc' => array('clob', $a_rcp_bcc),
1704  'm_type' => array('text', serialize($a_m_type)),
1705  'm_email' => array('integer', $a_m_email),
1706  'm_subject' => array('text', $a_m_subject),
1707  'm_message' => array('clob', $a_m_message),
1708  'use_placeholders' => array('integer', $a_use_placeholders),
1709  ));
1710 
1711  $this->getSavedData();
1712 
1713  return true;
1714  }
1715 
1721  function getSavedData()
1722  {
1723  global $ilDB;
1724 
1725  $res = $ilDB->queryf('
1726  SELECT * FROM '. $this->table_mail_saved .'
1727  WHERE user_id = %s',
1728  array('integer'),
1729  array($this->user_id));
1730 
1731  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
1732 
1733  return $this->mail_data;
1734  }
1735 
1749  function sendMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$a_type, $a_use_placeholders = 0)
1750  {
1751  global $lng,$rbacsystem,$log;
1752  //$log->write('class.ilMail.sendMail '.$a_rcp_to.' '.$a_m_subject);
1753  $error_message = '';
1754  $message = '';
1755 
1756  if (in_array("system",$a_type))
1757  {
1758  $this->__checkSystemRecipients($a_rcp_to);
1759  $a_type = array('system');
1760  }
1761 
1762  if ($a_attachment)
1763  {
1764  if (!$this->mfile->checkFilesExist($a_attachment))
1765  {
1766  return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
1767  }
1768  }
1769  // CHECK NECESSARY MAIL DATA FOR ALL TYPES
1770  if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
1771  {
1772  return $error_message;
1773  }
1774  // check recipients
1775  if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
1776  {
1777  $message .= $error_message;
1778  }
1779 
1780  if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
1781  {
1782  $message .= $error_message;
1783  }
1784 
1785  if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
1786  {
1787  $message .= $error_message;
1788  }
1789  // if there was an error
1790  if (!empty($message))
1791  {
1792  return $this->lng->txt("mail_following_rcp_not_valid").$message;
1793  }
1794 
1795  // CHECK FOR SYSTEM MAIL
1796  if (in_array('system',$a_type))
1797  {
1798  if (!empty($a_attachment))
1799  {
1800  return $lng->txt("mail_no_attach_allowed");
1801  }
1802  }
1803 
1804  // ACTIONS FOR ALL TYPES
1805  // GET RCPT OF MAILING LISTS
1806  $rcp_to = $this->parseRcptOfMailingLists($a_rcp_to);
1807  $rcp_cc = $this->parseRcptOfMailingLists($a_rcp_cc);
1808  $rcp_bc = $this->parseRcptOfMailingLists($a_rcp_bc);
1809 
1810  if (! ilMail::_usePearMail())
1811  {
1812  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1813  $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
1814  $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
1815  $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
1816  }
1817 
1818  // COUNT EMAILS
1819  $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
1820  $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
1821 
1822  // currently disabled..
1823  /*
1824  if (count($c_emails))
1825  {
1826  if (!$this->getEmailOfSender())
1827  {
1828  return $lng->txt("mail_check_your_email_addr");
1829  }
1830  }
1831  */
1832 
1833  // check smtp permission
1834  if($c_emails && $this->user_id != ANONYMOUS_USER_ID &&
1835  !$rbacsystem->checkAccess('smtp_mail', $this->mail_obj_ref_id))
1836  {
1837  return $this->lng->txt('mail_no_permissions_write_smtp');
1838  }
1839 
1840  if($this->appendInstallationSignature())
1841  {
1842  $a_m_message .= self::_getInstallationSignature();
1843  }
1844 
1845  // save mail in sent box
1846  $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
1847  $a_m_subject,$a_m_message);
1848 
1849  if($a_attachment)
1850  {
1851  $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
1852 
1853  if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
1854  {
1855  return $error;
1856  }
1857  }
1858 
1859  // FILTER EMAILS
1860  // IF EMAIL RECIPIENT
1861  if($c_emails)
1862  {
1863  $this->sendMimeMail($this->__getEmailRecipients($rcp_to),
1864  $this->__getEmailRecipients($rcp_cc),
1865  $this->__getEmailRecipients($rcp_bc),
1866  $a_m_subject,
1867  $a_m_message,
1868  $a_attachment,
1869  0);
1870  }
1871 
1872  if (in_array('system',$a_type))
1873  {
1874  if (!$this->distributeMail($rcp_to,$rcp_cc,$rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$sent_id,$a_type,'system', $a_use_placeholders))
1875  {
1876  return $lng->txt("mail_send_error");
1877  }
1878  }
1879  // ACTIONS FOR TYPE SYSTEM AND NORMAL
1880  if (in_array('normal',$a_type))
1881  {
1882  // TRY BOTH internal and email (depends on user settings)
1883  if (!$this->distributeMail($rcp_to,$rcp_cc,$rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$sent_id,$a_type,'normal', $a_use_placeholders))
1884  {
1885  return $lng->txt("mail_send_error");
1886  }
1887  }
1888 
1889  // Temporary bugfix
1890  if (!$this->getSaveInSentbox())
1891  {
1892  $this->deleteMails(array($sent_id));
1893  }
1894 
1895  return '';
1896  }
1897 
1898  function parseRcptOfMailingLists($rcpt = '')
1899  {
1900  if ($rcpt == '') return $rcpt;
1901 
1902  $arrRcpt = $this->explodeRecipients(trim($rcpt));
1903  if (!is_array($arrRcpt) || empty($arrRcpt)) return $rcpt;
1904 
1905  $new_rcpt = array();
1906 
1907  foreach ($arrRcpt as $item)
1908  {
1909  if (ilMail::_usePearMail())
1910  {
1911  if (substr($item->mailbox, 0, 7) == '#il_ml_')
1912  {
1913  if ($this->mlists->mailingListExists($item->mailbox))
1914  {
1915  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1916  {
1917  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1918  }
1919  }
1920  }
1921  else
1922  {
1923  $new_rcpt[] = $item->mailbox.'@'.$item->host;
1924  }
1925  }
1926  else
1927  {
1928  if (substr($item, 0, 7) == '#il_ml_')
1929  {
1930  if ($this->mlists->mailingListExists($item))
1931  {
1932  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1933  {
1934  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1935  }
1936  }
1937  }
1938  else
1939  {
1940  $new_rcpt[] = $item;
1941  }
1942  }
1943  }
1944 
1945  return implode(',', $new_rcpt);
1946  }
1947 
1960  function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
1961  $a_m_subject,$a_m_message)
1962  {
1963  include_once "Services/Mail/classes/class.ilMailbox.php";
1964 
1965  $mbox = new ilMailbox($this->user_id);
1966  $sent_id = $mbox->getSentFolder();
1967 
1968  return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
1969  $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
1970  }
1971 
1972 
1973 
1979  function addFullname($a_email)
1980  {
1981  include_once 'Services/Mail/classes/class.ilMimeMail.php';
1982 
1983  global $ilUser;
1984 
1985  return ilMimeMail::_mimeEncode($ilUser->getFullname()).' <'.$a_email.'>';
1986  }
1987 
1994  public function getMimeMailSender()
1995  {
1996  include_once "Services/Mail/classes/class.ilMimeMail.php";
1997 
1998  if($this->user_id != ANONYMOUS_USER_ID)
1999  {
2000  $sender = $this->addFullname($this->getEmailOfSender());
2001  }
2002  else
2003  {
2004  $no_reply_adress = trim($this->ilias->getSetting('mail_external_sender_noreply'));
2005  if(strlen($no_reply_adress))
2006  {
2007  if(strpos($no_reply_adress, '@') === false)
2008  $no_reply_adress = 'noreply@'.$no_reply_adress;
2009  if(!ilUtil::is_email($no_reply_adress))
2010  {
2011  $no_reply_adress = 'noreply@'.$_SERVER['SERVER_NAME'];
2012  }
2013 
2014  $sender = ilMimeMail::_mimeEncode(self::_getAnonymousName()).
2015  ' <'.$no_reply_adress.'>';
2016  }
2017  else
2018  {
2019  $sender = ilMimeMail::_mimeEncode(self::_getAnonymousName()).
2020  ' <noreply@'.$_SERVER['SERVER_NAME'].'>';
2021  }
2022  }
2023 
2024  return $sender;
2025  }
2026 
2039  function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
2040  {
2041  include_once "Services/Mail/classes/class.ilMimeMail.php";
2042 
2043  #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
2044 
2045  #$inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 4";
2046  #$a_m_subject = "[".$inst_name."] ".$a_m_subject;
2047 
2048  $a_m_subject = self::getSubjectPrefix().' '.$a_m_subject;
2049 
2050  $sender = $this->getMimeMailSender();
2051 
2052  if($this->isSOAPEnabled())
2053  {
2054  // Send per soap
2055  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
2056 
2057  $soap_client = new ilSoapClient();
2058  $soap_client->setTimeout(1);
2059  $soap_client->setResponseTimeout(1);
2060  $soap_client->enableWSDL(true);
2061  $soap_client->init();
2062 
2063  $attachments = array();
2064  $a_attachments = $a_attachments ? $a_attachments : array();
2065  foreach($a_attachments as $attachment)
2066  {
2067  $attachments[] = $this->mfile->getAbsolutePath($attachment);
2068  }
2069  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
2070  $attachments = implode('#:#',$attachments);
2071  // mjansen: use "#:#" as leading delimiter
2072  if(strlen($attachments))
2073  $attachments = "#:#".$attachments;
2074 
2075  $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'], // session id
2076  $a_rcp_to,
2077  $a_rcp_cc,
2078  $a_rcp_bcc,
2079  $sender,
2080  $a_m_subject,
2081  $a_m_message,
2082  $attachments));
2083 
2084  return true;
2085  }
2086  else
2087  {
2088  // send direct
2089  include_once "Services/Mail/classes/class.ilMimeMail.php";
2090 
2091  $mmail = new ilMimeMail();
2092  $mmail->autoCheck(false);
2093  $mmail->From($sender);
2094  $mmail->To($a_rcp_to);
2095  // Add installation name to subject
2096  $mmail->Subject($a_m_subject);
2097  $mmail->Body($a_m_message);
2098 
2099  if ($a_rcp_cc)
2100  {
2101  $mmail->Cc($a_rcp_cc);
2102  }
2103 
2104  if ($a_rcp_bcc)
2105  {
2106  $mmail->Bcc($a_rcp_bcc);
2107  }
2108 
2109  if (is_array($a_attachments))
2110  {
2111  foreach ($a_attachments as $attachment)
2112  {
2113  $mmail->Attach($this->mfile->getAbsolutePath($attachment));
2114  }
2115  }
2116 
2117  $mmail->Send();
2118  }
2119  }
2125  function getEmailOfSender()
2126  {
2127  $umail = new ilObjUser($this->user_id);
2128  $sender = $umail->getEmail();
2129 
2130  if (ilUtil::is_email($sender))
2131  {
2132  return $sender;
2133  }
2134  else
2135  {
2136  return '';
2137  }
2138  }
2139 
2146  function saveAttachments($a_attachments)
2147  {
2148  global $ilDB;
2149 
2150  $ilDB->update($this->table_mail_saved,
2151  array
2152  (
2153  'attachments' => array('clob', serialize($a_attachments))
2154  ),
2155  array
2156  (
2157  'user_id' => array('integer', $this->user_id)
2158  )
2159  );
2160 
2161  return true;
2162  }
2163 
2169  function getAttachments()
2170  {
2171  return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
2172  }
2173 
2187  function explodeRecipients($a_recipients)
2188  {
2189  if (ilMail::_usePearMail())
2190  {
2191  if (strlen(trim($a_recipients)) > 0)
2192  {
2193  require_once 'Mail/RFC822.php';
2194  $parser = &new Mail_RFC822();
2195  return $parser->parseAddressList($a_recipients, "ilias", false, true);
2196  } else {
2197  return array();
2198  }
2199  }
2200  else
2201  {
2202  $a_recipients = trim($a_recipients);
2203 
2204  // WHITESPACE IS NOT ALLOWED AS SEPERATOR
2205  #$a_recipients = preg_replace("/ /",",",$a_recipients);
2206  $a_recipients = preg_replace("/;/",",",$a_recipients);
2207 
2208 
2209  foreach(explode(',',$a_recipients) as $tmp_rec)
2210  {
2211  if($tmp_rec)
2212  {
2213  $rcps[] = trim($tmp_rec);
2214  }
2215  }
2216  return is_array($rcps) ? $rcps : array();
2217  }
2218  }
2219 
2220  function __getCountRecipient($rcp,$a_only_email = true)
2221  {
2222  $counter = 0;
2223 
2224  if (ilMail::_usePearMail())
2225  {
2226  $tmp_rcp = $this->explodeRecipients($rcp);
2227  if (! is_a($tmp_rcp, 'PEAR_Error'))
2228  {
2229  foreach ($tmp_rcp as $to)
2230  {
2231  if ($a_only_email)
2232  {
2233  // Fixed mantis bug #5875
2234  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2235  {
2236  continue;
2237  }
2238 
2239  // Addresses which aren't on the ilias host, and
2240  // which have a mailbox which does not start with '#',
2241  // are external e-mail addresses
2242  if ($to->host != 'ilias' && substr($to->mailbox,0,1) != '#')
2243  {
2244  ++$counter;
2245  }
2246  }
2247  else
2248  {
2249  ++$counter;
2250  }
2251  }
2252  }
2253  }
2254  else
2255  {
2256  foreach ($this->explodeRecipients($rcp) as $to)
2257  {
2258  if ($a_only_email)
2259  {
2260  $to = $this->__substituteRecipients($to,"resubstitute");
2261  if (strpos($to,'@'))
2262  {
2263  // Fixed mantis bug #5875
2264  if(ilObjUser::_lookupId($to))
2265  {
2266  continue;
2267  }
2268 
2269  ++$counter;
2270  }
2271  }
2272  else
2273  {
2274  ++$counter;
2275  }
2276  }
2277  }
2278  return $counter;
2279  }
2280 
2281 
2282  function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
2283  {
2284  return $this->__getCountRecipient($a_to,$a_only_email)
2285  + $this->__getCountRecipient($a_cc,$a_only_email)
2286  + $this->__getCountRecipient($a_bcc,$a_only_email);
2287  }
2288 
2289  function __getEmailRecipients($a_rcp)
2290  {
2291  if (ilMail::_usePearMail())
2292  {
2293  $rcp = array();
2294  $tmp_rcp = $this->explodeRecipients($a_rcp);
2295  if (! is_a($tmp_rcp, 'PEAR_Error'))
2296  {
2297  foreach ($tmp_rcp as $to)
2298  {
2299  if(substr($to->mailbox,0,1) != '#' && $to->host != 'ilias')
2300  {
2301  // Fixed mantis bug #5875
2302  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2303  {
2304  continue;
2305  }
2306 
2307  $rcp[] = $to->mailbox.'@'.$to->host;
2308  }
2309  }
2310  }
2311  return implode(',',$rcp);
2312  }
2313  else
2314  {
2315  foreach ($this->explodeRecipients($a_rcp) as $to)
2316  {
2317  $to = $this->__substituteRecipients($to,"resubstitute");
2318  if(strpos($to,'@'))
2319  {
2320  // Fixed mantis bug #5875
2321  if(ilObjUser::_lookupId($to))
2322  {
2323  continue;
2324  }
2325 
2326  $rcp[] = $to;
2327  }
2328  }
2329  return implode(',',$rcp ? $rcp : array());
2330  }
2331  }
2332 
2333  function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
2334  {
2335  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
2336 
2337  $message = $inst_name." To:".$rcp_to."\n";
2338 
2339  if ($rcp_cc)
2340  {
2341  $message .= "Cc: ".$rcp_cc;
2342  }
2343 
2344  $message .= "\n\n";
2345  $message .= $a_m_message;
2346 
2347  return $message;
2348  }
2349 
2350  function __checkSystemRecipients(&$a_rcp_to)
2351  {
2352  if (preg_match("/@all/",$a_rcp_to))
2353  {
2354  // GET ALL LOGINS
2355  $all = ilObjUser::_getAllUserLogins($this->ilias);
2356  $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
2357  }
2358 
2359  return;
2360  }
2361 
2369  function __substituteRecipients($a_rcp,$direction)
2370  {
2371  $new_name = array();
2372 
2373  $tmp_names = $this->explodeRecipients($a_rcp);
2374 
2375 
2376  foreach($tmp_names as $name)
2377  {
2378  if(strpos($name,"#") === 0)
2379  {
2380  $new_name[] = $name;
2381  continue;
2382  }
2383  switch($direction)
2384  {
2385  case "substitute":
2386  if(strpos($name,"@") and ilObjUser::_loginExists($name))
2387  {
2388  $new_name[] = preg_replace("/@/","�#�",$name);
2389  }
2390  else
2391  {
2392  $new_name[] = $name;
2393  }
2394  break;
2395 
2396  case "resubstitute":
2397  if(stristr($name,"�#�"))
2398  {
2399  $new_name[] = preg_replace("/�#�/","@",$name);
2400  }
2401  else
2402  {
2403  $new_name[] = $name;
2404  }
2405  break;
2406  }
2407  }
2408  return implode(",",$new_name);
2409  }
2410 
2426  public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null) {
2427  if (ilMail::_usePearMail())
2428  {
2429  if ($login == null)
2430  {
2431  require_once './Services/User/classes/class.ilObjUser.php';
2432  $usr_obj = new ilObjUser($usr_id);
2433  $usr_obj->read();
2434  $login = $usr_obj->getLogin();
2435  $firstname = $usr_obj->getFirstname();
2436  $lastname = $usr_obj->getLastname();
2437  }
2438  // The following line of code creates a properly formatted mailbox
2439  // address. Unfortunately, it does not work, because ILIAS removes
2440  // everything between '<' '>' characters
2441  // Therefore, we just return the login - sic.
2442  // FIXME - Make this work in a future release
2443  /*
2444  return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
2445  */
2446  return $login.'hhho';
2447  }
2448  else
2449  {
2450  return $login;
2451  }
2452  }
2459  public static function _usePearMail() {
2460  global $ilias;
2461 
2462  $result = false;
2463  if ($ilias->getSetting('pear_mail_enable') == true)
2464  {
2465  // Note: We use the include statement to determine whether PEAR MAIL is
2466  // installed. We use the @ operator to prevent PHP from issuing a
2467  // warning while we test for PEAR MAIL.
2468  $is_pear_mail_installed = @include_once 'Mail/RFC822.php';
2469  if ($is_pear_mail_installed) {
2470  $result = true;
2471  } else {
2472  // Disable Pear Mail, when we detect that it is not
2473  // installed
2474  global $log;
2475  $log->write("WARNING: ilMail::_userPearMail disabled Pear Mail support, because include 'Mail/RFC822.php' failed.");
2476  $ilias->setSetting('pear_mail_enable', false);
2477  }
2478  }
2479  return $result;
2480  }
2481 
2490  public static function _getAutoGeneratedMessageString($lang = null)
2491  {
2492  global $ilSetting;
2493 
2494  if(!$lang)
2495  {
2496  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
2498  }
2499  $lang->loadLanguageModule('mail');
2500  return sprintf($lang->txt('mail_auto_generated_info'),
2501  $ilSetting->get('inst_name','ILIAS 4'),
2502  ILIAS_HTTP_PATH."\n\n");
2503  }
2504 
2513  public static function _getAnonymousName()
2514  {
2515  return 'ILIAS';
2516  }
2517 
2526  public function appendInstallationSignature($a_flag = null)
2527  {
2528  if(null === $a_flag) {
2530  }
2531 
2532  $this->appendInstallationSignature = $a_flag;
2533 
2534  return $this;
2535  }
2536 
2545  public static function _getInstallationSignature()
2546  {
2547  global $ilClientIniFile;
2548 
2549  $signature = "\n\n* * * * *\n";
2550  $signature .= $ilClientIniFile->readVariable('client', 'name')."\n";
2551  if(strlen($desc = $ilClientIniFile->readVariable('client', 'description')))
2552  {
2553  $signature .= $desc."\n";
2554  }
2555  $signature .= ILIAS_HTTP_PATH;
2556 
2557  $clientdirs = glob(ILIAS_WEB_DIR."/*", GLOB_ONLYDIR);
2558  if(is_array($clientdirs) && count($clientdirs) > 1)
2559  {
2560  $signature .= '/?client_id='.CLIENT_ID;
2561  }
2562 
2563  return $signature;
2564  }
2565 
2570  public static function getSubjectPrefix()
2571  {
2572  global $ilSetting;
2573  static $prefix = null;
2574 
2575  return $prefix == null ? $ilSetting->get('mail_subject_prefix','') : $prefix;
2576  }
2577 
2583  public static function getSalutation($a_usr_id,$a_language = null)
2584  {
2585  global $lng;
2586 
2587  $lang = $a_language ? $a_language : $lng;
2588 
2589  $gender = ilObjUser::_lookupGender($a_usr_id);
2590  $gender = $gender ? $gender : 'n';
2591  $name = ilObjUser::_lookupName($a_usr_id);
2592 
2593  if(!strlen($name['firstname']))
2594  {
2595  return $lang->txt('mail_salutation_anonymous').',';
2596  }
2597 
2598  return $lang->txt('mail_salutation_'.$gender).' '.
2599  ($name['title'] ? $name['title'].' ' : '').
2600  ($name['firstname'] ? $name['firstname'].' ' : '').
2601  $name['lastname'].',';
2602  }
2603 } // END class.ilMail
2604 ?>