ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMail.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once 'Services/User/classes/class.ilObjUser.php';
25 
129 class ilMail
130 {
137  var $ilias;
138 
144  var $lng;
145 
151  var $mfile;
152 
154 
160  var $user_id;
161 
168 
175 
182 
189 
190 
197 
204 
211 
223 
224  var $soap_enabled = true;
226 
227  private $use_pear = true;
228  protected $appendInstallationSignature = false;
229 
238  protected $properties = array();
239 
243  protected static $userInstances = array();
244 
251  function ilMail($a_user_id)
252  {
253  require_once "classes/class.ilFileDataMail.php";
254  require_once "Services/Mail/classes/class.ilMailOptions.php";
255 
256  global $ilias, $lng, $ilUser;
257 
258  $lng->loadLanguageModule("mail");
259 
260  // Initiate variables
261  $this->ilias =& $ilias;
262  $this->lng =& $lng;
263  $this->table_mail = 'mail';
264  $this->table_mail_saved = 'mail_saved';
265  $this->user_id = $a_user_id;
266  $this->mfile = new ilFileDataMail($this->user_id);
267  $this->mail_options = new ilMailOptions($a_user_id);
268 
269  // DEFAULT: sent mail aren't stored insentbox of user.
270  $this->setSaveInSentbox(false);
271 
272  // GET REFERENCE ID OF MAIL OBJECT
273  $this->readMailObjectReferenceId();
274  }
275 
283  public function __get($name)
284  {
285  global $ilUser;
286 
287  if(isset($this->properties[$name]))
288  {
289  return $this->properties[$name];
290  }
291 
292  // Used to include files / instantiate objects at runtime
293  if($name == 'mlists')
294  {
295  if(is_object($ilUser))
296  {
297  require_once 'Services/Contact/classes/class.ilMailingLists.php';
298  $this->properties[$name] = new ilMailingLists($ilUser);
299  return $this->properties[$name];
300  }
301  }
302  }
303 
304  public function doesRecipientStillExists($a_recipient, $a_existing_recipients)
305  {
306  if(self::_usePearMail())
307  {
308  $recipients = $this->explodeRecipients($a_existing_recipients);
309  if(is_a($recipients, 'PEAR_Error'))
310  {
311  return false;
312  }
313  else
314  {
315  foreach($recipients as $rcp)
316  {
317  if (substr($rcp->mailbox, 0, 1) != '#')
318  {
319  if(trim($rcp->mailbox) == trim($a_recipient) ||
320  trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
321  {
322  return true;
323  }
324  }
325  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
326  {
327  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
328  {
329  return true;
330  }
331  }
332  else
333  {
334  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
335  {
336  return true;
337  }
338  }
339  }
340  }
341  }
342  else
343  {
344  $recipients = $this->explodeRecipients($a_existing_recipients);
345  if(count($recipients))
346  {
347  foreach($recipients as $recipient)
348  {
349  if(trim($recipient) == trim($a_recipient))
350  {
351  return true;
352  }
353  }
354  }
355  }
356 
357  return false;
358  }
359 
367  function enableSOAP($a_status)
368  {
369  $this->soap_enabled = $a_status;
370  }
371  function isSOAPEnabled()
372  {
373  global $ilSetting;
374 
375  if(!extension_loaded('curl') || !$ilSetting->get('soap_user_administration'))
376  {
377  return false;
378  }
379 
380  return (bool) $this->soap_enabled;
381  }
382 
383 
384  function setSaveInSentbox($a_save_in_sentbox)
385  {
386  $this->save_in_sentbox = $a_save_in_sentbox;
387  }
388 
389  function getSaveInSentbox()
390  {
391  return $this->save_in_sentbox;
392  }
393 
399  function setMailSendType($a_types)
400  {
401  $this->mail_send_type = $a_types;
402  }
403 
409  function setMailRcpTo($a_rcp_to)
410  {
411  $this->mail_rcp_to = $a_rcp_to;
412  }
413 
419  function setMailRcpCc($a_rcp_cc)
420  {
421  $this->mail_rcp_cc = $a_rcp_cc;
422  }
423 
429  function setMailRcpBc($a_rcp_bc)
430  {
431  $this->mail_rcp_bc = $a_rcp_bc;
432  }
433 
439  function setMailSubject($a_subject)
440  {
441  $this->mail_subject = $a_subject;
442  }
443 
449  function setMailMessage($a_message)
450  {
451  $this->mail_message = $a_message;
452  }
453 
459  {
460  include_once 'Services/Mail/classes/class.ilMailGlobalServices.php';
461  $this->mail_obj_ref_id = ilMailGlobalServices::getMailObjectRefId();
462  }
463 
465  {
466  return $this->mail_obj_ref_id;
467  }
468 
479  public function formatNamesForOutput($users = '')
480  {
481  $users = trim($users);
482  if($users)
483  {
484  if(strstr($users, ','))
485  {
486  $rcp_to_array = array();
487 
488  $recipients = explode(',', $users);
489  foreach($recipients as $recipient)
490  {
491  $recipient = trim($recipient);
492  if($uid = ilObjUser::_lookupId($recipient))
493  {
494  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
495  {
496  $tmp_obj = self::getCachedUserInstance($uid);
497  $rcp_to_array[] = $tmp_obj->getFullname().' ['.$recipient.']';
498  }
499  else
500  {
501  $rcp_to_array[] = $recipient;
502  }
503  }
504  else
505  {
506  $rcp_to_array[] = $recipient;
507  }
508  }
509 
510  return trim(implode(', ', $rcp_to_array));
511  }
512  else
513  {
514  if($uid = ilObjUser::_lookupId($users))
515  {
516  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
517  {
518  $tmp_obj = self::getCachedUserInstance($uid);
519  return $tmp_obj->getFullname().' ['.$users.']';
520  }
521  else
522  {
523  return $users;
524  }
525  }
526  else
527  {
528  return $users;
529  }
530  }
531  }
532  else
533  {
534  return $this->lng->txt('not_available');
535  }
536  }
537 
538  function getPreviousMail($a_mail_id)
539  {
540  global $ilDB;
541 
542  $ilDB->setLimit(1);
543  $res = $ilDB->queryf("
544  SELECT b.* FROM " . $this->table_mail ." a
545  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
546  AND b.user_id = a.user_id AND b.send_time > a.send_time
547  WHERE a.user_id = %s
548  AND a.mail_id = %s ORDER BY b.send_time ASC",
549  array('integer', 'integer'),
550  array($this->user_id, $a_mail_id));
551 
552  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
553 
554  return $this->mail_data;
555  }
556 
557  function getNextMail($a_mail_id)
558  {
559  global $ilDB;
560 
561  $ilDB->setLimit(1);
562  $res = $ilDB->queryf("
563  SELECT b.* FROM " . $this->table_mail ." a
564  INNER JOIN ".$this->table_mail ." b ON b.folder_id = a.folder_id
565  AND b.user_id = a.user_id AND b.send_time < a.send_time
566  WHERE a.user_id = %s
567  AND a.mail_id = %s ORDER BY b.send_time DESC",
568  array('integer', 'integer'),
569  array($this->user_id, $a_mail_id));
570 
571  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
572 
573  return $this->mail_data;
574  }
575 
583  function getMailsOfFolder($a_folder_id, $filter = array())
584  {
585  global $ilDB;
586 
587  $this->mail_counter = array();
588  $this->mail_counter['read'] = 0;
589  $this->mail_counter['unread'] = 0;
590 
591  $query = "SELECT sender_id, m_subject, mail_id, m_status, send_time FROM ". $this->table_mail ."
592  LEFT JOIN object_data ON obj_id = sender_id
593  WHERE user_id = %s
594  AND folder_id = %s
595  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)) ";
596 
597  if($filter['status'])
598  {
599  $query .= ' AND m_status = '.$ilDB->quote($filter['status'], 'text');
600  }
601  if($filter['type'])
602  {
603  $query .= ' AND '.$ilDB->like('m_type', 'text', '%%:"'.$filter['type'].'"%%', false);
604  }
605 
606  $query .= " ORDER BY send_time DESC";
607 
608  $res = $ilDB->queryf($query,
609  array('integer', 'integer'),
610  array($this->user_id, $a_folder_id));
611 
612  while ($row = $ilDB->fetchObject($res))
613  {
614  $tmp = $this->fetchMailData($row);
615 
616  if($tmp['m_status'] == 'read')
617  {
618  ++$this->mail_counter['read'];
619  }
620 
621  if($tmp['m_status'] == 'unread')
622  {
623  ++$this->mail_counter['unread'];
624  }
625 
626  $output[] = $tmp;
627  }
628 
629  $this->mail_counter['total'] = count($output);
630 
631  return $output ? $output : array();
632  }
633 
640  function countMailsOfFolder($a_folder_id)
641  {
642  global $ilDB;
643 
644  $res = $ilDB->queryf("
645  SELECT COUNT(*) FROM ". $this->table_mail ."
646  WHERE user_id = %s
647  AND folder_id = %s",
648  array('integer', 'integer'),
649  array($this->user_id, $a_folder_id));
650 
651  return $res->numRows();
652  }
653 
660  function deleteMailsOfFolder($a_folder_id)
661  {
662  if ($a_folder_id)
663  {
664  global $ilDB;
665 
666  /*$statement = $ilDB->manipulateF("
667  DELETE FROM ". $this->table_mail ."
668  WHERE user_id = %s
669  AND folder_id = %s",
670  array('integer', 'integer'),
671  array($this->user_id, $a_folder_id));*/
672  $mails = $this->getMailsOfFolder($a_folder_id);
673  foreach((array)$mails as $mail_data)
674  {
675  $this->deleteMails(array($mail_data['mail_id']));
676  }
677 
678  return true;
679  }
680 
681  return false;
682  }
683 
691  {
692  return is_array($this->mail_counter) ? $this->mail_counter : array(
693  "total" => 0,
694  "read" => 0,
695  "unread" => 0);
696  }
697 
704  function getMail($a_mail_id)
705  {
706  global $ilDB;
707 
708  $res = $ilDB->queryf("
709  SELECT * FROM ". $this->table_mail ."
710  WHERE user_id = %s
711  AND mail_id = %s",
712  array('integer', 'integer'),
713  array($this->user_id, $a_mail_id));
714 
715  $this->mail_data =$this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
716 
717  return $this->mail_data;
718  }
719 
726  function markRead($a_mail_ids)
727  {
728 
729  global $ilDB;
730 
731  $data = array();
732  $data_types = array();
733 
734  $query = "UPDATE ". $this->table_mail ."
735  SET m_status = %s
736  WHERE user_id = %s ";
737  array_push($data_types, 'text', 'integer');
738  array_push($data, 'read', $this->user_id);
739 
740  $cnt_mail_ids = count($a_mail_ids);
741 
742  if (is_array($a_mail_ids) &&
743  count($a_mail_ids) > 0)
744  {
745 
746  $in = 'mail_id IN (';
747  $counter = 0;
748  foreach($a_mail_ids as $a_mail_id)
749  {
750  array_push($data, $a_mail_id);
751  array_push($data_types, 'integer');
752 
753  if($counter > 0) $in .= ',';
754  $in .= '%s';
755  ++$counter;
756  }
757  $in .= ')';
758 
759  $query .= ' AND '.$in;
760  }
761 
762  $res = $ilDB->manipulateF($query, $data_types, $data);
763 
764  return true;
765  }
766 
773  function markUnread($a_mail_ids)
774  {
775  global $ilDB;
776 
777  $data = array();
778  $data_types = array();
779 
780  $query = "UPDATE ". $this->table_mail ."
781  SET m_status = %s
782  WHERE user_id = %s ";
783  array_push($data_types, 'text', 'integer');
784  array_push($data, 'unread', $this->user_id);
785 
786  $cnt_mail_ids = count($a_mail_ids);
787 
788  if (is_array($a_mail_ids) &&
789  count($a_mail_ids) > 0)
790  {
791 
792  $in = 'mail_id IN (';
793  $counter = 0;
794  foreach($a_mail_ids as $a_mail_id)
795  {
796  array_push($data, $a_mail_id);
797  array_push($data_types, 'integer');
798 
799  if($counter > 0) $in .= ',';
800  $in .= '%s';
801  ++$counter;
802  }
803  $in .= ')';
804 
805  $query .= ' AND '.$in;
806  }
807 
808  $statement = $ilDB->manipulateF($query, $data_types, $data);
809 
810  return true;
811  }
812 
820  function moveMailsToFolder($a_mail_ids,$a_folder_id)
821  {
822  global $ilDB;
823 
824  $data = array();
825  $data_types = array();
826 
827  $query = "UPDATE ". $this->table_mail ."
828  SET folder_id = %s
829  WHERE user_id = %s ";
830  array_push($data_types, 'text', 'integer');
831  array_push($data, $a_folder_id, $this->user_id);
832 
833  $cnt_mail_ids = count($a_mail_ids);
834 
835  if (is_array($a_mail_ids) &&
836  count($a_mail_ids) > 0)
837  {
838 
839  $in = 'mail_id IN (';
840  $counter = 0;
841  foreach($a_mail_ids as $a_mail_id)
842  {
843  array_push($data, $a_mail_id);
844  array_push($data_types, 'integer');
845 
846  if($counter > 0) $in .= ',';
847  $in .= '%s';
848  ++$counter;
849  }
850  $in .= ')';
851 
852  $query .= ' AND '.$in;
853  }
854 
855  $statement = $ilDB->manipulateF($query, $data_types, $data);
856 
857  return true;
858  }
859 
866  function deleteMails($a_mail_ids)
867  {
868  global $ilDB;
869 
870  foreach ($a_mail_ids as $id)
871  {
872  $statement = $ilDB->manipulateF("
873  DELETE FROM ". $this->table_mail ."
874  WHERE user_id = %s
875  AND mail_id = %s ",
876  array('integer', 'integer'),
877  array($this->user_id, $id));
878 
879  $this->mfile->deassignAttachmentFromDirectory($id);
880  }
881 
882  return true;
883  }
884 
891  function fetchMailData($a_row)
892  {
893  if (!$a_row) return;
894 
895  return array(
896  "mail_id" => $a_row->mail_id,
897  "user_id" => $a_row->user_id,
898  "folder_id" => $a_row->folder_id,
899  "sender_id" => $a_row->sender_id,
900  "attachments" => unserialize(stripslashes($a_row->attachments)),
901  "send_time" => $a_row->send_time,
902  "rcp_to" => $a_row->rcp_to,
903  "rcp_cc" => $a_row->rcp_cc,
904  "rcp_bcc" => $a_row->rcp_bcc,
905  "m_status" => $a_row->m_status,
906  "m_type" => unserialize(stripslashes($a_row->m_type)),
907  "m_email" => $a_row->m_email,
908  "m_subject" => $a_row->m_subject,
909  "m_message" => $a_row->m_message,
910  "import_name" => $a_row->import_name,
911  "use_placeholders"=> $a_row->use_placeholders);
912  }
913 
914  function updateDraft($a_folder_id,
915  $a_attachments,
916  $a_rcp_to,
917  $a_rcp_cc,
918  $a_rcp_bcc,
919  $a_m_type,
920  $a_m_email,
921  $a_m_subject,
922  $a_m_message,
923  $a_draft_id = 0, $a_use_placeholders = 0)
924  {
925  global $ilDB;
926 
927  $ilDB->update($this->table_mail,
928  array(
929  'folder_id' => array('integer', $a_folder_id),
930  'attachments' => array('clob', serialize($a_attachments)),
931  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
932  'rcp_to' => array('clob', $a_rcp_to),
933  'rcp_cc' => array('clob', $a_rcp_cc),
934  'rcp_bcc' => array('clob', $a_rcp_bcc),
935  'm_status' => array('text', 'read'),
936  'm_type' => array('text', serialize($a_m_type)),
937  'm_email' => array('integer', $a_m_email),
938  'm_subject' => array('text', $a_m_subject),
939  'm_message' => array('clob', $a_m_message),
940  'use_placeholders' => array('integer', $a_use_placeholders)
941  ),
942  array(
943  'mail_id' => array('integer', $a_draft_id)
944  )
945  );
946 
947  return $a_draft_id;
948  }
949 
967  function sendInternalMail($a_folder_id,
968  $a_sender_id,
969  $a_attachments,
970  $a_rcp_to,
971  $a_rcp_cc,
972  $a_rcp_bcc,
973  $a_status,
974  $a_m_type,
975  $a_m_email,
976  $a_m_subject,
977  $a_m_message,
978  $a_user_id = 0, $a_use_placeholders = 0)
979  {
980  $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
981 
982  global $ilDB, $log;
983  //$log->write('class.ilMail->sendInternalMail to user_id:'.$a_rcp_to.' '.$a_m_message);
984 
985  if ($a_use_placeholders)
986  {
987  $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
988  }
989  else
990  {
991  $a_use_placeholders = '0';
992  }
993 
994 
995  if(!$a_user_id) $a_user_id = '0';
996  if(!$a_folder_id) $a_folder_id = '0';
997  if(!$a_sender_id) $a_sender_id = NULL;
998  if(!$a_attachments) $a_attachments = NULL;
999  if(!$a_rcp_to) $a_rcp_to = NULL;
1000  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1001  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1002  if(!$a_status) $a_status = NULL;
1003  if(!$a_m_type) $a_m_type = NULL;
1004  if(!$a_m_email) $a_m_email = NULL;
1005  if(!$a_m_subject) $a_m_subject = NULL;
1006  if(!$a_m_message) $a_m_message = NULL;
1007 
1008 
1009  $next_id = $ilDB->nextId($this->table_mail);
1010 
1011  $ilDB->insert($this->table_mail, array(
1012  'mail_id' => array('integer', $next_id),
1013  'user_id' => array('integer', $a_user_id),
1014  'folder_id' => array('integer', $a_folder_id),
1015  'sender_id' => array('integer', $a_sender_id),
1016  'attachments' => array('clob', serialize($a_attachments)),
1017  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
1018  'rcp_to' => array('clob', $a_rcp_to),
1019  'rcp_cc' => array('clob', $a_rcp_cc),
1020  'rcp_bcc' => array('clob', $a_rcp_bcc),
1021  'm_status' => array('text', $a_status),
1022  'm_type' => array('text', serialize($a_m_type)),
1023  'm_email' => array('integer', $a_m_email),
1024  'm_subject' => array('text', $a_m_subject),
1025  'm_message' => array('clob', $a_m_message)
1026  ));
1027 
1028  return $next_id; //$ilDB->getLastInsertId();
1029 
1030  }
1031 
1032  function replacePlaceholders($a_message, $a_user_id)
1033  {
1034  global $lng;
1035 
1036  $user = self::getCachedUserInstance($a_user_id);
1037 
1038  // determine salutation
1039  switch ($user->getGender())
1040  {
1041  case 'f': $gender_salut = $lng->txt('salutation_f');
1042  break;
1043  case 'm': $gender_salut = $lng->txt('salutation_m');
1044  break;
1045  }
1046 
1047  $a_message = str_replace('[MAIL_SALUTATION]', $gender_salut, $a_message);
1048  $a_message = str_replace('[LOGIN]', $user->getLogin(), $a_message);
1049  $a_message = str_replace('[FIRST_NAME]', $user->getFirstname(), $a_message);
1050  $a_message = str_replace('[LAST_NAME]', $user->getLastname(), $a_message);
1051  $a_message = str_replace('[ILIAS_URL]', ILIAS_HTTP_PATH.'/login.php?client_id='.CLIENT_ID, $a_message);
1052  $a_message = str_replace('[CLIENT_NAME]', CLIENT_NAME, $a_message);
1053 
1054  return $a_message;
1055  }
1056 
1070  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)
1071  {
1072  global $log;
1073 
1074  include_once 'Services/Mail/classes/class.ilMailbox.php';
1075  include_once './Services/User/classes/class.ilObjUser.php';
1076 
1077  if (!ilMail::_usePearMail())
1078  {
1079  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1080  $a_rcp_to = $this->__substituteRecipients($a_rcp_to, 'resubstitute');
1081  $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc, 'resubstitute');
1082  $a_rcp_bcc = $this->__substituteRecipients($a_rcp_bcc, 'resubstitute');
1083  }
1084 
1085  $mbox = new ilMailbox();
1086 
1087  if (!$a_use_placeholders) # No Placeholders
1088  {
1089  $rcp_ids = $this->getUserIds(trim($a_rcp_to).','.trim($a_rcp_cc).','.trim($a_rcp_bcc));
1090 
1091  $as_email = array();
1092 
1093  foreach($rcp_ids as $id)
1094  {
1095  $tmp_mail_options = new ilMailOptions($id);
1096 
1097  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1098  $tmp_user = self::getCachedUserInstance($id);
1099  $user_is_active = $tmp_user->getActive();
1100  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1101  $tmp_user->checkTimeLimit();
1102 
1103  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1104  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1105  {
1106  continue;
1107  }
1108 
1109  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS/HER MAIL
1110  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1111 
1112  // Do not send external mails to inactive users!!!
1113  if($user_is_active)
1114  {
1115  if (!$user_can_read_internal_mails ||
1116  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1117  {
1118  $as_email[] = $tmp_user->getEmail();
1119  continue;
1120  }
1121 
1122  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1123  {
1124  $as_email[] = $tmp_user->getEmail();
1125  }
1126  }
1127  $mbox->setUserId($id);
1128  $inbox_id = $mbox->getInboxFolder();
1129 
1130  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1131  $a_attachments, $a_rcp_to,
1132  $a_rcp_cc, '', 'unread', $a_type,
1133  0, $a_subject, $a_message, $id, 0);
1134  if ($a_attachments)
1135  {
1136  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1137  }
1138  }
1139 
1140  // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
1141  $to = array();
1142  $bcc = array();
1143 
1144  if(count($as_email) == 1)
1145  {
1146  $to[] = $as_email[0];
1147  }
1148  else
1149  {
1150  foreach ($as_email as $email)
1151  {
1152  $bcc[] = $email;
1153  }
1154  }
1155 
1156  if(count($to) > 0 || count($bcc) > 0)
1157  {
1158  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
1159  }
1160  }
1161  else # Use Placeholders
1162  {
1163  // to
1164  $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
1165 
1166  // cc / bcc
1167  $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
1168 
1169  $as_email = array();
1170 
1171  // to
1172  foreach($rcp_ids_replace as $id)
1173  {
1174  $tmp_mail_options = new ilMailOptions($id);
1175 
1176  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1177  $tmp_user = self::getCachedUserInstance($id);
1178  $user_is_active = $tmp_user->getActive();
1179  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1180  $tmp_user->checkTimeLimit();
1181 
1182  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1183  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1184  {
1185  continue;
1186  }
1187 
1188  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1189  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1190 
1191  // Do not send external mails to inactive users!!!
1192  if($user_is_active)
1193  {
1194  if (!$user_can_read_internal_mails ||
1195  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1196  {
1197  $as_email[$tmp_user->getId()] = $tmp_user->getEmail();
1198  continue;
1199  }
1200 
1201  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1202  {
1203  $as_email[$tmp_user->getId()] = $tmp_user->getEmail();
1204  }
1205  }
1206  $mbox->setUserId($id);
1207  $inbox_id = $mbox->getInboxFolder();
1208 
1209  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1210  $a_attachments, $a_rcp_to,
1211  $a_rcp_cc, '', 'unread', $a_type,
1212  0, $a_subject, $a_message, $id, 1);
1213  if ($a_attachments)
1214  {
1215  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1216  }
1217  }
1218 
1219  if (count($as_email))
1220  {
1221  foreach ($as_email as $id => $email)
1222  {
1223  $this->sendMimeMail($email, '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
1224  }
1225  }
1226 
1227  $as_email = array();
1228 
1229  // cc / bcc
1230  foreach($rcp_ids_no_replace as $id)
1231  {
1232  $tmp_mail_options = new ilMailOptions($id);
1233 
1234  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1235  $tmp_user = self::getCachedUserInstance($id);
1236  $user_is_active = $tmp_user->getActive();
1237  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement()
1238  && $tmp_user->checkTimeLimit();
1239 
1240  // Do not send external mails to inactive users!!!
1241  if($user_is_active)
1242  {
1243  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1244  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1245  {
1246  continue;
1247  }
1248 
1249  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1250  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1251  if (!$user_can_read_internal_mails ||
1252  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1253  {
1254  $as_email[] = $tmp_user->getEmail();
1255  continue;
1256  }
1257 
1258  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1259  {
1260  $as_email[] = $tmp_user->getEmail();
1261  }
1262  }
1263  $mbox->setUserId($id);
1264  $inbox_id = $mbox->getInboxFolder();
1265 
1266  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1267  $a_attachments, $a_rcp_to,
1268  $a_rcp_cc, '', 'unread', $a_type,
1269  0, $a_subject, $a_message, $id, 0);
1270  if ($a_attachments)
1271  {
1272  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1273  }
1274  }
1275 
1276  if (count($as_email))
1277  {
1278  $this->sendMimeMail('', '', implode(',', $as_email), $a_subject, $a_message, $a_attachments);
1279  }
1280  }
1281 
1282  return true;
1283  }
1284 
1290  function getUserIds($a_recipients)
1291  {
1292  global $log, $rbacreview;
1293  $ids = array();
1294 
1295  $this->validatePear($a_recipients);
1296  if (ilMail::_usePearMail() && $this->getUsePear() == true)
1297  {
1298  $tmp_names = $this->explodeRecipients($a_recipients );
1299  if (! is_a($tmp_names, 'PEAR_Error'))
1300  {
1301  for ($i = 0;$i < count($tmp_names); $i++)
1302  {
1303  if ( substr($tmp_names[$i]->mailbox,0,1) === '#' ||
1304  (substr($tmp_names[$i]->mailbox,0,1) === '"' &&
1305  substr($tmp_names[$i]->mailbox,1,1) === '#' ) )
1306  {
1307  $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1308  foreach($role_ids as $role_id)
1309  {
1310  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1311  {
1312  $ids[] = $usr_id;
1313  }
1314  }
1315  }
1316  else if (strtolower($tmp_names[$i]->host) == 'ilias')
1317  {
1318  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
1319  {
1320  //$log->write('class.ilMail->getUserIds() recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host.' user_id:'.$id);
1321  $ids[] = $id;
1322  }
1323  else
1324  {
1325  //$log->write('class.ilMail->getUserIds() no user account found for recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1326  }
1327  }
1328  else
1329  {
1330  // Fixed mantis bug #5875
1331  if($id = ilObjUser::_lookupId($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host))
1332  {
1333  $ids[] = $id;
1334  }
1335  else
1336  {
1337  //$log->write('class.ilMail->getUserIds() external recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1338  }
1339  }
1340  }
1341  }
1342  else
1343  {
1344  //$log->write('class.ilMail->getUserIds() illegal recipients:'.$a_recipients);
1345  }
1346  }
1347  else
1348  {
1349  $tmp_names = $this->explodeRecipients($a_recipients, $this->getUsePear());
1350  for ($i = 0;$i < count($tmp_names); $i++)
1351  {
1352  if (substr($tmp_names[$i],0,1) == '#')
1353  {
1354  if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
1355  {
1356  include_once("./classes/class.ilObjectFactory.php");
1357  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1358 
1359  foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1360  {
1361  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1362  break;
1363  }
1364  // STORE MEMBER IDS IN $ids
1365  foreach ($grp_object->getGroupMemberIds() as $id)
1366  {
1367  $ids[] = $id;
1368  }
1369  }
1370  // is role: get role ids
1371  elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
1372  {
1373  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1374  {
1375  $ids[] = $usr_id;
1376  }
1377  }
1378 
1379  }
1380  else if (!empty($tmp_names[$i]))
1381  {
1382  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
1383  {
1384  $ids[] = $id;
1385  }
1386  }
1387  }
1388  }
1389  return array_unique($ids);
1390  }
1391 
1402  function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
1403  {
1404  $error_message = '';
1405 
1406  $a_m_subject = trim($a_m_subject);
1407  $a_rcp_to = trim($a_rcp_to);
1408 
1409  if (empty($a_m_subject))
1410  {
1411  $error_message .= $error_message ? "<br>" : '';
1412  $error_message .= $this->lng->txt("mail_add_subject");
1413  }
1414 
1415  if (empty($a_rcp_to))
1416  {
1417  $error_message .= $error_message ? "<br>" : '';
1418  $error_message .= $this->lng->txt("mail_add_recipient");
1419  }
1420 
1421  return $error_message;
1422  }
1423 
1430  function getEmailsOfRecipients($a_rcp)
1431  {
1432  global $rbacreview;
1433 
1434  $addresses = array();
1435 
1436  $this->validatePear($a_rcp);
1437  if (ilMail::_usePearMail() && $this->getUsePear())
1438  {
1439  $tmp_rcp = $this->explodeRecipients($a_rcp);
1440  if (! is_a($tmp_rcp, 'PEAR_Error'))
1441  {
1442  foreach ($tmp_rcp as $rcp)
1443  {
1444  // NO GROUP
1445  if (substr($rcp->mailbox,0,1) != '#')
1446  {
1447  if (strtolower($rcp->host) != 'ilias')
1448  {
1449  $addresses[] = $rcp->mailbox.'@'.$rcp->host;
1450  continue;
1451  }
1452 
1453  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
1454  {
1455  $tmp_user = self::getCachedUserInstance($id);
1456  $addresses[] = $tmp_user->getEmail();
1457  continue;
1458  }
1459  }
1460  else
1461  {
1462  // Roles
1463  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1464  foreach($role_ids as $role_id)
1465  {
1466  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1467  {
1468  $tmp_user = self::getCachedUserInstance($usr_id);
1469  $addresses[] = $tmp_user->getEmail();
1470  }
1471  }
1472  }
1473  }
1474  }
1475  }
1476  else
1477  {
1478  $tmp_rcp = $this->explodeRecipients($a_rcp, $this->getUsePear());
1479 
1480  foreach ($tmp_rcp as $rcp)
1481  {
1482  // NO GROUP
1483  if (substr($rcp,0,1) != '#')
1484  {
1485  if (strpos($rcp,'@'))
1486  {
1487  $addresses[] = $rcp;
1488  continue;
1489  }
1490 
1491  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1492  {
1493  $tmp_user = self::getCachedUserInstance($id);
1494  $addresses[] = $tmp_user->getEmail();
1495  continue;
1496  }
1497  }
1498  else
1499  {
1500  // GROUP THINGS
1501  include_once("./classes/class.ilObjectFactory.php");
1502  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1503 
1504  // Fix
1505  foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($rcp,1)))) as $ref_id)
1506  {
1507  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1508  break;
1509  }
1510  // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
1511  foreach ($grp_object->getGroupMemberIds() as $id)
1512  {
1513  $tmp_user = self::getCachedUserInstance($id);
1514  $addresses[] = $tmp_user->getEmail();
1515  }
1516  }
1517  }
1518  }
1519 
1520  return $addresses;
1521  }
1522 
1530  function checkRecipients($a_recipients,$a_type)
1531  {
1532  global $rbacsystem,$rbacreview;
1533  $wrong_rcps = '';
1534 
1535  $this->validatePear($a_recipients);
1536  if (ilMail::_usePearMail() && $this->getUsePear())
1537  {
1538  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1539 
1540  if (is_a($tmp_rcp, 'PEAR_Error'))
1541  {
1542  $colon_pos = strpos($tmp_rcp->message, ':');
1543  $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
1544  }
1545  else
1546  {
1547  foreach ($tmp_rcp as $rcp)
1548  {
1549  // NO ROLE MAIL ADDRESS
1550  if (substr($rcp->mailbox,0,1) != '#')
1551  {
1552  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1553  $user_id = ($rcp->host == 'ilias') ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
1554  if ($user_id == false && $rcp->host == 'ilias')
1555  {
1556  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
1557  continue;
1558  }
1559 
1560  // CHECK IF USER CAN RECEIVE MAIL
1561  if ($user_id)
1562  {
1563  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1564  {
1565  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1566  " (".$this->lng->txt("user_cant_receive_mail").")";
1567  continue;
1568  }
1569  }
1570  }
1571  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
1572  {
1573  if (!$this->mlists->mailingListExists($rcp->mailbox))
1574  {
1575  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1576  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1577  }
1578 
1579  continue;
1580  }
1581  else
1582  {
1583 
1584  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1585 
1586  if(!$this->mail_to_global_roles && is_array($role_ids))
1587  {
1588  foreach($role_ids as $role_id)
1589  {
1590  if($rbacreview->isGlobalRole($role_id))
1591  {
1592  include_once('Services/Mail/exceptions/class.ilMailException.php');
1593  throw new ilMailException('mail_to_global_roles_not_allowed');
1594 
1595  }
1596  }
1597  }
1598  if (count($role_ids) == 0)
1599  {
1600  $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
1601  ' ('.$this->lng->txt('mail_no_recipient_found').')';
1602  continue;
1603  }
1604  else if (count($role_ids) > 1)
1605  {
1606  $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
1607  ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
1608  }
1609  }
1610  }
1611  }
1612  }
1613  else // NO PEAR
1614  {
1615  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1616 
1617  foreach ($tmp_rcp as $rcp)
1618  {
1619  if (empty($rcp))
1620  {
1621  continue;
1622  }
1623  // NO GROUP
1624  if (substr($rcp,0,1) != '#')
1625  {
1626  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1627  if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
1628  !ilUtil::is_email($rcp))
1629  {
1630  $wrong_rcps .= "<br />".htmlentities($rcp);
1631  continue;
1632  }
1633 
1634  // CHECK IF USER CAN RECEIVE MAIL
1635  if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1636  {
1637  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1638  {
1639  $wrong_rcps .= "<br />".htmlentities($rcp).
1640  " (".$this->lng->txt("user_cant_receive_mail").")";
1641  continue;
1642  }
1643  }
1644  }
1645  else if (substr($rcp, 0, 7) == '#il_ml_')
1646  {
1647  if (!$this->mlists->mailingListExists($rcp))
1648  {
1649  $wrong_rcps .= "<br />".htmlentities($rcp).
1650  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1651  }
1652 
1653  continue;
1654  }
1655  else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
1656  {
1657  continue;
1658  }
1659  else if (!$rbacreview->roleExists(addslashes(substr($rcp,1))))
1660  {
1661  $wrong_rcps .= "<br />".htmlentities($rcp).
1662  " (".$this->lng->txt("mail_no_valid_group_role").")";
1663  continue;
1664  }
1665  else if (!$this->mail_to_global_roles)
1666  {
1667  $role_id = $rbacreview->roleExists(addslashes(substr($rcp,1)));
1668  if((int)$role_id && $rbacreview->isGlobalRole($role_id))
1669  {
1670  include_once('Services/Mail/exceptions/class.ilMailException.php');
1671  throw new ilMailException('mail_to_global_roles_not_allowed');
1672  }
1673  }
1674  }
1675  }
1676  return $wrong_rcps;
1677  }
1678 
1694  function savePostData($a_user_id,
1695  $a_attachments,
1696  $a_rcp_to,
1697  $a_rcp_cc,
1698  $a_rcp_bcc,
1699  $a_m_type,
1700  $a_m_email,
1701  $a_m_subject,
1702  $a_m_message,
1703  $a_use_placeholders)
1704  {
1705  global $ilDB;
1706 
1707 
1708  if(!$a_attachments) $a_attachments = NULL;
1709  if(!$a_rcp_to) $a_rcp_to = NULL;
1710  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1711  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1712  if(!$a_m_type) $a_m_type = NULL;
1713  if(!$a_m_email) $a_m_email = NULL;
1714  if(!$a_m_message) $a_m_message = NULL;
1715  if(!$a_use_placeholders) $a_use_placeholders = '0';
1716 
1717 
1718  $statement = $ilDB->manipulateF('
1719  DELETE FROM '. $this->table_mail_saved .'
1720  WHERE user_id = %s',
1721  array('integer'), array($this->user_id));
1722 
1723  $ilDB->insert($this->table_mail_saved, array(
1724  'user_id' => array('integer', $a_user_id),
1725  'attachments' => array('clob', serialize($a_attachments)),
1726  'rcp_to' => array('clob', $a_rcp_to),
1727  'rcp_cc' => array('clob', $a_rcp_cc),
1728  'rcp_bcc' => array('clob', $a_rcp_bcc),
1729  'm_type' => array('text', serialize($a_m_type)),
1730  'm_email' => array('integer', $a_m_email),
1731  'm_subject' => array('text', $a_m_subject),
1732  'm_message' => array('clob', $a_m_message),
1733  'use_placeholders' => array('integer', $a_use_placeholders),
1734  ));
1735 
1736  $this->getSavedData();
1737 
1738  return true;
1739  }
1740 
1746  function getSavedData()
1747  {
1748  global $ilDB;
1749 
1750  $res = $ilDB->queryf('
1751  SELECT * FROM '. $this->table_mail_saved .'
1752  WHERE user_id = %s',
1753  array('integer'),
1754  array($this->user_id));
1755 
1756  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
1757 
1758  return $this->mail_data;
1759  }
1760 
1774  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)
1775  {
1776  global $lng,$rbacsystem,$log;
1777  //$log->write('class.ilMail.sendMail '.$a_rcp_to.' '.$a_m_subject);
1778 
1779  $this->mail_to_global_roles = true;
1780  if($this->user_id != ANONYMOUS_USER_ID)
1781  {
1782  $this->mail_to_global_roles = $rbacsystem->checkAccessOfUser($this->user_id, 'mail_to_global_roles', $this->mail_obj_ref_id);
1783  }
1784 
1785  $error_message = '';
1786  $message = '';
1787 
1788  if (in_array("system",$a_type))
1789  {
1790  $this->__checkSystemRecipients($a_rcp_to);
1791  $a_type = array('system');
1792  }
1793 
1794  if ($a_attachment)
1795  {
1796  if (!$this->mfile->checkFilesExist($a_attachment))
1797  {
1798  return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
1799  }
1800  }
1801  // CHECK NECESSARY MAIL DATA FOR ALL TYPES
1802  if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
1803  {
1804  return $error_message;
1805  }
1806 
1807  try
1808  {
1809  // check recipients
1810  if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
1811  {
1812  $message .= $error_message;
1813  }
1814 
1815  if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
1816  {
1817  $message .= $error_message;
1818  }
1819 
1820  if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
1821  {
1822  $message .= $error_message;
1823  }
1824  }
1825 
1826  catch(ilMailException $e)
1827  {
1828  return $this->lng->txt($e->getMessage());
1829  }
1830 
1831  // if there was an error
1832  if (!empty($message))
1833  {
1834  return $this->lng->txt("mail_following_rcp_not_valid").$message;
1835  }
1836 
1837  // CHECK FOR SYSTEM MAIL
1838  if (in_array('system',$a_type))
1839  {
1840  if (!empty($a_attachment))
1841  {
1842  return $lng->txt("mail_no_attach_allowed");
1843  }
1844  }
1845 
1846  // ACTIONS FOR ALL TYPES
1847  // GET RCPT OF MAILING LISTS
1848  $rcp_to = $this->parseRcptOfMailingLists($a_rcp_to);
1849  $rcp_cc = $this->parseRcptOfMailingLists($a_rcp_cc);
1850  $rcp_bc = $this->parseRcptOfMailingLists($a_rcp_bc);
1851 
1852  if (! ilMail::_usePearMail() )
1853  {
1854  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1855  $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
1856  $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
1857  $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
1858  }
1859 
1860  // COUNT EMAILS
1861  $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
1862  $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
1863 
1864  // currently disabled..
1865  /*
1866  if (count($c_emails))
1867  {
1868  if (!$this->getEmailOfSender())
1869  {
1870  return $lng->txt("mail_check_your_email_addr");
1871  }
1872  }
1873  */
1874 
1875  // check smtp permission
1876  if($c_emails && $this->user_id != ANONYMOUS_USER_ID &&
1877  !$rbacsystem->checkAccessOfUser($this->user_id, 'smtp_mail', $this->mail_obj_ref_id))
1878  {
1879  return $this->lng->txt('mail_no_permissions_write_smtp');
1880  }
1881 
1882  if($this->appendInstallationSignature())
1883  {
1884  $a_m_message .= self::_getInstallationSignature();
1885  }
1886 
1887  // save mail in sent box
1888  $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
1889  $a_m_subject,$a_m_message);
1890 
1891  if($a_attachment)
1892  {
1893  $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
1894 
1895  if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
1896  {
1897  return $error;
1898  }
1899  }
1900 
1901  // FILTER EMAILS
1902  // IF EMAIL RECIPIENT
1903  if($c_emails)
1904  {
1905  $this->sendMimeMail($this->__getEmailRecipients($rcp_to),
1906  $this->__getEmailRecipients($rcp_cc),
1907  $this->__getEmailRecipients($rcp_bc),
1908  $a_m_subject,
1909  $a_m_message,
1910  $a_attachment,
1911  0);
1912  }
1913 
1914  if (in_array('system',$a_type))
1915  {
1916  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))
1917  {
1918  return $lng->txt("mail_send_error");
1919  }
1920  }
1921  // ACTIONS FOR TYPE SYSTEM AND NORMAL
1922  if (in_array('normal',$a_type))
1923  {
1924  // TRY BOTH internal and email (depends on user settings)
1925  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))
1926  {
1927  return $lng->txt("mail_send_error");
1928  }
1929  }
1930 
1931  // Temporary bugfix
1932  if (!$this->getSaveInSentbox())
1933  {
1934  $this->deleteMails(array($sent_id));
1935  }
1936 
1937  return '';
1938  }
1939 
1940  function parseRcptOfMailingLists($rcpt = '')
1941  {
1942  if ($rcpt == '') return $rcpt;
1943 //@todo check rcp pear validation
1944  $arrRcpt = $this->explodeRecipients(trim($rcpt));
1945  if (!is_array($arrRcpt) || empty($arrRcpt)) return $rcpt;
1946 
1947  $new_rcpt = array();
1948 
1949  foreach ($arrRcpt as $item)
1950  {
1951  if (ilMail::_usePearMail())
1952  {
1953  if (substr($item->mailbox, 0, 7) == '#il_ml_')
1954  {
1955  if ($this->mlists->mailingListExists($item->mailbox))
1956  {
1957  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1958  {
1959  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1960  }
1961  }
1962  }
1963  else
1964  {
1965  $new_rcpt[] = $item->mailbox.'@'.$item->host;
1966  }
1967  }
1968  else
1969  {
1970  if (substr($item, 0, 7) == '#il_ml_')
1971  {
1972  if ($this->mlists->mailingListExists($item))
1973  {
1974  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1975  {
1976  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1977  }
1978  }
1979  }
1980  else
1981  {
1982  $new_rcpt[] = $item;
1983  }
1984  }
1985  }
1986 
1987  return implode(',', $new_rcpt);
1988  }
1989 
2002  function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
2003  $a_m_subject,$a_m_message)
2004  {
2005  include_once "Services/Mail/classes/class.ilMailbox.php";
2006 
2007  $mbox = new ilMailbox($this->user_id);
2008  $sent_id = $mbox->getSentFolder();
2009 
2011  return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
2012  $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
2013  }
2014 
2021  public static function addFullname($a_email, $a_fullname)
2022  {
2023  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2024  return ilMimeMail::_mimeEncode($a_fullname).' <'.$a_email.'>';
2025  }
2026 
2033  public function getMimeMailSender()
2034  {
2038  global $ilUser;
2039 
2040  include_once "Services/Mail/classes/class.ilMimeMail.php";
2041 
2042  if($this->user_id != ANONYMOUS_USER_ID)
2043  {
2044  $email = $ilUser->getEmail();
2045  $fullname = $ilUser->getFullname();
2046  if($ilUser->getId() != $this->user_id)
2047  {
2048  $user = self::getCachedUserInstance($this->user_id);
2049  $email = $user->getEmail();
2050  $fullname = $user->getFullname();
2051  }
2052 
2053  $sender = self::addFullname($email, $fullname);
2054  }
2055  else
2056  {
2057  $sender = self::getIliasMailerAddress();
2058  }
2059 
2060  return $sender;
2061  }
2062 
2072  public static function getIliasMailerAddress()
2073  {
2074  global $ilSetting;
2075 
2076  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2077 
2078  $no_reply_adress = trim($ilSetting->get('mail_external_sender_noreply'));
2079  if(strlen($no_reply_adress))
2080  {
2081  if(strpos($no_reply_adress, '@') === false)
2082  $no_reply_adress = 'noreply@'.$no_reply_adress;
2083 
2084  if(!ilUtil::is_email($no_reply_adress))
2085  {
2086  $no_reply_adress = 'noreply@'.$_SERVER['SERVER_NAME'];
2087  }
2088 
2089  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2090  ' <'.$no_reply_adress.'>';
2091  }
2092  else
2093  {
2094  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2095  ' <noreply@'.$_SERVER['SERVER_NAME'].'>';
2096  }
2097 
2098  return $sender;
2099  }
2100 
2113  function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
2114  {
2115  include_once "Services/Mail/classes/class.ilMimeMail.php";
2116 
2117  #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
2118 
2119  #$inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 4";
2120  #$a_m_subject = "[".$inst_name."] ".$a_m_subject;
2121 
2122  $a_m_subject = self::getSubjectPrefix().' '.$a_m_subject;
2123 
2124  $sender = $this->getMimeMailSender();
2125 
2126  if($this->isSOAPEnabled())
2127  {
2128  // Send per soap
2129  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
2130 
2131  $soap_client = new ilSoapClient();
2132  $soap_client->setResponseTimeout(1);
2133  $soap_client->enableWSDL(true);
2134  $soap_client->init();
2135 
2136  $attachments = array();
2137  $a_attachments = $a_attachments ? $a_attachments : array();
2138  foreach($a_attachments as $attachment)
2139  {
2140  $attachments[] = $this->mfile->getAbsolutePath($attachment);
2141  }
2142  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
2143  $attachments = implode('#:#',$attachments);
2144  // mjansen: use "#:#" as leading delimiter
2145  if(strlen($attachments))
2146  $attachments = "#:#".$attachments;
2147 
2148  $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'], // session id
2149  $a_rcp_to,
2150  $a_rcp_cc,
2151  $a_rcp_bcc,
2152  $sender,
2153  $a_m_subject,
2154  $a_m_message,
2155  $attachments));
2156 
2157  return true;
2158  }
2159  else
2160  {
2161  // send direct
2162  include_once "Services/Mail/classes/class.ilMimeMail.php";
2163 
2164  $mmail = new ilMimeMail();
2165  $mmail->autoCheck(false);
2166  $mmail->From($sender);
2167  $mmail->To($a_rcp_to);
2168  // Add installation name to subject
2169  $mmail->Subject($a_m_subject);
2170  $mmail->Body($a_m_message);
2171 
2172  if ($a_rcp_cc)
2173  {
2174  $mmail->Cc($a_rcp_cc);
2175  }
2176 
2177  if ($a_rcp_bcc)
2178  {
2179  $mmail->Bcc($a_rcp_bcc);
2180  }
2181 
2182  if (is_array($a_attachments))
2183  {
2184  foreach ($a_attachments as $attachment)
2185  {
2186  $mmail->Attach($this->mfile->getAbsolutePath($attachment));
2187  }
2188  }
2189 
2190  $mmail->Send();
2191  }
2192  }
2198  function getEmailOfSender()
2199  {
2200  $umail = self::getCachedUserInstance($this->user_id);
2201  $sender = $umail->getEmail();
2202 
2203  if (ilUtil::is_email($sender))
2204  {
2205  return $sender;
2206  }
2207  else
2208  {
2209  return '';
2210  }
2211  }
2212 
2219  function saveAttachments($a_attachments)
2220  {
2221  global $ilDB;
2222 
2223  $ilDB->update($this->table_mail_saved,
2224  array
2225  (
2226  'attachments' => array('clob', serialize($a_attachments))
2227  ),
2228  array
2229  (
2230  'user_id' => array('integer', $this->user_id)
2231  )
2232  );
2233 
2234  return true;
2235  }
2236 
2242  function getAttachments()
2243  {
2244  return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
2245  }
2246 
2260  function explodeRecipients($a_recipients, $use_pear = true)
2261  {
2262  $a_recipients = trim($a_recipients);
2263 
2264  // WHITESPACE IS NOT ALLOWED AS SEPERATOR
2265  #$a_recipients = preg_replace("/ /",",",$a_recipients);
2266  $a_recipients = preg_replace("/;/",",",$a_recipients);
2267 
2268  if (ilMail::_usePearMail() && $use_pear == true)
2269  {
2270  if (strlen(trim($a_recipients)) > 0)
2271  {
2272  require_once 'Mail/RFC822.php';
2273  $parser = &new Mail_RFC822();
2274  return $parser->parseAddressList($a_recipients, "ilias", false, true);
2275  } else {
2276  return array();
2277  }
2278  }
2279  else
2280  {
2281  foreach(explode(',',$a_recipients) as $tmp_rec)
2282  {
2283  if($tmp_rec)
2284  {
2285  $rcps[] = trim($tmp_rec);
2286  }
2287  }
2288  return is_array($rcps) ? $rcps : array();
2289  }
2290  }
2291 
2292  function __getCountRecipient($rcp,$a_only_email = true)
2293  {
2294  $counter = 0;
2295 
2296  $this->validatePear($rcp);
2297  if (ilMail::_usePearMail() && $this->getUsePear())
2298  {
2299  $tmp_rcp = $this->explodeRecipients($rcp);
2300  if (! is_a($tmp_rcp, 'PEAR_Error'))
2301  {
2302  foreach ($tmp_rcp as $to)
2303  {
2304  if ($a_only_email)
2305  {
2306  // Fixed mantis bug #5875
2307  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2308  {
2309  continue;
2310  }
2311 
2312  // Addresses which aren't on the ilias host, and
2313  // which have a mailbox which does not start with '#',
2314  // are external e-mail addresses
2315  if ($to->host != 'ilias' && substr($to->mailbox,0,1) != '#')
2316  {
2317  ++$counter;
2318  }
2319  }
2320  else
2321  {
2322  ++$counter;
2323  }
2324  }
2325  }
2326  }
2327  else
2328  {
2329  foreach ($this->explodeRecipients($rcp,$this->getUsePear()) as $to)
2330  {
2331  if ($a_only_email)
2332  {
2333  $to = $this->__substituteRecipients($to,"resubstitute");
2334  if (strpos($to,'@'))
2335  {
2336  // Fixed mantis bug #5875
2337  if(ilObjUser::_lookupId($to))
2338  {
2339  continue;
2340  }
2341 
2342  ++$counter;
2343  }
2344  }
2345  else
2346  {
2347  ++$counter;
2348  }
2349  }
2350  }
2351  return $counter;
2352  }
2353 
2354 
2355  function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
2356  {
2357  return $this->__getCountRecipient($a_to,$a_only_email)
2358  + $this->__getCountRecipient($a_cc,$a_only_email)
2359  + $this->__getCountRecipient($a_bcc,$a_only_email);
2360  }
2361 
2362  function __getEmailRecipients($a_rcp)
2363  {
2364  if (ilMail::_usePearMail())
2365  {
2366  $rcp = array();
2367  $tmp_rcp = $this->explodeRecipients($a_rcp);
2368  if (! is_a($tmp_rcp, 'PEAR_Error'))
2369  {
2370  foreach ($tmp_rcp as $to)
2371  {
2372  if(substr($to->mailbox,0,1) != '#' && $to->host != 'ilias')
2373  {
2374  // Fixed mantis bug #5875
2375  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2376  {
2377  continue;
2378  }
2379 
2380  $rcp[] = $to->mailbox.'@'.$to->host;
2381  }
2382  }
2383  }
2384  return implode(',',$rcp);
2385  }
2386  else
2387  {
2388  foreach ($this->explodeRecipients($a_rcp) as $to)
2389  {
2390  $to = $this->__substituteRecipients($to,"resubstitute");
2391  if(strpos($to,'@'))
2392  {
2393  // Fixed mantis bug #5875
2394  if(ilObjUser::_lookupId($to))
2395  {
2396  continue;
2397  }
2398 
2399  $rcp[] = $to;
2400  }
2401  }
2402  return implode(',',$rcp ? $rcp : array());
2403  }
2404  }
2405 
2406  function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
2407  {
2408  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
2409 
2410  $message = $inst_name." To:".$rcp_to."\n";
2411 
2412  if ($rcp_cc)
2413  {
2414  $message .= "Cc: ".$rcp_cc;
2415  }
2416 
2417  $message .= "\n\n";
2418  $message .= $a_m_message;
2419 
2420  return $message;
2421  }
2422 
2423  function __checkSystemRecipients(&$a_rcp_to)
2424  {
2425  if (preg_match("/@all/",$a_rcp_to))
2426  {
2427  // GET ALL LOGINS
2428  $all = ilObjUser::_getAllUserLogins($this->ilias);
2429  $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
2430  }
2431 
2432  return;
2433  }
2434 
2442  function __substituteRecipients($a_rcp,$direction)
2443  {
2444  $new_name = array();
2445 
2446  $tmp_names = $this->explodeRecipients($a_rcp);
2447 
2448 
2449  foreach($tmp_names as $name)
2450  {
2451  if(strpos($name,"#") === 0)
2452  {
2453  $new_name[] = $name;
2454  continue;
2455  }
2456  switch($direction)
2457  {
2458  case "substitute":
2459  if(strpos($name,"@") and ilObjUser::_loginExists($name))
2460  {
2461  $new_name[] = preg_replace("/@/","�#�",$name);
2462  }
2463  else
2464  {
2465  $new_name[] = $name;
2466  }
2467  break;
2468 
2469  case "resubstitute":
2470  if(stristr($name,"�#�"))
2471  {
2472  $new_name[] = preg_replace("/�#�/","@",$name);
2473  }
2474  else
2475  {
2476  $new_name[] = $name;
2477  }
2478  break;
2479  }
2480  }
2481  return implode(",",$new_name);
2482  }
2483 
2499  public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null)
2500  {
2501  if (ilMail::_usePearMail())
2502  {
2503  if ($login == null)
2504  {
2505  require_once './Services/User/classes/class.ilObjUser.php';
2507  $login = $usr_obj->getLogin();
2508  $firstname = $usr_obj->getFirstname();
2509  $lastname = $usr_obj->getLastname();
2510  }
2511  // The following line of code creates a properly formatted mailbox
2512  // address. Unfortunately, it does not work, because ILIAS removes
2513  // everything between '<' '>' characters
2514  // Therefore, we just return the login - sic.
2515  // FIXME - Make this work in a future release
2516  /*
2517  return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
2518  */
2519  return $login.'hhho';
2520  }
2521  else
2522  {
2523  return $login;
2524  }
2525  }
2532  public static function _usePearMail()
2533  {
2534  global $ilias;
2535 
2536  $result = false;
2537  if ($ilias->getSetting('pear_mail_enable', 0))
2538  {
2539  // Note: We use the include statement to determine whether PEAR MAIL is
2540  // installed. We use the @ operator to prevent PHP from issuing a
2541  // warning while we test for PEAR MAIL.
2542  $is_pear_mail_installed = @include_once 'Mail/RFC822.php';
2543  if($is_pear_mail_installed == false)
2544  {
2545  //try_use_ilias_pear
2546  $is_pear_mail_installed = @include_once './Services/PEAR/lib/Mail/RFC822.php';
2547  }
2548 
2549  if ($is_pear_mail_installed)
2550  {
2551  $result = true;
2552  }
2553  else
2554  {
2555  // Disable Pear Mail, when we detect that it is not
2556  // installed
2557  global $log;
2558  $log->write("WARNING: ilMail::_userPearMail disabled Pear Mail support, because include 'Mail/RFC822.php' failed.");
2559  $ilias->setSetting('pear_mail_enable', 0);
2560  }
2561  }
2562  return $result;
2563  }
2564 
2573  public static function _getAutoGeneratedMessageString($lang = null)
2574  {
2575  global $ilSetting;
2576 
2577  if(!$lang)
2578  {
2579  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
2581  }
2582  $http_path = ilUtil::_getHttpPath();
2583 
2584  $lang->loadLanguageModule('mail');
2585  return sprintf($lang->txt('mail_auto_generated_info'),
2586  $ilSetting->get('inst_name','ILIAS 4'),
2587  $http_path."\n\n");
2588  }
2589 
2598  public static function _getIliasMailerName()
2599  {
2603  global $ilSetting;
2604 
2605  if(strlen($ilSetting->get('short_inst_name')))
2606  {
2607  return $ilSetting->get('short_inst_name');
2608  }
2609 
2610  return 'ILIAS';
2611  }
2612 
2621  public function appendInstallationSignature($a_flag = null)
2622  {
2623  if(null === $a_flag) {
2625  }
2626 
2627  $this->appendInstallationSignature = $a_flag;
2628 
2629  return $this;
2630  }
2631 
2640  public static function _getInstallationSignature()
2641  {
2642  global $ilClientIniFile;
2643 
2644  $signature = "\n\n* * * * *\n";
2645  $signature .= $ilClientIniFile->readVariable('client', 'name')."\n";
2646  if(strlen($desc = $ilClientIniFile->readVariable('client', 'description')))
2647  {
2648  $signature .= $desc."\n";
2649  }
2650 
2651  $signature .= ilUtil::_getHttpPath();
2652 
2653  $clientdirs = glob(ILIAS_WEB_DIR."/*", GLOB_ONLYDIR);
2654  if(is_array($clientdirs) && count($clientdirs) > 1)
2655  {
2656  $signature .= '/?client_id='.CLIENT_ID;
2657  }
2658 
2659  $signature .= "\n\n";
2660 
2661  return $signature;
2662  }
2663 
2668  public static function getSubjectPrefix()
2669  {
2670  global $ilSetting;
2671  static $prefix = null;
2672 
2673  return $prefix == null ? $ilSetting->get('mail_subject_prefix','') : $prefix;
2674  }
2675 
2681  public static function getSalutation($a_usr_id,$a_language = null)
2682  {
2683  global $lng;
2684 
2685  $lang = $a_language ? $a_language : $lng;
2686 
2687  $gender = ilObjUser::_lookupGender($a_usr_id);
2688  $gender = $gender ? $gender : 'n';
2689  $name = ilObjUser::_lookupName($a_usr_id);
2690 
2691  if(!strlen($name['firstname']))
2692  {
2693  return $lang->txt('mail_salutation_anonymous').',';
2694  }
2695 
2696  return $lang->txt('mail_salutation_'.$gender).' '.
2697  ($name['title'] ? $name['title'].' ' : '').
2698  ($name['firstname'] ? $name['firstname'].' ' : '').
2699  $name['lastname'].',';
2700  }
2701 
2702  private function setUsePear($bool)
2703  {
2704  $this->use_pear = $bool;
2705  }
2706 
2707  private function getUsePear()
2708  {
2709  return $this->use_pear;
2710  }
2711 
2712  // Force fallback for sending mails via ILIAS, if internal Pear-Validation returns PEAR_Error
2717  private function validatePear($a_recipients)
2718  {
2719  if(ilMail::_usePearMail())
2720  {
2721  $this->setUsePear(true);
2722  $tmp_names = $this->explodeRecipients($a_recipients, true);
2723  if(is_a($tmp_names, 'PEAR_Error'))
2724  {
2725  $this->setUsePear(false);
2726  }
2727  }
2728  else
2729  {
2730  $this->setUsePear(false);
2731  }
2732  }
2733 
2742  protected static function getCachedUserInstance($a_usr_id)
2743  {
2744  if(isset(self::$userInstances[$a_usr_id]))
2745  {
2746  return self::$userInstances[$a_usr_id];
2747  }
2748 
2749  self::$userInstances[$a_usr_id] = new ilObjUser($a_usr_id);
2750  return self::$userInstances[$a_usr_id];
2751  }
2752 } // END class.ilMail
2753 ?>