ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMail.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
128 require_once "Services/User/classes/class.ilObjUser.php";
129 
130 class ilMail
131 {
138  var $ilias;
139 
145  var $lng;
146 
152  var $mfile;
153 
155 
161  var $user_id;
162 
169 
176 
183 
190 
191 
198 
205 
212 
224 
225  var $soap_enabled = true;
227 
228  private $use_pear = true;
229 
230  protected $mlists = null;
231 
232  protected $appendInstallationSignature = false;
233 
240  function ilMail($a_user_id)
241  {
242  require_once "classes/class.ilFileDataMail.php";
243  require_once "Services/Mail/classes/class.ilMailOptions.php";
244  require_once "Services/Contact/classes/class.ilMailingLists.php";
245 
246  global $ilias, $lng, $ilUser;
247 
248  $lng->loadLanguageModule("mail");
249 
250  // Initiate variables
251  $this->ilias =& $ilias;
252  $this->lng =& $lng;
253  $this->table_mail = 'mail';
254  $this->table_mail_saved = 'mail_saved';
255  $this->user_id = $a_user_id;
256  $this->mfile =& new ilFileDataMail($this->user_id);
257  $this->mail_options =& new ilMailOptions($a_user_id);
258  if(is_object($ilUser))
259  {
260  $this->mlists = new ilMailingLists($ilUser);
261  }
262 
263  // DEFAULT: sent mail aren't stored insentbox of user.
264  $this->setSaveInSentbox(false);
265 
266  // GET REFERENCE ID OF MAIL OBJECT
267  $this->readMailObjectReferenceId();
268 
269  }
270 
271  public function doesRecipientStillExists($a_recipient, $a_existing_recipients)
272  {
273  if(self::_usePearMail())
274  {
275  $recipients = $this->explodeRecipients($a_existing_recipients);
276  if(is_a($recipients, 'PEAR_Error'))
277  {
278  return false;
279  }
280  else
281  {
282  foreach($recipients as $rcp)
283  {
284  if (substr($rcp->mailbox, 0, 1) != '#')
285  {
286  if(trim($rcp->mailbox) == trim($a_recipient) ||
287  trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
288  {
289  return true;
290  }
291  }
292  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
293  {
294  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
295  {
296  return true;
297  }
298  }
299  else
300  {
301  if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
302  {
303  return true;
304  }
305  }
306  }
307  }
308  }
309  else
310  {
311  $recipients = $this->explodeRecipients($a_existing_recipients);
312  if(count($recipients))
313  {
314  foreach($recipients as $recipient)
315  {
316  if(trim($recipient) == trim($a_recipient))
317  {
318  return true;
319  }
320  }
321  }
322  }
323 
324  return false;
325  }
326 
334  function enableSOAP($a_status)
335  {
336  $this->soap_enabled = $a_status;
337  }
338  function isSOAPEnabled()
339  {
340  global $ilSetting;
341 
342  if(!extension_loaded('curl') || !$ilSetting->get('soap_user_administration'))
343  {
344  return false;
345  }
346 
347  return (bool) $this->soap_enabled;
348  }
349 
350 
351  function setSaveInSentbox($a_save_in_sentbox)
352  {
353  $this->save_in_sentbox = $a_save_in_sentbox;
354  }
355 
356  function getSaveInSentbox()
357  {
358  return $this->save_in_sentbox;
359  }
360 
366  function setMailSendType($a_types)
367  {
368  $this->mail_send_type = $a_types;
369  }
370 
376  function setMailRcpTo($a_rcp_to)
377  {
378  $this->mail_rcp_to = $a_rcp_to;
379  }
380 
386  function setMailRcpCc($a_rcp_cc)
387  {
388  $this->mail_rcp_cc = $a_rcp_cc;
389  }
390 
396  function setMailRcpBc($a_rcp_bc)
397  {
398  $this->mail_rcp_bc = $a_rcp_bc;
399  }
400 
406  function setMailSubject($a_subject)
407  {
408  $this->mail_subject = $a_subject;
409  }
410 
416  function setMailMessage($a_message)
417  {
418  $this->mail_message = $a_message;
419  }
420 
426  {
427  global $ilDB;
428 
429  // mail settings id is set by a constant in ilias.ini. Keep the select for some time until everyone has updated his ilias.ini
430  if (!MAIL_SETTINGS_ID)
431  {
432  $res = $ilDB->queryf('
433  SELECT object_reference.ref_id FROM object_reference, tree, object_data
434  WHERE tree.parent = %s
435  AND object_data.type = %s
436  AND object_reference.ref_id = tree.child
437  AND object_reference.obj_id = object_data.obj_id',
438  array('integer', 'text'),
439  array(SYSTEM_FOLDER_ID, 'mail'));
440 
441  while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
442  {
443  $this->mail_obj_ref_id = $row["ref_id"];
444  }
445  }
446  else
447  {
448  $this->mail_obj_ref_id = MAIL_SETTINGS_ID;
449  }
450  }
451 
453  {
454  return $this->mail_obj_ref_id;
455  }
456 
467  public function formatNamesForOutput($users = '')
468  {
469  $users = trim($users);
470  if($users)
471  {
472  if(strstr($users, ','))
473  {
474  $rcp_to_array = array();
475 
476  $recipients = explode(',', $users);
477  foreach($recipients as $recipient)
478  {
479  $recipient = trim($recipient);
480  if($uid = ilObjUser::_lookupId($recipient))
481  {
482  $tmp_obj = new ilObjUser($uid);
483 
484  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
485  {
486  $rcp_to_array[] = $tmp_obj->getFullname().' ['.$recipient.']';
487  }
488  else
489  {
490  $rcp_to_array[] = $recipient;
491  }
492  unset($tmp_obj);
493  }
494  else
495  {
496  $rcp_to_array[] = $recipient;
497  }
498  }
499 
500  return trim(implode(', ', $rcp_to_array));
501  }
502  else
503  {
504  if($uid = ilObjUser::_lookupId($users))
505  {
506  $tmp_obj = new ilObjUser($uid);
507  if (in_array(ilObjUser::_lookupPref($uid, 'public_profile'), array("y", "g")))
508  {
509  return $tmp_obj->getFullname().' ['.$users.']';
510  }
511  else
512  {
513  unset($tmp_obj);
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 
857  function deleteMails($a_mail_ids)
858  {
859  global $ilDB;
860 
861  foreach ($a_mail_ids as $id)
862  {
863  $statement = $ilDB->manipulateF("
864  DELETE FROM ". $this->table_mail ."
865  WHERE user_id = %s
866  AND mail_id = %s ",
867  array('integer', 'integer'),
868  array($this->user_id, $id));
869 
870  $this->mfile->deassignAttachmentFromDirectory($id);
871  }
872 
873  return true;
874  }
875 
882  function fetchMailData($a_row)
883  {
884  if (!$a_row) return;
885 
886  return array(
887  "mail_id" => $a_row->mail_id,
888  "user_id" => $a_row->user_id,
889  "folder_id" => $a_row->folder_id,
890  "sender_id" => $a_row->sender_id,
891  "attachments" => unserialize(stripslashes($a_row->attachments)),
892  "send_time" => $a_row->send_time,
893  "rcp_to" => $a_row->rcp_to,
894  "rcp_cc" => $a_row->rcp_cc,
895  "rcp_bcc" => $a_row->rcp_bcc,
896  "m_status" => $a_row->m_status,
897  "m_type" => unserialize(stripslashes($a_row->m_type)),
898  "m_email" => $a_row->m_email,
899  "m_subject" => $a_row->m_subject,
900  "m_message" => $a_row->m_message,
901  "import_name" => $a_row->import_name,
902  "use_placeholders"=> $a_row->use_placeholders);
903  }
904 
905  function updateDraft($a_folder_id,
906  $a_attachments,
907  $a_rcp_to,
908  $a_rcp_cc,
909  $a_rcp_bcc,
910  $a_m_type,
911  $a_m_email,
912  $a_m_subject,
913  $a_m_message,
914  $a_draft_id = 0, $a_use_placeholders = 0)
915  {
916  global $ilDB;
917 
918  $ilDB->update($this->table_mail,
919  array(
920  'folder_id' => array('integer', $a_folder_id),
921  'attachments' => array('clob', serialize($a_attachments)),
922  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
923  'rcp_to' => array('clob', $a_rcp_to),
924  'rcp_cc' => array('clob', $a_rcp_cc),
925  'rcp_bcc' => array('clob', $a_rcp_bcc),
926  'm_status' => array('text', 'read'),
927  'm_type' => array('text', serialize($a_m_type)),
928  'm_email' => array('integer', $a_m_email),
929  'm_subject' => array('text', $a_m_subject),
930  'm_message' => array('clob', $a_m_message),
931  'use_placeholders' => array('integer', $a_use_placeholders)
932  ),
933  array(
934  'mail_id' => array('integer', $a_draft_id)
935  )
936  );
937 
938  return $a_draft_id;
939  }
940 
958  function sendInternalMail($a_folder_id,
959  $a_sender_id,
960  $a_attachments,
961  $a_rcp_to,
962  $a_rcp_cc,
963  $a_rcp_bcc,
964  $a_status,
965  $a_m_type,
966  $a_m_email,
967  $a_m_subject,
968  $a_m_message,
969  $a_user_id = 0, $a_use_placeholders = 0)
970  {
971  $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
972 
973  global $ilDB, $log;
974  //$log->write('class.ilMail->sendInternalMail to user_id:'.$a_rcp_to.' '.$a_m_message);
975 
976  if ($a_use_placeholders)
977  {
978  $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
979  }
980  else
981  {
982  $a_use_placeholders = '0';
983  }
984 
985 
986  if(!$a_user_id) $a_user_id = '0';
987  if(!$a_folder_id) $a_folder_id = '0';
988  if(!$a_sender_id) $a_sender_id = NULL;
989  if(!$a_attachments) $a_attachments = NULL;
990  if(!$a_rcp_to) $a_rcp_to = NULL;
991  if(!$a_rcp_cc) $a_rcp_cc = NULL;
992  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
993  if(!$a_status) $a_status = NULL;
994  if(!$a_m_type) $a_m_type = NULL;
995  if(!$a_m_email) $a_m_email = NULL;
996  if(!$a_m_subject) $a_m_subject = NULL;
997  if(!$a_m_message) $a_m_message = NULL;
998 
999 
1000  $next_id = $ilDB->nextId($this->table_mail);
1001 
1002  $ilDB->insert($this->table_mail, array(
1003  'mail_id' => array('integer', $next_id),
1004  'user_id' => array('integer', $a_user_id),
1005  'folder_id' => array('integer', $a_folder_id),
1006  'sender_id' => array('integer', $a_sender_id),
1007  'attachments' => array('clob', serialize($a_attachments)),
1008  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
1009  'rcp_to' => array('clob', $a_rcp_to),
1010  'rcp_cc' => array('clob', $a_rcp_cc),
1011  'rcp_bcc' => array('clob', $a_rcp_bcc),
1012  'm_status' => array('text', $a_status),
1013  'm_type' => array('text', serialize($a_m_type)),
1014  'm_email' => array('integer', $a_m_email),
1015  'm_subject' => array('text', $a_m_subject),
1016  'm_message' => array('clob', $a_m_message)
1017  ));
1018 
1019  return $next_id; //$ilDB->getLastInsertId();
1020 
1021  }
1022 
1023  function replacePlaceholders($a_message, $a_user_id)
1024  {
1025  global $lng;
1026 
1027  $user = new ilObjUser($a_user_id);
1028 
1029  // determine salutation
1030  switch ($user->getGender())
1031  {
1032  case 'f': $gender_salut = $lng->txt('salutation_f');
1033  break;
1034  case 'm': $gender_salut = $lng->txt('salutation_m');
1035  break;
1036  }
1037 
1038  $a_message = str_replace('[MAIL_SALUTATION]', $gender_salut, $a_message);
1039  $a_message = str_replace('[LOGIN]', $user->getLogin(), $a_message);
1040  $a_message = str_replace('[FIRST_NAME]', $user->getFirstname(), $a_message);
1041  $a_message = str_replace('[LAST_NAME]', $user->getLastname(), $a_message);
1042  $a_message = str_replace('[ILIAS_URL]', ILIAS_HTTP_PATH.'/login.php?client_id='.CLIENT_ID, $a_message);
1043  $a_message = str_replace('[CLIENT_NAME]', CLIENT_NAME, $a_message);
1044 
1045  unset($user);
1046 
1047  return $a_message;
1048  }
1049 
1063  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)
1064  {
1065  global $log;
1066 
1067  include_once 'Services/Mail/classes/class.ilMailbox.php';
1068  include_once './Services/User/classes/class.ilObjUser.php';
1069 
1070  if (!ilMail::_usePearMail())
1071  {
1072  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1073  $a_rcp_to = $this->__substituteRecipients($a_rcp_to, 'resubstitute');
1074  $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc, 'resubstitute');
1075  $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc, 'resubstitute');
1076  }
1077 
1078  $mbox =& new ilMailbox();
1079 
1080  if (!$a_use_placeholders) # No Placeholders
1081  {
1082  $rcp_ids = $this->getUserIds(trim($a_rcp_to).','.trim($a_rcp_cc).','.trim($a_rcp_bcc));
1083 
1084  $as_email = array();
1085 
1086  foreach($rcp_ids as $id)
1087  {
1088  $tmp_mail_options =& new ilMailOptions($id);
1089 
1090  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1091  $tmp_user =& new ilObjUser($id);
1092  $tmp_user->read();
1093  $user_is_active = $tmp_user->getActive();
1094  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1095  $tmp_user->checkTimeLimit();
1096 
1097  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1098  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1099  {
1100  continue;
1101  }
1102 
1103  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS/HER MAIL
1104  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1105 
1106  // Do not send external mails to inactive users!!!
1107  if($user_is_active)
1108  {
1109  if (!$user_can_read_internal_mails ||
1110  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1111  {
1112  $as_email[] = $id;
1113  continue;
1114  }
1115 
1116  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1117  {
1118  $as_email[] = $id;
1119  }
1120  }
1121  $mbox->setUserId($id);
1122  $inbox_id = $mbox->getInboxFolder();
1123 
1124  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1125  $a_attachments, $a_rcp_to,
1126  $a_rcp_cc, '', 'unread', $a_type,
1127  0, $a_subject, $a_message, $id, 0);
1128  if ($a_attachments)
1129  {
1130  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1131  }
1132  }
1133 
1134  // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
1135  $to = array();
1136  $bcc = array();
1137 
1138  if (count($as_email) == 1)
1139  {
1140  $to[] = ilObjUser::_lookupEmail($as_email[0]);
1141  }
1142  else
1143  {
1144  foreach ($as_email as $id)
1145  {
1146  $bcc[] = ilObjUser::_lookupEmail($id);
1147  }
1148  }
1149 
1150  if(count($to) > 0 || count($bcc) > 0)
1151  {
1152  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
1153  }
1154  }
1155  else # Use Placeholders
1156  {
1157  // to
1158  $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
1159 
1160  // cc / bcc
1161  $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
1162 
1163  $as_email = array();
1164 
1165  // to
1166  foreach($rcp_ids_replace as $id)
1167  {
1168  $tmp_mail_options =& new ilMailOptions($id);
1169 
1170  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1171  $tmp_user =& new ilObjUser($id);
1172  $tmp_user->read();
1173  $user_is_active = $tmp_user->getActive();
1174  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1175  $tmp_user->checkTimeLimit();
1176 
1177  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1178  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1179  {
1180  continue;
1181  }
1182 
1183  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1184  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1185 
1186  // Do not send external mails to inactive users!!!
1187  if($user_is_active)
1188  {
1189  if (!$user_can_read_internal_mails ||
1190  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1191  {
1192  $as_email[] = $id;
1193  continue;
1194  }
1195 
1196  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1197  {
1198  $as_email[] = $id;
1199  }
1200  }
1201  $mbox->setUserId($id);
1202  $inbox_id = $mbox->getInboxFolder();
1203 
1204  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1205  $a_attachments, $a_rcp_to,
1206  $a_rcp_cc, '', 'unread', $a_type,
1207  0, $a_subject, $a_message, $id, 1);
1208  if ($a_attachments)
1209  {
1210  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1211  }
1212  }
1213 
1214  if (count($as_email))
1215  {
1216  foreach ($as_email as $id)
1217  {
1218  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
1219  }
1220  }
1221 
1222  $as_email = array();
1223 
1224  // cc / bcc
1225  foreach($rcp_ids_no_replace as $id)
1226  {
1227  $tmp_mail_options =& new ilMailOptions($id);
1228 
1229  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1230  $tmp_user =& new ilObjUser($id);
1231  $tmp_user->read();
1232  $user_is_active = $tmp_user->getActive();
1233  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement()
1234  && $tmp_user->checkTimeLimit();
1235 
1236  // Do not send external mails to inactive users!!!
1237  if($user_is_active)
1238  {
1239  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1240  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1241  {
1242  continue;
1243  }
1244 
1245  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1246  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1247  if (!$user_can_read_internal_mails ||
1248  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1249  {
1250  $as_email[] = $id;
1251  continue;
1252  }
1253 
1254  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1255  {
1256  $as_email[] = $id;
1257  }
1258  }
1259  $mbox->setUserId($id);
1260  $inbox_id = $mbox->getInboxFolder();
1261 
1262  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1263  $a_attachments, $a_rcp_to,
1264  $a_rcp_cc, '', 'unread', $a_type,
1265  0, $a_subject, $a_message, $id, 0);
1266  if ($a_attachments)
1267  {
1268  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1269  }
1270  }
1271 
1272  if (count($as_email))
1273  {
1274  foreach ($as_email as $id)
1275  {
1276  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $a_message, $a_attachments);
1277  }
1278  }
1279  }
1280 
1281  return true;
1282  }
1283 
1289  function getUserIds($a_recipients)
1290  {
1291  global $log, $rbacreview;
1292  $ids = array();
1293 
1294  $this->validatePear($a_recipients);
1295  if (ilMail::_usePearMail() && $this->getUsePear() == true)
1296  {
1297  $tmp_names = $this->explodeRecipients($a_recipients);
1298  if (! is_a($tmp_names, 'PEAR_Error'))
1299  {
1300  for ($i = 0;$i < count($tmp_names); $i++)
1301  {
1302  if ( substr($tmp_names[$i]->mailbox,0,1) === '#' ||
1303  (substr($tmp_names[$i]->mailbox,0,1) === '"' &&
1304  substr($tmp_names[$i]->mailbox,1,1) === '#' ) )
1305  {
1306  $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1307  foreach($role_ids as $role_id)
1308  {
1309  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1310  {
1311  $ids[] = $usr_id;
1312  }
1313  }
1314  }
1315  else if (strtolower($tmp_names[$i]->host) == 'ilias')
1316  {
1317  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
1318  {
1319  //$log->write('class.ilMail->getUserIds() recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host.' user_id:'.$id);
1320  $ids[] = $id;
1321  }
1322  else
1323  {
1324  //$log->write('class.ilMail->getUserIds() no user account found for recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1325  }
1326  }
1327  else
1328  {
1329  // Fixed mantis bug #5875
1330  if($id = ilObjUser::_lookupId($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host))
1331  {
1332  $ids[] = $id;
1333  }
1334  else
1335  {
1336  //$log->write('class.ilMail->getUserIds() external recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1337  }
1338  }
1339  }
1340  }
1341  else
1342  {
1343  //$log->write('class.ilMail->getUserIds() illegal recipients:'.$a_recipients);
1344  }
1345  }
1346  else
1347  {
1348  $tmp_names = $this->explodeRecipients($a_recipients, $this->getUsePear());
1349  for ($i = 0;$i < count($tmp_names); $i++)
1350  {
1351  if (substr($tmp_names[$i],0,1) == '#')
1352  {
1353  if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
1354  {
1355  include_once("./classes/class.ilObjectFactory.php");
1356  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1357 
1358  foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1359  {
1360  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1361  break;
1362  }
1363  // STORE MEMBER IDS IN $ids
1364  foreach ($grp_object->getGroupMemberIds() as $id)
1365  {
1366  $ids[] = $id;
1367  }
1368  }
1369  // is role: get role ids
1370  elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
1371  {
1372  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1373  {
1374  $ids[] = $usr_id;
1375  }
1376  }
1377 
1378  }
1379  else if (!empty($tmp_names[$i]))
1380  {
1381  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
1382  {
1383  $ids[] = $id;
1384  }
1385  }
1386  }
1387  }
1388  return array_unique($ids);
1389  }
1390 
1401  function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
1402  {
1403  $error_message = '';
1404 
1405  $a_m_subject = trim($a_m_subject);
1406  $a_rcp_to = trim($a_rcp_to);
1407 
1408  if (empty($a_m_subject))
1409  {
1410  $error_message .= $error_message ? "<br>" : '';
1411  $error_message .= $this->lng->txt("mail_add_subject");
1412  }
1413 
1414  if (empty($a_rcp_to))
1415  {
1416  $error_message .= $error_message ? "<br>" : '';
1417  $error_message .= $this->lng->txt("mail_add_recipient");
1418  }
1419 
1420  return $error_message;
1421  }
1422 
1429  function getEmailsOfRecipients($a_rcp)
1430  {
1431  $addresses = array();
1432 
1433  $this->validatePear($a_rcp);
1434  if (ilMail::_usePearMail() && $this->getUsePear())
1435  {
1436  $tmp_rcp = $this->explodeRecipients($a_rcp);
1437  if (! is_a($tmp_rcp, 'PEAR_Error'))
1438  {
1439  foreach ($tmp_rcp as $rcp)
1440  {
1441  // NO GROUP
1442  if (substr($rcp->mailbox,0,1) != '#')
1443  {
1444  if (strtolower($rcp->host) != 'ilias')
1445  {
1446  $addresses[] = $rcp->mailbox.'@'.$rcp->host;
1447  continue;
1448  }
1449 
1450  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
1451  {
1452  $tmp_user = new ilObjUser($id);
1453  $addresses[] = $tmp_user->getEmail();
1454  continue;
1455  }
1456  }
1457  else
1458  {
1459  // Roles
1460  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1461  foreach($role_ids as $role_id)
1462  {
1463  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1464  {
1465  $tmp_user = new ilObjUser($usr_id);
1466  $addresses[] = $tmp_user->getEmail();
1467  }
1468  }
1469  }
1470  }
1471  }
1472  }
1473  else
1474  {
1475  $tmp_rcp = $this->explodeRecipients($a_rcp, $this->getUsePear());
1476 
1477  foreach ($tmp_rcp as $rcp)
1478  {
1479  // NO GROUP
1480  if (substr($rcp,0,1) != '#')
1481  {
1482  if (strpos($rcp,'@'))
1483  {
1484  $addresses[] = $rcp;
1485  continue;
1486  }
1487 
1488  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1489  {
1490  $tmp_user = new ilObjUser($id);
1491  $addresses[] = $tmp_user->getEmail();
1492  continue;
1493  }
1494  }
1495  else
1496  {
1497  // GROUP THINGS
1498  include_once("./classes/class.ilObjectFactory.php");
1499  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1500 
1501  // Fix
1502  foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1503  {
1504  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1505  break;
1506  }
1507  // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
1508  foreach ($grp_object->getGroupMemberIds() as $id)
1509  {
1510  $tmp_user = new ilObjUser($id);
1511  $addresses[] = $tmp_user->getEmail();
1512  }
1513  }
1514  }
1515  }
1516 
1517  return $addresses;
1518  }
1519 
1527  function checkRecipients($a_recipients,$a_type)
1528  {
1529  global $rbacsystem,$rbacreview;
1530  $wrong_rcps = '';
1531 
1532  $this->validatePear($a_recipients);
1533  if (ilMail::_usePearMail() && $this->getUsePear())
1534  {
1535  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1536 
1537  if (is_a($tmp_rcp, 'PEAR_Error'))
1538  {
1539  $colon_pos = strpos($tmp_rcp->message, ':');
1540  $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
1541  }
1542  else
1543  {
1544  foreach ($tmp_rcp as $rcp)
1545  {
1546  // NO ROLE MAIL ADDRESS
1547  if (substr($rcp->mailbox,0,1) != '#')
1548  {
1549  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1550  $user_id = ($rcp->host == 'ilias') ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
1551  if ($user_id == false && $rcp->host == 'ilias')
1552  {
1553  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
1554  continue;
1555  }
1556 
1557  // CHECK IF USER CAN RECEIVE MAIL
1558  if ($user_id)
1559  {
1560  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1561  {
1562  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1563  " (".$this->lng->txt("user_cant_receive_mail").")";
1564  continue;
1565  }
1566  }
1567  }
1568  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
1569  {
1570  if (!$this->mlists->mailingListExists($rcp->mailbox))
1571  {
1572  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1573  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1574  }
1575 
1576  continue;
1577  }
1578  else
1579  {
1580 
1581  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1582 
1583  if(!$this->mail_to_global_roles && is_array($role_ids))
1584  {
1585  foreach($role_ids as $role_id)
1586  {
1587  if($rbacreview->isGlobalRole($role_id))
1588  {
1589  include_once('Services/Mail/exceptions/class.ilMailException.php');
1590  throw new ilMailException('mail_to_global_roles_not_allowed');
1591 
1592  }
1593  }
1594  }
1595  if (count($role_ids) == 0)
1596  {
1597  $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
1598  ' ('.$this->lng->txt('mail_no_recipient_found').')';
1599  continue;
1600  }
1601  else if (count($role_ids) > 1)
1602  {
1603  $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
1604  ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
1605  }
1606  }
1607  }
1608  }
1609  }
1610  else // NO PEAR
1611  {
1612  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1613 
1614  foreach ($tmp_rcp as $rcp)
1615  {
1616  if (empty($rcp))
1617  {
1618  continue;
1619  }
1620  // NO GROUP
1621  if (substr($rcp,0,1) != '#')
1622  {
1623  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1624  if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
1625  !ilUtil::is_email($rcp))
1626  {
1627  $wrong_rcps .= "<br />".htmlentities($rcp);
1628  continue;
1629  }
1630 
1631  // CHECK IF USER CAN RECEIVE MAIL
1632  if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1633  {
1634  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1635  {
1636  $wrong_rcps .= "<br />".htmlentities($rcp).
1637  " (".$this->lng->txt("user_cant_receive_mail").")";
1638  continue;
1639  }
1640  }
1641  }
1642  else if (substr($rcp, 0, 7) == '#il_ml_')
1643  {
1644  if (!$this->mlists->mailingListExists($rcp))
1645  {
1646  $wrong_rcps .= "<br />".htmlentities($rcp).
1647  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1648  }
1649 
1650  continue;
1651  }
1652  else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
1653  {
1654  continue;
1655  }
1656  else if (!$rbacreview->roleExists(addslashes(substr($rcp,1))))
1657  {
1658  $wrong_rcps .= "<br />".htmlentities($rcp).
1659  " (".$this->lng->txt("mail_no_valid_group_role").")";
1660  continue;
1661  }
1662  else if (!$this->mail_to_global_roles)
1663  {
1664  $role_id = $rbacreview->roleExists(addslashes(substr($rcp,1)));
1665  if((int)$role_id && $rbacreview->isGlobalRole($role_id))
1666  {
1667  include_once('Services/Mail/exceptions/class.ilMailException.php');
1668  throw new ilMailException('mail_to_global_roles_not_allowed');
1669  }
1670  }
1671  }
1672  }
1673  return $wrong_rcps;
1674  }
1675 
1691  function savePostData($a_user_id,
1692  $a_attachments,
1693  $a_rcp_to,
1694  $a_rcp_cc,
1695  $a_rcp_bcc,
1696  $a_m_type,
1697  $a_m_email,
1698  $a_m_subject,
1699  $a_m_message,
1700  $a_use_placeholders)
1701  {
1702  global $ilDB;
1703 
1704 
1705  if(!$a_attachments) $a_attachments = NULL;
1706  if(!$a_rcp_to) $a_rcp_to = NULL;
1707  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1708  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1709  if(!$a_m_type) $a_m_type = NULL;
1710  if(!$a_m_email) $a_m_email = NULL;
1711  if(!$a_m_message) $a_m_message = NULL;
1712  if(!$a_use_placeholders) $a_use_placeholders = '0';
1713 
1714 
1715  $statement = $ilDB->manipulateF('
1716  DELETE FROM '. $this->table_mail_saved .'
1717  WHERE user_id = %s',
1718  array('integer'), array($this->user_id));
1719 
1720  $ilDB->insert($this->table_mail_saved, array(
1721  'user_id' => array('integer', $a_user_id),
1722  'attachments' => array('clob', serialize($a_attachments)),
1723  'rcp_to' => array('clob', $a_rcp_to),
1724  'rcp_cc' => array('clob', $a_rcp_cc),
1725  'rcp_bcc' => array('clob', $a_rcp_bcc),
1726  'm_type' => array('text', serialize($a_m_type)),
1727  'm_email' => array('integer', $a_m_email),
1728  'm_subject' => array('text', $a_m_subject),
1729  'm_message' => array('clob', $a_m_message),
1730  'use_placeholders' => array('integer', $a_use_placeholders),
1731  ));
1732 
1733  $this->getSavedData();
1734 
1735  return true;
1736  }
1737 
1743  function getSavedData()
1744  {
1745  global $ilDB;
1746 
1747  $res = $ilDB->queryf('
1748  SELECT * FROM '. $this->table_mail_saved .'
1749  WHERE user_id = %s',
1750  array('integer'),
1751  array($this->user_id));
1752 
1753  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
1754 
1755  return $this->mail_data;
1756  }
1757 
1771  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)
1772  {
1773  global $lng,$rbacsystem,$log;
1774  //$log->write('class.ilMail.sendMail '.$a_rcp_to.' '.$a_m_subject);
1775 
1776  $this->mail_to_global_roles = true;
1777  if($this->user_id != ANONYMOUS_USER_ID)
1778  {
1779  $this->mail_to_global_roles = $rbacsystem->checkAccessOfUser($this->user_id, 'mail_to_global_roles', $this->mail_obj_ref_id);
1780  }
1781 
1782  $error_message = '';
1783  $message = '';
1784 
1785  if (in_array("system",$a_type))
1786  {
1787  $this->__checkSystemRecipients($a_rcp_to);
1788  $a_type = array('system');
1789  }
1790 
1791  if ($a_attachment)
1792  {
1793  if (!$this->mfile->checkFilesExist($a_attachment))
1794  {
1795  return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
1796  }
1797  }
1798  // CHECK NECESSARY MAIL DATA FOR ALL TYPES
1799  if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
1800  {
1801  return $error_message;
1802  }
1803 
1804  try
1805  {
1806  // check recipients
1807  if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
1808  {
1809  $message .= $error_message;
1810  }
1811 
1812  if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
1813  {
1814  $message .= $error_message;
1815  }
1816 
1817  if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
1818  {
1819  $message .= $error_message;
1820  }
1821  }
1822 
1823  catch(ilMailException $e)
1824  {
1825  return $this->lng->txt($e->getMessage());
1826  }
1827 
1828  // if there was an error
1829  if (!empty($message))
1830  {
1831  return $this->lng->txt("mail_following_rcp_not_valid").$message;
1832  }
1833 
1834  // CHECK FOR SYSTEM MAIL
1835  if (in_array('system',$a_type))
1836  {
1837  if (!empty($a_attachment))
1838  {
1839  return $lng->txt("mail_no_attach_allowed");
1840  }
1841  }
1842 
1843  // ACTIONS FOR ALL TYPES
1844  // GET RCPT OF MAILING LISTS
1845  $rcp_to = $this->parseRcptOfMailingLists($a_rcp_to);
1846  $rcp_cc = $this->parseRcptOfMailingLists($a_rcp_cc);
1847  $rcp_bc = $this->parseRcptOfMailingLists($a_rcp_bc);
1848 
1849  if (! ilMail::_usePearMail())
1850  {
1851  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1852  $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
1853  $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
1854  $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
1855  }
1856 
1857  // COUNT EMAILS
1858  $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
1859  $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
1860 
1861  // currently disabled..
1862  /*
1863  if (count($c_emails))
1864  {
1865  if (!$this->getEmailOfSender())
1866  {
1867  return $lng->txt("mail_check_your_email_addr");
1868  }
1869  }
1870  */
1871 
1872  // check smtp permission
1873  if($c_emails && $this->user_id != ANONYMOUS_USER_ID &&
1874  !$rbacsystem->checkAccessOfUser($this->user_id, 'smtp_mail', $this->mail_obj_ref_id))
1875  {
1876  return $this->lng->txt('mail_no_permissions_write_smtp');
1877  }
1878 
1879  if($this->appendInstallationSignature())
1880  {
1881  $a_m_message .= self::_getInstallationSignature();
1882  }
1883 
1884  // save mail in sent box
1885  $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
1886  $a_m_subject,$a_m_message);
1887 
1888  if($a_attachment)
1889  {
1890  $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
1891 
1892  if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
1893  {
1894  return $error;
1895  }
1896  }
1897 
1898  // FILTER EMAILS
1899  // IF EMAIL RECIPIENT
1900  if($c_emails)
1901  {
1902  $this->sendMimeMail($this->__getEmailRecipients($rcp_to),
1903  $this->__getEmailRecipients($rcp_cc),
1904  $this->__getEmailRecipients($rcp_bc),
1905  $a_m_subject,
1906  $a_m_message,
1907  $a_attachment,
1908  0);
1909  }
1910 
1911  if (in_array('system',$a_type))
1912  {
1913  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))
1914  {
1915  return $lng->txt("mail_send_error");
1916  }
1917  }
1918  // ACTIONS FOR TYPE SYSTEM AND NORMAL
1919  if (in_array('normal',$a_type))
1920  {
1921  // TRY BOTH internal and email (depends on user settings)
1922  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))
1923  {
1924  return $lng->txt("mail_send_error");
1925  }
1926  }
1927 
1928  // Temporary bugfix
1929  if (!$this->getSaveInSentbox())
1930  {
1931  $this->deleteMails(array($sent_id));
1932  }
1933 
1934  return '';
1935  }
1936 
1937  function parseRcptOfMailingLists($rcpt = '')
1938  {
1939  if ($rcpt == '') return $rcpt;
1940 
1941  $arrRcpt = $this->explodeRecipients(trim($rcpt));
1942  if (!is_array($arrRcpt) || empty($arrRcpt)) return $rcpt;
1943 
1944  $new_rcpt = array();
1945 
1946  foreach ($arrRcpt as $item)
1947  {
1948  if (ilMail::_usePearMail())
1949  {
1950  if (substr($item->mailbox, 0, 7) == '#il_ml_')
1951  {
1952  if ($this->mlists->mailingListExists($item->mailbox))
1953  {
1954  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1955  {
1956  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1957  }
1958  }
1959  }
1960  else
1961  {
1962  $new_rcpt[] = $item->mailbox.'@'.$item->host;
1963  }
1964  }
1965  else
1966  {
1967  if (substr($item, 0, 7) == '#il_ml_')
1968  {
1969  if ($this->mlists->mailingListExists($item))
1970  {
1971  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1972  {
1973  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1974  }
1975  }
1976  }
1977  else
1978  {
1979  $new_rcpt[] = $item;
1980  }
1981  }
1982  }
1983 
1984  return implode(',', $new_rcpt);
1985  }
1986 
1999  function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
2000  $a_m_subject,$a_m_message)
2001  {
2002  include_once "Services/Mail/classes/class.ilMailbox.php";
2003 
2004  $mbox = new ilMailbox($this->user_id);
2005  $sent_id = $mbox->getSentFolder();
2006 
2007  return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
2008  $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
2009  }
2010 
2011 
2012 
2018  function addFullname($a_email)
2019  {
2020  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2021 
2022  global $ilUser;
2023 
2024  return ilMimeMail::_mimeEncode($ilUser->getFullname()).' <'.$a_email.'>';
2025  }
2026 
2033  public function getMimeMailSender()
2034  {
2035  include_once "Services/Mail/classes/class.ilMimeMail.php";
2036 
2037  if($this->user_id != ANONYMOUS_USER_ID)
2038  {
2039  $sender = $this->addFullname($this->getEmailOfSender());
2040  }
2041  else
2042  {
2043  $sender = self::getIliasMailerAddress();
2044  }
2045 
2046  return $sender;
2047  }
2048 
2058  public static function getIliasMailerAddress()
2059  {
2060  global $ilSetting;
2061 
2062  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2063 
2064  $no_reply_adress = trim($ilSetting->get('mail_external_sender_noreply'));
2065  if(strlen($no_reply_adress))
2066  {
2067  if(strpos($no_reply_adress, '@') === false)
2068  $no_reply_adress = 'noreply@'.$no_reply_adress;
2069 
2070  if(!ilUtil::is_email($no_reply_adress))
2071  {
2072  $no_reply_adress = 'noreply@'.$_SERVER['SERVER_NAME'];
2073  }
2074 
2075  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2076  ' <'.$no_reply_adress.'>';
2077  }
2078  else
2079  {
2080  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2081  ' <noreply@'.$_SERVER['SERVER_NAME'].'>';
2082  }
2083 
2084  return $sender;
2085  }
2086 
2099  function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
2100  {
2101  include_once "Services/Mail/classes/class.ilMimeMail.php";
2102 
2103  #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
2104 
2105  #$inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 4";
2106  #$a_m_subject = "[".$inst_name."] ".$a_m_subject;
2107 
2108  $a_m_subject = self::getSubjectPrefix().' '.$a_m_subject;
2109 
2110  $sender = $this->getMimeMailSender();
2111 
2112  if($this->isSOAPEnabled())
2113  {
2114  // Send per soap
2115  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
2116 
2117  $soap_client = new ilSoapClient();
2118  $soap_client->setTimeout(1);
2119  $soap_client->setResponseTimeout(1);
2120  $soap_client->enableWSDL(true);
2121  $soap_client->init();
2122 
2123  $attachments = array();
2124  $a_attachments = $a_attachments ? $a_attachments : array();
2125  foreach($a_attachments as $attachment)
2126  {
2127  $attachments[] = $this->mfile->getAbsolutePath($attachment);
2128  }
2129  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
2130  $attachments = implode('#:#',$attachments);
2131  // mjansen: use "#:#" as leading delimiter
2132  if(strlen($attachments))
2133  $attachments = "#:#".$attachments;
2134 
2135  $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'], // session id
2136  $a_rcp_to,
2137  $a_rcp_cc,
2138  $a_rcp_bcc,
2139  $sender,
2140  $a_m_subject,
2141  $a_m_message,
2142  $attachments));
2143 
2144  return true;
2145  }
2146  else
2147  {
2148  // send direct
2149  include_once "Services/Mail/classes/class.ilMimeMail.php";
2150 
2151  $mmail = new ilMimeMail();
2152  $mmail->autoCheck(false);
2153  $mmail->From($sender);
2154  $mmail->To($a_rcp_to);
2155  // Add installation name to subject
2156  $mmail->Subject($a_m_subject);
2157  $mmail->Body($a_m_message);
2158 
2159  if ($a_rcp_cc)
2160  {
2161  $mmail->Cc($a_rcp_cc);
2162  }
2163 
2164  if ($a_rcp_bcc)
2165  {
2166  $mmail->Bcc($a_rcp_bcc);
2167  }
2168 
2169  if (is_array($a_attachments))
2170  {
2171  foreach ($a_attachments as $attachment)
2172  {
2173  $mmail->Attach($this->mfile->getAbsolutePath($attachment));
2174  }
2175  }
2176 
2177  $mmail->Send();
2178  }
2179  }
2185  function getEmailOfSender()
2186  {
2187  $umail = new ilObjUser($this->user_id);
2188  $sender = $umail->getEmail();
2189 
2190  if (ilUtil::is_email($sender))
2191  {
2192  return $sender;
2193  }
2194  else
2195  {
2196  return '';
2197  }
2198  }
2199 
2206  function saveAttachments($a_attachments)
2207  {
2208  global $ilDB;
2209 
2210  $ilDB->update($this->table_mail_saved,
2211  array
2212  (
2213  'attachments' => array('clob', serialize($a_attachments))
2214  ),
2215  array
2216  (
2217  'user_id' => array('integer', $this->user_id)
2218  )
2219  );
2220 
2221  return true;
2222  }
2223 
2229  function getAttachments()
2230  {
2231  return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
2232  }
2233 
2247  function explodeRecipients($a_recipients, $use_pear = true)
2248  {
2249  if (ilMail::_usePearMail() && $use_pear == true)
2250  {
2251  if (strlen(trim($a_recipients)) > 0)
2252  {
2253  require_once 'Mail/RFC822.php';
2254  $parser = &new Mail_RFC822();
2255  return $parser->parseAddressList($a_recipients, "ilias", false, true);
2256  } else {
2257  return array();
2258  }
2259  }
2260  else
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 
2269  foreach(explode(',',$a_recipients) as $tmp_rec)
2270  {
2271  if($tmp_rec)
2272  {
2273  $rcps[] = trim($tmp_rec);
2274  }
2275  }
2276  return is_array($rcps) ? $rcps : array();
2277  }
2278  }
2279 
2280  function __getCountRecipient($rcp,$a_only_email = true)
2281  {
2282  $counter = 0;
2283 
2284  $this->validatePear($rcp);
2285  if (ilMail::_usePearMail() && $this->getUsePear())
2286  {
2287  $tmp_rcp = $this->explodeRecipients($rcp);
2288  if (! is_a($tmp_rcp, 'PEAR_Error'))
2289  {
2290  foreach ($tmp_rcp as $to)
2291  {
2292  if ($a_only_email)
2293  {
2294  // Fixed mantis bug #5875
2295  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2296  {
2297  continue;
2298  }
2299 
2300  // Addresses which aren't on the ilias host, and
2301  // which have a mailbox which does not start with '#',
2302  // are external e-mail addresses
2303  if ($to->host != 'ilias' && substr($to->mailbox,0,1) != '#')
2304  {
2305  ++$counter;
2306  }
2307  }
2308  else
2309  {
2310  ++$counter;
2311  }
2312  }
2313  }
2314  }
2315  else
2316  {
2317  foreach ($this->explodeRecipients($rcp,$this->getUsePear()) as $to)
2318  {
2319  if ($a_only_email)
2320  {
2321  $to = $this->__substituteRecipients($to,"resubstitute");
2322  if (strpos($to,'@'))
2323  {
2324  // Fixed mantis bug #5875
2325  if(ilObjUser::_lookupId($to))
2326  {
2327  continue;
2328  }
2329 
2330  ++$counter;
2331  }
2332  }
2333  else
2334  {
2335  ++$counter;
2336  }
2337  }
2338  }
2339  return $counter;
2340  }
2341 
2342 
2343  function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
2344  {
2345  return $this->__getCountRecipient($a_to,$a_only_email)
2346  + $this->__getCountRecipient($a_cc,$a_only_email)
2347  + $this->__getCountRecipient($a_bcc,$a_only_email);
2348  }
2349 
2350  function __getEmailRecipients($a_rcp)
2351  {
2352  if (ilMail::_usePearMail())
2353  {
2354  $rcp = array();
2355  $tmp_rcp = $this->explodeRecipients($a_rcp);
2356  if (! is_a($tmp_rcp, 'PEAR_Error'))
2357  {
2358  foreach ($tmp_rcp as $to)
2359  {
2360  if(substr($to->mailbox,0,1) != '#' && $to->host != 'ilias')
2361  {
2362  // Fixed mantis bug #5875
2363  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2364  {
2365  continue;
2366  }
2367 
2368  $rcp[] = $to->mailbox.'@'.$to->host;
2369  }
2370  }
2371  }
2372  return implode(',',$rcp);
2373  }
2374  else
2375  {
2376  foreach ($this->explodeRecipients($a_rcp) as $to)
2377  {
2378  $to = $this->__substituteRecipients($to,"resubstitute");
2379  if(strpos($to,'@'))
2380  {
2381  // Fixed mantis bug #5875
2382  if(ilObjUser::_lookupId($to))
2383  {
2384  continue;
2385  }
2386 
2387  $rcp[] = $to;
2388  }
2389  }
2390  return implode(',',$rcp ? $rcp : array());
2391  }
2392  }
2393 
2394  function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
2395  {
2396  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
2397 
2398  $message = $inst_name." To:".$rcp_to."\n";
2399 
2400  if ($rcp_cc)
2401  {
2402  $message .= "Cc: ".$rcp_cc;
2403  }
2404 
2405  $message .= "\n\n";
2406  $message .= $a_m_message;
2407 
2408  return $message;
2409  }
2410 
2411  function __checkSystemRecipients(&$a_rcp_to)
2412  {
2413  if (preg_match("/@all/",$a_rcp_to))
2414  {
2415  // GET ALL LOGINS
2416  $all = ilObjUser::_getAllUserLogins($this->ilias);
2417  $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
2418  }
2419 
2420  return;
2421  }
2422 
2430  function __substituteRecipients($a_rcp,$direction)
2431  {
2432  $new_name = array();
2433 
2434  $tmp_names = $this->explodeRecipients($a_rcp);
2435 
2436 
2437  foreach($tmp_names as $name)
2438  {
2439  if(strpos($name,"#") === 0)
2440  {
2441  $new_name[] = $name;
2442  continue;
2443  }
2444  switch($direction)
2445  {
2446  case "substitute":
2447  if(strpos($name,"@") and ilObjUser::_loginExists($name))
2448  {
2449  $new_name[] = preg_replace("/@/","�#�",$name);
2450  }
2451  else
2452  {
2453  $new_name[] = $name;
2454  }
2455  break;
2456 
2457  case "resubstitute":
2458  if(stristr($name,"�#�"))
2459  {
2460  $new_name[] = preg_replace("/�#�/","@",$name);
2461  }
2462  else
2463  {
2464  $new_name[] = $name;
2465  }
2466  break;
2467  }
2468  }
2469  return implode(",",$new_name);
2470  }
2471 
2487  public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null)
2488  {
2489  if (ilMail::_usePearMail())
2490  {
2491  if ($login == null)
2492  {
2493  require_once './Services/User/classes/class.ilObjUser.php';
2494  $usr_obj = new ilObjUser($usr_id);
2495  $usr_obj->read();
2496  $login = $usr_obj->getLogin();
2497  $firstname = $usr_obj->getFirstname();
2498  $lastname = $usr_obj->getLastname();
2499  }
2500  // The following line of code creates a properly formatted mailbox
2501  // address. Unfortunately, it does not work, because ILIAS removes
2502  // everything between '<' '>' characters
2503  // Therefore, we just return the login - sic.
2504  // FIXME - Make this work in a future release
2505  /*
2506  return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
2507  */
2508  return $login.'hhho';
2509  }
2510  else
2511  {
2512  return $login;
2513  }
2514  }
2521  public static function _usePearMail()
2522  {
2523  global $ilias;
2524 
2525  $result = false;
2526  if ($ilias->getSetting('pear_mail_enable') == true)
2527  {
2528  // Note: We use the include statement to determine whether PEAR MAIL is
2529  // installed. We use the @ operator to prevent PHP from issuing a
2530  // warning while we test for PEAR MAIL.
2531  $is_pear_mail_installed = @include_once 'Mail/RFC822.php';
2532  if ($is_pear_mail_installed) {
2533  $result = true;
2534  } else {
2535  // Disable Pear Mail, when we detect that it is not
2536  // installed
2537  global $log;
2538  $log->write("WARNING: ilMail::_userPearMail disabled Pear Mail support, because include 'Mail/RFC822.php' failed.");
2539  $ilias->setSetting('pear_mail_enable', false);
2540  }
2541  }
2542  return $result;
2543  }
2544 
2553  public static function _getAutoGeneratedMessageString($lang = null)
2554  {
2555  global $ilSetting;
2556 
2557  if(!$lang)
2558  {
2559  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
2561  }
2562  $http_path = ilUtil::_getHttpPath();
2563 
2564  $lang->loadLanguageModule('mail');
2565  return sprintf($lang->txt('mail_auto_generated_info'),
2566  $ilSetting->get('inst_name','ILIAS 4'),
2567  $http_path."\n\n");
2568  }
2569 
2578  public static function _getIliasMailerName()
2579  {
2583  global $ilSetting;
2584 
2585  if(strlen($ilSetting->get('short_inst_name')))
2586  {
2587  return $ilSetting->get('short_inst_name');
2588  }
2589 
2590  return 'ILIAS';
2591  }
2592 
2601  public function appendInstallationSignature($a_flag = null)
2602  {
2603  if(null === $a_flag) {
2605  }
2606 
2607  $this->appendInstallationSignature = $a_flag;
2608 
2609  return $this;
2610  }
2611 
2620  public static function _getInstallationSignature()
2621  {
2622  global $ilClientIniFile;
2623 
2624  $signature = "\n\n* * * * *\n";
2625  $signature .= $ilClientIniFile->readVariable('client', 'name')."\n";
2626  if(strlen($desc = $ilClientIniFile->readVariable('client', 'description')))
2627  {
2628  $signature .= $desc."\n";
2629  }
2630 
2631  $signature .= ilUtil::_getHttpPath();
2632 
2633  $clientdirs = glob(ILIAS_WEB_DIR."/*", GLOB_ONLYDIR);
2634  if(is_array($clientdirs) && count($clientdirs) > 1)
2635  {
2636  $signature .= '/?client_id='.CLIENT_ID;
2637  }
2638 
2639  return $signature;
2640  }
2641 
2646  public static function getSubjectPrefix()
2647  {
2648  global $ilSetting;
2649  static $prefix = null;
2650 
2651  return $prefix == null ? $ilSetting->get('mail_subject_prefix','') : $prefix;
2652  }
2653 
2659  public static function getSalutation($a_usr_id,$a_language = null)
2660  {
2661  global $lng;
2662 
2663  $lang = $a_language ? $a_language : $lng;
2664 
2665  $gender = ilObjUser::_lookupGender($a_usr_id);
2666  $gender = $gender ? $gender : 'n';
2667  $name = ilObjUser::_lookupName($a_usr_id);
2668 
2669  if(!strlen($name['firstname']))
2670  {
2671  return $lang->txt('mail_salutation_anonymous').',';
2672  }
2673 
2674  return $lang->txt('mail_salutation_'.$gender).' '.
2675  ($name['title'] ? $name['title'].' ' : '').
2676  ($name['firstname'] ? $name['firstname'].' ' : '').
2677  $name['lastname'].',';
2678  }
2679 
2680  private function setUsePear($bool)
2681  {
2682  $this->use_pear = $bool;
2683  }
2684  private function getUsePear()
2685  {
2686  return $this->use_pear;
2687  }
2688 
2689  // Force fallbackfor sending mails via ILIAS, if internal Pear-Validation returns PEAR_Error
2694  private function validatePear($a_recipients)
2695  {
2696  if(ilMail::_usePearMail())
2697  {
2698  $this->setUsePear(true);
2699  $tmp_names = $this->explodeRecipients($a_recipients, $this->getUsePear());
2700  if(is_a($tmp_names, 'PEAR_Error'))
2701  {
2702  $this->setUsePear(false);
2703  }
2704  }
2705  else
2706  {
2707  $this->setUsePear(false);
2708  }
2709  }
2710 
2711 
2712 } // END class.ilMail
2713 ?>