ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMail.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/User/classes/class.ilObjUser.php';
5 
109 class ilMail
110 {
117  var $ilias;
118 
124  var $lng;
125 
131  var $mfile;
132 
134 
140  var $user_id;
141 
148 
155 
162 
169 
170 
177 
184 
191 
203 
204  var $soap_enabled = true;
206 
207  private $use_pear = true;
208  protected $appendInstallationSignature = false;
209 
218  protected $properties = array();
219 
223  protected static $userInstances = array();
224 
231  public function __construct($a_user_id)
232  {
233  require_once "./Services/Mail/classes/class.ilFileDataMail.php";
234  require_once "Services/Mail/classes/class.ilMailOptions.php";
235 
236  global $ilias, $lng, $ilUser;
237 
238  $lng->loadLanguageModule("mail");
239 
240  // Initiate variables
241  $this->ilias =& $ilias;
242  $this->lng =& $lng;
243  $this->table_mail = 'mail';
244  $this->table_mail_saved = 'mail_saved';
245  $this->user_id = $a_user_id;
246  $this->mfile = new ilFileDataMail($this->user_id);
247  $this->mail_options = new ilMailOptions($a_user_id);
248 
249  // DEFAULT: sent mail aren't stored insentbox of user.
250  $this->setSaveInSentbox(false);
251 
252  // GET REFERENCE ID OF MAIL OBJECT
253  $this->readMailObjectReferenceId();
254  }
255 
263  public function __get($name)
264  {
265  global $ilUser;
266 
267  if(isset($this->properties[$name]))
268  {
269  return $this->properties[$name];
270  }
271 
272  // Used to include files / instantiate objects at runtime
273  if($name == 'mlists')
274  {
275  if(is_object($ilUser))
276  {
277  require_once 'Services/Contact/classes/class.ilMailingLists.php';
278  $this->properties[$name] = new ilMailingLists($ilUser);
279  return $this->properties[$name];
280  }
281  }
282  }
283 
284  public function doesRecipientStillExists($a_recipient, $a_existing_recipients)
285  {
286  if(self::_usePearMail())
287  {
288  $recipients = $this->explodeRecipients($a_existing_recipients);
289  if(is_a($recipients, 'PEAR_Error'))
290  {
291  return false;
292  }
293  else
294  {
295  foreach($recipients as $rcp)
296  {
297  if (substr($rcp->mailbox, 0, 1) != '#')
298  {
299  if(trim($rcp->mailbox) == trim($a_recipient) ||
300  trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
301  {
302  return true;
303  }
304  }
305  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
306  {
307  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
308  {
309  return true;
310  }
311  }
312  else
313  {
314  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
315  {
316  return true;
317  }
318  }
319  }
320  }
321  }
322  else
323  {
324  $recipients = $this->explodeRecipients($a_existing_recipients);
325  if(count($recipients))
326  {
327  foreach($recipients as $recipient)
328  {
329  if(trim($recipient) == trim($a_recipient))
330  {
331  return true;
332  }
333  }
334  }
335  }
336 
337  return false;
338  }
339 
347  function enableSOAP($a_status)
348  {
349  $this->soap_enabled = $a_status;
350  }
351  function isSOAPEnabled()
352  {
353  global $ilSetting;
354 
355  if(!extension_loaded('curl') || !$ilSetting->get('soap_user_administration'))
356  {
357  return false;
358  }
359 
360  // Prevent using SOAP in cron context
362  {
363  return false;
364  }
365 
366  return (bool) $this->soap_enabled;
367  }
368 
369 
370  function setSaveInSentbox($a_save_in_sentbox)
371  {
372  $this->save_in_sentbox = $a_save_in_sentbox;
373  }
374 
375  function getSaveInSentbox()
376  {
377  return $this->save_in_sentbox;
378  }
379 
385  function setMailSendType($a_types)
386  {
387  $this->mail_send_type = $a_types;
388  }
389 
395  function setMailRcpTo($a_rcp_to)
396  {
397  $this->mail_rcp_to = $a_rcp_to;
398  }
399 
405  function setMailRcpCc($a_rcp_cc)
406  {
407  $this->mail_rcp_cc = $a_rcp_cc;
408  }
409 
415  function setMailRcpBc($a_rcp_bc)
416  {
417  $this->mail_rcp_bc = $a_rcp_bc;
418  }
419 
425  function setMailSubject($a_subject)
426  {
427  $this->mail_subject = $a_subject;
428  }
429 
435  function setMailMessage($a_message)
436  {
437  $this->mail_message = $a_message;
438  }
439 
445  {
446  include_once 'Services/Mail/classes/class.ilMailGlobalServices.php';
447  $this->mail_obj_ref_id = ilMailGlobalServices::getMailObjectRefId();
448  }
449 
451  {
452  return $this->mail_obj_ref_id;
453  }
454 
465  public function formatNamesForOutput($users = '')
466  {
467  $users = trim($users);
468  if($users)
469  {
470  if(strstr($users, ','))
471  {
472  $rcp_to_array = array();
473 
474  $recipients = explode(',', $users);
475  foreach($recipients as $recipient)
476  {
477  $recipient = trim($recipient);
478  if($uid = ilObjUser::_lookupId($recipient))
479  {
480  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
481  {
482  $tmp_obj = self::getCachedUserInstance($uid);
483  $rcp_to_array[] = $tmp_obj->getFullname().' ['.$recipient.']';
484  }
485  else
486  {
487  $rcp_to_array[] = $recipient;
488  }
489  }
490  else
491  {
492  $rcp_to_array[] = $recipient;
493  }
494  }
495 
496  return trim(implode(', ', $rcp_to_array));
497  }
498  else
499  {
500  if($uid = ilObjUser::_lookupId($users))
501  {
502  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
503  {
504  $tmp_obj = self::getCachedUserInstance($uid);
505  return $tmp_obj->getFullname().' ['.$users.']';
506  }
507  else
508  {
509  return $users;
510  }
511  }
512  else
513  {
514  return $users;
515  }
516  }
517  }
518  else
519  {
520  return $this->lng->txt('not_available');
521  }
522  }
523 
524  function getPreviousMail($a_mail_id)
525  {
526  global $ilDB;
527 
528  $ilDB->setLimit(1);
529  $res = $ilDB->queryf("
530  SELECT b.* FROM " . $this->table_mail ." a
531  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
532  AND b.user_id = a.user_id AND b.send_time > a.send_time
533  WHERE a.user_id = %s
534  AND a.mail_id = %s ORDER BY b.send_time ASC",
535  array('integer', 'integer'),
536  array($this->user_id, $a_mail_id));
537 
538  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
539 
540  return $this->mail_data;
541  }
542 
543  function getNextMail($a_mail_id)
544  {
545  global $ilDB;
546 
547  $ilDB->setLimit(1);
548  $res = $ilDB->queryf("
549  SELECT b.* FROM " . $this->table_mail ." a
550  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
551  AND b.user_id = a.user_id AND b.send_time < a.send_time
552  WHERE a.user_id = %s
553  AND a.mail_id = %s ORDER BY b.send_time DESC",
554  array('integer', 'integer'),
555  array($this->user_id, $a_mail_id));
556 
557  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
558 
559  return $this->mail_data;
560  }
561 
569  function getMailsOfFolder($a_folder_id, $filter = array())
570  {
571  global $ilDB;
572 
573  $this->mail_counter = array();
574  $this->mail_counter['read'] = 0;
575  $this->mail_counter['unread'] = 0;
576 
577  $query = "SELECT sender_id, m_subject, mail_id, m_status, send_time FROM ". $this->table_mail ."
578  LEFT JOIN object_data ON obj_id = sender_id
579  WHERE user_id = %s
580  AND folder_id = %s
581  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)) ";
582 
583  if($filter['status'])
584  {
585  $query .= ' AND m_status = '.$ilDB->quote($filter['status'], 'text');
586  }
587  if($filter['type'])
588  {
589  $query .= ' AND '.$ilDB->like('m_type', 'text', '%%:"'.$filter['type'].'"%%', false);
590  }
591 
592  $query .= " ORDER BY send_time DESC";
593 
594  $res = $ilDB->queryf($query,
595  array('integer', 'integer'),
596  array($this->user_id, $a_folder_id));
597 
598  while ($row = $ilDB->fetchObject($res))
599  {
600  $tmp = $this->fetchMailData($row);
601 
602  if($tmp['m_status'] == 'read')
603  {
604  ++$this->mail_counter['read'];
605  }
606 
607  if($tmp['m_status'] == 'unread')
608  {
609  ++$this->mail_counter['unread'];
610  }
611 
612  $output[] = $tmp;
613  }
614 
615  $this->mail_counter['total'] = count($output);
616 
617  return $output ? $output : array();
618  }
619 
626  function countMailsOfFolder($a_folder_id)
627  {
628  global $ilDB;
629 
630  $res = $ilDB->queryf("
631  SELECT COUNT(*) FROM ". $this->table_mail ."
632  WHERE user_id = %s
633  AND folder_id = %s",
634  array('integer', 'integer'),
635  array($this->user_id, $a_folder_id));
636 
637  return $res->numRows();
638  }
639 
646  function deleteMailsOfFolder($a_folder_id)
647  {
648  if ($a_folder_id)
649  {
650  global $ilDB;
651 
652  /*$statement = $ilDB->manipulateF("
653  DELETE 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  $mails = $this->getMailsOfFolder($a_folder_id);
659  foreach((array)$mails as $mail_data)
660  {
661  $this->deleteMails(array($mail_data['mail_id']));
662  }
663 
664  return true;
665  }
666 
667  return false;
668  }
669 
677  {
678  return is_array($this->mail_counter) ? $this->mail_counter : array(
679  "total" => 0,
680  "read" => 0,
681  "unread" => 0);
682  }
683 
690  function getMail($a_mail_id)
691  {
692  global $ilDB;
693 
694  $res = $ilDB->queryf("
695  SELECT * FROM ". $this->table_mail ."
696  WHERE user_id = %s
697  AND mail_id = %s",
698  array('integer', 'integer'),
699  array($this->user_id, $a_mail_id));
700 
701  $this->mail_data =$this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
702 
703  return $this->mail_data;
704  }
705 
712  function markRead($a_mail_ids)
713  {
714 
715  global $ilDB;
716 
717  $data = array();
718  $data_types = array();
719 
720  $query = "UPDATE ". $this->table_mail ."
721  SET m_status = %s
722  WHERE user_id = %s ";
723  array_push($data_types, 'text', 'integer');
724  array_push($data, 'read', $this->user_id);
725 
726  $cnt_mail_ids = count($a_mail_ids);
727 
728  if (is_array($a_mail_ids) &&
729  count($a_mail_ids) > 0)
730  {
731 
732  $in = 'mail_id IN (';
733  $counter = 0;
734  foreach($a_mail_ids as $a_mail_id)
735  {
736  array_push($data, $a_mail_id);
737  array_push($data_types, 'integer');
738 
739  if($counter > 0) $in .= ',';
740  $in .= '%s';
741  ++$counter;
742  }
743  $in .= ')';
744 
745  $query .= ' AND '.$in;
746  }
747 
748  $res = $ilDB->manipulateF($query, $data_types, $data);
749 
750  return true;
751  }
752 
759  function markUnread($a_mail_ids)
760  {
761  global $ilDB;
762 
763  $data = array();
764  $data_types = array();
765 
766  $query = "UPDATE ". $this->table_mail ."
767  SET m_status = %s
768  WHERE user_id = %s ";
769  array_push($data_types, 'text', 'integer');
770  array_push($data, 'unread', $this->user_id);
771 
772  $cnt_mail_ids = count($a_mail_ids);
773 
774  if (is_array($a_mail_ids) &&
775  count($a_mail_ids) > 0)
776  {
777 
778  $in = 'mail_id IN (';
779  $counter = 0;
780  foreach($a_mail_ids as $a_mail_id)
781  {
782  array_push($data, $a_mail_id);
783  array_push($data_types, 'integer');
784 
785  if($counter > 0) $in .= ',';
786  $in .= '%s';
787  ++$counter;
788  }
789  $in .= ')';
790 
791  $query .= ' AND '.$in;
792  }
793 
794  $statement = $ilDB->manipulateF($query, $data_types, $data);
795 
796  return true;
797  }
798 
806  function moveMailsToFolder($a_mail_ids,$a_folder_id)
807  {
808  global $ilDB;
809 
810  $data = array();
811  $data_types = array();
812 
813  $query = "UPDATE ". $this->table_mail ."
814  SET folder_id = %s
815  WHERE user_id = %s ";
816  array_push($data_types, 'text', 'integer');
817  array_push($data, $a_folder_id, $this->user_id);
818 
819  $cnt_mail_ids = count($a_mail_ids);
820 
821  if (is_array($a_mail_ids) &&
822  count($a_mail_ids) > 0)
823  {
824 
825  $in = 'mail_id IN (';
826  $counter = 0;
827  foreach($a_mail_ids as $a_mail_id)
828  {
829  array_push($data, $a_mail_id);
830  array_push($data_types, 'integer');
831 
832  if($counter > 0) $in .= ',';
833  $in .= '%s';
834  ++$counter;
835  }
836  $in .= ')';
837 
838  $query .= ' AND '.$in;
839  }
840 
841  $statement = $ilDB->manipulateF($query, $data_types, $data);
842 
843  return true;
844  }
845 
852  function deleteMails($a_mail_ids)
853  {
854  global $ilDB;
855 
856  foreach ($a_mail_ids as $id)
857  {
858  $statement = $ilDB->manipulateF("
859  DELETE FROM ". $this->table_mail ."
860  WHERE user_id = %s
861  AND mail_id = %s ",
862  array('integer', 'integer'),
863  array($this->user_id, $id));
864 
865  $this->mfile->deassignAttachmentFromDirectory($id);
866  }
867 
868  return true;
869  }
870 
877  function fetchMailData($a_row)
878  {
879  if (!$a_row) return;
880 
881  return array(
882  "mail_id" => $a_row->mail_id,
883  "user_id" => $a_row->user_id,
884  "folder_id" => $a_row->folder_id,
885  "sender_id" => $a_row->sender_id,
886  "attachments" => unserialize(stripslashes($a_row->attachments)),
887  "send_time" => $a_row->send_time,
888  "rcp_to" => $a_row->rcp_to,
889  "rcp_cc" => $a_row->rcp_cc,
890  "rcp_bcc" => $a_row->rcp_bcc,
891  "m_status" => $a_row->m_status,
892  "m_type" => unserialize(stripslashes($a_row->m_type)),
893  "m_email" => $a_row->m_email,
894  "m_subject" => $a_row->m_subject,
895  "m_message" => $a_row->m_message,
896  "import_name" => $a_row->import_name,
897  "use_placeholders"=> $a_row->use_placeholders);
898  }
899 
900  function updateDraft($a_folder_id,
901  $a_attachments,
902  $a_rcp_to,
903  $a_rcp_cc,
904  $a_rcp_bcc,
905  $a_m_type,
906  $a_m_email,
907  $a_m_subject,
908  $a_m_message,
909  $a_draft_id = 0, $a_use_placeholders = 0)
910  {
911  global $ilDB;
912 
913  $ilDB->update($this->table_mail,
914  array(
915  'folder_id' => array('integer', $a_folder_id),
916  'attachments' => array('clob', serialize($a_attachments)),
917  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
918  'rcp_to' => array('clob', $a_rcp_to),
919  'rcp_cc' => array('clob', $a_rcp_cc),
920  'rcp_bcc' => array('clob', $a_rcp_bcc),
921  'm_status' => array('text', 'read'),
922  'm_type' => array('text', serialize($a_m_type)),
923  'm_email' => array('integer', $a_m_email),
924  'm_subject' => array('text', $a_m_subject),
925  'm_message' => array('clob', $a_m_message),
926  'use_placeholders' => array('integer', $a_use_placeholders)
927  ),
928  array(
929  'mail_id' => array('integer', $a_draft_id)
930  )
931  );
932 
933  return $a_draft_id;
934  }
935 
953  function sendInternalMail($a_folder_id,
954  $a_sender_id,
955  $a_attachments,
956  $a_rcp_to,
957  $a_rcp_cc,
958  $a_rcp_bcc,
959  $a_status,
960  $a_m_type,
961  $a_m_email,
962  $a_m_subject,
963  $a_m_message,
964  $a_user_id = 0, $a_use_placeholders = 0)
965  {
966  $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
967 
968  global $ilDB, $log;
969  //$log->write('class.ilMail->sendInternalMail to user_id:'.$a_rcp_to.' '.$a_m_message);
970 
971  if ($a_use_placeholders)
972  {
973  $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
974  }
975  else
976  {
977  $a_use_placeholders = '0';
978  }
979 
980 
981  if(!$a_user_id) $a_user_id = '0';
982  if(!$a_folder_id) $a_folder_id = '0';
983  if(!$a_sender_id) $a_sender_id = NULL;
984  if(!$a_attachments) $a_attachments = NULL;
985  if(!$a_rcp_to) $a_rcp_to = NULL;
986  if(!$a_rcp_cc) $a_rcp_cc = NULL;
987  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
988  if(!$a_status) $a_status = NULL;
989  if(!$a_m_type) $a_m_type = NULL;
990  if(!$a_m_email) $a_m_email = NULL;
991  if(!$a_m_subject) $a_m_subject = NULL;
992  if(!$a_m_message) $a_m_message = NULL;
993 
994 
995  $next_id = $ilDB->nextId($this->table_mail);
996 
997  $ilDB->insert($this->table_mail, array(
998  'mail_id' => array('integer', $next_id),
999  'user_id' => array('integer', $a_user_id),
1000  'folder_id' => array('integer', $a_folder_id),
1001  'sender_id' => array('integer', $a_sender_id),
1002  'attachments' => array('clob', serialize($a_attachments)),
1003  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
1004  'rcp_to' => array('clob', $a_rcp_to),
1005  'rcp_cc' => array('clob', $a_rcp_cc),
1006  'rcp_bcc' => array('clob', $a_rcp_bcc),
1007  'm_status' => array('text', $a_status),
1008  'm_type' => array('text', serialize($a_m_type)),
1009  'm_email' => array('integer', $a_m_email),
1010  'm_subject' => array('text', $a_m_subject),
1011  'm_message' => array('clob', $a_m_message)
1012  ));
1013 
1014  return $next_id; //$ilDB->getLastInsertId();
1015 
1016  }
1017 
1018  function replacePlaceholders($a_message, $a_user_id)
1019  {
1020  global $lng;
1021 
1022  $user = self::getCachedUserInstance($a_user_id);
1023 
1024  // determine salutation
1025  switch ($user->getGender())
1026  {
1027  case 'f': $gender_salut = $lng->txt('salutation_f');
1028  break;
1029  case 'm': $gender_salut = $lng->txt('salutation_m');
1030  break;
1031  }
1032 
1033  $a_message = str_replace('[MAIL_SALUTATION]', $gender_salut, $a_message);
1034  $a_message = str_replace('[LOGIN]', $user->getLogin(), $a_message);
1035  $a_message = str_replace('[FIRST_NAME]', $user->getFirstname(), $a_message);
1036  $a_message = str_replace('[LAST_NAME]', $user->getLastname(), $a_message);
1037  $a_message = str_replace('[ILIAS_URL]', ILIAS_HTTP_PATH.'/login.php?client_id='.CLIENT_ID, $a_message);
1038  $a_message = str_replace('[CLIENT_NAME]', CLIENT_NAME, $a_message);
1039 
1040  return $a_message;
1041  }
1042 
1056  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)
1057  {
1058  global $log;
1059 
1060  include_once 'Services/Mail/classes/class.ilMailbox.php';
1061  include_once './Services/User/classes/class.ilObjUser.php';
1062 
1063  if (!ilMail::_usePearMail())
1064  {
1065  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1066  $a_rcp_to = $this->__substituteRecipients($a_rcp_to, 'resubstitute');
1067  $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc, 'resubstitute');
1068  $a_rcp_bcc = $this->__substituteRecipients($a_rcp_bcc, 'resubstitute');
1069  }
1070 
1071  $mbox = new ilMailbox();
1072 
1073  if (!$a_use_placeholders) # No Placeholders
1074  {
1075  $rcp_ids = $this->getUserIds(trim($a_rcp_to).','.trim($a_rcp_cc).','.trim($a_rcp_bcc));
1076 
1077  $as_email = array();
1078 
1079  foreach($rcp_ids as $id)
1080  {
1081  $tmp_mail_options = new ilMailOptions($id);
1082 
1083  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1084  $tmp_user = self::getCachedUserInstance($id);
1085  $user_is_active = $tmp_user->getActive();
1086  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1087  $tmp_user->checkTimeLimit();
1088 
1089  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1090  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1091  {
1092  continue;
1093  }
1094 
1095  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS/HER MAIL
1096  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1097 
1098  // Do not send external mails to inactive users!!!
1099  if($user_is_active)
1100  {
1101  if (!$user_can_read_internal_mails ||
1102  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1103  {
1104  $as_email[] = $tmp_user->getEmail();
1105  continue;
1106  }
1107 
1108  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1109  {
1110  $as_email[] = $tmp_user->getEmail();
1111  }
1112  }
1113  $mbox->setUserId($id);
1114  $inbox_id = $mbox->getInboxFolder();
1115 
1116  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1117  $a_attachments, $a_rcp_to,
1118  $a_rcp_cc, '', 'unread', $a_type,
1119  0, $a_subject, $a_message, $id, 0);
1120  if ($a_attachments)
1121  {
1122  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1123  }
1124  }
1125 
1126  // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
1127  $to = array();
1128  $bcc = array();
1129 
1130  if(count($as_email) == 1)
1131  {
1132  $to[] = $as_email[0];
1133  }
1134  else
1135  {
1136  foreach ($as_email as $email)
1137  {
1138  $bcc[] = $email;
1139  }
1140  }
1141 
1142  if(count($to) > 0 || count($bcc) > 0)
1143  {
1144  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
1145  }
1146  }
1147  else # Use Placeholders
1148  {
1149  // to
1150  $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
1151 
1152  // cc / bcc
1153  $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
1154 
1155  $as_email = array();
1156 
1157  // to
1158  foreach($rcp_ids_replace as $id)
1159  {
1160  $tmp_mail_options = new ilMailOptions($id);
1161 
1162  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1163  $tmp_user = self::getCachedUserInstance($id);
1164  $user_is_active = $tmp_user->getActive();
1165  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1166  $tmp_user->checkTimeLimit();
1167 
1168  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1169  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1170  {
1171  continue;
1172  }
1173 
1174  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1175  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1176 
1177  // Do not send external mails to inactive users!!!
1178  if($user_is_active)
1179  {
1180  if (!$user_can_read_internal_mails ||
1181  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1182  {
1183  $as_email[$tmp_user->getId()] = $tmp_user->getEmail();
1184  continue;
1185  }
1186 
1187  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1188  {
1189  $as_email[$tmp_user->getId()] = $tmp_user->getEmail();
1190  }
1191  }
1192  $mbox->setUserId($id);
1193  $inbox_id = $mbox->getInboxFolder();
1194 
1195  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1196  $a_attachments, $a_rcp_to,
1197  $a_rcp_cc, '', 'unread', $a_type,
1198  0, $a_subject, $a_message, $id, 1);
1199  if ($a_attachments)
1200  {
1201  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1202  }
1203  }
1204 
1205  if (count($as_email))
1206  {
1207  foreach ($as_email as $id => $email)
1208  {
1209  $this->sendMimeMail($email, '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
1210  }
1211  }
1212 
1213  $as_email = array();
1214 
1215  // cc / bcc
1216  foreach($rcp_ids_no_replace as $id)
1217  {
1218  $tmp_mail_options = new ilMailOptions($id);
1219 
1220  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1221  $tmp_user = self::getCachedUserInstance($id);
1222  $user_is_active = $tmp_user->getActive();
1223  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement()
1224  && $tmp_user->checkTimeLimit();
1225 
1226  // Do not send external mails to inactive users!!!
1227  if($user_is_active)
1228  {
1229  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1230  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1231  {
1232  continue;
1233  }
1234 
1235  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1236  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1237  if (!$user_can_read_internal_mails ||
1238  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1239  {
1240  $as_email[] = $tmp_user->getEmail();
1241  continue;
1242  }
1243 
1244  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1245  {
1246  $as_email[] = $tmp_user->getEmail();
1247  }
1248  }
1249  $mbox->setUserId($id);
1250  $inbox_id = $mbox->getInboxFolder();
1251 
1252  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1253  $a_attachments, $a_rcp_to,
1254  $a_rcp_cc, '', 'unread', $a_type,
1255  0, $a_subject, $a_message, $id, 0);
1256  if ($a_attachments)
1257  {
1258  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1259  }
1260  }
1261 
1262  if (count($as_email))
1263  {
1264  $this->sendMimeMail('', '', implode(',', $as_email), $a_subject, $a_message, $a_attachments);
1265  }
1266  }
1267 
1268  return true;
1269  }
1270 
1276  function getUserIds($a_recipients)
1277  {
1278  global $log, $rbacreview;
1279  $ids = array();
1280 
1281  $this->validatePear($a_recipients);
1282  if (ilMail::_usePearMail() && $this->getUsePear() == true)
1283  {
1284  $tmp_names = $this->explodeRecipients($a_recipients );
1285  if (! is_a($tmp_names, 'PEAR_Error'))
1286  {
1287  for ($i = 0;$i < count($tmp_names); $i++)
1288  {
1289  if ( substr($tmp_names[$i]->mailbox,0,1) === '#' ||
1290  (substr($tmp_names[$i]->mailbox,0,1) === '"' &&
1291  substr($tmp_names[$i]->mailbox,1,1) === '#' ) )
1292  {
1293  $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1294  foreach($role_ids as $role_id)
1295  {
1296  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1297  {
1298  $ids[] = $usr_id;
1299  }
1300  }
1301  }
1302  else if (strtolower($tmp_names[$i]->host) == 'ilias')
1303  {
1304  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
1305  {
1306  //$log->write('class.ilMail->getUserIds() recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host.' user_id:'.$id);
1307  $ids[] = $id;
1308  }
1309  else
1310  {
1311  //$log->write('class.ilMail->getUserIds() no user account found for recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1312  }
1313  }
1314  else
1315  {
1316  // Fixed mantis bug #5875
1317  if($id = ilObjUser::_lookupId($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host))
1318  {
1319  $ids[] = $id;
1320  }
1321  else
1322  {
1323  //$log->write('class.ilMail->getUserIds() external recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1324  }
1325  }
1326  }
1327  }
1328  else
1329  {
1330  //$log->write('class.ilMail->getUserIds() illegal recipients:'.$a_recipients);
1331  }
1332  }
1333  else
1334  {
1335  $tmp_names = $this->explodeRecipients($a_recipients, $this->getUsePear());
1336  for ($i = 0;$i < count($tmp_names); $i++)
1337  {
1338  if (substr($tmp_names[$i],0,1) == '#')
1339  {
1340  if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
1341  {
1342  include_once("./Services/Object/classes/class.ilObjectFactory.php");
1343  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1344 
1345  foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1346  {
1347  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1348  break;
1349  }
1350  // STORE MEMBER IDS IN $ids
1351  foreach ($grp_object->getGroupMemberIds() as $id)
1352  {
1353  $ids[] = $id;
1354  }
1355  }
1356  // is role: get role ids
1357  elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
1358  {
1359  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1360  {
1361  $ids[] = $usr_id;
1362  }
1363  }
1364 
1365  }
1366  else if (!empty($tmp_names[$i]))
1367  {
1368  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
1369  {
1370  $ids[] = $id;
1371  }
1372  }
1373  }
1374  }
1375  return array_unique($ids);
1376  }
1377 
1388  function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
1389  {
1390  $error_message = '';
1391 
1392  $a_m_subject = trim($a_m_subject);
1393  $a_rcp_to = trim($a_rcp_to);
1394 
1395  if (empty($a_m_subject))
1396  {
1397  $error_message .= $error_message ? "<br>" : '';
1398  $error_message .= $this->lng->txt("mail_add_subject");
1399  }
1400 
1401  if (empty($a_rcp_to))
1402  {
1403  $error_message .= $error_message ? "<br>" : '';
1404  $error_message .= $this->lng->txt("mail_add_recipient");
1405  }
1406 
1407  return $error_message;
1408  }
1409 
1416  function getEmailsOfRecipients($a_rcp)
1417  {
1418  global $rbacreview;
1419 
1420  $addresses = array();
1421 
1422  $this->validatePear($a_rcp);
1423  if (ilMail::_usePearMail() && $this->getUsePear())
1424  {
1425  $tmp_rcp = $this->explodeRecipients($a_rcp);
1426  if (! is_a($tmp_rcp, 'PEAR_Error'))
1427  {
1428  foreach ($tmp_rcp as $rcp)
1429  {
1430  // NO GROUP
1431  if (substr($rcp->mailbox,0,1) != '#')
1432  {
1433  if (strtolower($rcp->host) != 'ilias')
1434  {
1435  $addresses[] = $rcp->mailbox.'@'.$rcp->host;
1436  continue;
1437  }
1438 
1439  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
1440  {
1441  $tmp_user = self::getCachedUserInstance($id);
1442  $addresses[] = $tmp_user->getEmail();
1443  continue;
1444  }
1445  }
1446  else
1447  {
1448  // Roles
1449  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1450  foreach($role_ids as $role_id)
1451  {
1452  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1453  {
1454  $tmp_user = self::getCachedUserInstance($usr_id);
1455  $addresses[] = $tmp_user->getEmail();
1456  }
1457  }
1458  }
1459  }
1460  }
1461  }
1462  else
1463  {
1464  $tmp_rcp = $this->explodeRecipients($a_rcp, $this->getUsePear());
1465 
1466  foreach ($tmp_rcp as $rcp)
1467  {
1468  // NO GROUP
1469  if (substr($rcp,0,1) != '#')
1470  {
1471  if (strpos($rcp,'@'))
1472  {
1473  $addresses[] = $rcp;
1474  continue;
1475  }
1476 
1477  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1478  {
1479  $tmp_user = self::getCachedUserInstance($id);
1480  $addresses[] = $tmp_user->getEmail();
1481  continue;
1482  }
1483  }
1484  else
1485  {
1486  // GROUP THINGS
1487  include_once("./Services/Object/classes/class.ilObjectFactory.php");
1488  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1489 
1490  // Fix
1491  foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($rcp,1)))) as $ref_id)
1492  {
1493  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1494  break;
1495  }
1496  // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
1497  foreach ($grp_object->getGroupMemberIds() as $id)
1498  {
1499  $tmp_user = self::getCachedUserInstance($id);
1500  $addresses[] = $tmp_user->getEmail();
1501  }
1502  }
1503  }
1504  }
1505 
1506  return $addresses;
1507  }
1508 
1516  function checkRecipients($a_recipients,$a_type)
1517  {
1518  global $rbacsystem,$rbacreview;
1519  $wrong_rcps = '';
1520 
1521  $this->validatePear($a_recipients);
1522  if (ilMail::_usePearMail() && $this->getUsePear())
1523  {
1524  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1525 
1526  if (is_a($tmp_rcp, 'PEAR_Error'))
1527  {
1528  $colon_pos = strpos($tmp_rcp->message, ':');
1529  $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
1530  }
1531  else
1532  {
1533  foreach ($tmp_rcp as $rcp)
1534  {
1535  // NO ROLE MAIL ADDRESS
1536  if (substr($rcp->mailbox,0,1) != '#')
1537  {
1538  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1539  $user_id = ($rcp->host == 'ilias') ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
1540  if ($user_id == false && $rcp->host == 'ilias')
1541  {
1542  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
1543  continue;
1544  }
1545 
1546  // CHECK IF USER CAN RECEIVE MAIL
1547  if ($user_id)
1548  {
1549  if(!$rbacsystem->checkAccessOfUser($user_id, "internal_mail", $this->getMailObjectReferenceId()))
1550  {
1551  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1552  " (".$this->lng->txt("user_cant_receive_mail").")";
1553  continue;
1554  }
1555  }
1556  }
1557  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
1558  {
1559  if (!$this->mlists->mailingListExists($rcp->mailbox))
1560  {
1561  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1562  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1563  }
1564 
1565  continue;
1566  }
1567  else
1568  {
1569 
1570  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1571 
1572  if(!$this->mail_to_global_roles && is_array($role_ids))
1573  {
1574  foreach($role_ids as $role_id)
1575  {
1576  if($rbacreview->isGlobalRole($role_id))
1577  {
1578  include_once('Services/Mail/exceptions/class.ilMailException.php');
1579  throw new ilMailException('mail_to_global_roles_not_allowed');
1580 
1581  }
1582  }
1583  }
1584  if (count($role_ids) == 0)
1585  {
1586  $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
1587  ' ('.$this->lng->txt('mail_no_recipient_found').')';
1588  continue;
1589  }
1590  else if (count($role_ids) > 1)
1591  {
1592  $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
1593  ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
1594  }
1595  }
1596  }
1597  }
1598  }
1599  else // NO PEAR
1600  {
1601  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1602 
1603  foreach ($tmp_rcp as $rcp)
1604  {
1605  if (empty($rcp))
1606  {
1607  continue;
1608  }
1609  // NO GROUP
1610  if (substr($rcp,0,1) != '#')
1611  {
1612  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1613  if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
1614  !ilUtil::is_email($rcp))
1615  {
1616  $wrong_rcps .= "<br />".htmlentities($rcp);
1617  continue;
1618  }
1619 
1620  // CHECK IF USER CAN RECEIVE MAIL
1621  if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1622  {
1623  if(!$rbacsystem->checkAccessOfUser($user_id, "internal_mail", $this->getMailObjectReferenceId()))
1624  {
1625  $wrong_rcps .= "<br />".htmlentities($rcp).
1626  " (".$this->lng->txt("user_cant_receive_mail").")";
1627  continue;
1628  }
1629  }
1630  }
1631  else if (substr($rcp, 0, 7) == '#il_ml_')
1632  {
1633  if (!$this->mlists->mailingListExists($rcp))
1634  {
1635  $wrong_rcps .= "<br />".htmlentities($rcp).
1636  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1637  }
1638 
1639  continue;
1640  }
1641  else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
1642  {
1643  continue;
1644  }
1645  else if (!$rbacreview->roleExists(addslashes(substr($rcp,1))))
1646  {
1647  $wrong_rcps .= "<br />".htmlentities($rcp).
1648  " (".$this->lng->txt("mail_no_valid_group_role").")";
1649  continue;
1650  }
1651  else if (!$this->mail_to_global_roles)
1652  {
1653  $role_id = $rbacreview->roleExists(addslashes(substr($rcp,1)));
1654  if((int)$role_id && $rbacreview->isGlobalRole($role_id))
1655  {
1656  include_once('Services/Mail/exceptions/class.ilMailException.php');
1657  throw new ilMailException('mail_to_global_roles_not_allowed');
1658  }
1659  }
1660  }
1661  }
1662  return $wrong_rcps;
1663  }
1664 
1680  function savePostData($a_user_id,
1681  $a_attachments,
1682  $a_rcp_to,
1683  $a_rcp_cc,
1684  $a_rcp_bcc,
1685  $a_m_type,
1686  $a_m_email,
1687  $a_m_subject,
1688  $a_m_message,
1689  $a_use_placeholders)
1690  {
1691  global $ilDB;
1692 
1693 
1694  if(!$a_attachments) $a_attachments = NULL;
1695  if(!$a_rcp_to) $a_rcp_to = NULL;
1696  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1697  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1698  if(!$a_m_type) $a_m_type = NULL;
1699  if(!$a_m_email) $a_m_email = NULL;
1700  if(!$a_m_message) $a_m_message = NULL;
1701  if(!$a_use_placeholders) $a_use_placeholders = '0';
1702 
1703 
1704  $statement = $ilDB->manipulateF('
1705  DELETE FROM '. $this->table_mail_saved .'
1706  WHERE user_id = %s',
1707  array('integer'), array($this->user_id));
1708 
1709  $ilDB->insert($this->table_mail_saved, array(
1710  'user_id' => array('integer', $a_user_id),
1711  'attachments' => array('clob', serialize($a_attachments)),
1712  'rcp_to' => array('clob', $a_rcp_to),
1713  'rcp_cc' => array('clob', $a_rcp_cc),
1714  'rcp_bcc' => array('clob', $a_rcp_bcc),
1715  'm_type' => array('text', serialize($a_m_type)),
1716  'm_email' => array('integer', $a_m_email),
1717  'm_subject' => array('text', $a_m_subject),
1718  'm_message' => array('clob', $a_m_message),
1719  'use_placeholders' => array('integer', $a_use_placeholders),
1720  ));
1721 
1722  $this->getSavedData();
1723 
1724  return true;
1725  }
1726 
1732  function getSavedData()
1733  {
1734  global $ilDB;
1735 
1736  $res = $ilDB->queryf('
1737  SELECT * FROM '. $this->table_mail_saved .'
1738  WHERE user_id = %s',
1739  array('integer'),
1740  array($this->user_id));
1741 
1742  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
1743 
1744  return $this->mail_data;
1745  }
1746 
1760  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)
1761  {
1762  global $lng,$rbacsystem,$log;
1763  //$log->write('class.ilMail.sendMail '.$a_rcp_to.' '.$a_m_subject);
1764 
1765  $this->mail_to_global_roles = true;
1766  if($this->user_id != ANONYMOUS_USER_ID)
1767  {
1768  $this->mail_to_global_roles = $rbacsystem->checkAccessOfUser($this->user_id, 'mail_to_global_roles', $this->mail_obj_ref_id);
1769  }
1770 
1771  $error_message = '';
1772  $message = '';
1773 
1774  if (in_array("system",$a_type))
1775  {
1776  $this->__checkSystemRecipients($a_rcp_to);
1777  $a_type = array('system');
1778  }
1779 
1780  if ($a_attachment)
1781  {
1782  if (!$this->mfile->checkFilesExist($a_attachment))
1783  {
1784  return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
1785  }
1786  }
1787  // CHECK NECESSARY MAIL DATA FOR ALL TYPES
1788  if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
1789  {
1790  return $error_message;
1791  }
1792 
1793  try
1794  {
1795  // check recipients
1796  if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
1797  {
1798  $message .= $error_message;
1799  }
1800 
1801  if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
1802  {
1803  $message .= $error_message;
1804  }
1805 
1806  if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
1807  {
1808  $message .= $error_message;
1809  }
1810  }
1811 
1812  catch(ilMailException $e)
1813  {
1814  return $this->lng->txt($e->getMessage());
1815  }
1816 
1817  // if there was an error
1818  if (!empty($message))
1819  {
1820  return $this->lng->txt("mail_following_rcp_not_valid").$message;
1821  }
1822 
1823  // ACTIONS FOR ALL TYPES
1824  // GET RCPT OF MAILING LISTS
1825  $rcp_to = $this->parseRcptOfMailingLists($a_rcp_to);
1826  $rcp_cc = $this->parseRcptOfMailingLists($a_rcp_cc);
1827  $rcp_bc = $this->parseRcptOfMailingLists($a_rcp_bc);
1828 
1829  if (! ilMail::_usePearMail() )
1830  {
1831  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1832  $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
1833  $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
1834  $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
1835  }
1836 
1837  // COUNT EMAILS
1838  $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
1839  $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
1840 
1841  // currently disabled..
1842  /*
1843  if (count($c_emails))
1844  {
1845  if (!$this->getEmailOfSender())
1846  {
1847  return $lng->txt("mail_check_your_email_addr");
1848  }
1849  }
1850  */
1851 
1852  // check smtp permission
1853  if($c_emails && $this->user_id != ANONYMOUS_USER_ID &&
1854  !$rbacsystem->checkAccessOfUser($this->user_id, 'smtp_mail', $this->mail_obj_ref_id))
1855  {
1856  return $this->lng->txt('mail_no_permissions_write_smtp');
1857  }
1858 
1859  if($this->appendInstallationSignature())
1860  {
1861  $a_m_message .= self::_getInstallationSignature();
1862  }
1863 
1864  // save mail in sent box
1865  $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
1866  $a_m_subject,$a_m_message);
1867 
1868  if($a_attachment)
1869  {
1870  $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
1871 
1872  if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
1873  {
1874  return $error;
1875  }
1876  }
1877 
1878  // FILTER EMAILS
1879  // IF EMAIL RECIPIENT
1880  if($c_emails)
1881  {
1882  $this->sendMimeMail($this->__getEmailRecipients($rcp_to),
1883  $this->__getEmailRecipients($rcp_cc),
1884  $this->__getEmailRecipients($rcp_bc),
1885  $a_m_subject,
1886  $a_m_message,
1887  $a_attachment,
1888  0);
1889  }
1890 
1891  if (in_array('system',$a_type))
1892  {
1893  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))
1894  {
1895  return $lng->txt("mail_send_error");
1896  }
1897  }
1898  // ACTIONS FOR TYPE SYSTEM AND NORMAL
1899  if (in_array('normal',$a_type))
1900  {
1901  // TRY BOTH internal and email (depends on user settings)
1902  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))
1903  {
1904  return $lng->txt("mail_send_error");
1905  }
1906  }
1907 
1908  // Temporary bugfix
1909  if (!$this->getSaveInSentbox())
1910  {
1911  $this->deleteMails(array($sent_id));
1912  }
1913 
1914  return '';
1915  }
1916 
1917  function parseRcptOfMailingLists($rcpt = '')
1918  {
1919  if ($rcpt == '') return $rcpt;
1920 //@todo check rcp pear validation
1921  $arrRcpt = $this->explodeRecipients(trim($rcpt));
1922  if (!is_array($arrRcpt) || empty($arrRcpt)) return $rcpt;
1923 
1924  $new_rcpt = array();
1925 
1926  foreach ($arrRcpt as $item)
1927  {
1928  if (ilMail::_usePearMail())
1929  {
1930  if (substr($item->mailbox, 0, 7) == '#il_ml_')
1931  {
1932  if ($this->mlists->mailingListExists($item->mailbox))
1933  {
1934  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1935  {
1936  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1937  }
1938  }
1939  }
1940  else
1941  {
1942  $new_rcpt[] = $item->mailbox.'@'.$item->host;
1943  }
1944  }
1945  else
1946  {
1947  if (substr($item, 0, 7) == '#il_ml_')
1948  {
1949  if ($this->mlists->mailingListExists($item))
1950  {
1951  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1952  {
1953  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1954  }
1955  }
1956  }
1957  else
1958  {
1959  $new_rcpt[] = $item;
1960  }
1961  }
1962  }
1963 
1964  return implode(',', $new_rcpt);
1965  }
1966 
1979  function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
1980  $a_m_subject,$a_m_message)
1981  {
1982  include_once "Services/Mail/classes/class.ilMailbox.php";
1983 
1984  $mbox = new ilMailbox($this->user_id);
1985  $sent_id = $mbox->getSentFolder();
1986 
1988  return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
1989  $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
1990  }
1991 
1998  public static function addFullname($a_email, $a_fullname)
1999  {
2000  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2001  return ilMimeMail::_mimeEncode($a_fullname).' <'.$a_email.'>';
2002  }
2003 
2010  public function getMimeMailSender()
2011  {
2015  global $ilUser;
2016 
2017  include_once "Services/Mail/classes/class.ilMimeMail.php";
2018 
2019  if($this->user_id != ANONYMOUS_USER_ID)
2020  {
2021  $email = $ilUser->getEmail();
2022  $fullname = $ilUser->getFullname();
2023  if($ilUser->getId() != $this->user_id)
2024  {
2025  $user = self::getCachedUserInstance($this->user_id);
2026  $email = $user->getEmail();
2027  $fullname = $user->getFullname();
2028  }
2029 
2030  $sender = self::addFullname($email, $fullname);
2031  }
2032  else
2033  {
2034  $sender = self::getIliasMailerAddress();
2035  }
2036 
2037  return $sender;
2038  }
2039 
2049  public static function getIliasMailerAddress()
2050  {
2051  global $ilSetting;
2052 
2053  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2054 
2055  $no_reply_adress = trim($ilSetting->get('mail_external_sender_noreply'));
2056  if(strlen($no_reply_adress))
2057  {
2058  if(strpos($no_reply_adress, '@') === false)
2059  $no_reply_adress = 'noreply@'.$no_reply_adress;
2060 
2061  if(!ilUtil::is_email($no_reply_adress))
2062  {
2063  $no_reply_adress = 'noreply@'.$_SERVER['SERVER_NAME'];
2064  }
2065 
2066  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2067  ' <'.$no_reply_adress.'>';
2068  }
2069  else
2070  {
2071  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2072  ' <noreply@'.$_SERVER['SERVER_NAME'].'>';
2073  }
2074 
2075  return $sender;
2076  }
2077 
2091  function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,$a_no_soap = false)
2092  {
2093  include_once "Services/Mail/classes/class.ilMimeMail.php";
2094 
2095  #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
2096 
2097  #$inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 4";
2098  #$a_m_subject = "[".$inst_name."] ".$a_m_subject;
2099 
2100  $a_m_subject = self::getSubjectPrefix().' '.$a_m_subject;
2101 
2102  $sender = $this->getMimeMailSender();
2103 
2104  // #10854
2105  if($this->isSOAPEnabled() && !$a_no_soap)
2106  {
2107  // Send per soap
2108  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
2109 
2110  $soap_client = new ilSoapClient();
2111  $soap_client->setResponseTimeout(1);
2112  $soap_client->enableWSDL(true);
2113  $soap_client->init();
2114 
2115  $attachments = array();
2116  $a_attachments = $a_attachments ? $a_attachments : array();
2117  foreach($a_attachments as $attachment)
2118  {
2119  $attachments[] = $this->mfile->getAbsolutePath($attachment);
2120  }
2121  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
2122  $attachments = implode('#:#',$attachments);
2123  // mjansen: use "#:#" as leading delimiter
2124  if(strlen($attachments))
2125  $attachments = "#:#".$attachments;
2126 
2127  $soap_client->call('sendMail',array(session_id().'::'.$_COOKIE['ilClientId'], // session id
2128  $a_rcp_to,
2129  $a_rcp_cc,
2130  $a_rcp_bcc,
2131  $sender,
2132  $a_m_subject,
2133  $a_m_message,
2134  $attachments));
2135 
2136  return true;
2137  }
2138  else
2139  {
2140  // send direct
2141  include_once "Services/Mail/classes/class.ilMimeMail.php";
2142 
2143  $mmail = new ilMimeMail();
2144  $mmail->autoCheck(false);
2145  $mmail->From($sender);
2146  $mmail->To($a_rcp_to);
2147  // Add installation name to subject
2148  $mmail->Subject($a_m_subject);
2149  $mmail->Body($a_m_message);
2150 
2151  if ($a_rcp_cc)
2152  {
2153  $mmail->Cc($a_rcp_cc);
2154  }
2155 
2156  if ($a_rcp_bcc)
2157  {
2158  $mmail->Bcc($a_rcp_bcc);
2159  }
2160 
2161  if (is_array($a_attachments))
2162  {
2163  foreach ($a_attachments as $attachment)
2164  {
2165  $mmail->Attach($this->mfile->getAbsolutePath($attachment));
2166  }
2167  }
2168 
2169  $mmail->Send();
2170  }
2171  }
2177  function getEmailOfSender()
2178  {
2179  $umail = self::getCachedUserInstance($this->user_id);
2180  $sender = $umail->getEmail();
2181 
2182  if (ilUtil::is_email($sender))
2183  {
2184  return $sender;
2185  }
2186  else
2187  {
2188  return '';
2189  }
2190  }
2191 
2198  function saveAttachments($a_attachments)
2199  {
2200  global $ilDB;
2201 
2202  $ilDB->update($this->table_mail_saved,
2203  array
2204  (
2205  'attachments' => array('clob', serialize($a_attachments))
2206  ),
2207  array
2208  (
2209  'user_id' => array('integer', $this->user_id)
2210  )
2211  );
2212 
2213  return true;
2214  }
2215 
2221  function getAttachments()
2222  {
2223  return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
2224  }
2225 
2239  function explodeRecipients($a_recipients, $use_pear = true)
2240  {
2241  $a_recipients = trim($a_recipients);
2242 
2243  // WHITESPACE IS NOT ALLOWED AS SEPERATOR
2244  #$a_recipients = preg_replace("/ /",",",$a_recipients);
2245  $a_recipients = preg_replace("/;/",",",$a_recipients);
2246 
2247  if (ilMail::_usePearMail() && $use_pear == true)
2248  {
2249  if (strlen(trim($a_recipients)) > 0)
2250  {
2251  require_once './Services/PEAR/lib/Mail/RFC822.php';
2252  $parser = new Mail_RFC822();
2253  return $parser->parseAddressList($a_recipients, "ilias", false, true);
2254  } else {
2255  return array();
2256  }
2257  }
2258  else
2259  {
2260  foreach(explode(',',$a_recipients) as $tmp_rec)
2261  {
2262  if($tmp_rec)
2263  {
2264  $rcps[] = trim($tmp_rec);
2265  }
2266  }
2267  return is_array($rcps) ? $rcps : array();
2268  }
2269  }
2270 
2271  function __getCountRecipient($rcp,$a_only_email = true)
2272  {
2273  $counter = 0;
2274 
2275  $this->validatePear($rcp);
2276  if (ilMail::_usePearMail() && $this->getUsePear())
2277  {
2278  $tmp_rcp = $this->explodeRecipients($rcp);
2279  if (! is_a($tmp_rcp, 'PEAR_Error'))
2280  {
2281  foreach ($tmp_rcp as $to)
2282  {
2283  if ($a_only_email)
2284  {
2285  // Fixed mantis bug #5875
2286  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2287  {
2288  continue;
2289  }
2290 
2291  // Addresses which aren't on the ilias host, and
2292  // which have a mailbox which does not start with '#',
2293  // are external e-mail addresses
2294  if ($to->host != 'ilias' && substr($to->mailbox,0,1) != '#')
2295  {
2296  ++$counter;
2297  }
2298  }
2299  else
2300  {
2301  ++$counter;
2302  }
2303  }
2304  }
2305  }
2306  else
2307  {
2308  foreach ($this->explodeRecipients($rcp,$this->getUsePear()) as $to)
2309  {
2310  if ($a_only_email)
2311  {
2312  $to = $this->__substituteRecipients($to,"resubstitute");
2313  if (strpos($to,'@'))
2314  {
2315  // Fixed mantis bug #5875
2316  if(ilObjUser::_lookupId($to))
2317  {
2318  continue;
2319  }
2320 
2321  ++$counter;
2322  }
2323  }
2324  else
2325  {
2326  ++$counter;
2327  }
2328  }
2329  }
2330  return $counter;
2331  }
2332 
2333 
2334  function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
2335  {
2336  return $this->__getCountRecipient($a_to,$a_only_email)
2337  + $this->__getCountRecipient($a_cc,$a_only_email)
2338  + $this->__getCountRecipient($a_bcc,$a_only_email);
2339  }
2340 
2341  function __getEmailRecipients($a_rcp)
2342  {
2343  if (ilMail::_usePearMail())
2344  {
2345  $rcp = array();
2346  $tmp_rcp = $this->explodeRecipients($a_rcp);
2347  if (! is_a($tmp_rcp, 'PEAR_Error'))
2348  {
2349  foreach ($tmp_rcp as $to)
2350  {
2351  if(substr($to->mailbox,0,1) != '#' && $to->host != 'ilias')
2352  {
2353  // Fixed mantis bug #5875
2354  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2355  {
2356  continue;
2357  }
2358 
2359  $rcp[] = $to->mailbox.'@'.$to->host;
2360  }
2361  }
2362  }
2363  return implode(',',$rcp);
2364  }
2365  else
2366  {
2367  foreach ($this->explodeRecipients($a_rcp) as $to)
2368  {
2369  $to = $this->__substituteRecipients($to,"resubstitute");
2370  if(strpos($to,'@'))
2371  {
2372  // Fixed mantis bug #5875
2373  if(ilObjUser::_lookupId($to))
2374  {
2375  continue;
2376  }
2377 
2378  $rcp[] = $to;
2379  }
2380  }
2381  return implode(',',$rcp ? $rcp : array());
2382  }
2383  }
2384 
2385  function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
2386  {
2387  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
2388 
2389  $message = $inst_name." To:".$rcp_to."\n";
2390 
2391  if ($rcp_cc)
2392  {
2393  $message .= "Cc: ".$rcp_cc;
2394  }
2395 
2396  $message .= "\n\n";
2397  $message .= $a_m_message;
2398 
2399  return $message;
2400  }
2401 
2402  function __checkSystemRecipients(&$a_rcp_to)
2403  {
2404  if (preg_match("/@all/",$a_rcp_to))
2405  {
2406  // GET ALL LOGINS
2407  $all = ilObjUser::_getAllUserLogins($this->ilias);
2408  $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
2409  }
2410 
2411  return;
2412  }
2413 
2421  function __substituteRecipients($a_rcp,$direction)
2422  {
2423  $new_name = array();
2424 
2425  $tmp_names = $this->explodeRecipients($a_rcp);
2426 
2427 
2428  foreach($tmp_names as $name)
2429  {
2430  if(strpos($name,"#") === 0)
2431  {
2432  $new_name[] = $name;
2433  continue;
2434  }
2435  switch($direction)
2436  {
2437  case "substitute":
2438  if(strpos($name,"@") and ilObjUser::_loginExists($name))
2439  {
2440  $new_name[] = preg_replace("/@/","�#�",$name);
2441  }
2442  else
2443  {
2444  $new_name[] = $name;
2445  }
2446  break;
2447 
2448  case "resubstitute":
2449  if(stristr($name,"�#�"))
2450  {
2451  $new_name[] = preg_replace("/�#�/","@",$name);
2452  }
2453  else
2454  {
2455  $new_name[] = $name;
2456  }
2457  break;
2458  }
2459  }
2460  return implode(",",$new_name);
2461  }
2462 
2478  public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null)
2479  {
2480  if (ilMail::_usePearMail())
2481  {
2482  if ($login == null)
2483  {
2484  require_once './Services/User/classes/class.ilObjUser.php';
2486  $login = $usr_obj->getLogin();
2487  $firstname = $usr_obj->getFirstname();
2488  $lastname = $usr_obj->getLastname();
2489  }
2490  // The following line of code creates a properly formatted mailbox
2491  // address. Unfortunately, it does not work, because ILIAS removes
2492  // everything between '<' '>' characters
2493  // Therefore, we just return the login - sic.
2494  // FIXME - Make this work in a future release
2495  /*
2496  return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
2497  */
2498  return $login.'hhho';
2499  }
2500  else
2501  {
2502  return $login;
2503  }
2504  }
2511  public static function _usePearMail()
2512  {
2516  global $ilSetting;
2517 
2518  return $ilSetting->get('pear_mail_enable', 0);
2519  }
2520 
2529  public static function _getAutoGeneratedMessageString($lang = null)
2530  {
2531  global $ilSetting;
2532 
2533  if(!$lang)
2534  {
2535  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
2537  }
2538  $http_path = ilUtil::_getHttpPath();
2539 
2540  $lang->loadLanguageModule('mail');
2541  return sprintf($lang->txt('mail_auto_generated_info'),
2542  $ilSetting->get('inst_name','ILIAS 4'),
2543  $http_path)."\n\n";
2544  }
2545 
2552  public static function _getIliasMailerName()
2553  {
2557  global $ilSetting;
2558 
2559  if(strlen($ilSetting->get('mail_system_sender_name')))
2560  {
2561  return $ilSetting->get('mail_system_sender_name');
2562  }
2563  else if(strlen($ilSetting->get('short_inst_name')))
2564  {
2565  return $ilSetting->get('short_inst_name');
2566  }
2567 
2568  return 'ILIAS';
2569  }
2570 
2579  public function appendInstallationSignature($a_flag = null)
2580  {
2581  if(null === $a_flag) {
2583  }
2584 
2585  $this->appendInstallationSignature = $a_flag;
2586 
2587  return $this;
2588  }
2589 
2598  public static function _getInstallationSignature()
2599  {
2600  global $ilClientIniFile;
2601 
2602  $signature = "\n\n* * * * *\n";
2603  $signature .= $ilClientIniFile->readVariable('client', 'name')."\n";
2604  if(strlen($desc = $ilClientIniFile->readVariable('client', 'description')))
2605  {
2606  $signature .= $desc."\n";
2607  }
2608 
2609  $signature .= ilUtil::_getHttpPath();
2610 
2611  $clientdirs = glob(ILIAS_WEB_DIR."/*", GLOB_ONLYDIR);
2612  if(is_array($clientdirs) && count($clientdirs) > 1)
2613  {
2614  $signature .= '/?client_id='.CLIENT_ID;
2615  }
2616 
2617  $signature .= "\n\n";
2618 
2619  return $signature;
2620  }
2621 
2626  public static function getSubjectPrefix()
2627  {
2628  global $ilSetting;
2629  static $prefix = null;
2630 
2631  return $prefix == null ? $ilSetting->get('mail_subject_prefix','') : $prefix;
2632  }
2633 
2639  public static function getSalutation($a_usr_id,$a_language = null)
2640  {
2641  global $lng;
2642 
2643  $lang = $a_language ? $a_language : $lng;
2644 
2645  $lang->loadLanguageModule('mail');
2646  $gender = ilObjUser::_lookupGender($a_usr_id);
2647  $gender = $gender ? $gender : 'n';
2648  $name = ilObjUser::_lookupName($a_usr_id);
2649 
2650  if(!strlen($name['firstname']))
2651  {
2652  return $lang->txt('mail_salutation_anonymous').',';
2653  }
2654 
2655  return $lang->txt('mail_salutation_'.$gender).' '.
2656  ($name['title'] ? $name['title'].' ' : '').
2657  ($name['firstname'] ? $name['firstname'].' ' : '').
2658  $name['lastname'].',';
2659  }
2660 
2661  private function setUsePear($bool)
2662  {
2663  $this->use_pear = $bool;
2664  }
2665 
2666  private function getUsePear()
2667  {
2668  return $this->use_pear;
2669  }
2670 
2671  // Force fallback for sending mails via ILIAS, if internal Pear-Validation returns PEAR_Error
2676  private function validatePear($a_recipients)
2677  {
2678  if(ilMail::_usePearMail())
2679  {
2680  $this->setUsePear(true);
2681  $tmp_names = $this->explodeRecipients($a_recipients, true);
2682  if(is_a($tmp_names, 'PEAR_Error'))
2683  {
2684  $this->setUsePear(false);
2685  }
2686  }
2687  else
2688  {
2689  $this->setUsePear(false);
2690  }
2691  }
2692 
2701  protected static function getCachedUserInstance($a_usr_id)
2702  {
2703  if(isset(self::$userInstances[$a_usr_id]))
2704  {
2705  return self::$userInstances[$a_usr_id];
2706  }
2707 
2708  self::$userInstances[$a_usr_id] = new ilObjUser($a_usr_id);
2709  return self::$userInstances[$a_usr_id];
2710  }
2711 } // END class.ilMail
2712 ?>