ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
213  const ILIAS_HOST = 'ilias';
214 
223  protected $properties = array();
224 
228  protected static $userInstances = array();
229 
236  public function __construct($a_user_id)
237  {
238  require_once "./Services/Mail/classes/class.ilFileDataMail.php";
239  require_once "Services/Mail/classes/class.ilMailOptions.php";
240 
241  global $ilias, $lng, $ilUser;
242 
243  $lng->loadLanguageModule("mail");
244 
245  // Initiate variables
246  $this->ilias =& $ilias;
247  $this->lng =& $lng;
248  $this->table_mail = 'mail';
249  $this->table_mail_saved = 'mail_saved';
250  $this->user_id = $a_user_id;
251  $this->mfile = new ilFileDataMail($this->user_id);
252  $this->mail_options = new ilMailOptions($a_user_id);
253 
254  // DEFAULT: sent mail aren't stored insentbox of user.
255  $this->setSaveInSentbox(false);
256 
257  // GET REFERENCE ID OF MAIL OBJECT
258  $this->readMailObjectReferenceId();
259  }
260 
268  public function __get($name)
269  {
270  global $ilUser;
271 
272  if(isset($this->properties[$name]))
273  {
274  return $this->properties[$name];
275  }
276 
277  // Used to include files / instantiate objects at runtime
278  if($name == 'mlists')
279  {
280  if(is_object($ilUser))
281  {
282  require_once 'Services/Contact/classes/class.ilMailingLists.php';
283  $this->properties[$name] = new ilMailingLists($ilUser);
284  return $this->properties[$name];
285  }
286  }
287  }
288 
289  public function doesRecipientStillExists($a_recipient, $a_existing_recipients)
290  {
291  if(self::_usePearMail())
292  {
293  $recipients = $this->explodeRecipients($a_existing_recipients);
294  if(is_a($recipients, 'PEAR_Error'))
295  {
296  return false;
297  }
298  else
299  {
300  foreach($recipients as $rcp)
301  {
302  if (substr($rcp->mailbox, 0, 1) != '#')
303  {
304  if(trim($rcp->mailbox) == trim($a_recipient) ||
305  trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
306  {
307  return true;
308  }
309  }
310  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
311  {
312  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
313  {
314  return true;
315  }
316  }
317  else
318  {
319  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
320  {
321  return true;
322  }
323  }
324  }
325  }
326  }
327  else
328  {
329  $recipients = $this->explodeRecipients($a_existing_recipients);
330  if(count($recipients))
331  {
332  foreach($recipients as $recipient)
333  {
334  if(trim($recipient) == trim($a_recipient))
335  {
336  return true;
337  }
338  }
339  }
340  }
341 
342  return false;
343  }
344 
352  function enableSOAP($a_status)
353  {
354  $this->soap_enabled = $a_status;
355  }
356  function isSOAPEnabled()
357  {
358  global $ilSetting;
359 
360  if(!extension_loaded('curl') || !$ilSetting->get('soap_user_administration'))
361  {
362  return false;
363  }
364 
365  // Prevent using SOAP in cron context
367  {
368  return false;
369  }
370 
371  return (bool) $this->soap_enabled;
372  }
373 
374 
375  function setSaveInSentbox($a_save_in_sentbox)
376  {
377  $this->save_in_sentbox = $a_save_in_sentbox;
378  }
379 
380  function getSaveInSentbox()
381  {
382  return $this->save_in_sentbox;
383  }
384 
390  function setMailSendType($a_types)
391  {
392  $this->mail_send_type = $a_types;
393  }
394 
400  function setMailRcpTo($a_rcp_to)
401  {
402  $this->mail_rcp_to = $a_rcp_to;
403  }
404 
410  function setMailRcpCc($a_rcp_cc)
411  {
412  $this->mail_rcp_cc = $a_rcp_cc;
413  }
414 
420  function setMailRcpBc($a_rcp_bc)
421  {
422  $this->mail_rcp_bc = $a_rcp_bc;
423  }
424 
430  function setMailSubject($a_subject)
431  {
432  $this->mail_subject = $a_subject;
433  }
434 
440  function setMailMessage($a_message)
441  {
442  $this->mail_message = $a_message;
443  }
444 
450  {
451  include_once 'Services/Mail/classes/class.ilMailGlobalServices.php';
452  $this->mail_obj_ref_id = ilMailGlobalServices::getMailObjectRefId();
453  }
454 
456  {
457  return $this->mail_obj_ref_id;
458  }
459 
470  public function formatNamesForOutput($users = '')
471  {
472  $users = trim($users);
473  if($users)
474  {
475  if(strstr($users, ','))
476  {
477  $rcp_to_array = array();
478 
479  $recipients = explode(',', $users);
480  foreach($recipients as $recipient)
481  {
482  $recipient = trim($recipient);
483  if($uid = ilObjUser::_lookupId($recipient))
484  {
485  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
486  {
487  $tmp_obj = self::getCachedUserInstance($uid);
488  $rcp_to_array[] = $tmp_obj->getFullname().' ['.$recipient.']';
489  }
490  else
491  {
492  $rcp_to_array[] = $recipient;
493  }
494  }
495  else
496  {
497  $rcp_to_array[] = $recipient;
498  }
499  }
500 
501  return trim(implode(', ', $rcp_to_array));
502  }
503  else
504  {
505  if($uid = ilObjUser::_lookupId($users))
506  {
507  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
508  {
509  $tmp_obj = self::getCachedUserInstance($uid);
510  return $tmp_obj->getFullname().' ['.$users.']';
511  }
512  else
513  {
514  return $users;
515  }
516  }
517  else
518  {
519  return $users;
520  }
521  }
522  }
523  else
524  {
525  return $this->lng->txt('not_available');
526  }
527  }
528 
529  function getPreviousMail($a_mail_id)
530  {
531  global $ilDB;
532 
533  $ilDB->setLimit(1);
534  $res = $ilDB->queryf("
535  SELECT b.* FROM " . $this->table_mail ." a
536  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
537  AND b.user_id = a.user_id AND b.send_time > a.send_time
538  WHERE a.user_id = %s
539  AND a.mail_id = %s ORDER BY b.send_time ASC",
540  array('integer', 'integer'),
541  array($this->user_id, $a_mail_id));
542 
543  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
544 
545  return $this->mail_data;
546  }
547 
548  function getNextMail($a_mail_id)
549  {
550  global $ilDB;
551 
552  $ilDB->setLimit(1);
553  $res = $ilDB->queryf("
554  SELECT b.* FROM " . $this->table_mail ." a
555  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
556  AND b.user_id = a.user_id AND b.send_time < a.send_time
557  WHERE a.user_id = %s
558  AND a.mail_id = %s ORDER BY b.send_time DESC",
559  array('integer', 'integer'),
560  array($this->user_id, $a_mail_id));
561 
562  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
563 
564  return $this->mail_data;
565  }
566 
574  function getMailsOfFolder($a_folder_id, $filter = array())
575  {
576  global $ilDB;
577 
578  $this->mail_counter = array();
579  $this->mail_counter['read'] = 0;
580  $this->mail_counter['unread'] = 0;
581 
582  $query = "SELECT sender_id, m_subject, mail_id, m_status, send_time FROM ". $this->table_mail ."
583  LEFT JOIN object_data ON obj_id = sender_id
584  WHERE user_id = %s
585  AND folder_id = %s
586  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)) ";
587 
588  if($filter['status'])
589  {
590  $query .= ' AND m_status = '.$ilDB->quote($filter['status'], 'text');
591  }
592  if($filter['type'])
593  {
594  $query .= ' AND '.$ilDB->like('m_type', 'text', '%%:"'.$filter['type'].'"%%', false);
595  }
596 
597  $query .= " ORDER BY send_time DESC";
598 
599  $res = $ilDB->queryf($query,
600  array('integer', 'integer'),
601  array($this->user_id, $a_folder_id));
602 
603  while ($row = $ilDB->fetchObject($res))
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  $res = $ilDB->queryf("
636  SELECT COUNT(*) FROM ". $this->table_mail ."
637  WHERE user_id = %s
638  AND folder_id = %s",
639  array('integer', 'integer'),
640  array($this->user_id, $a_folder_id));
641 
642  return $res->numRows();
643  }
644 
651  function deleteMailsOfFolder($a_folder_id)
652  {
653  if ($a_folder_id)
654  {
655  global $ilDB;
656 
657  /*$statement = $ilDB->manipulateF("
658  DELETE FROM ". $this->table_mail ."
659  WHERE user_id = %s
660  AND folder_id = %s",
661  array('integer', 'integer'),
662  array($this->user_id, $a_folder_id));*/
663  $mails = $this->getMailsOfFolder($a_folder_id);
664  foreach((array)$mails as $mail_data)
665  {
666  $this->deleteMails(array($mail_data['mail_id']));
667  }
668 
669  return true;
670  }
671 
672  return false;
673  }
674 
682  {
683  return is_array($this->mail_counter) ? $this->mail_counter : array(
684  "total" => 0,
685  "read" => 0,
686  "unread" => 0);
687  }
688 
695  function getMail($a_mail_id)
696  {
697  global $ilDB;
698 
699  $res = $ilDB->queryf("
700  SELECT * FROM ". $this->table_mail ."
701  WHERE user_id = %s
702  AND mail_id = %s",
703  array('integer', 'integer'),
704  array($this->user_id, $a_mail_id));
705 
706  $this->mail_data =$this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
707 
708  return $this->mail_data;
709  }
710 
717  function markRead($a_mail_ids)
718  {
719 
720  global $ilDB;
721 
722  $data = array();
723  $data_types = array();
724 
725  $query = "UPDATE ". $this->table_mail ."
726  SET m_status = %s
727  WHERE user_id = %s ";
728  array_push($data_types, 'text', 'integer');
729  array_push($data, 'read', $this->user_id);
730 
731  $cnt_mail_ids = count($a_mail_ids);
732 
733  if (is_array($a_mail_ids) &&
734  count($a_mail_ids) > 0)
735  {
736 
737  $in = 'mail_id IN (';
738  $counter = 0;
739  foreach($a_mail_ids as $a_mail_id)
740  {
741  array_push($data, $a_mail_id);
742  array_push($data_types, 'integer');
743 
744  if($counter > 0) $in .= ',';
745  $in .= '%s';
746  ++$counter;
747  }
748  $in .= ')';
749 
750  $query .= ' AND '.$in;
751  }
752 
753  $res = $ilDB->manipulateF($query, $data_types, $data);
754 
755  return true;
756  }
757 
764  function markUnread($a_mail_ids)
765  {
766  global $ilDB;
767 
768  $data = array();
769  $data_types = array();
770 
771  $query = "UPDATE ". $this->table_mail ."
772  SET m_status = %s
773  WHERE user_id = %s ";
774  array_push($data_types, 'text', 'integer');
775  array_push($data, 'unread', $this->user_id);
776 
777  $cnt_mail_ids = count($a_mail_ids);
778 
779  if (is_array($a_mail_ids) &&
780  count($a_mail_ids) > 0)
781  {
782 
783  $in = 'mail_id IN (';
784  $counter = 0;
785  foreach($a_mail_ids as $a_mail_id)
786  {
787  array_push($data, $a_mail_id);
788  array_push($data_types, 'integer');
789 
790  if($counter > 0) $in .= ',';
791  $in .= '%s';
792  ++$counter;
793  }
794  $in .= ')';
795 
796  $query .= ' AND '.$in;
797  }
798 
799  $statement = $ilDB->manipulateF($query, $data_types, $data);
800 
801  return true;
802  }
803 
811  function moveMailsToFolder($a_mail_ids,$a_folder_id)
812  {
813  global $ilDB;
814 
815  $data = array();
816  $data_types = array();
817 
818  $query = "UPDATE ". $this->table_mail ."
819  SET folder_id = %s
820  WHERE user_id = %s ";
821  array_push($data_types, 'text', 'integer');
822  array_push($data, $a_folder_id, $this->user_id);
823 
824  $cnt_mail_ids = count($a_mail_ids);
825 
826  if (is_array($a_mail_ids) &&
827  count($a_mail_ids) > 0)
828  {
829 
830  $in = 'mail_id IN (';
831  $counter = 0;
832  foreach($a_mail_ids as $a_mail_id)
833  {
834  array_push($data, $a_mail_id);
835  array_push($data_types, 'integer');
836 
837  if($counter > 0) $in .= ',';
838  $in .= '%s';
839  ++$counter;
840  }
841  $in .= ')';
842 
843  $query .= ' AND '.$in;
844  }
845 
846  $statement = $ilDB->manipulateF($query, $data_types, $data);
847 
848  return true;
849  }
850 
856  public function deleteMails(array $a_mail_ids)
857  {
858  global $ilDB;
859 
860  foreach($a_mail_ids as $id)
861  {
862  $ilDB->manipulateF("
863  DELETE FROM ". $this->table_mail ."
864  WHERE user_id = %s
865  AND mail_id = %s ",
866  array('integer', 'integer'),
867  array($this->user_id, $id)
868  );
869  $this->mfile->deassignAttachmentFromDirectory($id);
870  }
871 
872  return true;
873  }
874 
881  function fetchMailData($a_row)
882  {
883  if (!$a_row) return;
884 
885  return array(
886  "mail_id" => $a_row->mail_id,
887  "user_id" => $a_row->user_id,
888  "folder_id" => $a_row->folder_id,
889  "sender_id" => $a_row->sender_id,
890  "attachments" => unserialize(stripslashes($a_row->attachments)),
891  "send_time" => $a_row->send_time,
892  "rcp_to" => $a_row->rcp_to,
893  "rcp_cc" => $a_row->rcp_cc,
894  "rcp_bcc" => $a_row->rcp_bcc,
895  "m_status" => $a_row->m_status,
896  "m_type" => unserialize(stripslashes($a_row->m_type)),
897  "m_email" => $a_row->m_email,
898  "m_subject" => $a_row->m_subject,
899  "m_message" => $a_row->m_message,
900  "import_name" => $a_row->import_name,
901  "use_placeholders"=> $a_row->use_placeholders,
902  "tpl_ctx_id" => $a_row->tpl_ctx_id,
903  "tpl_ctx_params" => (array)(@json_decode($a_row->tpl_ctx_params, true))
904  );
905  }
906 
912  public function getNewDraftId($usrId, $folderId)
913  {
914  global $ilDB;
915 
916  $next_id = $ilDB->nextId($this->table_mail);
917  $ilDB->insert($this->table_mail, array(
918  'mail_id' => array('integer', $next_id),
919  'user_id' => array('integer', $usrId),
920  'folder_id' => array('integer', $folderId),
921  'sender_id' => array('integer', $usrId)
922  ));
923 
924  return $next_id;
925  }
926 
927  public function updateDraft(
928  $a_folder_id, $a_attachments, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc,
929  $a_m_type, $a_m_email, $a_m_subject, $a_m_message, $a_draft_id = 0,
930  $a_use_placeholders = 0, $a_tpl_context_id = null, $a_tpl_context_params = array()
931  )
932  {
933  global $ilDB;
934 
935  $ilDB->update($this->table_mail,
936  array(
937  'folder_id' => array('integer', $a_folder_id),
938  'attachments' => array('clob', serialize($a_attachments)),
939  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
940  'rcp_to' => array('clob', $a_rcp_to),
941  'rcp_cc' => array('clob', $a_rcp_cc),
942  'rcp_bcc' => array('clob', $a_rcp_bcc),
943  'm_status' => array('text', 'read'),
944  'm_type' => array('text', serialize($a_m_type)),
945  'm_email' => array('integer', $a_m_email),
946  'm_subject' => array('text', $a_m_subject),
947  'm_message' => array('clob', $a_m_message),
948  'use_placeholders' => array('integer', $a_use_placeholders),
949  'tpl_ctx_id' => array('text', $a_tpl_context_id),
950  'tpl_ctx_params' => array('blob', @json_encode((array)$a_tpl_context_params))
951  ),
952  array(
953  'mail_id' => array('integer', $a_draft_id)
954  )
955  );
956 
957  return $a_draft_id;
958  }
959 
980  function sendInternalMail($a_folder_id,
981  $a_sender_id,
982  $a_attachments,
983  $a_rcp_to,
984  $a_rcp_cc,
985  $a_rcp_bcc,
986  $a_status,
987  $a_m_type,
988  $a_m_email,
989  $a_m_subject,
990  $a_m_message,
991  $a_user_id = 0,
992  $a_use_placeholders = 0,
993  $a_tpl_context_id = null,
994  $a_tpl_context_params = array()
995  )
996  {
997  $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
998 
999  global $ilDB, $log;
1000 
1001  if ($a_use_placeholders)
1002  {
1003  $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
1004  }
1005 
1006 
1007  if(!$a_user_id) $a_user_id = '0';
1008  if(!$a_folder_id) $a_folder_id = '0';
1009  if(!$a_sender_id) $a_sender_id = NULL;
1010  if(!$a_attachments) $a_attachments = NULL;
1011  if(!$a_rcp_to) $a_rcp_to = NULL;
1012  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1013  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1014  if(!$a_status) $a_status = NULL;
1015  if(!$a_m_type) $a_m_type = NULL;
1016  if(!$a_m_email) $a_m_email = NULL;
1017  if(!$a_m_subject) $a_m_subject = NULL;
1018  if(!$a_m_message) $a_m_message = NULL;
1019 
1020 
1021  $next_id = $ilDB->nextId($this->table_mail);
1022  $ilDB->insert($this->table_mail, array(
1023  'mail_id' => array('integer', $next_id),
1024  'user_id' => array('integer', $a_user_id),
1025  'folder_id' => array('integer', $a_folder_id),
1026  'sender_id' => array('integer', $a_sender_id),
1027  'attachments' => array('clob', serialize($a_attachments)),
1028  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
1029  'rcp_to' => array('clob', $a_rcp_to),
1030  'rcp_cc' => array('clob', $a_rcp_cc),
1031  'rcp_bcc' => array('clob', $a_rcp_bcc),
1032  'm_status' => array('text', $a_status),
1033  'm_type' => array('text', serialize($a_m_type)),
1034  'm_email' => array('integer', $a_m_email),
1035  'm_subject' => array('text', $a_m_subject),
1036  'm_message' => array('clob', $a_m_message),
1037  'tpl_ctx_id' => array('text', $a_tpl_context_id),
1038  'tpl_ctx_params' => array('blob', @json_encode((array)$a_tpl_context_params))
1039  ));
1040 
1041  return $next_id; //$ilDB->getLastInsertId();
1042 
1043  }
1044 
1050  protected function replacePlaceholders($a_message, $a_user_id = 0)
1051  {
1052  try
1053  {
1054  include_once 'Services/Mail/classes/class.ilMailFormCall.php';
1055 
1057  {
1058  require_once 'Services/Mail/classes/class.ilMailTemplateService.php';
1060  }
1061  else
1062  {
1063  require_once 'Services/Mail/classes/class.ilMailTemplateGenericContext.php';
1064  $context = new ilMailTemplateGenericContext();
1065  }
1066 
1067  $user = $a_user_id > 0 ? self::getCachedUserInstance($a_user_id) : null;
1068  foreach($context->getPlaceholders() as $key => $ph_definition)
1069  {
1070  $result = $context->resolvePlaceholder($key, ilMailFormCall::getContextParameters(), $user);
1071  $a_message = str_replace('[' . $ph_definition['placeholder'] . ']', $result, $a_message);
1072  }
1073  }
1074  catch(Exception $e)
1075  {
1076  require_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
1077  ilLoggerFactory::getLogger('mail')->error(__METHOD__ . ' has been called with invalid context.');
1078  }
1079 
1080  return $a_message;
1081  }
1082 
1096  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)
1097  {
1098  global $log;
1099 
1100  include_once 'Services/Mail/classes/class.ilMailbox.php';
1101  include_once './Services/User/classes/class.ilObjUser.php';
1102 
1103  if (!ilMail::_usePearMail())
1104  {
1105  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1106  $a_rcp_to = $this->__substituteRecipients($a_rcp_to, 'resubstitute');
1107  $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc, 'resubstitute');
1108  $a_rcp_bcc = $this->__substituteRecipients($a_rcp_bcc, 'resubstitute');
1109  }
1110 
1111  $mbox = new ilMailbox();
1112 
1113  if (!$a_use_placeholders) # No Placeholders
1114  {
1115  $rcp_ids = $this->getUserIds(trim($a_rcp_to).','.trim($a_rcp_cc).','.trim($a_rcp_bcc));
1116 
1117  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1118  "Parsed TO/CC/BCC user ids from given recipients: %s", implode(', ', $rcp_ids)
1119  ));
1120 
1121  $as_email = array();
1122 
1123  foreach($rcp_ids as $id)
1124  {
1125  $tmp_mail_options = new ilMailOptions($id);
1126 
1127  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1128  $tmp_user = self::getCachedUserInstance($id);
1129  $user_is_active = $tmp_user->getActive();
1130  $user_can_read_internal_mails = !$tmp_user->hasToAcceptTermsOfService() && $tmp_user->checkTimeLimit();
1131 
1132  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1133  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1134  {
1135  continue;
1136  }
1137 
1138  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS/HER MAIL
1139  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1140 
1141  // Do not send external mails to inactive users!!!
1142  if($user_is_active)
1143  {
1144  if (!$user_can_read_internal_mails ||
1145  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1146  {
1147  $as_email[] = $tmp_user->getEmail();
1148  continue;
1149  }
1150 
1151  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1152  {
1153  $as_email[] = $tmp_user->getEmail();
1154  }
1155  }
1156  $mbox->setUserId($id);
1157  $inbox_id = $mbox->getInboxFolder();
1158 
1159  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1160  $a_attachments, $a_rcp_to,
1161  $a_rcp_cc, '', 'unread', $a_type,
1162  0, $a_subject, $a_message, $id, 0);
1163  if ($a_attachments)
1164  {
1165  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1166  }
1167  }
1168 
1169  // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
1170  $to = array();
1171  $bcc = array();
1172 
1173  if(count($as_email) == 1)
1174  {
1175  $to[] = $as_email[0];
1176  }
1177  else
1178  {
1179  foreach ($as_email as $email)
1180  {
1181  $bcc[] = $email;
1182  }
1183  }
1184 
1185  if(count($to) > 0 || count($bcc) > 0)
1186  {
1187  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
1188  }
1189  }
1190  else # Use Placeholders
1191  {
1192  // to
1193  $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
1194 
1195  // cc / bcc
1196  $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
1197 
1198  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1199  "Parsed TO user ids from given recipients for serial letter notification: %s", implode(', ', $rcp_ids_replace)
1200  ));
1201  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1202  "Parsed CC/BCC user ids from given recipients for serial letter notification: %s", implode(', ', $rcp_ids_no_replace)
1203  ));
1204 
1205  $as_email = array();
1206 
1207  // to
1208  foreach($rcp_ids_replace as $id)
1209  {
1210  $tmp_mail_options = new ilMailOptions($id);
1211 
1212  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1213  $tmp_user = self::getCachedUserInstance($id);
1214  $user_is_active = $tmp_user->getActive();
1215  $user_can_read_internal_mails = !$tmp_user->hasToAcceptTermsOfService() && $tmp_user->checkTimeLimit();
1216 
1217  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1218  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1219  {
1220  continue;
1221  }
1222 
1223  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1224  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1225 
1226  // Do not send external mails to inactive users!!!
1227  if($user_is_active)
1228  {
1229  if (!$user_can_read_internal_mails ||
1230  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1231  {
1232  $as_email[$tmp_user->getId()] = $tmp_user->getEmail();
1233  continue;
1234  }
1235 
1236  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1237  {
1238  $as_email[$tmp_user->getId()] = $tmp_user->getEmail();
1239  }
1240  }
1241  $mbox->setUserId($id);
1242  $inbox_id = $mbox->getInboxFolder();
1243 
1244  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1245  $a_attachments, $a_rcp_to,
1246  $a_rcp_cc, '', 'unread', $a_type,
1247  0, $a_subject, $a_message, $id, 1);
1248  if ($a_attachments)
1249  {
1250  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1251  }
1252  }
1253 
1254  if (count($as_email))
1255  {
1256  foreach ($as_email as $id => $email)
1257  {
1258  $this->sendMimeMail($email, '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
1259  }
1260  }
1261 
1262  $as_email = array();
1263 
1264  // cc / bcc
1265  foreach($rcp_ids_no_replace as $id)
1266  {
1267  $tmp_mail_options = new ilMailOptions($id);
1268 
1269  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1270  $tmp_user = self::getCachedUserInstance($id);
1271  $user_is_active = $tmp_user->getActive();
1272  $user_can_read_internal_mails = !$tmp_user->hasToAcceptTermsOfService() && $tmp_user->checkTimeLimit();
1273 
1274  // Do not send external mails to inactive users!!!
1275  if($user_is_active)
1276  {
1277  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1278  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1279  {
1280  continue;
1281  }
1282 
1283  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1284  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1285  if (!$user_can_read_internal_mails ||
1286  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1287  {
1288  $as_email[] = $tmp_user->getEmail();
1289  continue;
1290  }
1291 
1292  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1293  {
1294  $as_email[] = $tmp_user->getEmail();
1295  }
1296  }
1297  $mbox->setUserId($id);
1298  $inbox_id = $mbox->getInboxFolder();
1299 
1300  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1301  $a_attachments, $a_rcp_to,
1302  $a_rcp_cc, '', 'unread', $a_type,
1303  0, $a_subject, $a_message, $id, 0);
1304  if ($a_attachments)
1305  {
1306  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1307  }
1308  }
1309 
1310  if (count($as_email))
1311  {
1312  $this->sendMimeMail('', '', implode(',', $as_email), $a_subject, $a_message, $a_attachments);
1313  }
1314  }
1315 
1316  return true;
1317  }
1318 
1324  function getUserIds($a_recipients)
1325  {
1326  global $log, $rbacreview;
1327  $ids = array();
1328 
1329  $this->validatePear($a_recipients);
1330  if (ilMail::_usePearMail() && $this->getUsePear() == true)
1331  {
1332  $tmp_names = $this->explodeRecipients($a_recipients );
1333  if (! is_a($tmp_names, 'PEAR_Error'))
1334  {
1335  for ($i = 0;$i < count($tmp_names); $i++)
1336  {
1337  if (substr($tmp_names[$i]->mailbox,0,1) == '#' || substr($tmp_names[$i]->mailbox,0,2) == '"#')
1338  {
1339  $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1340  $foundUserIds = array();
1341  foreach($role_ids as $role_id)
1342  {
1343  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1344  {
1345  $ids[] = $usr_id;
1346  $foundUserIds[] = $usr_id;
1347  }
1348  }
1349 
1350  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1351  "Found the following user ids by role assignments for address '%s': %s", $tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host, implode(', ', $foundUserIds)
1352  ));
1353  }
1354  else if (strtolower($tmp_names[$i]->host) == self::ILIAS_HOST)
1355  {
1356  if($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
1357  {
1358  $ids[] = $id;
1359  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1360  "Found the following user ids for address '%s': %s", addslashes($tmp_names[$i]->mailbox), $id
1361  ));
1362  }
1363  else if(strlen($tmp_names[$i]->mailbox) > 0)
1364  {
1365  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1366  "Did not find any user account for address '%s'", addslashes($tmp_names[$i]->mailbox)
1367  ));
1368  }
1369  }
1370  else
1371  {
1372  // Fixed mantis bug #5875
1373  if($id = ilObjUser::_lookupId($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host))
1374  {
1375  $ids[] = $id;
1376  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1377  "Found the following user ids for address '%s': %s", $tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host, $id
1378  ));
1379  }
1380  else
1381  {
1382  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1383  "Did not find any user account for address '%s'", $tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host
1384  ));
1385  }
1386  }
1387  }
1388  }
1389  }
1390  else
1391  {
1392  $tmp_names = $this->explodeRecipients($a_recipients, $this->getUsePear());
1393  for ($i = 0;$i < count($tmp_names); $i++)
1394  {
1395  if (substr($tmp_names[$i],0,1) == '#' || substr($tmp_names[$i], 0, 2) == '"#')
1396  {
1397  if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
1398  {
1399  include_once("./Services/Object/classes/class.ilObjectFactory.php");
1400  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1401 
1402  foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1403  {
1404  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1405  break;
1406  }
1407  $foundUserIds = array();
1408  // STORE MEMBER IDS IN $ids
1409  foreach ($grp_object->getGroupMemberIds() as $id)
1410  {
1411  $ids[] = $id;
1412  $foundUserIds[] = $id;
1413  }
1414 
1415  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1416  "Found the following user ids by group member assignment for address '%s': %s", addslashes(substr($tmp_names[$i],1)), implode(', ', $foundUserIds)
1417  ));
1418  }
1419  // is role: get role ids
1420  else
1421  {
1422  $possible_role_id = addslashes(substr($tmp_names[$i], 1));
1423  $role_id = $rbacreview->roleExists($possible_role_id);
1424  if($role_id)
1425  {
1426  $foundUserIds = array();
1427  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1428  {
1429  $ids[] = $usr_id;
1430  $foundUserIds[] = $usr_id;
1431  }
1432 
1433  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1434  "Found the following user ids by role assignments for address '%s': %s", addslashes(substr($tmp_names[$i], 1)), implode(', ', $foundUserIds)
1435  ));
1436  continue;
1437  }
1438 
1439  if(substr($possible_role_id, 0, 8) == 'il_role_')
1440  {
1441  $role_id = substr($possible_role_id, 8);
1442  $roles_object_id = $rbacreview->getObjectOfRole($role_id);
1443  if($roles_object_id > 0)
1444  {
1445  $foundUserIds = array();
1446  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1447  {
1448  $ids[] = $usr_id;
1449  $foundUserIds[] = $usr_id;
1450  }
1451  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1452  "Found the following user ids by role assignments for address '%s': %s", $possible_role_id, implode(', ', $foundUserIds)
1453  ));
1454  }
1455  continue;
1456  }
1457 
1458  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1459  "Found no user ids for address '%s'", $possible_role_id
1460  ));
1461  }
1462  }
1463  else if (!empty($tmp_names[$i]))
1464  {
1465  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
1466  {
1467  $ids[] = $id;
1468  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1469  "Found the following user ids for address '%s': %s", addslashes($tmp_names[$i]), $id
1470  ));
1471  }
1472  else if(strlen(addslashes($tmp_names[$i])) > 0)
1473  {
1474  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1475  "Did not find any user account for address '%s'", addslashes($tmp_names[$i])
1476  ));
1477  }
1478  }
1479  }
1480  }
1481  return array_unique($ids);
1482  }
1483 
1494  function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
1495  {
1496  $error_message = '';
1497 
1498  $a_m_subject = trim($a_m_subject);
1499  $a_rcp_to = trim($a_rcp_to);
1500 
1501  if (empty($a_m_subject))
1502  {
1503  $error_message .= $error_message ? "<br>" : '';
1504  $error_message .= $this->lng->txt("mail_add_subject");
1505  }
1506 
1507  if (empty($a_rcp_to))
1508  {
1509  $error_message .= $error_message ? "<br>" : '';
1510  $error_message .= $this->lng->txt("mail_add_recipient");
1511  }
1512 
1513  return $error_message;
1514  }
1515 
1522  function getEmailsOfRecipients($a_rcp)
1523  {
1524  global $rbacreview;
1525 
1526  $addresses = array();
1527 
1528  $this->validatePear($a_rcp);
1529  if (ilMail::_usePearMail() && $this->getUsePear())
1530  {
1531  $tmp_rcp = $this->explodeRecipients($a_rcp);
1532  if (! is_a($tmp_rcp, 'PEAR_Error'))
1533  {
1534  foreach ($tmp_rcp as $rcp)
1535  {
1536  // NO GROUP
1537  if (substr($rcp->mailbox,0,1) != '#' && substr($rcp->mailbox,0, 2) != '"#')
1538  {
1539  if (strtolower($rcp->host) != self::ILIAS_HOST)
1540  {
1541  $addresses[] = $rcp->mailbox.'@'.$rcp->host;
1542  continue;
1543  }
1544 
1545  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
1546  {
1547  $tmp_user = self::getCachedUserInstance($id);
1548  $addresses[] = $tmp_user->getEmail();
1549  continue;
1550  }
1551  }
1552  else
1553  {
1554  // Roles
1555  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1556  foreach($role_ids as $role_id)
1557  {
1558  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1559  {
1560  $tmp_user = self::getCachedUserInstance($usr_id);
1561  $addresses[] = $tmp_user->getEmail();
1562  }
1563  }
1564  }
1565  }
1566  }
1567  }
1568  else
1569  {
1570  $tmp_rcp = $this->explodeRecipients($a_rcp, $this->getUsePear());
1571 
1572  foreach ($tmp_rcp as $rcp)
1573  {
1574  // NO GROUP
1575  if (substr($rcp,0,1) != '#' && substr($rcp,0,2) != '"#')
1576  {
1577  if (strpos($rcp,'@'))
1578  {
1579  $addresses[] = $rcp;
1580  continue;
1581  }
1582 
1583  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1584  {
1585  $tmp_user = self::getCachedUserInstance($id);
1586  $addresses[] = $tmp_user->getEmail();
1587  continue;
1588  }
1589  }
1590  else
1591  {
1592  // GROUP THINGS
1593  include_once("./Services/Object/classes/class.ilObjectFactory.php");
1594  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1595 
1596  // Fix
1597  foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($rcp,1)))) as $ref_id)
1598  {
1599  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1600  break;
1601  }
1602  // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
1603  foreach ($grp_object->getGroupMemberIds() as $id)
1604  {
1605  $tmp_user = self::getCachedUserInstance($id);
1606  $addresses[] = $tmp_user->getEmail();
1607  }
1608  }
1609  }
1610  }
1611 
1612  return $addresses;
1613  }
1614 
1622  function checkRecipients($a_recipients)
1623  {
1624  global $rbacsystem,$rbacreview;
1625  $wrong_rcps = '';
1626 
1627  $this->validatePear($a_recipients);
1628  if (ilMail::_usePearMail() && $this->getUsePear())
1629  {
1630  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1631 
1632  if (is_a($tmp_rcp, 'PEAR_Error'))
1633  {
1634  $colon_pos = strpos($tmp_rcp->message, ':');
1635  $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
1636  }
1637  else
1638  {
1639  foreach ($tmp_rcp as $rcp)
1640  {
1641  // NO ROLE MAIL ADDRESS
1642  if (substr($rcp->mailbox,0,1) != '#' && substr($rcp->mailbox,0,2) != '"#')
1643  {
1644  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1645  $user_id = ($rcp->host == self::ILIAS_HOST) ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
1646  if ($user_id == false && $rcp->host == self::ILIAS_HOST)
1647  {
1648  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
1649  continue;
1650  }
1651 
1652  // CHECK IF USER CAN RECEIVE MAIL
1653  if ($user_id)
1654  {
1655  if(!$rbacsystem->checkAccessOfUser($user_id, "internal_mail", $this->getMailObjectReferenceId()))
1656  {
1657  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1658  " (".$this->lng->txt("user_cant_receive_mail").")";
1659  continue;
1660  }
1661  }
1662  }
1663  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
1664  {
1665  if (!$this->mlists->mailingListExists($rcp->mailbox))
1666  {
1667  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1668  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1669  }
1670 
1671  continue;
1672  }
1673  else
1674  {
1675 
1676  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1677 
1678  if(!$this->mail_to_global_roles && is_array($role_ids))
1679  {
1680  foreach($role_ids as $role_id)
1681  {
1682  if($rbacreview->isGlobalRole($role_id))
1683  {
1684  include_once('Services/Mail/exceptions/class.ilMailException.php');
1685  throw new ilMailException('mail_to_global_roles_not_allowed');
1686 
1687  }
1688  }
1689  }
1690  if (count($role_ids) == 0)
1691  {
1692  $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
1693  ' ('.$this->lng->txt('mail_no_recipient_found').')';
1694  continue;
1695  }
1696  else if (count($role_ids) > 1)
1697  {
1698  $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
1699  ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
1700  }
1701  }
1702  }
1703  }
1704  }
1705  else // NO PEAR
1706  {
1707  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1708 
1709  foreach ($tmp_rcp as $rcp)
1710  {
1711  if (empty($rcp))
1712  {
1713  continue;
1714  }
1715  // NO GROUP
1716  if (substr($rcp,0,1) != '#' && substr($rcp,0,2) != '"#')
1717  {
1718  $usr_id = ilObjUser::getUserIdByLogin(addslashes($rcp));
1719  if(!$usr_id && !ilUtil::is_email($rcp))
1720  {
1721  $wrong_rcps .= "<br />".htmlentities($rcp);
1722  continue;
1723  }
1724 
1725  // CHECK IF USER CAN RECEIVE MAIL
1726  if($usr_id)
1727  {
1728  if(!$rbacsystem->checkAccessOfUser($usr_id, "internal_mail", $this->getMailObjectReferenceId()))
1729  {
1730  $wrong_rcps .= "<br />".htmlentities($rcp).
1731  " (".$this->lng->txt("user_cant_receive_mail").")";
1732  continue;
1733  }
1734  }
1735  }
1736  else if(substr($rcp, 0, 7) == '#il_ml_')
1737  {
1738  if (!$this->mlists->mailingListExists($rcp))
1739  {
1740  $wrong_rcps .= "<br />".htmlentities($rcp).
1741  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1742  }
1743 
1744  continue;
1745  }
1746  else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
1747  {
1748  continue;
1749  }
1750  else
1751  {
1752  $role_id = $rbacreview->roleExists(addslashes(substr($rcp, 1)));
1753  if($role_id)
1754  {
1755  if(!$this->mail_to_global_roles && $rbacreview->isGlobalRole($role_id))
1756  {
1757  include_once('Services/Mail/exceptions/class.ilMailException.php');
1758  throw new ilMailException('mail_to_global_roles_not_allowed');
1759  }
1760  continue;
1761  }
1762 
1763  if(substr(addslashes($rcp), 1, 8) == 'il_role_')
1764  {
1765  $role_id = substr(addslashes($rcp), 9);
1766  $roles_object_id = $rbacreview->getObjectOfRole($role_id);
1767  if($roles_object_id > 0)
1768  {
1769  if(!$this->mail_to_global_roles && $rbacreview->isGlobalRole($role_id))
1770  {
1771  include_once('Services/Mail/exceptions/class.ilMailException.php');
1772  throw new ilMailException('mail_to_global_roles_not_allowed');
1773  }
1774  }
1775  continue;
1776  }
1777 
1778  $wrong_rcps .= "<br />" . htmlentities($rcp) . " (" . $this->lng->txt("mail_no_valid_group_role") . ")";
1779  continue;
1780  }
1781  }
1782  }
1783  return $wrong_rcps;
1784  }
1785 
1803  function savePostData($a_user_id,
1804  $a_attachments,
1805  $a_rcp_to,
1806  $a_rcp_cc,
1807  $a_rcp_bcc,
1808  $a_m_type,
1809  $a_m_email,
1810  $a_m_subject,
1811  $a_m_message,
1812  $a_use_placeholders,
1813  $a_tpl_context_id = null,
1814  $a_tpl_ctx_params = array()
1815  )
1816  {
1817  global $ilDB;
1818 
1819 
1820  if(!$a_attachments) $a_attachments = NULL;
1821  if(!$a_rcp_to) $a_rcp_to = NULL;
1822  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1823  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1824  if(!$a_m_type) $a_m_type = NULL;
1825  if(!$a_m_email) $a_m_email = NULL;
1826  if(!$a_m_message) $a_m_message = NULL;
1827  if(!$a_use_placeholders) $a_use_placeholders = '0';
1828 
1829 
1830  $ilDB->replace(
1831  $this->table_mail_saved,
1832  array(
1833  'user_id' => array('integer', $this->user_id)
1834  ),
1835  array(
1836  'attachments' => array('clob', serialize($a_attachments)),
1837  'rcp_to' => array('clob', $a_rcp_to),
1838  'rcp_cc' => array('clob', $a_rcp_cc),
1839  'rcp_bcc' => array('clob', $a_rcp_bcc),
1840  'm_type' => array('text', serialize($a_m_type)),
1841  'm_email' => array('integer', $a_m_email),
1842  'm_subject' => array('text', $a_m_subject),
1843  'm_message' => array('clob', $a_m_message),
1844  'use_placeholders' => array('integer', $a_use_placeholders),
1845  'tpl_ctx_id' => array('text', $a_tpl_context_id),
1846  'tpl_ctx_params' => array('blob', json_encode((array)$a_tpl_ctx_params))
1847  )
1848  );
1849 
1850  $this->getSavedData();
1851 
1852  return true;
1853  }
1854 
1860  function getSavedData()
1861  {
1862  global $ilDB;
1863 
1864  $res = $ilDB->queryf('
1865  SELECT * FROM '. $this->table_mail_saved .'
1866  WHERE user_id = %s',
1867  array('integer'),
1868  array($this->user_id));
1869 
1870  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
1871 
1872  return $this->mail_data;
1873  }
1874 
1881  public function validateRecipients($a_rcp_to, $a_rcp_cc, $a_rcp_bc)
1882  {
1883  try
1884  {
1885  $message = '';
1886 
1887  if($error_message = $this->checkRecipients($a_rcp_to))
1888  {
1889  $message .= $error_message;
1890  }
1891  if($error_message = $this->checkRecipients($a_rcp_cc))
1892  {
1893  $message .= $error_message;
1894  }
1895  if($error_message = $this->checkRecipients($a_rcp_bc))
1896  {
1897  $message .= $error_message;
1898  }
1899 
1900  if(strlen($message) > 0)
1901  {
1902  return $this->lng->txt('mail_following_rcp_not_valid') . $message;
1903  }
1904 
1905  return '';
1906  }
1907  catch(ilMailException $e)
1908  {
1909  return $this->lng->txt('mail_following_rcp_not_valid') . $this->lng->txt($e->getMessage());
1910  }
1911  }
1912 
1926  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)
1927  {
1928  global $lng,$rbacsystem;
1929 
1930  ilLoggerFactory::getLogger('mail')->debug(
1931  "New mail system task:" .
1932  " To: " . $a_rcp_to .
1933  " | CC: " . $a_rcp_cc .
1934  " | BCC: " . $a_rcp_bc .
1935  " | Subject: " . $a_m_subject
1936  );
1937 
1938  $this->mail_to_global_roles = true;
1939  if($this->user_id != ANONYMOUS_USER_ID)
1940  {
1941  $this->mail_to_global_roles = $rbacsystem->checkAccessOfUser($this->user_id, 'mail_to_global_roles', $this->mail_obj_ref_id);
1942  }
1943 
1944  if (in_array("system",$a_type))
1945  {
1946  $a_type = array('system');
1947  }
1948 
1949  if ($a_attachment)
1950  {
1951  if (!$this->mfile->checkFilesExist($a_attachment))
1952  {
1953  return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
1954  }
1955  }
1956  // CHECK NECESSARY MAIL DATA FOR ALL TYPES
1957  if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
1958  {
1959  return $error_message;
1960  }
1961 
1962  $error_message = $this->validateRecipients($a_rcp_to, $a_rcp_cc, $a_rcp_bc);
1963  if(strlen($error_message) > 0)
1964  {
1965  return $error_message;
1966  }
1967 
1968  // ACTIONS FOR ALL TYPES
1969 
1970 
1971  // GET RCPT OF MAILING LISTS
1972 
1973  $rcp_to_list = $this->parseRcptOfMailingLists($a_rcp_to, true);
1974  $rcp_cc_list = $this->parseRcptOfMailingLists($a_rcp_cc, true);
1975  $rcp_bc_list = $this->parseRcptOfMailingLists($a_rcp_bc, true);
1976 
1977  $rcp_to = $rcp_cc = $rcp_bc = array();
1978  foreach($rcp_to_list as $mlist_id => $mlist_rec)
1979  {
1980  if($mlist_id)
1981  {
1982  // internal mailing lists are sent as bcc
1983  $mlist_id = substr($mlist_id, 7);
1984  if($this->mlists->get($mlist_id)->getMode() == ilMailingList::MODE_TEMPORARY)
1985  {
1986  $rcp_bc = array_merge($rcp_bc, $mlist_rec);
1987  continue;
1988  }
1989  }
1990 
1991  $rcp_to = array_merge($rcp_to, $mlist_rec);
1992  }
1993  foreach($rcp_cc_list as $mlist_id => $mlist_rec)
1994  {
1995  if($mlist_id)
1996  {
1997  // internal mailing lists are sent as bcc
1998  $mlist_id = substr($mlist_id, 7);
1999  if($this->mlists->get($mlist_id)->getMode() == ilMailingList::MODE_TEMPORARY)
2000  {
2001  $rcp_bc = array_merge($rcp_bc, $mlist_rec);
2002  continue;
2003  }
2004  }
2005 
2006  $rcp_cc = array_merge($rcp_cc, $mlist_rec);
2007  }
2008  foreach($rcp_bc_list as $mlist_id => $mlist_rec)
2009  {
2010  $rcp_bc = array_merge($rcp_bc, $mlist_rec);
2011  }
2012 
2013  $rcp_to = implode(',', $rcp_to);
2014  $rcp_cc = implode(',', $rcp_cc);
2015  $rcp_bc = implode(',', $rcp_bc);
2016 
2017 
2018  if (! ilMail::_usePearMail() )
2019  {
2020  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
2021  $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
2022  $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
2023  $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
2024  }
2025 
2026  // COUNT EMAILS
2027  $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
2028  $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
2029 
2030  // currently disabled..
2031  /*
2032  if (count($c_emails))
2033  {
2034  if (!$this->getEmailOfSender())
2035  {
2036  return $lng->txt("mail_check_your_email_addr");
2037  }
2038  }
2039  */
2040 
2041  // check smtp permission
2042  if($c_emails && $this->user_id != ANONYMOUS_USER_ID &&
2043  !$rbacsystem->checkAccessOfUser($this->user_id, 'smtp_mail', $this->mail_obj_ref_id))
2044  {
2045  return $this->lng->txt('mail_no_permissions_write_smtp');
2046  }
2047 
2048  if($this->appendInstallationSignature())
2049  {
2050  $a_m_message .= self::_getInstallationSignature();
2051  }
2052 
2053  // save mail in sent box
2054  $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
2055  $a_m_subject,$a_m_message);
2056 
2057  if($a_attachment)
2058  {
2059  $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
2060 
2061  if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
2062  {
2063  return $error;
2064  }
2065  }
2066 
2067  // FILTER EMAILS
2068  // IF EMAIL RECIPIENT
2069  if($c_emails)
2070  {
2071  $externalMailRecipientsTo = $this->__getEmailRecipients($rcp_to);
2072  $externalMailRecipientsCc = $this->__getEmailRecipients($rcp_cc);
2073  $externalMailRecipientsBcc = $this->__getEmailRecipients($rcp_bc);
2074 
2075  ilLoggerFactory::getLogger('mail')->debug(
2076  "Parsed external mail addresses from given recipients:" .
2077  " To: " . $externalMailRecipientsTo .
2078  " | CC: " . $externalMailRecipientsCc .
2079  " | BCC: " . $externalMailRecipientsBcc .
2080  " | Subject: " . $a_m_subject
2081  );
2082 
2083  $this->sendMimeMail(
2084  $externalMailRecipientsTo,
2085  $externalMailRecipientsCc,
2086  $externalMailRecipientsBcc,
2087  $a_m_subject,
2088  $a_use_placeholders ? $this->replacePlaceholders($a_m_message) : $a_m_message,
2089  $a_attachment,
2090  0
2091  );
2092  }
2093  else
2094  {
2095  ilLoggerFactory::getLogger('mail')->debug("No external mail addresses given in recipient string");
2096  }
2097 
2098  if (in_array('system',$a_type))
2099  {
2100  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))
2101  {
2102  return $lng->txt("mail_send_error");
2103  }
2104  }
2105  // ACTIONS FOR TYPE SYSTEM AND NORMAL
2106  if (in_array('normal',$a_type))
2107  {
2108  // TRY BOTH internal and email (depends on user settings)
2109  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))
2110  {
2111  return $lng->txt("mail_send_error");
2112  }
2113  }
2114 
2115  // Temporary bugfix
2116  if (!$this->getSaveInSentbox())
2117  {
2118  $this->deleteMails(array($sent_id));
2119  }
2120 
2121  return '';
2122  }
2123 
2124  function parseRcptOfMailingLists($rcpt = '', $maintain_lists = false)
2125  {
2126  if ($rcpt == '')
2127  {
2128  if(!$maintain_lists)
2129  {
2130  return $rcpt;
2131  }
2132  else
2133  {
2134  return array();
2135  }
2136  }
2137 //@todo check rcp pear validation
2138  $arrRcpt = $this->explodeRecipients(trim($rcpt));
2139  if (!is_array($arrRcpt) || empty($arrRcpt))
2140  {
2141  if(!$maintain_lists)
2142  {
2143  return $rcpt;
2144  }
2145  else
2146  {
2147  return array();
2148  }
2149  }
2150 
2151  $new_rcpt = array();
2152 
2153  foreach ($arrRcpt as $item)
2154  {
2155  if (ilMail::_usePearMail())
2156  {
2157  if (substr($item->mailbox, 0, 7) == '#il_ml_')
2158  {
2159  if ($this->mlists->mailingListExists($item->mailbox))
2160  {
2161  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
2162  {
2163  $login = ilObjUser::_lookupLogin($entry['usr_id']);
2164  if(!$maintain_lists)
2165  {
2166  $new_rcpt[] = $login;
2167  }
2168  else
2169  {
2170  $new_rcpt[$item->mailbox][] = $login;
2171  }
2172  }
2173  }
2174  }
2175  else
2176  {
2177  $tmp_rcpt = '';
2178  if($item->host == self::ILIAS_HOST)
2179  {
2180  $tmp_rcpt = $item->mailbox;
2181  }
2182  else
2183  {
2184  $tmp_rcpt = $item->mailbox.'@'.$item->host;
2185  }
2186 
2187  if(!$maintain_lists)
2188  {
2189  $new_rcpt[] = $tmp_rcpt;
2190  }
2191  else
2192  {
2193  $new_rcpt[0][] = $tmp_rcpt;
2194  }
2195  }
2196  }
2197  else
2198  {
2199  if (substr($item, 0, 7) == '#il_ml_')
2200  {
2201  if ($this->mlists->mailingListExists($item))
2202  {
2203  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
2204  {
2205  $login = ilObjUser::_lookupLogin($entry['usr_id']);
2206  if(!$maintain_lists)
2207  {
2208  $new_rcpt[] = $login;
2209  }
2210  else
2211  {
2212  $new_rcpt[$item][] = $login;
2213  }
2214  }
2215  }
2216  }
2217  else
2218  {
2219  if(!$maintain_lists)
2220  {
2221  $new_rcpt[] = $item;
2222  }
2223  else
2224  {
2225  $new_rcpt[0][] = $item;
2226  }
2227  }
2228  }
2229  }
2230 
2231  if(!$maintain_lists)
2232  {
2233  return implode(',', $new_rcpt);
2234  }
2235  else
2236  {
2237  return $new_rcpt;
2238  }
2239  }
2240 
2253  function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
2254  $a_m_subject,$a_m_message)
2255  {
2256  include_once "Services/Mail/classes/class.ilMailbox.php";
2257 
2258  $mbox = new ilMailbox($this->user_id);
2259  $sent_id = $mbox->getSentFolder();
2260 
2262  return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
2263  $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
2264  }
2265 
2272  public static function addFullname($a_email, $a_fullname)
2273  {
2274  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2275  return ilMimeMail::_mimeEncode($a_fullname).' <'.$a_email.'>';
2276  }
2277 
2284  public function getMimeMailSender()
2285  {
2289  global $ilUser;
2290 
2291  include_once "Services/Mail/classes/class.ilMimeMail.php";
2292 
2293  if($this->user_id && $this->user_id != ANONYMOUS_USER_ID)
2294  {
2295  $email = $ilUser->getEmail();
2296  $fullname = $ilUser->getFullname();
2297  if($ilUser->getId() != $this->user_id)
2298  {
2299  $user = self::getCachedUserInstance($this->user_id);
2300  $email = $user->getEmail();
2301  $fullname = $user->getFullname();
2302  }
2303 
2304  $sender = array($email, $fullname);
2305  }
2306  else
2307  {
2308  $sender = self::getIliasMailerAddress();
2309  }
2310 
2311  return $sender;
2312  }
2313 
2323  public static function getIliasMailerAddress()
2324  {
2325  global $ilSetting;
2326 
2327  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2328 
2329  $no_reply_adress = trim($ilSetting->get('mail_external_sender_noreply'));
2330  if(strlen($no_reply_adress))
2331  {
2332  if(strpos($no_reply_adress, '@') === false)
2333  $no_reply_adress = 'noreply@'.$no_reply_adress;
2334 
2335  if(!ilUtil::is_email($no_reply_adress))
2336  {
2337  $no_reply_adress = 'noreply@'.$_SERVER['SERVER_NAME'];
2338  }
2339 
2340  $sender = array($no_reply_adress, self::_getIliasMailerName());
2341  }
2342  else
2343  {
2344  $sender = array('noreply@'.$_SERVER['SERVER_NAME'], self::_getIliasMailerName());
2345  }
2346 
2347  return $sender;
2348  }
2349 
2363  function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,$a_no_soap = false)
2364  {
2365  include_once "Services/Mail/classes/class.ilMimeMail.php";
2366 
2367  #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
2368 
2369  #$inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 4";
2370  #$a_m_subject = "[".$inst_name."] ".$a_m_subject;
2371 
2372  $a_m_subject = self::getSubjectPrefix().' '.$a_m_subject;
2373 
2374  $sender = $this->getMimeMailSender();
2375 
2376  // #10854
2377  if($this->isSOAPEnabled() && !$a_no_soap)
2378  {
2379  // Send per soap
2380  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
2381 
2382  $soap_client = new ilSoapClient();
2383  $soap_client->setResponseTimeout(5);
2384  $soap_client->enableWSDL(true);
2385  $soap_client->init();
2386 
2387  $attachments = array();
2388  $a_attachments = $a_attachments ? $a_attachments : array();
2389  foreach($a_attachments as $attachment)
2390  {
2391  $attachments[] = $this->mfile->getAbsolutePath($attachment);
2392  }
2393  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
2394  $attachments = implode('#:#',$attachments);
2395  // mjansen: use "#:#" as leading delimiter
2396  if(strlen($attachments))
2397  $attachments = "#:#".$attachments;
2398 
2399  $soap_client->call('sendMail',array(session_id().'::'.$_COOKIE['ilClientId'], // session id
2400  $a_rcp_to,
2401  $a_rcp_cc,
2402  $a_rcp_bcc,
2403  is_array($sender) ? implode('#:#', $sender) : $sender,
2404  $a_m_subject,
2405  $a_m_message,
2406  $attachments));
2407 
2408  return true;
2409  }
2410  else
2411  {
2412  // send direct
2413  include_once "Services/Mail/classes/class.ilMimeMail.php";
2414 
2415  $mmail = new ilMimeMail();
2416  $mmail->autoCheck(false);
2417  $mmail->From($sender);
2418  $mmail->To($a_rcp_to);
2419  // Add installation name to subject
2420  $mmail->Subject($a_m_subject);
2421  $mmail->Body($a_m_message);
2422 
2423  if ($a_rcp_cc)
2424  {
2425  $mmail->Cc($a_rcp_cc);
2426  }
2427 
2428  if ($a_rcp_bcc)
2429  {
2430  $mmail->Bcc($a_rcp_bcc);
2431  }
2432 
2433  if (is_array($a_attachments))
2434  {
2435  foreach ($a_attachments as $attachment)
2436  {
2437  $mmail->Attach($this->mfile->getAbsolutePath($attachment), '', 'inline', $attachment);
2438  }
2439  }
2440 
2441  $mmail->Send();
2442  }
2443  }
2449  function getEmailOfSender()
2450  {
2451  $umail = self::getCachedUserInstance($this->user_id);
2452  $sender = $umail->getEmail();
2453 
2454  if (ilUtil::is_email($sender))
2455  {
2456  return $sender;
2457  }
2458  else
2459  {
2460  return '';
2461  }
2462  }
2463 
2470  function saveAttachments($a_attachments)
2471  {
2472  global $ilDB;
2473 
2474  $ilDB->update($this->table_mail_saved,
2475  array
2476  (
2477  'attachments' => array('clob', serialize($a_attachments))
2478  ),
2479  array
2480  (
2481  'user_id' => array('integer', $this->user_id)
2482  )
2483  );
2484 
2485  return true;
2486  }
2487 
2493  function getAttachments()
2494  {
2495  return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
2496  }
2497 
2511  function explodeRecipients($a_recipients, $use_pear = true)
2512  {
2513  $a_recipients = trim($a_recipients);
2514 
2515  // WHITESPACE IS NOT ALLOWED AS SEPERATOR
2516  #$a_recipients = preg_replace("/ /",",",$a_recipients);
2517  $a_recipients = preg_replace("/;/",",",$a_recipients);
2518 
2519  if (ilMail::_usePearMail() && $use_pear == true)
2520  {
2521  if (strlen(trim($a_recipients)) > 0)
2522  {
2523  ilLoggerFactory::getLogger('mail')->debug(sprintf(
2524  "Started Mail_RFC822 parsing of recipient string: %s", $a_recipients
2525  ));
2526 
2527  require_once './Services/PEAR/lib/Mail/RFC822.php';
2528  $parser = new Mail_RFC822();
2529  $addresses = $parser->parseAddressList($a_recipients, self::ILIAS_HOST, false, true);
2530 
2531  if(!is_a($a_recipients, 'PEAR_Error'))
2532  {
2533  ilLoggerFactory::getLogger('mail')->debug(sprintf(
2534  "Parsed addresses: %s", implode(',', array_map(function($address) {
2535  return $address->mailbox . '@' . $address->host;
2536  }, $addresses))
2537  ));
2538  }
2539  else
2540  {
2541  ilLoggerFactory::getLogger('mail')->debug(sprintf(
2542  "Parsing with Mail_RFC822 failed ..."
2543  ));
2544  }
2545 
2546  return $addresses;
2547  } else {
2548  return array();
2549  }
2550  }
2551  else
2552  {
2553  $rcps = array();
2554  foreach(explode(',',$a_recipients) as $tmp_rec)
2555  {
2556  if($tmp_rec)
2557  {
2558  $rcps[] = trim($tmp_rec);
2559  }
2560  }
2561  if(strlen($a_recipients) > 0)
2562  {
2563  ilLoggerFactory::getLogger('mail')->debug(sprintf(
2564  "Parsed recipient string %s with result: %s", $a_recipients, implode(',', $rcps)
2565  ));
2566  }
2567  return is_array($rcps) ? $rcps : array();
2568  }
2569  }
2570 
2571  function __getCountRecipient($rcp,$a_only_email = true)
2572  {
2573  $counter = 0;
2574 
2575  $this->validatePear($rcp);
2576  if (ilMail::_usePearMail() && $this->getUsePear())
2577  {
2578  $tmp_rcp = $this->explodeRecipients($rcp);
2579  if (! is_a($tmp_rcp, 'PEAR_Error'))
2580  {
2581  foreach ($tmp_rcp as $to)
2582  {
2583  if ($a_only_email)
2584  {
2585  // Fixed mantis bug #5875
2586  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2587  {
2588  continue;
2589  }
2590 
2591  // Addresses which aren't on the self::ILIAS_HOST host, and
2592  // which have a mailbox which does not start with '#',
2593  // are external e-mail addresses
2594  if ($to->host != self::ILIAS_HOST && substr($to->mailbox,0,1) != '#' && substr($to->mailbox,0,2) != '"#')
2595  {
2596  ++$counter;
2597  }
2598  }
2599  else
2600  {
2601  ++$counter;
2602  }
2603  }
2604  }
2605  }
2606  else
2607  {
2608  foreach ($this->explodeRecipients($rcp,$this->getUsePear()) as $to)
2609  {
2610  if ($a_only_email)
2611  {
2612  $to = $this->__substituteRecipients($to,"resubstitute");
2613  if (strpos($to,'@'))
2614  {
2615  // Fixed mantis bug #5875
2616  if(ilObjUser::_lookupId($to))
2617  {
2618  continue;
2619  }
2620 
2621  ++$counter;
2622  }
2623  }
2624  else
2625  {
2626  ++$counter;
2627  }
2628  }
2629  }
2630  return $counter;
2631  }
2632 
2633 
2634  function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
2635  {
2636  return $this->__getCountRecipient($a_to,$a_only_email)
2637  + $this->__getCountRecipient($a_cc,$a_only_email)
2638  + $this->__getCountRecipient($a_bcc,$a_only_email);
2639  }
2640 
2641  function __getEmailRecipients($a_rcp)
2642  {
2643  if (ilMail::_usePearMail())
2644  {
2645  $rcp = array();
2646  $tmp_rcp = $this->explodeRecipients($a_rcp);
2647  if (! is_a($tmp_rcp, 'PEAR_Error'))
2648  {
2649  foreach ($tmp_rcp as $to)
2650  {
2651  if(substr($to->mailbox,0,1) != '#' && substr($to->mailbox,0,2) != '"#' && $to->host != self::ILIAS_HOST)
2652  {
2653  // Fixed mantis bug #5875
2654  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2655  {
2656  continue;
2657  }
2658 
2659  $rcp[] = $to->mailbox.'@'.$to->host;
2660  }
2661  }
2662  }
2663  return implode(',',$rcp);
2664  }
2665  else
2666  {
2667  foreach ($this->explodeRecipients($a_rcp) as $to)
2668  {
2669  $to = $this->__substituteRecipients($to,"resubstitute");
2670  if(strpos($to,'@'))
2671  {
2672  // Fixed mantis bug #5875
2673  if(ilObjUser::_lookupId($to))
2674  {
2675  continue;
2676  }
2677 
2678  $rcp[] = $to;
2679  }
2680  }
2681  return implode(',',$rcp ? $rcp : array());
2682  }
2683  }
2684 
2685  function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
2686  {
2687  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
2688 
2689  $message = $inst_name." To:".$rcp_to."\n";
2690 
2691  if ($rcp_cc)
2692  {
2693  $message .= "Cc: ".$rcp_cc;
2694  }
2695 
2696  $message .= "\n\n";
2697  $message .= $a_m_message;
2698 
2699  return $message;
2700  }
2701 
2709  function __substituteRecipients($a_rcp,$direction)
2710  {
2711  $new_name = array();
2712 
2713  $tmp_names = $this->explodeRecipients($a_rcp);
2714 
2715 
2716  foreach($tmp_names as $name)
2717  {
2718  if(strpos($name,"#") === 0)
2719  {
2720  $new_name[] = $name;
2721  continue;
2722  }
2723  switch($direction)
2724  {
2725  case "substitute":
2726  if(strpos($name,"@") and ilObjUser::_loginExists($name))
2727  {
2728  $new_name[] = preg_replace("/@/","�#�",$name);
2729  }
2730  else
2731  {
2732  $new_name[] = $name;
2733  }
2734  break;
2735 
2736  case "resubstitute":
2737  if(stristr($name,"�#�"))
2738  {
2739  $new_name[] = preg_replace("/�#�/","@",$name);
2740  }
2741  else
2742  {
2743  $new_name[] = $name;
2744  }
2745  break;
2746  }
2747  }
2748  return implode(",",$new_name);
2749  }
2750 
2766  public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null)
2767  {
2768  if (ilMail::_usePearMail())
2769  {
2770  if ($login == null)
2771  {
2772  require_once './Services/User/classes/class.ilObjUser.php';
2773  $usr_obj = self::getCachedUserInstance($usr_id);
2774  $login = $usr_obj->getLogin();
2775  $firstname = $usr_obj->getFirstname();
2776  $lastname = $usr_obj->getLastname();
2777  }
2778  // The following line of code creates a properly formatted mailbox
2779  // address. Unfortunately, it does not work, because ILIAS removes
2780  // everything between '<' '>' characters
2781  // Therefore, we just return the login - sic.
2782  // FIXME - Make this work in a future release
2783  /*
2784  return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
2785  */
2786  return $login.'hhho';
2787  }
2788  else
2789  {
2790  return $login;
2791  }
2792  }
2799  public static function _usePearMail()
2800  {
2804  global $ilSetting;
2805 
2806  return $ilSetting->get('pear_mail_enable', 0);
2807  }
2808 
2817  public static function _getAutoGeneratedMessageString($lang = null)
2818  {
2819  global $ilSetting;
2820 
2821  if(!$lang)
2822  {
2823  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
2825  }
2826  $http_path = ilUtil::_getHttpPath();
2827 
2828  $lang->loadLanguageModule('mail');
2829  return sprintf($lang->txt('mail_auto_generated_info'),
2830  $ilSetting->get('inst_name','ILIAS 5'),
2831  $http_path)."\n\n";
2832  }
2833 
2840  public static function _getIliasMailerName()
2841  {
2845  global $ilSetting;
2846 
2847  if(strlen($ilSetting->get('mail_system_sender_name')))
2848  {
2849  return $ilSetting->get('mail_system_sender_name');
2850  }
2851  else if(strlen($ilSetting->get('short_inst_name')))
2852  {
2853  return $ilSetting->get('short_inst_name');
2854  }
2855 
2856  return 'ILIAS';
2857  }
2858 
2867  public function appendInstallationSignature($a_flag = null)
2868  {
2869  if(null === $a_flag) {
2871  }
2872 
2873  $this->appendInstallationSignature = $a_flag;
2874 
2875  return $this;
2876  }
2877 
2886  public static function _getInstallationSignature()
2887  {
2888  global $ilClientIniFile;
2889 
2890  $signature = "\n\n* * * * *\n";
2891 
2892  $signature .= $ilClientIniFile->readVariable('client', 'name')."\n";
2893  if(strlen($desc = $ilClientIniFile->readVariable('client', 'description')))
2894  {
2895  $signature .= $desc."\n";
2896  }
2897 
2898  $signature .= ilUtil::_getHttpPath();
2899 
2900  $clientdirs = glob(ILIAS_WEB_DIR."/*", GLOB_ONLYDIR);
2901  if(is_array($clientdirs) && count($clientdirs) > 1)
2902  {
2903  // #18051
2904  $signature .= '/login.php?client_id='.CLIENT_ID;
2905  }
2906 
2907  $signature .= "\n\n";
2908 
2909  return $signature;
2910  }
2911 
2916  public static function getSubjectPrefix()
2917  {
2918  global $ilSetting;
2919  static $prefix = null;
2920 
2921  return $prefix == null ? $ilSetting->get('mail_subject_prefix','') : $prefix;
2922  }
2923 
2929  public static function getSalutation($a_usr_id,$a_language = null)
2930  {
2931  global $lng;
2932 
2933  $lang = $a_language ? $a_language : $lng;
2934 
2935  $lang->loadLanguageModule('mail');
2936  $gender = ilObjUser::_lookupGender($a_usr_id);
2937  $gender = $gender ? $gender : 'n';
2938  $name = ilObjUser::_lookupName($a_usr_id);
2939 
2940  if(!strlen($name['firstname']))
2941  {
2942  return $lang->txt('mail_salutation_anonymous').',';
2943  }
2944 
2945  return $lang->txt('mail_salutation_'.$gender).' '.
2946  ($name['title'] ? $name['title'].' ' : '').
2947  ($name['firstname'] ? $name['firstname'].' ' : '').
2948  $name['lastname'].',';
2949  }
2950 
2951  private function setUsePear($bool)
2952  {
2953  $this->use_pear = $bool;
2954  }
2955 
2956  private function getUsePear()
2957  {
2958  return $this->use_pear;
2959  }
2960 
2961  // Force fallback for sending mails via ILIAS, if internal Pear-Validation returns PEAR_Error
2966  private function validatePear($a_recipients)
2967  {
2968  if(ilMail::_usePearMail())
2969  {
2970  $this->setUsePear(true);
2971  $tmp_names = $this->explodeRecipients($a_recipients, true);
2972  if(is_a($tmp_names, 'PEAR_Error'))
2973  {
2974  $this->setUsePear(false);
2975  }
2976  }
2977  else
2978  {
2979  $this->setUsePear(false);
2980  }
2981  }
2982 
2991  protected static function getCachedUserInstance($a_usr_id)
2992  {
2993  if(isset(self::$userInstances[$a_usr_id]))
2994  {
2995  return self::$userInstances[$a_usr_id];
2996  }
2997 
2998  self::$userInstances[$a_usr_id] = new ilObjUser($a_usr_id);
2999  return self::$userInstances[$a_usr_id];
3000  }
3001 } // END class.ilMail
3002 ?>
static _lookupLogin($a_user_id)
lookup login
static _lookupName($a_user_id)
lookup user name
checkMail($a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_m_subject, $a_m_message, $a_type)
check if mail is complete, recipients are valid public
Class UserMail this class handles user mails.
getEmailsOfRecipients($a_rcp)
get email addresses of recipients public
deleteMails(array $a_mail_ids)
Delete mails.
setMailRcpCc($a_rcp_cc)
isSOAPEnabled()
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)
send internal message to recipients private
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
getNewDraftId($usrId, $folderId)
__getCountRecipients($a_to, $a_cc, $a_bcc, $a_only_email=true)
getSavedData()
get saved data public
$result
const ILIAS_HOST
static getCachedUserInstance($a_usr_id)
Returns a cached instance of ilObjUser.
This class handles all operations on files (attachments) in directory ilias_data/mail.
$mail_use_placeholders
__get($name)
Magic interceptor method __get Used to include files / instantiate objects at runtime.
static _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null)
STATIC METHOD.
const CONTEXT_CRON
static $userInstances
moveMailsToFolder($a_mail_ids, $a_folder_id)
move mail to folder public
$_COOKIE["ilClientId"]
Definition: cron.php:11
static is_email($a_email)
This preg-based function checks whether an e-mail address is formally valid.
ILIAS Exception for Service Mail.
static groupNameExists($a_group_name, $a_id=0)
checks if group name already exists.
static _lookupId($a_user_str)
Lookup id by login.
setSaveInSentbox($a_save_in_sentbox)
explodeRecipients($a_recipients, $use_pear=true)
explode recipient string allowed seperators are &#39;,&#39; &#39;;&#39; &#39; &#39;
getMailCounterData()
get mail counter data returns data array with indexes "total","read","unread" public ...
validatePear($a_recipients)
markRead($a_mail_ids)
mark mails as read public
sendInternalMail($a_folder_id, $a_sender_id, $a_attachments, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_status, $a_m_type, $a_m_email, $a_m_subject, $a_m_message, $a_user_id=0, $a_use_placeholders=0, $a_tpl_context_id=null, $a_tpl_context_params=array())
save mail in folder private
_lookupPref($a_usr_id, $a_keyword)
updateDraft( $a_folder_id, $a_attachments, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_m_type, $a_m_email, $a_m_subject, $a_m_message, $a_draft_id=0, $a_use_placeholders=0, $a_tpl_context_id=null, $a_tpl_context_params=array())
static _lookupGender($a_user_id)
Lookup gender.
deleteMailsOfFolder($a_folder_id)
delete all mails of a specific folder public
getEmailOfSender()
get email of sender public
getAttachments()
get attachments public
static _getAllReferences($a_id)
get all reference ids of object
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
getUserIdByLogin($a_login)
_lookupIdByTitle($a_title)
static _loginExists($a_login, $a_user_id=0)
check if a login name already exists You may exclude a user from the check by giving his user id as 2...
const ILIAS_WEB_DIR
static _getLanguage($a_lang_key='')
Get langauge object.
validateRecipients($a_rcp_to, $a_rcp_cc, $a_rcp_bc)
__getCountRecipient($rcp, $a_only_email=true)
$data
this class encapsulates the PHP mail() function.
$appendInstallationSignature
Mail Box class Base class for creating and handling mail boxes.
Class Mail this class handles base functions for mail handling.
__construct($a_user_id)
Constructor setup an mail object public.
$mail_to_global_roles
__substituteRecipients($a_rcp, $direction)
Note: This function can only be used, when ILIAS is configured to not use standards compliant mail ad...
countMailsOfFolder($a_folder_id)
count all mails of a specific folder public
_mimeEncode($a_string)
static getSalutation($a_usr_id, $a_language=null)
Get salutation.
redirection script todo: (a better solution should control the processing via a xml file) ...
getNextMail($a_mail_id)
setMailRcpTo($a_rcp_to)
getMail($a_mail_id)
get data of one mail public
getSaveInSentbox()
replacePlaceholders($a_message, $a_user_id=0)
parseRcptOfMailingLists($rcpt='', $maintain_lists=false)
appendInstallationSignature($a_flag=null)
Setter/Getter for appending the installation signarue.
doesRecipientStillExists($a_recipient, $a_existing_recipients)
getMailObjectReferenceId()
static addFullname($a_email, $a_fullname)
getMailsOfFolder($a_folder_id, $filter=array())
get all mails of a specific folder public
setMailSubject($a_subject)
sendMail($a_rcp_to, $a_rcp_cc, $a_rcp_bc, $a_m_subject, $a_m_message, $a_attachment, $a_type, $a_use_placeholders=0)
send external mail using class.ilMimeMail.php
setMailSendType($a_types)
static _getHttpPath()
global $ilUser
Definition: imgupload.php:15
$ref_id
Definition: sahs_server.php:39
global $ilSetting
Definition: privfeed.php:40
setUsePear($bool)
global $ilDB
static getMailObjectRefId()
Determines the reference id of the mail object and stores this information in a local cache variable...
fetchMailData($a_row)
fetch all query data from table mail public
__prependMessage($a_m_message, $rcp_to, $rcp_cc)
static getLogger($a_component_id)
Get component logger.
enableSOAP($a_status)
static getType()
Get context type.
sendMimeMail($a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_m_subject, $a_m_message, $a_attachments, $a_no_soap=false)
send mime mail using class.ilMimeMail.php All external mails are send to SOAP::sendMail starting a ki...
formatNamesForOutput($users='')
Prepends the fullname of each ILIAS login name (is user has a public profile) found in the passed str...
static getSubjectPrefix()
Get text that will be prepended to auto generated mails.
__getEmailRecipients($a_rcp)
setMailMessage($a_message)
markUnread($a_mail_ids)
mark mails as unread public
setMailRcpBc($a_rcp_bc)
static _getAutoGeneratedMessageString($lang=null)
get auto generated info string
readMailObjectReferenceId()
read and set mail object id private
getPreviousMail($a_mail_id)
saveInSentbox($a_attachment, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_type, $a_m_subject, $a_m_message)
send mime mail using class.ilMimeMail.php
static _getInstallationSignature()
Static getter for the installation signature.
savePostData($a_user_id, $a_attachments, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_m_type, $a_m_email, $a_m_subject, $a_m_message, $a_use_placeholders, $a_tpl_context_id=null, $a_tpl_ctx_params=array())
save post data in table public
checkRecipients($a_recipients)
check if recipients are valid public
static getIliasMailerAddress()
Builds an email address used for system notifications.
getUserIds($a_recipients)
get user_ids
saveAttachments($a_attachments)
set attachments