ILIAS  eassessment Revision 61809
 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_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1094  $tmp_user->getActive() &&
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  if (!$user_can_read_internal_mails ||
1106  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1107  {
1108  $as_email[] = $id;
1109  continue;
1110  }
1111 
1112  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1113  {
1114  $as_email[] = $id;
1115  }
1116 
1117  $mbox->setUserId($id);
1118  $inbox_id = $mbox->getInboxFolder();
1119 
1120  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1121  $a_attachments, $a_rcp_to,
1122  $a_rcp_cc, '', 'unread', $a_type,
1123  0, $a_subject, $a_message, $id, 0);
1124  if ($a_attachments)
1125  {
1126  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1127  }
1128  }
1129 
1130  // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
1131  $to = array();
1132  $bcc = array();
1133 
1134  if (count($as_email) == 1)
1135  {
1136  $to[] = ilObjUser::_lookupEmail($as_email[0]);
1137  }
1138  else
1139  {
1140  foreach ($as_email as $id)
1141  {
1142  $bcc[] = ilObjUser::_lookupEmail($id);
1143  }
1144  }
1145 
1146  if(count($to) > 0 || count($bcc) > 0)
1147  {
1148  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
1149  }
1150  }
1151  else # Use Placeholders
1152  {
1153  // to
1154  $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
1155 
1156  // cc / bcc
1157  $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
1158 
1159  $as_email = array();
1160 
1161  // to
1162  foreach($rcp_ids_replace as $id)
1163  {
1164  $tmp_mail_options =& new ilMailOptions($id);
1165 
1166  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1167  $tmp_user =& new ilObjUser($id);
1168  $tmp_user->read();
1169  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
1170  $tmp_user->getActive() &&
1171  $tmp_user->checkTimeLimit();
1172 
1173  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1174  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1175  {
1176  continue;
1177  }
1178 
1179  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1180  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1181  if (!$user_can_read_internal_mails ||
1182  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1183  {
1184  $as_email[] = $id;
1185  continue;
1186  }
1187 
1188  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1189  {
1190  $as_email[] = $id;
1191  }
1192 
1193  $mbox->setUserId($id);
1194  $inbox_id = $mbox->getInboxFolder();
1195 
1196  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1197  $a_attachments, $a_rcp_to,
1198  $a_rcp_cc, '', 'unread', $a_type,
1199  0, $a_subject, $a_message, $id, 1);
1200  if ($a_attachments)
1201  {
1202  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1203  }
1204  }
1205 
1206  if (count($as_email))
1207  {
1208  foreach ($as_email as $id)
1209  {
1210  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
1211  }
1212  }
1213 
1214  $as_email = array();
1215 
1216  // cc / bcc
1217  foreach($rcp_ids_no_replace as $id)
1218  {
1219  $tmp_mail_options =& new ilMailOptions($id);
1220 
1221  // DETERMINE IF THE USER CAN READ INTERNAL MAILS
1222  $tmp_user =& new ilObjUser($id);
1223  $tmp_user->read();
1224  $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement()
1225  && $tmp_user->getActive() && $tmp_user->checkTimeLimit();
1226 
1227  // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
1228  if (in_array('system', $a_type) && !$user_can_read_internal_mails)
1229  {
1230  continue;
1231  }
1232 
1233  // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
1234  // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
1235  if (!$user_can_read_internal_mails ||
1236  $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
1237  {
1238  $as_email[] = $id;
1239  continue;
1240  }
1241 
1242  if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
1243  {
1244  $as_email[] = $id;
1245  }
1246 
1247  $mbox->setUserId($id);
1248  $inbox_id = $mbox->getInboxFolder();
1249 
1250  $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
1251  $a_attachments, $a_rcp_to,
1252  $a_rcp_cc, '', 'unread', $a_type,
1253  0, $a_subject, $a_message, $id, 0);
1254  if ($a_attachments)
1255  {
1256  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
1257  }
1258  }
1259 
1260  if (count($as_email))
1261  {
1262  foreach ($as_email as $id)
1263  {
1264  $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $a_message, $a_attachments);
1265  }
1266  }
1267  }
1268 
1269  return true;
1270  }
1271 
1277  function getUserIds($a_recipients)
1278  {
1279  global $log, $rbacreview;
1280  $ids = array();
1281 
1282  $this->validatePear($a_recipients);
1283  if (ilMail::_usePearMail() && $this->getUsePear() == true)
1284  {
1285  $tmp_names = $this->explodeRecipients($a_recipients);
1286  if (! is_a($tmp_names, 'PEAR_Error'))
1287  {
1288  for ($i = 0;$i < count($tmp_names); $i++)
1289  {
1290  if ( substr($tmp_names[$i]->mailbox,0,1) === '#' ||
1291  (substr($tmp_names[$i]->mailbox,0,1) === '"' &&
1292  substr($tmp_names[$i]->mailbox,1,1) === '#' ) )
1293  {
1294  $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1295  foreach($role_ids as $role_id)
1296  {
1297  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1298  {
1299  $ids[] = $usr_id;
1300  }
1301  }
1302  }
1303  else if (strtolower($tmp_names[$i]->host) == 'ilias')
1304  {
1305  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
1306  {
1307  //$log->write('class.ilMail->getUserIds() recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host.' user_id:'.$id);
1308  $ids[] = $id;
1309  }
1310  else
1311  {
1312  //$log->write('class.ilMail->getUserIds() no user account found for recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1313  }
1314  }
1315  else
1316  {
1317  // Fixed mantis bug #5875
1318  if($id = ilObjUser::_lookupId($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host))
1319  {
1320  $ids[] = $id;
1321  }
1322  else
1323  {
1324  //$log->write('class.ilMail->getUserIds() external recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
1325  }
1326  }
1327  }
1328  }
1329  else
1330  {
1331  //$log->write('class.ilMail->getUserIds() illegal recipients:'.$a_recipients);
1332  }
1333  }
1334  else
1335  {
1336  $tmp_names = $this->explodeRecipients($a_recipients, $this->getUsePear());
1337  for ($i = 0;$i < count($tmp_names); $i++)
1338  {
1339  if (substr($tmp_names[$i],0,1) == '#')
1340  {
1341  if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
1342  {
1343  include_once("./classes/class.ilObjectFactory.php");
1344  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1345 
1346  foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1347  {
1348  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1349  break;
1350  }
1351  // STORE MEMBER IDS IN $ids
1352  foreach ($grp_object->getGroupMemberIds() as $id)
1353  {
1354  $ids[] = $id;
1355  }
1356  }
1357  // is role: get role ids
1358  elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
1359  {
1360  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1361  {
1362  $ids[] = $usr_id;
1363  }
1364  }
1365 
1366  }
1367  else if (!empty($tmp_names[$i]))
1368  {
1369  if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
1370  {
1371  $ids[] = $id;
1372  }
1373  }
1374  }
1375  }
1376  return array_unique($ids);
1377  }
1378 
1389  function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
1390  {
1391  $error_message = '';
1392 
1393  $a_m_subject = trim($a_m_subject);
1394  $a_rcp_to = trim($a_rcp_to);
1395 
1396  if (empty($a_m_subject))
1397  {
1398  $error_message .= $error_message ? "<br>" : '';
1399  $error_message .= $this->lng->txt("mail_add_subject");
1400  }
1401 
1402  if (empty($a_rcp_to))
1403  {
1404  $error_message .= $error_message ? "<br>" : '';
1405  $error_message .= $this->lng->txt("mail_add_recipient");
1406  }
1407 
1408  return $error_message;
1409  }
1410 
1417  function getEmailsOfRecipients($a_rcp)
1418  {
1419  $addresses = array();
1420 
1421  $this->validatePear($a_rcp);
1422  if (ilMail::_usePearMail() && $this->getUsePear())
1423  {
1424  $tmp_rcp = $this->explodeRecipients($a_rcp);
1425  if (! is_a($tmp_rcp, 'PEAR_Error'))
1426  {
1427  foreach ($tmp_rcp as $rcp)
1428  {
1429  // NO GROUP
1430  if (substr($rcp->mailbox,0,1) != '#')
1431  {
1432  if (strtolower($rcp->host) != 'ilias')
1433  {
1434  $addresses[] = $rcp->mailbox.'@'.$rcp->host;
1435  continue;
1436  }
1437 
1438  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
1439  {
1440  $tmp_user = new ilObjUser($id);
1441  $addresses[] = $tmp_user->getEmail();
1442  continue;
1443  }
1444  }
1445  else
1446  {
1447  // Roles
1448  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1449  foreach($role_ids as $role_id)
1450  {
1451  foreach($rbacreview->assignedUsers($role_id) as $usr_id)
1452  {
1453  $tmp_user = new ilObjUser($usr_id);
1454  $addresses[] = $tmp_user->getEmail();
1455  }
1456  }
1457  }
1458  }
1459  }
1460  }
1461  else
1462  {
1463  $tmp_rcp = $this->explodeRecipients($a_rcp, $this->getUsePear());
1464 
1465  foreach ($tmp_rcp as $rcp)
1466  {
1467  // NO GROUP
1468  if (substr($rcp,0,1) != '#')
1469  {
1470  if (strpos($rcp,'@'))
1471  {
1472  $addresses[] = $rcp;
1473  continue;
1474  }
1475 
1476  if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1477  {
1478  $tmp_user = new ilObjUser($id);
1479  $addresses[] = $tmp_user->getEmail();
1480  continue;
1481  }
1482  }
1483  else
1484  {
1485  // GROUP THINGS
1486  include_once("./classes/class.ilObjectFactory.php");
1487  include_once('./Modules/Group/classes/class.ilObjGroup.php');
1488 
1489  // Fix
1490  foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
1491  {
1492  $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
1493  break;
1494  }
1495  // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
1496  foreach ($grp_object->getGroupMemberIds() as $id)
1497  {
1498  $tmp_user = new ilObjUser($id);
1499  $addresses[] = $tmp_user->getEmail();
1500  }
1501  }
1502  }
1503  }
1504 
1505  return $addresses;
1506  }
1507 
1515  function checkRecipients($a_recipients,$a_type)
1516  {
1517  global $rbacsystem,$rbacreview;
1518  $wrong_rcps = '';
1519 
1520  $this->validatePear($a_recipients);
1521  if (ilMail::_usePearMail() && $this->getUsePear())
1522  {
1523  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1524 
1525  if (is_a($tmp_rcp, 'PEAR_Error'))
1526  {
1527  $colon_pos = strpos($tmp_rcp->message, ':');
1528  $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
1529  }
1530  else
1531  {
1532  foreach ($tmp_rcp as $rcp)
1533  {
1534  // NO ROLE MAIL ADDRESS
1535  if (substr($rcp->mailbox,0,1) != '#')
1536  {
1537  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1538  $user_id = ($rcp->host == 'ilias') ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
1539  if ($user_id == false && $rcp->host == 'ilias')
1540  {
1541  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
1542  continue;
1543  }
1544 
1545  // CHECK IF USER CAN RECEIVE MAIL
1546  if ($user_id)
1547  {
1548  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1549  {
1550  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1551  " (".$this->lng->txt("user_cant_receive_mail").")";
1552  continue;
1553  }
1554  }
1555  }
1556  else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
1557  {
1558  if (!$this->mlists->mailingListExists($rcp->mailbox))
1559  {
1560  $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
1561  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1562  }
1563 
1564  continue;
1565  }
1566  else
1567  {
1568 
1569  $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
1570 
1571  if(!$this->mail_to_global_roles && is_array($role_ids))
1572  {
1573  foreach($role_ids as $role_id)
1574  {
1575  if($rbacreview->isGlobalRole($role_id))
1576  {
1577  include_once('Services/Mail/exceptions/class.ilMailException.php');
1578  throw new ilMailException('mail_to_global_roles_not_allowed');
1579 
1580  }
1581  }
1582  }
1583  if (count($role_ids) == 0)
1584  {
1585  $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
1586  ' ('.$this->lng->txt('mail_no_recipient_found').')';
1587  continue;
1588  }
1589  else if (count($role_ids) > 1)
1590  {
1591  $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
1592  ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
1593  }
1594  }
1595  }
1596  }
1597  }
1598  else // NO PEAR
1599  {
1600  $tmp_rcp = $this->explodeRecipients($a_recipients, $this->getUsePear());
1601 
1602  foreach ($tmp_rcp as $rcp)
1603  {
1604  if (empty($rcp))
1605  {
1606  continue;
1607  }
1608  // NO GROUP
1609  if (substr($rcp,0,1) != '#')
1610  {
1611  // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
1612  if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
1613  !ilUtil::is_email($rcp))
1614  {
1615  $wrong_rcps .= "<br />".htmlentities($rcp);
1616  continue;
1617  }
1618 
1619  // CHECK IF USER CAN RECEIVE MAIL
1620  if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
1621  {
1622  if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
1623  {
1624  $wrong_rcps .= "<br />".htmlentities($rcp).
1625  " (".$this->lng->txt("user_cant_receive_mail").")";
1626  continue;
1627  }
1628  }
1629  }
1630  else if (substr($rcp, 0, 7) == '#il_ml_')
1631  {
1632  if (!$this->mlists->mailingListExists($rcp))
1633  {
1634  $wrong_rcps .= "<br />".htmlentities($rcp).
1635  " (".$this->lng->txt("mail_no_valid_mailing_list").")";
1636  }
1637 
1638  continue;
1639  }
1640  else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
1641  {
1642  continue;
1643  }
1644  else if (!$rbacreview->roleExists(addslashes(substr($rcp,1))))
1645  {
1646  $wrong_rcps .= "<br />".htmlentities($rcp).
1647  " (".$this->lng->txt("mail_no_valid_group_role").")";
1648  continue;
1649  }
1650  else if (!$this->mail_to_global_roles)
1651  {
1652  $role_id = $rbacreview->roleExists(addslashes(substr($rcp,1)));
1653  if((int)$role_id && $rbacreview->isGlobalRole($role_id))
1654  {
1655  include_once('Services/Mail/exceptions/class.ilMailException.php');
1656  throw new ilMailException('mail_to_global_roles_not_allowed');
1657  }
1658  }
1659  }
1660  }
1661  return $wrong_rcps;
1662  }
1663 
1679  function savePostData($a_user_id,
1680  $a_attachments,
1681  $a_rcp_to,
1682  $a_rcp_cc,
1683  $a_rcp_bcc,
1684  $a_m_type,
1685  $a_m_email,
1686  $a_m_subject,
1687  $a_m_message,
1688  $a_use_placeholders)
1689  {
1690  global $ilDB;
1691 
1692 
1693  if(!$a_attachments) $a_attachments = NULL;
1694  if(!$a_rcp_to) $a_rcp_to = NULL;
1695  if(!$a_rcp_cc) $a_rcp_cc = NULL;
1696  if(!$a_rcp_bcc) $a_rcp_bcc = NULL;
1697  if(!$a_m_type) $a_m_type = NULL;
1698  if(!$a_m_email) $a_m_email = NULL;
1699  if(!$a_m_message) $a_m_message = NULL;
1700  if(!$a_use_placeholders) $a_use_placeholders = '0';
1701 
1702 
1703  $statement = $ilDB->manipulateF('
1704  DELETE FROM '. $this->table_mail_saved .'
1705  WHERE user_id = %s',
1706  array('integer'), array($this->user_id));
1707 
1708  $ilDB->insert($this->table_mail_saved, array(
1709  'user_id' => array('integer', $a_user_id),
1710  'attachments' => array('clob', serialize($a_attachments)),
1711  'rcp_to' => array('clob', $a_rcp_to),
1712  'rcp_cc' => array('clob', $a_rcp_cc),
1713  'rcp_bcc' => array('clob', $a_rcp_bcc),
1714  'm_type' => array('text', serialize($a_m_type)),
1715  'm_email' => array('integer', $a_m_email),
1716  'm_subject' => array('text', $a_m_subject),
1717  'm_message' => array('clob', $a_m_message),
1718  'use_placeholders' => array('integer', $a_use_placeholders),
1719  ));
1720 
1721  $this->getSavedData();
1722 
1723  return true;
1724  }
1725 
1731  function getSavedData()
1732  {
1733  global $ilDB;
1734 
1735  $res = $ilDB->queryf('
1736  SELECT * FROM '. $this->table_mail_saved .'
1737  WHERE user_id = %s',
1738  array('integer'),
1739  array($this->user_id));
1740 
1741  $this->mail_data = $this->fetchMailData($res->fetchRow(DB_FETCHMODE_OBJECT));
1742 
1743  return $this->mail_data;
1744  }
1745 
1759  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)
1760  {
1761  global $lng,$rbacsystem,$log;
1762  //$log->write('class.ilMail.sendMail '.$a_rcp_to.' '.$a_m_subject);
1763 
1764  $this->mail_to_global_roles = $rbacsystem->checkAccess('mail_to_global_roles', $this->mail_obj_ref_id);
1765 
1766  $error_message = '';
1767  $message = '';
1768 
1769  if (in_array("system",$a_type))
1770  {
1771  $this->__checkSystemRecipients($a_rcp_to);
1772  $a_type = array('system');
1773  }
1774 
1775  if ($a_attachment)
1776  {
1777  if (!$this->mfile->checkFilesExist($a_attachment))
1778  {
1779  return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
1780  }
1781  }
1782  // CHECK NECESSARY MAIL DATA FOR ALL TYPES
1783  if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
1784  {
1785  return $error_message;
1786  }
1787 
1788  try
1789  {
1790  // check recipients
1791  if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
1792  {
1793  $message .= $error_message;
1794  }
1795 
1796  if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
1797  {
1798  $message .= $error_message;
1799  }
1800 
1801  if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
1802  {
1803  $message .= $error_message;
1804  }
1805  }
1806 
1807  catch(ilMailException $e)
1808  {
1809  return $this->lng->txt($e->getMessage());
1810  }
1811 
1812  // if there was an error
1813  if (!empty($message))
1814  {
1815  return $this->lng->txt("mail_following_rcp_not_valid").$message;
1816  }
1817 
1818  // CHECK FOR SYSTEM MAIL
1819  if (in_array('system',$a_type))
1820  {
1821  if (!empty($a_attachment))
1822  {
1823  return $lng->txt("mail_no_attach_allowed");
1824  }
1825  }
1826 
1827  // ACTIONS FOR ALL TYPES
1828  // GET RCPT OF MAILING LISTS
1829  $rcp_to = $this->parseRcptOfMailingLists($a_rcp_to);
1830  $rcp_cc = $this->parseRcptOfMailingLists($a_rcp_cc);
1831  $rcp_bc = $this->parseRcptOfMailingLists($a_rcp_bc);
1832 
1833  if (! ilMail::_usePearMail())
1834  {
1835  // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
1836  $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
1837  $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
1838  $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
1839  }
1840 
1841  // COUNT EMAILS
1842  $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
1843  $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
1844 
1845  // currently disabled..
1846  /*
1847  if (count($c_emails))
1848  {
1849  if (!$this->getEmailOfSender())
1850  {
1851  return $lng->txt("mail_check_your_email_addr");
1852  }
1853  }
1854  */
1855 
1856  // check smtp permission
1857  if($c_emails && $this->user_id != ANONYMOUS_USER_ID &&
1858  !$rbacsystem->checkAccess('smtp_mail', $this->mail_obj_ref_id))
1859  {
1860  return $this->lng->txt('mail_no_permissions_write_smtp');
1861  }
1862 
1863  if($this->appendInstallationSignature())
1864  {
1865  $a_m_message .= self::_getInstallationSignature();
1866  }
1867 
1868  // save mail in sent box
1869  $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
1870  $a_m_subject,$a_m_message);
1871 
1872  if($a_attachment)
1873  {
1874  $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
1875 
1876  if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
1877  {
1878  return $error;
1879  }
1880  }
1881 
1882  // FILTER EMAILS
1883  // IF EMAIL RECIPIENT
1884  if($c_emails)
1885  {
1886  $this->sendMimeMail($this->__getEmailRecipients($rcp_to),
1887  $this->__getEmailRecipients($rcp_cc),
1888  $this->__getEmailRecipients($rcp_bc),
1889  $a_m_subject,
1890  $a_m_message,
1891  $a_attachment,
1892  0);
1893  }
1894 
1895  if (in_array('system',$a_type))
1896  {
1897  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))
1898  {
1899  return $lng->txt("mail_send_error");
1900  }
1901  }
1902  // ACTIONS FOR TYPE SYSTEM AND NORMAL
1903  if (in_array('normal',$a_type))
1904  {
1905  // TRY BOTH internal and email (depends on user settings)
1906  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))
1907  {
1908  return $lng->txt("mail_send_error");
1909  }
1910  }
1911 
1912  // Temporary bugfix
1913  if (!$this->getSaveInSentbox())
1914  {
1915  $this->deleteMails(array($sent_id));
1916  }
1917 
1918  return '';
1919  }
1920 
1921  function parseRcptOfMailingLists($rcpt = '')
1922  {
1923  if ($rcpt == '') return $rcpt;
1924 
1925  $arrRcpt = $this->explodeRecipients(trim($rcpt));
1926  if (!is_array($arrRcpt) || empty($arrRcpt)) return $rcpt;
1927 
1928  $new_rcpt = array();
1929 
1930  foreach ($arrRcpt as $item)
1931  {
1932  if (ilMail::_usePearMail())
1933  {
1934  if (substr($item->mailbox, 0, 7) == '#il_ml_')
1935  {
1936  if ($this->mlists->mailingListExists($item->mailbox))
1937  {
1938  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1939  {
1940  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1941  }
1942  }
1943  }
1944  else
1945  {
1946  $new_rcpt[] = $item->mailbox.'@'.$item->host;
1947  }
1948  }
1949  else
1950  {
1951  if (substr($item, 0, 7) == '#il_ml_')
1952  {
1953  if ($this->mlists->mailingListExists($item))
1954  {
1955  foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
1956  {
1957  $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
1958  }
1959  }
1960  }
1961  else
1962  {
1963  $new_rcpt[] = $item;
1964  }
1965  }
1966  }
1967 
1968  return implode(',', $new_rcpt);
1969  }
1970 
1983  function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
1984  $a_m_subject,$a_m_message)
1985  {
1986  include_once "Services/Mail/classes/class.ilMailbox.php";
1987 
1988  $mbox = new ilMailbox($this->user_id);
1989  $sent_id = $mbox->getSentFolder();
1990 
1991  return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
1992  $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
1993  }
1994 
1995 
1996 
2002  function addFullname($a_email)
2003  {
2004  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2005 
2006  global $ilUser;
2007 
2008  return ilMimeMail::_mimeEncode($ilUser->getFullname()).' <'.$a_email.'>';
2009  }
2010 
2017  public function getMimeMailSender()
2018  {
2019  include_once "Services/Mail/classes/class.ilMimeMail.php";
2020 
2021  if($this->user_id != ANONYMOUS_USER_ID)
2022  {
2023  $sender = $this->addFullname($this->getEmailOfSender());
2024  }
2025  else
2026  {
2027  $sender = self::getIliasMailerAddress();
2028  }
2029 
2030  return $sender;
2031  }
2032 
2042  public static function getIliasMailerAddress()
2043  {
2044  global $ilSetting;
2045 
2046  include_once 'Services/Mail/classes/class.ilMimeMail.php';
2047 
2048  $no_reply_adress = trim($ilSetting->get('mail_external_sender_noreply'));
2049  if(strlen($no_reply_adress))
2050  {
2051  if(strpos($no_reply_adress, '@') === false)
2052  $no_reply_adress = 'noreply@'.$no_reply_adress;
2053 
2054  if(!ilUtil::is_email($no_reply_adress))
2055  {
2056  $no_reply_adress = 'noreply@'.$_SERVER['SERVER_NAME'];
2057  }
2058 
2059  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2060  ' <'.$no_reply_adress.'>';
2061  }
2062  else
2063  {
2064  $sender = ilMimeMail::_mimeEncode(self::_getIliasMailerName()).
2065  ' <noreply@'.$_SERVER['SERVER_NAME'].'>';
2066  }
2067 
2068  return $sender;
2069  }
2070 
2083  function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
2084  {
2085  include_once "Services/Mail/classes/class.ilMimeMail.php";
2086 
2087  #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
2088 
2089  #$inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 4";
2090  #$a_m_subject = "[".$inst_name."] ".$a_m_subject;
2091 
2092  $a_m_subject = self::getSubjectPrefix().' '.$a_m_subject;
2093 
2094  $sender = $this->getMimeMailSender();
2095 
2096  if($this->isSOAPEnabled())
2097  {
2098  // Send per soap
2099  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
2100 
2101  $soap_client = new ilSoapClient();
2102  $soap_client->setTimeout(1);
2103  $soap_client->setResponseTimeout(1);
2104  $soap_client->enableWSDL(true);
2105  $soap_client->init();
2106 
2107  $attachments = array();
2108  $a_attachments = $a_attachments ? $a_attachments : array();
2109  foreach($a_attachments as $attachment)
2110  {
2111  $attachments[] = $this->mfile->getAbsolutePath($attachment);
2112  }
2113  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
2114  $attachments = implode('#:#',$attachments);
2115  // mjansen: use "#:#" as leading delimiter
2116  if(strlen($attachments))
2117  $attachments = "#:#".$attachments;
2118 
2119  $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'], // session id
2120  $a_rcp_to,
2121  $a_rcp_cc,
2122  $a_rcp_bcc,
2123  $sender,
2124  $a_m_subject,
2125  $a_m_message,
2126  $attachments));
2127 
2128  return true;
2129  }
2130  else
2131  {
2132  // send direct
2133  include_once "Services/Mail/classes/class.ilMimeMail.php";
2134 
2135  $mmail = new ilMimeMail();
2136  $mmail->autoCheck(false);
2137  $mmail->From($sender);
2138  $mmail->To($a_rcp_to);
2139  // Add installation name to subject
2140  $mmail->Subject($a_m_subject);
2141  $mmail->Body($a_m_message);
2142 
2143  if ($a_rcp_cc)
2144  {
2145  $mmail->Cc($a_rcp_cc);
2146  }
2147 
2148  if ($a_rcp_bcc)
2149  {
2150  $mmail->Bcc($a_rcp_bcc);
2151  }
2152 
2153  if (is_array($a_attachments))
2154  {
2155  foreach ($a_attachments as $attachment)
2156  {
2157  $mmail->Attach($this->mfile->getAbsolutePath($attachment));
2158  }
2159  }
2160 
2161  $mmail->Send();
2162  }
2163  }
2169  function getEmailOfSender()
2170  {
2171  $umail = new ilObjUser($this->user_id);
2172  $sender = $umail->getEmail();
2173 
2174  if (ilUtil::is_email($sender))
2175  {
2176  return $sender;
2177  }
2178  else
2179  {
2180  return '';
2181  }
2182  }
2183 
2190  function saveAttachments($a_attachments)
2191  {
2192  global $ilDB;
2193 
2194  $ilDB->update($this->table_mail_saved,
2195  array
2196  (
2197  'attachments' => array('clob', serialize($a_attachments))
2198  ),
2199  array
2200  (
2201  'user_id' => array('integer', $this->user_id)
2202  )
2203  );
2204 
2205  return true;
2206  }
2207 
2213  function getAttachments()
2214  {
2215  return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
2216  }
2217 
2231  function explodeRecipients($a_recipients, $use_pear = true)
2232  {
2233  if (ilMail::_usePearMail() && $use_pear == true)
2234  {
2235  if (strlen(trim($a_recipients)) > 0)
2236  {
2237  require_once 'Mail/RFC822.php';
2238  $parser = &new Mail_RFC822();
2239  return $parser->parseAddressList($a_recipients, "ilias", false, true);
2240  } else {
2241  return array();
2242  }
2243  }
2244  else
2245  {
2246  $a_recipients = trim($a_recipients);
2247 
2248  // WHITESPACE IS NOT ALLOWED AS SEPERATOR
2249  #$a_recipients = preg_replace("/ /",",",$a_recipients);
2250  $a_recipients = preg_replace("/;/",",",$a_recipients);
2251 
2252 
2253  foreach(explode(',',$a_recipients) as $tmp_rec)
2254  {
2255  if($tmp_rec)
2256  {
2257  $rcps[] = trim($tmp_rec);
2258  }
2259  }
2260  return is_array($rcps) ? $rcps : array();
2261  }
2262  }
2263 
2264  function __getCountRecipient($rcp,$a_only_email = true)
2265  {
2266  $counter = 0;
2267 
2268  $this->validatePear($rcp);
2269  if (ilMail::_usePearMail() && $this->getUsePear())
2270  {
2271  $tmp_rcp = $this->explodeRecipients($rcp);
2272  if (! is_a($tmp_rcp, 'PEAR_Error'))
2273  {
2274  foreach ($tmp_rcp as $to)
2275  {
2276  if ($a_only_email)
2277  {
2278  // Fixed mantis bug #5875
2279  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2280  {
2281  continue;
2282  }
2283 
2284  // Addresses which aren't on the ilias host, and
2285  // which have a mailbox which does not start with '#',
2286  // are external e-mail addresses
2287  if ($to->host != 'ilias' && substr($to->mailbox,0,1) != '#')
2288  {
2289  ++$counter;
2290  }
2291  }
2292  else
2293  {
2294  ++$counter;
2295  }
2296  }
2297  }
2298  }
2299  else
2300  {
2301  foreach ($this->explodeRecipients($rcp,$this->getUsePear()) as $to)
2302  {
2303  if ($a_only_email)
2304  {
2305  $to = $this->__substituteRecipients($to,"resubstitute");
2306  if (strpos($to,'@'))
2307  {
2308  // Fixed mantis bug #5875
2309  if(ilObjUser::_lookupId($to))
2310  {
2311  continue;
2312  }
2313 
2314  ++$counter;
2315  }
2316  }
2317  else
2318  {
2319  ++$counter;
2320  }
2321  }
2322  }
2323  return $counter;
2324  }
2325 
2326 
2327  function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
2328  {
2329  return $this->__getCountRecipient($a_to,$a_only_email)
2330  + $this->__getCountRecipient($a_cc,$a_only_email)
2331  + $this->__getCountRecipient($a_bcc,$a_only_email);
2332  }
2333 
2334  function __getEmailRecipients($a_rcp)
2335  {
2336  if (ilMail::_usePearMail())
2337  {
2338  $rcp = array();
2339  $tmp_rcp = $this->explodeRecipients($a_rcp);
2340  if (! is_a($tmp_rcp, 'PEAR_Error'))
2341  {
2342  foreach ($tmp_rcp as $to)
2343  {
2344  if(substr($to->mailbox,0,1) != '#' && $to->host != 'ilias')
2345  {
2346  // Fixed mantis bug #5875
2347  if(ilObjUser::_lookupId($to->mailbox.'@'.$to->host))
2348  {
2349  continue;
2350  }
2351 
2352  $rcp[] = $to->mailbox.'@'.$to->host;
2353  }
2354  }
2355  }
2356  return implode(',',$rcp);
2357  }
2358  else
2359  {
2360  foreach ($this->explodeRecipients($a_rcp) as $to)
2361  {
2362  $to = $this->__substituteRecipients($to,"resubstitute");
2363  if(strpos($to,'@'))
2364  {
2365  // Fixed mantis bug #5875
2366  if(ilObjUser::_lookupId($to))
2367  {
2368  continue;
2369  }
2370 
2371  $rcp[] = $to;
2372  }
2373  }
2374  return implode(',',$rcp ? $rcp : array());
2375  }
2376  }
2377 
2378  function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
2379  {
2380  $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
2381 
2382  $message = $inst_name." To:".$rcp_to."\n";
2383 
2384  if ($rcp_cc)
2385  {
2386  $message .= "Cc: ".$rcp_cc;
2387  }
2388 
2389  $message .= "\n\n";
2390  $message .= $a_m_message;
2391 
2392  return $message;
2393  }
2394 
2395  function __checkSystemRecipients(&$a_rcp_to)
2396  {
2397  if (preg_match("/@all/",$a_rcp_to))
2398  {
2399  // GET ALL LOGINS
2400  $all = ilObjUser::_getAllUserLogins($this->ilias);
2401  $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
2402  }
2403 
2404  return;
2405  }
2406 
2414  function __substituteRecipients($a_rcp,$direction)
2415  {
2416  $new_name = array();
2417 
2418  $tmp_names = $this->explodeRecipients($a_rcp);
2419 
2420 
2421  foreach($tmp_names as $name)
2422  {
2423  if(strpos($name,"#") === 0)
2424  {
2425  $new_name[] = $name;
2426  continue;
2427  }
2428  switch($direction)
2429  {
2430  case "substitute":
2431  if(strpos($name,"@") and ilObjUser::_loginExists($name))
2432  {
2433  $new_name[] = preg_replace("/@/","�#�",$name);
2434  }
2435  else
2436  {
2437  $new_name[] = $name;
2438  }
2439  break;
2440 
2441  case "resubstitute":
2442  if(stristr($name,"�#�"))
2443  {
2444  $new_name[] = preg_replace("/�#�/","@",$name);
2445  }
2446  else
2447  {
2448  $new_name[] = $name;
2449  }
2450  break;
2451  }
2452  }
2453  return implode(",",$new_name);
2454  }
2455 
2471  public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null)
2472  {
2473  if (ilMail::_usePearMail())
2474  {
2475  if ($login == null)
2476  {
2477  require_once './Services/User/classes/class.ilObjUser.php';
2478  $usr_obj = new ilObjUser($usr_id);
2479  $usr_obj->read();
2480  $login = $usr_obj->getLogin();
2481  $firstname = $usr_obj->getFirstname();
2482  $lastname = $usr_obj->getLastname();
2483  }
2484  // The following line of code creates a properly formatted mailbox
2485  // address. Unfortunately, it does not work, because ILIAS removes
2486  // everything between '<' '>' characters
2487  // Therefore, we just return the login - sic.
2488  // FIXME - Make this work in a future release
2489  /*
2490  return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
2491  */
2492  return $login.'hhho';
2493  }
2494  else
2495  {
2496  return $login;
2497  }
2498  }
2505  public static function _usePearMail()
2506  {
2507  global $ilias;
2508 
2509  $result = false;
2510  if ($ilias->getSetting('pear_mail_enable') == true)
2511  {
2512  // Note: We use the include statement to determine whether PEAR MAIL is
2513  // installed. We use the @ operator to prevent PHP from issuing a
2514  // warning while we test for PEAR MAIL.
2515  $is_pear_mail_installed = @include_once 'Mail/RFC822.php';
2516  if ($is_pear_mail_installed) {
2517  $result = true;
2518  } else {
2519  // Disable Pear Mail, when we detect that it is not
2520  // installed
2521  global $log;
2522  $log->write("WARNING: ilMail::_userPearMail disabled Pear Mail support, because include 'Mail/RFC822.php' failed.");
2523  $ilias->setSetting('pear_mail_enable', false);
2524  }
2525  }
2526  return $result;
2527  }
2528 
2537  public static function _getAutoGeneratedMessageString($lang = null)
2538  {
2539  global $ilSetting;
2540 
2541  if(!$lang)
2542  {
2543  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
2545  }
2546  $http_path = ilUtil::_getHttpPath();
2547 
2548  $lang->loadLanguageModule('mail');
2549  return sprintf($lang->txt('mail_auto_generated_info'),
2550  $ilSetting->get('inst_name','ILIAS 4'),
2551  $http_path."\n\n");
2552  }
2553 
2562  public static function _getIliasMailerName()
2563  {
2564  return 'ILIAS';
2565  }
2566 
2575  public function appendInstallationSignature($a_flag = null)
2576  {
2577  if(null === $a_flag) {
2579  }
2580 
2581  $this->appendInstallationSignature = $a_flag;
2582 
2583  return $this;
2584  }
2585 
2594  public static function _getInstallationSignature()
2595  {
2596  global $ilClientIniFile;
2597 
2598  $signature = "\n\n* * * * *\n";
2599  $signature .= $ilClientIniFile->readVariable('client', 'name')."\n";
2600  if(strlen($desc = $ilClientIniFile->readVariable('client', 'description')))
2601  {
2602  $signature .= $desc."\n";
2603  }
2604 
2605  $signature .= ilUtil::_getHttpPath();
2606 
2607  $clientdirs = glob(ILIAS_WEB_DIR."/*", GLOB_ONLYDIR);
2608  if(is_array($clientdirs) && count($clientdirs) > 1)
2609  {
2610  $signature .= '/?client_id='.CLIENT_ID;
2611  }
2612 
2613  return $signature;
2614  }
2615 
2620  public static function getSubjectPrefix()
2621  {
2622  global $ilSetting;
2623  static $prefix = null;
2624 
2625  return $prefix == null ? $ilSetting->get('mail_subject_prefix','') : $prefix;
2626  }
2627 
2633  public static function getSalutation($a_usr_id,$a_language = null)
2634  {
2635  global $lng;
2636 
2637  $lang = $a_language ? $a_language : $lng;
2638 
2639  $gender = ilObjUser::_lookupGender($a_usr_id);
2640  $gender = $gender ? $gender : 'n';
2641  $name = ilObjUser::_lookupName($a_usr_id);
2642 
2643  if(!strlen($name['firstname']))
2644  {
2645  return $lang->txt('mail_salutation_anonymous').',';
2646  }
2647 
2648  return $lang->txt('mail_salutation_'.$gender).' '.
2649  ($name['title'] ? $name['title'].' ' : '').
2650  ($name['firstname'] ? $name['firstname'].' ' : '').
2651  $name['lastname'].',';
2652  }
2653 
2654  private function setUsePear($bool)
2655  {
2656  $this->use_pear = $bool;
2657  }
2658  private function getUsePear()
2659  {
2660  return $this->use_pear;
2661  }
2662 
2663  // Force fallbackfor sending mails via ILIAS, if internal Pear-Validation returns PEAR_Error
2668  private function validatePear($a_recipients)
2669  {
2670  if(ilMail::_usePearMail())
2671  {
2672  $this->setUsePear(true);
2673  $tmp_names = $this->explodeRecipients($a_recipients, $this->getUsePear());
2674  if(is_a($tmp_names, 'PEAR_Error'))
2675  {
2676  $this->setUsePear(false);
2677  }
2678  }
2679  else
2680  {
2681  $this->setUsePear(false);
2682  }
2683  }
2684 
2685 
2686 } // END class.ilMail
2687 ?>