ILIAS  Release_3_10_x_branch Revision 61812
 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  private $mlists = null;
228 
229 
236  function ilMail($a_user_id)
237  {
238  require_once "classes/class.ilFileDataMail.php";
239  require_once "Services/Mail/classes/class.ilMailOptions.php";
240  require_once "Services/Mail/classes/class.ilMailingLists.php";
241 
242  global $ilias, $lng, $ilUser;
243 
244  $lng->loadLanguageModule("mail");
245 
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  if(!extension_loaded('curl'))
337  {
338  return false;
339  }
340  return (bool) $this->soap_enabled;
341  }
342 
343 
344  function setSaveInSentbox($a_save_in_sentbox)
345  {
346  $this->save_in_sentbox = $a_save_in_sentbox;
347  }
348 
349  function getSaveInSentbox()
350  {
351  return $this->save_in_sentbox;
352  }
353 
359  function setMailSendType($a_types)
360  {
361  $this->mail_send_type = $a_types;
362  }
363 
369  function setMailRcpTo($a_rcp_to)
370  {
371  $this->mail_rcp_to = $a_rcp_to;
372  }
373 
379  function setMailRcpCc($a_rcp_cc)
380  {
381  $this->mail_rcp_cc = $a_rcp_cc;
382  }
383 
389  function setMailRcpBc($a_rcp_bc)
390  {
391  $this->mail_rcp_bc = $a_rcp_bc;
392  }
393 
399  function setMailSubject($a_subject)
400  {
401  $this->mail_subject = $a_subject;
402  }
403 
409  function setMailMessage($a_message)
410  {
411  $this->mail_message = $a_message;
412  }
413 
419  {
420  global $ilDB;
421 
422  // mail settings id is set by a constant in ilias.ini. Keep the select for some time until everyone has updated his ilias.ini
423  if (!MAIL_SETTINGS_ID)
424  {
425  $query = "SELECT object_reference.ref_id FROM object_reference,tree,object_data ".
426  "WHERE tree.parent = ".$ilDB->quote(SYSTEM_FOLDER_ID)." ".
427  "AND object_data.type = 'mail' ".
428  "AND object_reference.ref_id = tree.child ".
429  "AND object_reference.obj_id = object_data.obj_id";
430  $res = $this->ilias->db->query($query);
431 
432  while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
433  {
434  $this->mail_obj_ref_id = $row["ref_id"];
435  }
436  }
437  else
438  {
439  $this->mail_obj_ref_id = MAIL_SETTINGS_ID;
440  }
441  }
442 
444  {
445  return $this->mail_obj_ref_id;
446  }
447 
449  {
450  return $this->lng->txt('mail_notification_subject');
451  }
452 
454  {
455  global $tpl, $lng;
456 
457  $tpl =& new ilTemplate('tpl.mail_notifications.html', true, true, 'Services/Mail');
458 
459  $tpl->setVariable('TXT_RECEIVED_MAILS', sprintf($this->lng->txt('mail_received_x_new_mails'), count($mail_data)));
460 
461  $counter = 0;
462  foreach ($mail_data as $mail)
463  {
464  $tpl->setCurrentBlock('mails');
465  $tpl->setVariable('NR', $counter + 1);
466  $tpl->setVariable('TXT_SENT', $this->lng->txt('sent'));
467  $tpl->setVariable('SEND_TIME', ilDatePresentation::formatDate(new ilDateTime($mail['send_time'],IL_CAL_DATETIME)));
468 
469  $tpl->setVariable('TXT_SUBJECT', $this->lng->txt('subject'));
470  $tpl->setVariable('SUBJECT', $mail['m_subject']);
471  $tpl->parseCurrentBlock();
472 
473  ++$counter;
474  }
475 
476  $message = $this->replacePlaceholders($tpl->get(), $user_id);
477 
478  return $message;
479  }
480 
491  public function formatNamesForOutput($users = '')
492  {
493  $users = trim($users);
494  if($users)
495  {
496  if(strstr($users, ','))
497  {
498  $rcp_to_array = array();
499 
500  $recipients = explode(',', $users);
501  foreach($recipients as $recipient)
502  {
503  $recipient = trim($recipient);
504  if($uid = ilObjUser::_lookupId($recipient))
505  {
506  $tmp_obj = new ilObjUser($uid);
507 
508  if(ilObjUser::_lookupPref($uid, 'public_profile') == 'y')
509  {
510  $rcp_to_array[] = $tmp_obj->getFullname().' ['.$recipient.']';
511  }
512  else
513  {
514  $rcp_to_array[] = $recipient;
515  }
516  unset($tmp_obj);
517  }
518  else
519  {
520  $rcp_to_array[] = $recipient;
521  }
522  }
523 
524  return trim(implode(', ', $rcp_to_array));
525  }
526  else
527  {
528  if($uid = ilObjUser::_lookupId($users))
529  {
530  $tmp_obj = new ilObjUser($uid);
531  if(ilObjUser::_lookupPref($uid, 'public_profile') == 'y')
532  {
533  return $tmp_obj->getFullname().' ['.$users.']';
534  }
535  else
536  {
537  unset($tmp_obj);
538  return $users;
539  }
540  }
541  else
542  {
543  return $users;
544  }
545  }
546  }
547  else
548  {
549  return $this->lng->txt('not_available');
550  }
551  }
552 
553  function getPreviousMail($a_mail_id)
554  {
555  global $ilDB;
556 
557  $query = "SELECT b.* FROM " . $this->table_mail ." AS a ".
558  "INNER JOIN ".$this->table_mail ." AS b ON b.folder_id = a.folder_id AND b.user_id = a.user_id AND b.send_time > a.send_time ".
559  "WHERE a.user_id = ".$ilDB->quote($this->user_id) ." ".
560  "AND a.mail_id = ".$ilDB->quote($a_mail_id)." ORDER BY b.send_time ASC LIMIT 1";
561 
562  $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
563 
564  return $this->mail_data;
565  }
566 
567  function getNextMail($a_mail_id)
568  {
569  global $ilDB;
570 
571  $query = "SELECT b.* FROM " . $this->table_mail ." AS a ".
572  "INNER JOIN ".$this->table_mail ." AS b ON b.folder_id = a.folder_id AND b.user_id = a.user_id AND b.send_time < a.send_time ".
573  "WHERE a.user_id = ".$ilDB->quote($this->user_id) ." ".
574  "AND a.mail_id = ".$ilDB->quote($a_mail_id)." ORDER BY b.send_time DESC LIMIT 1";
575 
576  $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
577 
578  return $this->mail_data;
579  }
580 
587  function getMailsOfFolder($a_folder_id)
588  {
589  global $ilDB;
590 
591  $this->mail_counter = array();
592  $this->mail_counter["read"] = 0;
593  $this->mail_counter["unread"] = 0;
594 
595  $query = "SELECT ".$this->table_mail.".* FROM ". $this->table_mail ." ".
596  "LEFT JOIN object_data ON obj_id = sender_id ".
597  "WHERE user_id = ".$ilDB->quote($this->user_id) ." ".
598  "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)) ".
599  "AND folder_id = ".$ilDB->quote($a_folder_id)." ORDER BY send_time DESC";
600 
601  $res = $this->ilias->db->query($query);
602 
603  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
604  {
605  $tmp = $this->fetchMailData($row);
606 
607  if ($tmp["m_status"] == 'read')
608  {
609  ++$this->mail_counter["read"];
610  }
611 
612  if ($tmp["m_status"] == 'unread')
613  {
614  ++$this->mail_counter["unread"];
615  }
616 
617  $output[] = $tmp;
618  }
619 
620  $this->mail_counter["total"] = count($output);
621 
622  return $output ? $output : array();
623  }
624 
631  function countMailsOfFolder($a_folder_id)
632  {
633  global $ilDB;
634 
635  $query = "SELECT COUNT(*) FROM $this->table_mail ".
636  "WHERE user_id = ".$ilDB->quote($this->user_id) ." ".
637  "AND folder_id = ".$ilDB->quote($a_folder_id)." ";
638 
639  if (is_object($res = $this->ilias->db->query($query)))
640  {
641  return $res->numRows();
642  }
643 
644  return 0;
645  }
646 
653  function deleteMailsOfFolder($a_folder_id)
654  {
655  if ($a_folder_id)
656  {
657  global $ilDB;
658 
659  $query = "DELETE FROM $this->table_mail ".
660  "WHERE user_id = ".$ilDB->quote($this->user_id) ." ".
661  "AND folder_id = ".$ilDB->quote($a_folder_id)." ";
662 
663  $res = $this->ilias->db->query($query);
664 
665  return true;
666  }
667 
668  return false;
669  }
670 
678  {
679  return is_array($this->mail_counter) ? $this->mail_counter : array(
680  "total" => 0,
681  "read" => 0,
682  "unread" => 0);
683  }
684 
691  function getMail($a_mail_id)
692  {
693  global $ilDB;
694 
695  $query = "SELECT * FROM $this->table_mail ".
696  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
697  "AND mail_id = ".$ilDB->quote($a_mail_id)." ";
698 
699  $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
700 
701  return $this->mail_data;
702  }
703 
710  function markRead($a_mail_ids)
711  {
712  global $ilDB;
713  // CREATE IN STATEMENT
714  $in = "(". implode(",",ilUtil::quoteArray($a_mail_ids)) . ")";
715 
716  $query = "UPDATE $this->table_mail ".
717  "SET m_status = 'read' ".
718  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
719  "AND mail_id IN $in";
720 
721  $res = $this->ilias->db->query($query);
722 
723  return true;
724  }
725 
732  function markUnread($a_mail_ids)
733  {
734  global $ilDB;
735  // CREATE IN STATEMENT
736  $in = "(". implode(",",ilUtil::quoteArray($a_mail_ids)) . ")";
737 
738  $query = "UPDATE $this->table_mail ".
739  "SET m_status = 'unread' ".
740  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
741  "AND mail_id IN $in";
742 
743  $res = $this->ilias->db->query($query);
744 
745  return true;
746  }
747 
755  function moveMailsToFolder($a_mail_ids,$a_folder_id)
756  {
757  global $ilDB;
758  // CREATE IN STATEMENT
759  $in = "(". implode(",",ilUtil::quoteArray($a_mail_ids)) . ")";
760 
761  $query = "UPDATE $this->table_mail ".
762  "SET folder_id = ".$ilDB->quote($a_folder_id)." ".
763  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
764  "AND mail_id IN $in";
765 
766  $res = $this->ilias->db->query($query);
767 
768  return true;
769  }
770 
777  function deleteMails($a_mail_ids)
778  {
779  global $ilDB;
780 
781  foreach ($a_mail_ids as $id)
782  {
783  $query = "DELETE FROM $this->table_mail ".
784  "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
785  "AND mail_id = ".$ilDB->quote($id)." ";
786  $res = $this->ilias->db->query($query);
787  $this->mfile->deassignAttachmentFromDirectory($id);
788  }
789 
790  return true;
791  }
792 
799  function fetchMailData($a_row)
800  {
801  if (!$a_row) return;
802 
803  return array(
804  "mail_id" => $a_row->mail_id,
805  "user_id" => $a_row->user_id,
806  "folder_id" => $a_row->folder_id,
807  "sender_id" => $a_row->sender_id,
808  "attachments" => unserialize(stripslashes($a_row->attachments)),
809  "send_time" => $a_row->send_time,
810  "rcp_to" => $a_row->rcp_to,
811  "rcp_cc" => $a_row->rcp_cc,
812  "rcp_bcc" => $a_row->rcp_bcc,
813  "m_status" => $a_row->m_status,
814  "m_type" => unserialize(stripslashes($a_row->m_type)),
815  "m_email" => $a_row->m_email,
816  "m_subject" => $a_row->m_subject,
817  "m_message" => $a_row->m_message,
818  "import_name" => $a_row->import_name,
819  "use_placeholders"=> $a_row->use_placeholders);
820  }
821 
822  function updateDraft($a_folder_id,
823  $a_attachments,
824  $a_rcp_to,
825  $a_rcp_cc,
826  $a_rcp_bcc,
827  $a_m_type,
828  $a_m_email,
829  $a_m_subject,
830  $a_m_message,
831  $a_draft_id = 0, $a_use_placeholders = 0)
832  {
833  global $ilDB;
834 
835  $query = "UPDATE $this->table_mail ".
836  "SET folder_id = ".$ilDB->quote($a_folder_id).",".
837  "attachments = '".addslashes(serialize($a_attachments))."',".
838  "send_time = now(),".
839  "rcp_to = ".$ilDB->quote($a_rcp_to).",".
840  "rcp_cc = ".$ilDB->quote($a_rcp_cc).",".
841  "rcp_bcc = ".$ilDB->quote($a_rcp_bcc).",".
842  "m_status = 'read',".
843  "m_type = '".addslashes(serialize($a_m_type))."',".
844  "m_email = ".$ilDB->quote($a_m_email).",".
845  "m_subject = ".$ilDB->quote($a_m_subject).",".
846  "m_message = ".$ilDB->quote($a_m_message).",".
847  "use_placeholders = ".$ilDB->quote($a_use_placeholders)." ".
848  "WHERE mail_id = ".$ilDB->quote($a_draft_id)."";
849 
850  $res = $this->ilias->db->query($query);
851 
852  return $a_draft_id;
853  }
854 
872  function sendInternalMail($a_folder_id,
873  $a_sender_id,
874  $a_attachments,
875  $a_rcp_to,
876  $a_rcp_cc,
877  $a_rcp_bcc,
878  $a_status,
879  $a_m_type,
880  $a_m_email,
881  $a_m_subject,
882  $a_m_message,
883  $a_user_id = 0, $a_use_placeholders = 0)
884  {
885  $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
886 
887  global $ilDB, $log;
888  //$log->write('class.ilMail->sendInternalMail to user_id:'.$a_rcp_to.' '.$a_m_message);
889 
890  if ($a_use_placeholders) $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
891 
892  $query = "INSERT INTO $this->table_mail ".
893  "SET user_id = ".$ilDB->quote($a_user_id).",".
894  "folder_id = ".$ilDB->quote($a_folder_id).",".
895  "sender_id = ".$ilDB->quote($a_sender_id).",".
896  "attachments = '".addslashes(serialize($a_attachments))."',".
897  "send_time = now(),".
898  "rcp_to = ".$ilDB->quote($a_rcp_to).",".
899  "rcp_cc = ".$ilDB->quote($a_rcp_cc).",".
900  "rcp_bcc = ".$ilDB->quote($a_rcp_bcc).",".
901  "m_status = ".$ilDB->quote($a_status).",".
902  "m_type = '".addslashes(serialize($a_m_type))."',".
903  "m_email = ".$ilDB->quote($a_m_email).",".
904  "m_subject = ".$ilDB->quote($a_m_subject).",".
905  "m_message = ".$ilDB->quote($a_m_message)." ";
906 
907  $res = $this->ilias->db->query($query);
908  $query = "SELECT LAST_INSERT_ID() as id";
909  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_ASSOC);
910 
911  return $row["id"];
912  }
913 
914  function replacePlaceholders($a_message, $a_user_id)
915  {
916  global $lng;
917 
918  $user = new ilObjUser($a_user_id);
919 
920  // determine salutation
921  switch ($user->getGender())
922  {
923  case 'f': $gender_salut = $lng->txt('salutation_f');
924  break;
925  case 'm': $gender_salut = $lng->txt('salutation_m');
926  break;
927  }
928 
929  $a_message = str_replace('[MAIL_SALUTATION]', $gender_salut, $a_message);
930  $a_message = str_replace('[LOGIN]', $user->getLogin(), $a_message);
931  $a_message = str_replace('[FIRST_NAME]', $user->getFirstname(), $a_message);
932  $a_message = str_replace('[LAST_NAME]', $user->getLastname(), $a_message);
933  $a_message = str_replace('[ILIAS_URL]', ILIAS_HTTP_PATH.'/login.php?client_id='.CLIENT_ID, $a_message);
934  $a_message = str_replace('[CLIENT_NAME]', CLIENT_NAME, $a_message);
935 
936  unset($user);
937 
938  return $a_message;
939  }
940 
954  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)
955  {
956  global $log;
957 
958  include_once 'Services/Mail/classes/class.ilMailbox.php';
959  include_once './Services/User/classes/class.ilObjUser.php';
960 
961  if (!ilMail::_usePearMail())
962  {
963  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
964  $a_rcp_to = $this->__substituteRecipients($a_rcp_to, 'resubstitute');
965  $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc, 'resubstitute');
966  $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc, 'resubstitute');
967  }
968 
969  $mbox =& new ilMailbox();
970 
971  if (!$a_use_placeholders) # No Placeholders
972  {
973  $rcp_ids = $this->getUserIds(trim($a_rcp_to).','.trim($a_rcp_cc).','.trim($a_rcp_bcc));
974 
975  $as_email = array();
976 
977  foreach($rcp_ids as $id)
978  {
979  $tmp_mail_options =& new ilMailOptions($id);
980 
981  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
982  $tmp_user =& new ilObjUser($id);
983  $tmp_user->read();
984  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
985  $tmp_user->getActive() &&
986  $tmp_user->checkTimeLimit();
987 
988  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
989  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
990  {
991  continue;
992  }
993 
994  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS/HER MAIL
995  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
996  if (!$user_can_read_internal_mails ||
997  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
998  {
999  $as_email[] = $id;
1000  continue;
1001  }
1002 
1003  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1004  {
1005  $as_email[] = $id;
1006  }
1007 
1008  $mbox->setUserId($id);
1009  $inbox_id = $mbox->getInboxFolder();
1010 
1011  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1012  $a_attachments, $a_rcp_to,
1013  $a_rcp_cc, '', 'unread', $a_type,
1014  0, $a_subject, $a_message, $id, 0);
1015  if ($a_attachments)
1016  {
1017  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1018  }
1019  }
1020 
1021  // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
1022  $to = array();
1023  $bcc = array();
1024 
1025  if (count($as_email) == 1)
1026  {
1027  $to[] = ilObjUser::_lookupEmail($as_email[0]);
1028  }
1029  else
1030  {
1031  foreach ($as_email as $id)
1032  {
1033  $bcc[] = ilObjUser::_lookupEmail($id);
1034  }
1035  }
1036 
1037  if(count($to) > 0 || count($bcc) > 0)
1038  {
1039  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
1040  }
1041  }
1042  else # Use Placeholders
1043  {
1044  // to
1045  $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
1046 
1047  // cc / bcc
1048  $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
1049 
1050  $as_email = array();
1051 
1052  // to
1053  foreach($rcp_ids_replace as $id)
1054  {
1055  $tmp_mail_options =& new ilMailOptions($id);
1056 
1057  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1058  $tmp_user =& new ilObjUser($id);
1059  $tmp_user->read();
1060  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1061  $tmp_user->getActive() &&
1062  $tmp_user->checkTimeLimit();
1063 
1064  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1065  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1066  {
1067  continue;
1068  }
1069 
1070  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1071  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1072  if (!$user_can_read_internal_mails ||
1073  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1074  {
1075  $as_email[] = $id;
1076  continue;
1077  }
1078 
1079  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1080  {
1081  $as_email[] = $id;
1082  }
1083 
1084  $mbox->setUserId($id);
1085  $inbox_id = $mbox->getInboxFolder();
1086 
1087  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1088  $a_attachments, $a_rcp_to,
1089  $a_rcp_cc, '', 'unread', $a_type,
1090  0, $a_subject, $a_message, $id, 1);
1091  if ($a_attachments)
1092  {
1093  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1094  }
1095  }
1096 
1097  if (count($as_email))
1098  {
1099  foreach ($as_email as $id)
1100  {
1101  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
1102  }
1103  }
1104 
1105  $as_email = array();
1106 
1107  // cc / bcc
1108  foreach($rcp_ids_no_replace as $id)
1109  {
1110  $tmp_mail_options =& new ilMailOptions($id);
1111 
1112  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1113  $tmp_user =& new ilObjUser($id);
1114  $tmp_user->read();
1115  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement()
1116  && $tmp_user->getActive() && $tmp_user->checkTimeLimit();
1117 
1118  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1119  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1120  {
1121  continue;
1122  }
1123 
1124  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1125  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1126  if (!$user_can_read_internal_mails ||
1127  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1128  {
1129  $as_email[] = $id;
1130  continue;
1131  }
1132 
1133  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1134  {
1135  $as_email[] = $id;
1136  }
1137 
1138  $mbox->setUserId($id);
1139  $inbox_id = $mbox->getInboxFolder();
1140 
1141  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1142  $a_attachments, $a_rcp_to,
1143  $a_rcp_cc, '', 'unread', $a_type,
1144  0, $a_subject, $a_message, $id, 0);
1145  if ($a_attachments)
1146  {
1147  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1148  }
1149  }
1150 
1151  if (count($as_email))
1152  {
1153  foreach ($as_email as $id)
1154  {
1155  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $a_message, $a_attachments);
1156  }
1157  }
1158  }
1159 
1160  return true;
1161  }
1162 
1168  function getUserIds($a_recipients)
1169  {
1170  global $log, $rbacreview;
1171  $ids = array();
1172 
1173  if (ilMail::_usePearMail())
1174  {
1175  $tmp_names = $this->explodeRecipients($a_recipients);
1176  if (! is_a($tmp_names, 'PEAR_Error'))
1177  {
1178  for ($i = 0;$i < count($tmp_names); $i++)
1179  {
1180  if (substr($tmp_names[$i]->mailbox,0,1) === '#')
1181  {
1182  $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1183  foreach($role_ids as $role_id)
1184  {
1185  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1186  {
1187  $ids[] = $usr_id;
1188  }
1189  }
1190  }
1191  else if (strtolower($tmp_names[$i]->host) == 'ilias')
1192  {
1193  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
1194  {
1195  //$log->write('class.ilMail->getUserIds() recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host.' user_id:'.$id);
1196  $ids[] = $id;
1197  }
1198  else
1199  {
1200  //$log->write('class.ilMail->getUserIds() no user account found for recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1201  }
1202  }
1203  else
1204  {
1205  //$log->write('class.ilMail->getUserIds() external recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1206  }
1207  }
1208  }
1209  else
1210  {
1211  //$log->write('class.ilMail->getUserIds() illegal recipients:'.$a_recipients);
1212  }
1213  }
1214  else
1215  {
1216  $tmp_names = $this->explodeRecipients($a_recipients);
1217  for ($i = 0;$i < count($tmp_names); $i++)
1218  {
1219  if (substr($tmp_names[$i],0,1) == '#')
1220  {
1221  if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
1222  {
1223  include_once("./classes/class.ilObjectFactory.php");
1224  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1225 
1226  foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1227  {
1228  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1229  break;
1230  }
1231  // STORE MEMBER IDS IN $ids
1232  foreach ($grp_object->getGroupMemberIds() as $id)
1233  {
1234  $ids[] = $id;
1235  }
1236  }
1237  // is role: get role ids
1238  elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
1239  {
1240  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1241  {
1242  $ids[] = $usr_id;
1243  }
1244  }
1245 
1246  }
1247  else if (!empty($tmp_names[$i]))
1248  {
1249  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
1250  {
1251  $ids[] = $id;
1252  }
1253  }
1254  }
1255  }
1256  return array_unique($ids);
1257  }
1258 
1269  function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
1270  {
1271  $error_message = '';
1272 
1273  $a_m_subject = trim($a_m_subject);
1274  $a_rcp_to = trim($a_rcp_to);
1275 
1276  if (empty($a_m_subject))
1277  {
1278  $error_message .= $error_message ? "<br>" : '';
1279  $error_message .= $this->lng->txt("mail_add_subject");
1280  }
1281 
1282  if (empty($a_rcp_to))
1283  {
1284  $error_message .= $error_message ? "<br>" : '';
1285  $error_message .= $this->lng->txt("mail_add_recipient");
1286  }
1287 
1288  return $error_message;
1289  }
1290 
1297  function getEmailsOfRecipients($a_rcp)
1298  {
1299  $addresses = array();
1300 
1301  if (ilMail::_usePearMail())
1302  {
1303  $tmp_rcp = $this->explodeRecipients($a_rcp);
1304  if (! is_a($tmp_rcp, 'PEAR_Error'))
1305  {
1306  foreach ($tmp_rcp as $rcp)
1307  {
1308  // NO GROUP
1309  if (substr($rcp->mailbox,0,1) != '#')
1310  {
1311  if (strtolower($rcp->host) != 'ilias')
1312  {
1313  $addresses[] = $rcp->mailbox.'@'.$rcp->host;
1314  continue;
1315  }
1316 
1317  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
1318  {
1319  $tmp_user = new ilObjUser($id);
1320  $addresses[] = $tmp_user->getEmail();
1321  continue;
1322  }
1323  }
1324  else
1325  {
1326  // Roles
1327  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1328  foreach($role_ids as $role_id)
1329  {
1330  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1331  {
1332  $tmp_user = new ilObjUser($usr_id);
1333  $addresses[] = $tmp_user->getEmail();
1334  }
1335  }
1336  }
1337  }
1338  }
1339  }
1340  else
1341  {
1342  $tmp_rcp = $this->explodeRecipients($a_rcp);
1343 
1344  foreach ($tmp_rcp as $rcp)
1345  {
1346  // NO GROUP
1347  if (substr($rcp,0,1) != '#')
1348  {
1349  if (strpos($rcp,'@'))
1350  {
1351  $addresses[] = $rcp;
1352  continue;
1353  }
1354 
1355  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1356  {
1357  $tmp_user = new ilObjUser($id);
1358  $addresses[] = $tmp_user->getEmail();
1359  continue;
1360  }
1361  }
1362  else
1363  {
1364  // GROUP THINGS
1365  include_once("./classes/class.ilObjectFactory.php");
1366  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1367 
1368  // Fix
1369  foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1370  {
1371  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1372  break;
1373  }
1374  // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
1375  foreach ($grp_object->getGroupMemberIds() as $id)
1376  {
1377  $tmp_user = new ilObjUser($id);
1378  $addresses[] = $tmp_user->getEmail();
1379  }
1380  }
1381  }
1382  }
1383 
1384  return $addresses;
1385  }
1386 
1394  function checkRecipients($a_recipients,$a_type)
1395  {
1396  global $rbacsystem,$rbacreview;
1397  $wrong_rcps = '';
1398 
1399  if (ilMail::_usePearMail())
1400  {
1401  $tmp_rcp = $this->explodeRecipients($a_recipients);
1402  if (is_a($tmp_rcp, 'PEAR_Error'))
1403  {
1404  $colon_pos = strpos($tmp_rcp->message, ':');
1405  $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
1406  }
1407  else
1408  {
1409  foreach ($tmp_rcp as $rcp)
1410  {
1411  // NO ROLE MAIL ADDRESS
1412  if (substr($rcp->mailbox,0,1) != '#')
1413  {
1414  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1415  $user_id = ($rcp->host == 'ilias') ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
1416  if ($user_id == false && $rcp->host == 'ilias')
1417  {
1418  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
1419  continue;
1420  }
1421 
1422  // CHECK IF USER CAN RECEIVE MAIL
1423  if ($user_id)
1424  {
1425  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1426  {
1427  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1428  " (".$this->lng->txt("user_cant_receive_mail").")";
1429  continue;
1430  }
1431  }
1432  }
1433  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
1434  {
1435  if (!$this->mlists->mailingListExists($rcp->mailbox))
1436  {
1437  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1438  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1439  }
1440 
1441  continue;
1442  }
1443  else
1444  {
1445  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1446  if (count($role_ids) == 0)
1447  {
1448  $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
1449  ' ('.$this->lng->txt('mail_no_recipient_found').')';
1450  continue;
1451  }
1452  else if (count($role_ids) > 1)
1453  {
1454  $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
1455  ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
1456  }
1457  }
1458  }
1459  }
1460  }
1461  else
1462  {
1463  $tmp_rcp = $this->explodeRecipients($a_recipients);
1464 
1465  foreach ($tmp_rcp as $rcp)
1466  {
1467  if (empty($rcp))
1468  {
1469  continue;
1470  }
1471  // NO GROUP
1472  if (substr($rcp,0,1) != '#')
1473  {
1474  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1475  if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
1476  !ilUtil::is_email($rcp))
1477  {
1478  $wrong_rcps .= "<br />".htmlentities($rcp);
1479  continue;
1480  }
1481 
1482  // CHECK IF USER CAN RECEIVE MAIL
1483  if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1484  {
1485  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1486  {
1487  $wrong_rcps .= "<br />".htmlentities($rcp).
1488  " (".$this->lng->txt("user_cant_receive_mail").")";
1489  continue;
1490  }
1491  }
1492  }
1493  else if (substr($rcp, 0, 7) == '#il_ml_')
1494  {
1495  if (!$this->mlists->mailingListExists($rcp))
1496  {
1497  $wrong_rcps .= "<br />".htmlentities($rcp).
1498  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1499  }
1500 
1501  continue;
1502  }
1503  else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
1504  {
1505  continue;
1506  }
1507  else if (!$rbacreview->roleExists(addslashes(substr($rcp,1))))
1508  {
1509  $wrong_rcps .= "<br />".htmlentities($rcp).
1510  " (".$this->lng->txt("mail_no_valid_group_role").")";
1511  continue;
1512  }
1513  }
1514  }
1515  return $wrong_rcps;
1516  }
1517 
1533  function savePostData($a_user_id,
1534  $a_attachments,
1535  $a_rcp_to,
1536  $a_rcp_cc,
1537  $a_rcp_bcc,
1538  $a_m_type,
1539  $a_m_email,
1540  $a_m_subject,
1541  $a_m_message,
1542  $a_use_placeholders)
1543  {
1544  global $ilDB;
1545 
1546  $query = "DELETE FROM $this->table_mail_saved ".
1547  "WHERE user_id = ".$ilDB->quote($this->user_id)." ";
1548  $res = $this->ilias->db->query($query);
1549 
1550  $query = "INSERT INTO $this->table_mail_saved ".
1551  "SET user_id = ".$ilDB->quote($a_user_id).",".
1552  "attachments = '".addslashes(serialize($a_attachments))."',".
1553  "rcp_to = ".$ilDB->quote($a_rcp_to).",".
1554  "rcp_cc = ".$ilDB->quote($a_rcp_cc).",".
1555  "rcp_bcc = ".$ilDB->quote($a_rcp_bcc).",".
1556  "m_type = '".addslashes(serialize($a_m_type))."',".
1557  "m_email = '',".
1558  "m_subject = ".$ilDB->quote($a_m_subject).",".
1559  "m_message = ".$ilDB->quote($a_m_message).",".
1560  "use_placeholders = ".$ilDB->quote($a_use_placeholders)."";
1561  $res = $this->ilias->db->query($query);
1562 
1563  $this->getSavedData();
1564 
1565  return true;
1566  }
1567 
1573  function getSavedData()
1574  {
1575  global $ilDB;
1576 
1577  $query = "SELECT * FROM $this->table_mail_saved ".
1578  "WHERE user_id = ".$ilDB->quote($this->user_id)." ";
1579 
1580  $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query, DB_FETCHMODE_OBJECT));
1581 
1582  return $this->mail_data;
1583  }
1584 
1598  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)
1599  {
1600  global $lng,$rbacsystem,$log;
1601  //$log->write('class.ilMail.sendMail '.$a_rcp_to.' '.$a_m_subject);
1602  $error_message = '';
1603  $message = '';
1604 
1605  if (in_array("system",$a_type))
1606  {
1607  $this->__checkSystemRecipients($a_rcp_to);
1608  $a_type = array('system');
1609  }
1610 
1611  if ($a_attachment)
1612  {
1613  if (!$this->mfile->checkFilesExist($a_attachment))
1614  {
1615  return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
1616  }
1617  }
1618  // CHECK NECESSARY MAIL DATA FOR ALL TYPES
1619  if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
1620  {
1621  return $error_message;
1622  }
1623  // check recipients
1624  if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
1625  {
1626  $message .= $error_message;
1627  }
1628 
1629  if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
1630  {
1631  $message .= $error_message;
1632  }
1633 
1634  if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
1635  {
1636  $message .= $error_message;
1637  }
1638  // if there was an error
1639  if (!empty($message))
1640  {
1641  return $this->lng->txt("mail_following_rcp_not_valid").$message;
1642  }
1643 
1644  // CHECK FOR SYSTEM MAIL
1645  if (in_array('system',$a_type))
1646  {
1647  if (!empty($a_attachment))
1648  {
1649  return $lng->txt("mail_no_attach_allowed");
1650  }
1651  }
1652 
1653  // ACTIONS FOR ALL TYPES
1654  // GET RCPT OF MAILING LISTS
1655  $rcp_to = $this->parseRcptOfMailingLists($a_rcp_to);
1656  $rcp_cc = $this->parseRcptOfMailingLists($a_rcp_cc);
1657  $rcp_bc = $this->parseRcptOfMailingLists($a_rcp_bc);
1658 
1659  if (! ilMail::_usePearMail())
1660  {
1661  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1662  $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
1663  $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
1664  $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
1665  }
1666 
1667  // COUNT EMAILS
1668  $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
1669  $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
1670 
1671  // currently disabled..
1672  /*
1673  if (count($c_emails))
1674  {
1675  if (!$this->getEmailOfSender())
1676  {
1677  return $lng->txt("mail_check_your_email_addr");
1678  }
1679  }
1680  */
1681 
1682  // check smtp permission
1683  if($c_emails && $this->user_id != ANONYMOUS_USER_ID &&
1684  !$rbacsystem->checkAccess('smtp_mail', $this->mail_obj_ref_id))
1685  {
1686  return $this->lng->txt('mail_no_permissions_write_smtp');
1687  }
1688 
1689  // save mail in sent box
1690  $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
1691  $a_m_subject,$a_m_message);
1692 
1693  if($a_attachment)
1694  {
1695  $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
1696 
1697  if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
1698  {
1699  return $error;
1700  }
1701  }
1702 
1703  // FILTER EMAILS
1704  // IF EMAIL RECIPIENT
1705  if($c_emails)
1706  {
1707  $this->sendMimeMail($this->__getEmailRecipients($rcp_to),
1708  $this->__getEmailRecipients($rcp_cc),
1709  $this->__getEmailRecipients($rcp_bc),
1710  $a_m_subject,
1711  $a_m_message,
1712  $a_attachment,
1713  0);
1714  }
1715 
1716  if (in_array('system',$a_type))
1717  {
1718  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))
1719  {
1720  return $lng->txt("mail_send_error");
1721  }
1722  }
1723  // ACTIONS FOR TYPE SYSTEM AND NORMAL
1724  if (in_array('normal',$a_type))
1725  {
1726  // TRY BOTH internal and email (depends on user settings)
1727  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))
1728  {
1729  return $lng->txt("mail_send_error");
1730  }
1731  }
1732 
1733  // Temporary bugfix
1734  if (!$this->getSaveInSentbox())
1735  {
1736  $this->deleteMails(array($sent_id));
1737  }
1738 
1739  return '';
1740  }
1741 
1742  function parseRcptOfMailingLists($rcpt = '')
1743  {
1744  if ($rcpt == '') return $rcpt;
1745 
1746  $arrRcpt = $this->explodeRecipients(trim($rcpt));
1747  if (!is_array($arrRcpt) || empty($arrRcpt)) return $rcpt;
1748 
1749  $new_rcpt = array();
1750 
1751  foreach ($arrRcpt as $item)
1752  {
1753  if (ilMail::_usePearMail())
1754  {
1755  if (substr($item->mailbox, 0, 7) == '#il_ml_')
1756  {
1757  if ($this->mlists->mailingListExists($item->mailbox))
1758  {
1759  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1760  {
1761  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1762  }
1763  }
1764  }
1765  else
1766  {
1767  $new_rcpt[] = $item->mailbox.'@'.$item->host;
1768  }
1769  }
1770  else
1771  {
1772  if (substr($item, 0, 7) == '#il_ml_')
1773  {
1774  if ($this->mlists->mailingListExists($item))
1775  {
1776  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1777  {
1778  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1779  }
1780  }
1781  }
1782  else
1783  {
1784  $new_rcpt[] = $item;
1785  }
1786  }
1787  }
1788 
1789  return implode(',', $new_rcpt);
1790  }
1791 
1804  function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
1805  $a_m_subject,$a_m_message)
1806  {
1807  include_once "Services/Mail/classes/class.ilMailbox.php";
1808 
1809  $mbox = new ilMailbox($this->user_id);
1810  $sent_id = $mbox->getSentFolder();
1811 
1812  return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
1813  $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
1814  }
1815 
1816 
1817 
1823  function addFullname($a_email)
1824  {
1825  include_once 'Services/Mail/classes/class.ilMimeMail.php';
1826 
1827  global $ilUser;
1828 
1829  return ilMimeMail::_mimeEncode($ilUser->getFullname()).'<'.$a_email.'>';
1830  }
1831 
1844  function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
1845  {
1846  #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
1847 
1848  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
1849  $a_m_subject = "[".$inst_name."] ".$a_m_subject;
1850 
1851  if($this->user_id != ANONYMOUS_USER_ID)
1852  {
1853  $sender = $this->addFullname($this->getEmailOfSender());
1854  }
1855  else
1856  {
1857  if(trim($this->ilias->getSetting('mail_external_sender_noreply')) != '')
1858  {
1859  include_once "Services/Mail/classes/class.ilMimeMail.php";
1860  $sender = ilMimeMail::_mimeEncode(self::_getAnonymousName()).
1861  '<noreply@'.trim($this->ilias->getSetting('mail_external_sender_noreply')).'>';
1862  }
1863  else
1864  {
1865  include_once "Services/Mail/classes/class.ilMimeMail.php";
1866  $sender = ilMimeMail::_mimeEncode(self::_getAnonymousName()).
1867  '<noreply@'.$_SERVER['SERVER_NAME'].'>';
1868  }
1869  }
1870 
1871  if($this->isSOAPEnabled())
1872  {
1873  // Send per soap
1874  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
1875 
1876  $soap_client = new ilSoapClient();
1877  $soap_client->setTimeout(1);
1878  $soap_client->setResponseTimeout(1);
1879  $soap_client->enableWSDL(true);
1880  $soap_client->init();
1881 
1882  $attachments = array();
1883  $a_attachments = $a_attachments ? $a_attachments : array();
1884  foreach($a_attachments as $attachment)
1885  {
1886  $attachments[] = $this->mfile->getAbsolutePath($attachment);
1887  }
1888  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
1889  $attachments = implode('#:#',$attachments);
1890  // mjansen: use "#:#" as leading delimiter
1891  if(strlen($attachments))
1892  $attachments = "#:#".$attachments;
1893 
1894  $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'], // session id
1895  $a_rcp_to,
1896  $a_rcp_cc,
1897  $a_rcp_bcc,
1898  $sender,
1899  $a_m_subject,
1900  $a_m_message,
1901  $attachments));
1902 
1903  return true;
1904  }
1905  else
1906  {
1907  // send direct
1908  include_once "Services/Mail/classes/class.ilMimeMail.php";
1909 
1910  $mmail = new ilMimeMail();
1911  $mmail->autoCheck(false);
1912  $mmail->From($sender);
1913  $mmail->To($a_rcp_to);
1914  // Add installation name to subject
1915  $mmail->Subject($a_m_subject);
1916  $mmail->Body($a_m_message);
1917 
1918  if ($a_rcp_cc)
1919  {
1920  $mmail->Cc($a_rcp_cc);
1921  }
1922 
1923  if ($a_rcp_bcc)
1924  {
1925  $mmail->Bcc($a_rcp_bcc);
1926  }
1927 
1928  if (is_array($a_attachments))
1929  {
1930  foreach ($a_attachments as $attachment)
1931  {
1932  $mmail->Attach($this->mfile->getAbsolutePath($attachment));
1933  }
1934  }
1935 
1936  $mmail->Send();
1937  }
1938  }
1944  function getEmailOfSender()
1945  {
1946  $umail = new ilObjUser($this->user_id);
1947  $sender = $umail->getEmail();
1948 
1949  if (ilUtil::is_email($sender))
1950  {
1951  return $sender;
1952  }
1953  else
1954  {
1955  return '';
1956  }
1957  }
1958 
1965  function saveAttachments($a_attachments)
1966  {
1967  global $ilDB;
1968 
1969  $query = "UPDATE $this->table_mail_saved ".
1970  "SET attachments = '".addslashes(serialize($a_attachments))."' ".
1971  "WHERE user_id = ".$ilDB->quote($this->user_id)." ";
1972 
1973  $res = $this->ilias->db->query($query);
1974 
1975  return true;
1976  }
1977 
1983  function getAttachments()
1984  {
1985  return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
1986  }
1987 
2001  function explodeRecipients($a_recipients)
2002  {
2003  if (ilMail::_usePearMail())
2004  {
2005  if (strlen(trim($a_recipients)) > 0)
2006  {
2007  require_once 'Mail/RFC822.php';
2008  $parser = &new Mail_RFC822();
2009  return $parser->parseAddressList($a_recipients, "ilias", false, true);
2010  } else {
2011  return array();
2012  }
2013  }
2014  else
2015  {
2016  $a_recipients = trim($a_recipients);
2017 
2018  // WHITESPACE IS NOT ALLOWED AS SEPERATOR
2019  #$a_recipients = preg_replace("/ /",",",$a_recipients);
2020  $a_recipients = preg_replace("/;/",",",$a_recipients);
2021 
2022 
2023  foreach(explode(',',$a_recipients) as $tmp_rec)
2024  {
2025  if($tmp_rec)
2026  {
2027  $rcps[] = trim($tmp_rec);
2028  }
2029  }
2030  return is_array($rcps) ? $rcps : array();
2031  }
2032  }
2033 
2034  function __getCountRecipient($rcp,$a_only_email = true)
2035  {
2036  $counter = 0;
2037 
2038  if (ilMail::_usePearMail())
2039  {
2040  $tmp_rcp = $this->explodeRecipients($rcp);
2041  if (! is_a($tmp_rcp, 'PEAR_Error'))
2042  {
2043  foreach ($tmp_rcp as $to)
2044  {
2045  if ($a_only_email)
2046  {
2047  // Addresses which aren't on the ilias host, and
2048  // which have a mailbox which does not start with '#',
2049  // are external e-mail addresses
2050  if ($to->host != 'ilias' && substr($to->mailbox,0,1) != '#')
2051  {
2052  ++$counter;
2053  }
2054  }
2055  else
2056  {
2057  ++$counter;
2058  }
2059  }
2060  }
2061  }
2062  else
2063  {
2064  foreach ($this->explodeRecipients($rcp) as $to)
2065  {
2066  if ($a_only_email)
2067  {
2068  if (strpos($to,'@'))
2069  {
2070  ++$counter;
2071  }
2072  }
2073  else
2074  {
2075  ++$counter;
2076  }
2077  }
2078  }
2079  return $counter;
2080  }
2081 
2082 
2083  function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
2084  {
2085  return $this->__getCountRecipient($a_to,$a_only_email)
2086  + $this->__getCountRecipient($a_cc,$a_only_email)
2087  + $this->__getCountRecipient($a_bcc,$a_only_email);
2088  }
2089 
2090  function __getEmailRecipients($a_rcp)
2091  {
2092  if (ilMail::_usePearMail())
2093  {
2094  $rcp = array();
2095  $tmp_rcp = $this->explodeRecipients($a_rcp);
2096  if (! is_a($tmp_rcp, 'PEAR_Error'))
2097  {
2098  foreach ($tmp_rcp as $to)
2099  {
2100  if(substr($to->mailbox,0,1) != '#' && $to->host != 'ilias')
2101  {
2102  $rcp[] = $to->mailbox.'@'.$to->host;
2103  }
2104  }
2105  }
2106  return implode(',',$rcp);
2107  }
2108  else
2109  {
2110  foreach ($this->explodeRecipients($a_rcp) as $to)
2111  {
2112  if(strpos($to,'@'))
2113  {
2114  $rcp[] = $to;
2115  }
2116  }
2117  return implode(',',$rcp ? $rcp : array());
2118  }
2119  }
2120 
2121  function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
2122  {
2123  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
2124 
2125  $message = $inst_name." To:".$rcp_to."\n";
2126 
2127  if ($rcp_cc)
2128  {
2129  $message .= "Cc: ".$rcp_cc;
2130  }
2131 
2132  $message .= "\n\n";
2133  $message .= $a_m_message;
2134 
2135  return $message;
2136  }
2137 
2138  function __checkSystemRecipients(&$a_rcp_to)
2139  {
2140  if (preg_match("/@all/",$a_rcp_to))
2141  {
2142  // GET ALL LOGINS
2143  $all = ilObjUser::_getAllUserLogins($this->ilias);
2144  $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
2145  }
2146 
2147  return;
2148  }
2149 
2157  function __substituteRecipients($a_rcp,$direction)
2158  {
2159  $new_name = array();
2160 
2161  $tmp_names = $this->explodeRecipients($a_rcp);
2162 
2163 
2164  foreach($tmp_names as $name)
2165  {
2166  if(strpos($name,"#") === 0)
2167  {
2168  $new_name[] = $name;
2169  continue;
2170  }
2171  switch($direction)
2172  {
2173  case "substitute":
2174  if(strpos($name,"@") and ilObjUser::_loginExists($name))
2175  {
2176  $new_name[] = preg_replace("/@/","�#�",$name);
2177  }
2178  else
2179  {
2180  $new_name[] = $name;
2181  }
2182  break;
2183 
2184  case "resubstitute":
2185  if(stristr($name,"�#�"))
2186  {
2187  $new_name[] = preg_replace("/�#�/","@",$name);
2188  }
2189  else
2190  {
2191  $new_name[] = $name;
2192  }
2193  break;
2194  }
2195  }
2196  return implode(",",$new_name);
2197  }
2198 
2214  public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null) {
2215  if (ilMail::_usePearMail())
2216  {
2217  if ($login == null)
2218  {
2219  require_once './Services/User/classes/class.ilObjUser.php';
2220  $usr_obj = new ilObjUser($usr_id);
2221  $usr_obj->read();
2222  $login = $usr_obj->getLogin();
2223  $firstname = $usr_obj->getFirstname();
2224  $lastname = $usr_obj->getLastname();
2225  }
2226  // The following line of code creates a properly formatted mailbox
2227  // address. Unfortunately, it does not work, because ILIAS removes
2228  // everything between '<' '>' characters
2229  // Therefore, we just return the login - sic.
2230  // FIXME - Make this work in a future release
2231  /*
2232  return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
2233  */
2234  return $login.'hhho';
2235  }
2236  else
2237  {
2238  return $login;
2239  }
2240  }
2247  public static function _usePearMail() {
2248  global $ilias;
2249 
2250  $result = false;
2251  if ($ilias->getSetting('pear_mail_enable') == true)
2252  {
2253  // Note: We use the include statement to determine whether PEAR MAIL is
2254  // installed. We use the @ operator to prevent PHP from issuing a
2255  // warning while we test for PEAR MAIL.
2256  $is_pear_mail_installed = @include_once 'Mail/RFC822.php';
2257  if ($is_pear_mail_installed) {
2258  $result = true;
2259  } else {
2260  // Disable Pear Mail, when we detect that it is not
2261  // installed
2262  global $log;
2263  $log->write("WARNING: ilMail::_userPearMail disabled Pear Mail support, because include 'Mail/RFC822.php' failed.");
2264  $ilias->setSetting('pear_mail_enable', false);
2265  }
2266  }
2267  return $result;
2268  }
2269 
2278  public static function _getAutoGeneratedMessageString($lang = null)
2279  {
2280  global $ilSetting;
2281 
2282  if(!$lang)
2283  {
2284  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
2286  }
2287  $lang->loadLanguageModule('mail');
2288  return sprintf($lang->txt('mail_auto_generated_info'),
2289  $ilSetting->get('inst_name','ILIAS 3'),
2290  ILIAS_HTTP_PATH."\n\n");
2291  }
2292 
2293  public static function _getAnonymousName()
2294  {
2295  return 'ILIAS';
2296  }
2297 } // END class.ilMail
2298 ?>