00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00035 class ilMail
00036 {
00043 var $ilias;
00044
00050 var $lng;
00051
00057 var $mfile;
00058
00059 var $mail_options;
00060
00066 var $user_id;
00067
00073 var $table_mail;
00074
00080 var $table_mail_saved;
00081
00087 var $mail_counter;
00088
00094 var $mail_data;
00095
00096
00102 var $mail_obj_ref_id;
00103
00109 var $mail_send_type;
00110
00116 var $save_in_sentbox;
00117
00123 var $mail_rcp_to;
00124 var $mail_rcp_cc;
00125 var $mail_rcp_bc;
00126 var $mail_subject;
00127 var $mail_message;
00128
00129 var $soap_enabled = true;
00130
00131
00138 function ilMail($a_user_id)
00139 {
00140 require_once "classes/class.ilFileDataMail.php";
00141 require_once "classes/class.ilMailOptions.php";
00142
00143 global $ilias, $lng;
00144
00145 $lng->loadLanguageModule("mail");
00146
00147
00148 $this->ilias =& $ilias;
00149 $this->lng =& $lng;
00150 $this->table_mail = 'mail';
00151 $this->table_mail_saved = 'mail_saved';
00152 $this->user_id = $a_user_id;
00153 $this->mfile =& new ilFileDataMail($this->user_id);
00154 $this->mail_options =& new ilMailOptions($a_user_id);
00155
00156
00157 $this->setSaveInSentbox(false);
00158
00159
00160 $this->readMailObjectReferenceId();
00161
00162 }
00163
00171 function enableSOAP($a_status)
00172 {
00173 $this->soap_enabled = $a_status;
00174 }
00175 function isSOAPEnabled()
00176 {
00177 if(!extension_loaded('curl'))
00178 {
00179 return false;
00180 }
00181 return (bool) $this->soap_enabled;
00182 }
00183
00184
00185 function setSaveInSentbox($a_save_in_sentbox)
00186 {
00187 $this->save_in_sentbox = $a_save_in_sentbox;
00188 }
00189
00190 function getSaveInSentbox()
00191 {
00192 return $this->save_in_sentbox;
00193 }
00194
00200 function setMailSendType($a_types)
00201 {
00202 $this->mail_send_type = $a_types;
00203 }
00204
00210 function setMailRcpTo($a_rcp_to)
00211 {
00212 $this->mail_rcp_to = $a_rcp_to;
00213 }
00214
00220 function setMailRcpCc($a_rcp_cc)
00221 {
00222 $this->mail_rcp_cc = $a_rcp_cc;
00223 }
00224
00230 function setMailRcpBc($a_rcp_bc)
00231 {
00232 $this->mail_rcp_bc = $a_rcp_bc;
00233 }
00234
00240 function setMailSubject($a_subject)
00241 {
00242 $this->mail_subject = $a_subject;
00243 }
00244
00250 function setMailMessage($a_message)
00251 {
00252 $this->mail_message = $a_message;
00253 }
00254
00259 function readMailObjectReferenceId()
00260 {
00261
00262 if (!MAIL_SETTINGS_ID)
00263 {
00264 $query = "SELECT object_reference.ref_id FROM object_reference,tree,object_data ".
00265 "WHERE tree.parent = '".SYSTEM_FOLDER_ID."' ".
00266 "AND object_data.type = 'mail' ".
00267 "AND object_reference.ref_id = tree.child ".
00268 "AND object_reference.obj_id = object_data.obj_id";
00269 $res = $this->ilias->db->query($query);
00270
00271 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00272 {
00273 $this->mail_obj_ref_id = $row["ref_id"];
00274 }
00275 }
00276 else
00277 {
00278 $this->mail_obj_ref_id = MAIL_SETTINGS_ID;
00279 }
00280 }
00281
00287 function getMailObjectReferenceId()
00288 {
00289 return $this->mail_obj_ref_id;
00290 }
00291
00298 function getMailsOfFolder($a_folder_id)
00299 {
00300 $this->mail_counter = array();
00301 $this->mail_counter["read"] = 0;
00302 $this->mail_counter["unread"] = 0;
00303
00304 $query = "SELECT * FROM $this->table_mail ".
00305 "WHERE user_id = $this->user_id ".
00306 "AND folder_id = '".$a_folder_id."' ORDER BY send_time DESC";
00307
00308 $res = $this->ilias->db->query($query);
00309
00310 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00311 {
00312 if($row->sender_id and !ilObjectFactory::ObjectIdExists($row->sender_id))
00313 {
00314 continue;
00315 }
00316 $tmp = $this->fetchMailData($row);
00317
00318 if ($tmp["m_status"] == 'read')
00319 {
00320 ++$this->mail_counter["read"];
00321 }
00322
00323 if ($tmp["m_status"] == 'unread')
00324 {
00325 ++$this->mail_counter["unread"];
00326 }
00327
00328 $output[] = $tmp;
00329 }
00330
00331 $this->mail_counter["total"] = count($output);
00332
00333 return $output ? $output : array();
00334 }
00335
00342 function getMailCounterData()
00343 {
00344 return is_array($this->mail_counter) ? $this->mail_counter : array(
00345 "total" => 0,
00346 "read" => 0,
00347 "unread" => 0);
00348 }
00349
00356 function getMail($a_mail_id)
00357 {
00358 $query = "SELECT * FROM $this->table_mail ".
00359 "WHERE user_id = $this->user_id ".
00360 "AND mail_id = '".$a_mail_id."'";
00361
00362 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00363
00364 return $this->mail_data;
00365 }
00366
00373 function markRead($a_mail_ids)
00374 {
00375
00376 $in = "(". implode(",",$a_mail_ids) . ")";
00377
00378 $query = "UPDATE $this->table_mail ".
00379 "SET m_status = 'read' ".
00380 "WHERE user_id = '".$this->user_id."' ".
00381 "AND mail_id IN $in";
00382
00383 $res = $this->ilias->db->query($query);
00384
00385 return true;
00386 }
00387
00394 function markUnread($a_mail_ids)
00395 {
00396
00397 $in = "(". implode(",",$a_mail_ids) . ")";
00398
00399 $query = "UPDATE $this->table_mail ".
00400 "SET m_status = 'unread' ".
00401 "WHERE user_id = '".$this->user_id."' ".
00402 "AND mail_id IN $in";
00403
00404 $res = $this->ilias->db->query($query);
00405
00406 return true;
00407 }
00408
00416 function moveMailsToFolder($a_mail_ids,$a_folder_id)
00417 {
00418
00419 $in = "(". implode(",",$a_mail_ids) . ")";
00420
00421 $query = "UPDATE $this->table_mail ".
00422 "SET folder_id = '".$a_folder_id."' ".
00423 "WHERE user_id = '".$this->user_id."' ".
00424 "AND mail_id IN $in";
00425
00426 $res = $this->ilias->db->query($query);
00427
00428 return true;
00429 }
00430
00437 function deleteMails($a_mail_ids)
00438 {
00439
00440 foreach ($a_mail_ids as $id)
00441 {
00442 $query = "DELETE FROM $this->table_mail ".
00443 "WHERE user_id = '".$this->user_id."' ".
00444 "AND mail_id = '".$id."'";
00445 $res = $this->ilias->db->query($query);
00446 $this->mfile->deassignAttachmentFromDirectory($id);
00447 }
00448
00449 return true;
00450 }
00451
00458 function fetchMailData($a_row)
00459 {
00460 return array(
00461 "mail_id" => $a_row->mail_id,
00462 "user_id" => $a_row->user_id,
00463 "folder_id" => $a_row->folder_id,
00464 "sender_id" => $a_row->sender_id,
00465 "attachments" => unserialize(stripslashes($a_row->attachments)),
00466 "send_time" => $a_row->send_time,
00467 "rcp_to" => stripslashes($a_row->rcp_to),
00468 "rcp_cc" => stripslashes($a_row->rcp_cc),
00469 "rcp_bcc" => stripslashes($a_row->rcp_bcc),
00470 "m_status" => $a_row->m_status,
00471 "m_type" => unserialize(stripslashes($a_row->m_type)),
00472 "m_email" => $a_row->m_email,
00473 "m_subject" => stripslashes($a_row->m_subject),
00474 "m_message" => stripslashes($a_row->m_message),
00475 "import_name" => stripslashes($a_row->import_name));
00476 }
00477
00478 function updateDraft($a_folder_id,
00479 $a_attachments,
00480 $a_rcp_to,
00481 $a_rcp_cc,
00482 $a_rcp_bcc,
00483 $a_m_type,
00484 $a_m_email,
00485 $a_m_subject,
00486 $a_m_message,
00487 $a_draft_id = 0)
00488 {
00489 $query = "UPDATE $this->table_mail ".
00490 "SET folder_id = '".$a_folder_id."',".
00491 "attachments = '".addslashes(serialize($a_attachments))."',".
00492 "send_time = now(),".
00493 "rcp_to = '".addslashes($a_rcp_to)."',".
00494 "rcp_cc = '".addslashes($a_rcp_cc)."',".
00495 "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00496 "m_status = 'read',".
00497 "m_type = '".addslashes(serialize($a_m_type))."',".
00498 "m_email = '".$a_m_email."',".
00499 "m_subject = '".addslashes($a_m_subject)."',".
00500 "m_message = '".addslashes($a_m_message)."' ".
00501 "WHERE mail_id = '".$a_draft_id."'";
00502
00503
00504 $res = $this->ilias->db->query($query);
00505
00506 return $a_draft_id;
00507 }
00508
00526 function sendInternalMail($a_folder_id,
00527 $a_sender_id,
00528 $a_attachments,
00529 $a_rcp_to,
00530 $a_rcp_cc,
00531 $a_rcp_bcc,
00532 $a_status,
00533 $a_m_type,
00534 $a_m_email,
00535 $a_m_subject,
00536 $a_m_message,
00537 $a_user_id = 0)
00538 {
00539 $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
00540
00541 $query = "INSERT INTO $this->table_mail ".
00542 "SET user_id = '".$a_user_id."',".
00543 "folder_id = '".$a_folder_id."',".
00544 "sender_id = '".$a_sender_id."',".
00545 "attachments = '".addslashes(serialize($a_attachments))."',".
00546 "send_time = now(),".
00547 "rcp_to = '".addslashes($a_rcp_to)."',".
00548 "rcp_cc = '".addslashes($a_rcp_cc)."',".
00549 "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00550 "m_status = '".$a_status."',".
00551 "m_type = '".addslashes(serialize($a_m_type))."',".
00552 "m_email = '".$a_m_email."',".
00553 "m_subject = '".addslashes($a_m_subject)."',".
00554 "m_message = '".addslashes($a_m_message)."'";
00555
00556 $res = $this->ilias->db->query($query);
00557 $query = "SELECT LAST_INSERT_ID() as id";
00558 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_ASSOC);
00559
00560 return $row["id"];
00561 }
00575 function distributeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_subject,$a_message,$a_attachments,$sent_mail_id,$a_type,$a_action)
00576 {
00577 include_once "classes/class.ilMailbox.php";
00578 include_once "./classes/class.ilObjUser.php";
00579
00580
00581 $a_rcp_to = $this->__substituteRecipients($a_rcp_to,"resubstitute");
00582 $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc,"resubstitute");
00583 $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc,"resubstitute");
00584
00585
00586 $as_email = array();
00587
00588 $mbox =& new ilMailbox();
00589
00590 $rcp_ids = $this->getUserIds(trim($a_rcp_to).",".trim($a_rcp_cc).",".trim($a_rcp_bcc));
00591
00592 foreach($rcp_ids as $id)
00593 {
00594 $tmp_mail_options =& new ilMailOptions($id);
00595
00596
00597 if ($tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
00598 {
00599 $as_email[] = $id;
00600 continue;
00601 }
00602
00603 if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
00604 {
00605 $as_email[] = $id;
00606 }
00607
00608 if ($a_action == 'system')
00609 {
00610 $inbox_id = 0;
00611 }
00612 else
00613 {
00614 $mbox->setUserId($id);
00615 $inbox_id = $mbox->getInboxFolder();
00616 }
00617 $mail_id = $this->sendInternalMail($inbox_id,$this->user_id,
00618 $a_attachments,$a_rcp_to,
00619 $a_rcp_cc,'','unread',$a_type,
00620 0,$a_subject,$a_message,$id);
00621 if ($a_attachments)
00622 {
00623 $this->mfile->assignAttachmentsToDirectory($mail_id,$sent_mail_id,$a_attachments);
00624 }
00625 }
00626
00627
00628 $to = array();
00629 $bcc = array();
00630 if (count($as_email) == 1)
00631 {
00632 $to[] = ilObjUser::_lookupEmail($as_email[0]);
00633 }
00634 else
00635 {
00636 foreach ($as_email as $id)
00637 {
00638 $bcc[] = ilObjUser::_lookupEmail($id);
00639 }
00640 }
00641 if(count($to) > 0 || count($bcc) > 0)
00642 {
00643 $this->sendMimeMail(implode(',',$to),'',implode(',',$bcc),$a_subject,$a_message,$a_attachments);
00644 }
00645
00646 return true;
00647 }
00648
00649
00655 function getUserIds($a_recipients)
00656 {
00657 global $rbacreview;
00658
00659 $tmp_names = $this->explodeRecipients($a_recipients);
00660 $ids = array();
00661 for ($i = 0;$i < count($tmp_names); $i++)
00662 {
00663 if (substr($tmp_names[$i],0,1) == '#')
00664 {
00665 if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
00666 {
00667 include_once("./classes/class.ilObjectFactory.php");
00668 include_once('./classes/class.ilObjGroup.php');
00669
00670 foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
00671 {
00672 $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
00673 break;
00674 }
00675
00676 foreach ($grp_object->getGroupMemberIds() as $id)
00677 {
00678 $ids[] = $id;
00679 }
00680 }
00681
00682 elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
00683 {
00684 foreach($rbacreview->assignedUsers($role_id) as $usr_id)
00685 {
00686 $ids[] = $usr_id;
00687 }
00688 }
00689
00690 }
00691 else if (!empty($tmp_names[$i]))
00692 {
00693 if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
00694 {
00695 $ids[] = $id;
00696 }
00697 # else if ($id = ilObjUser::getUserIdByEmail(addslashes($tmp_names[$i])))
00698 # {
00699 # $ids[] = $id;
00700 # }
00701 }
00702 }
00703 return array_unique($ids);
00704 }
00715 function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
00716 {
00717 $error_message = '';
00718
00719 if (empty($a_m_subject))
00720 {
00721 $error_message .= $error_message ? "<br>" : '';
00722 $error_message .= $this->lng->txt("mail_add_subject");
00723 }
00724
00725 if (empty($a_rcp_to))
00726 {
00727 $error_message .= $error_message ? "<br>" : '';
00728 $error_message .= $this->lng->txt("mail_add_recipient");
00729 }
00730
00731 return $error_message;
00732 }
00733
00740 function getEmailsOfRecipients($a_rcp)
00741 {
00742 $addresses = array();
00743
00744 $tmp_rcp = $this->explodeRecipients($a_rcp);
00745
00746 foreach ($tmp_rcp as $rcp)
00747 {
00748
00749 if (substr($rcp,0,1) != '#')
00750 {
00751 if (strpos($rcp,'@'))
00752 {
00753 $addresses[] = $rcp;
00754 continue;
00755 }
00756
00757 if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00758 {
00759 $tmp_user = new ilObjUser($id);
00760 $addresses[] = $tmp_user->getEmail();
00761 continue;
00762 }
00763 }
00764 else
00765 {
00766
00767 include_once("./classes/class.ilObjectFactory.php");
00768 include_once('./classes/class.ilObjGroup.php');
00769
00770
00771 foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
00772 {
00773 $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
00774 break;
00775 }
00776
00777 foreach ($grp_object->getGroupMemberIds() as $id)
00778 {
00779 $tmp_user = new ilObjUser($id);
00780 $addresses[] = $tmp_user->getEmail();
00781 }
00782 }
00783 }
00784
00785 return $addresses;
00786 }
00787
00794 function checkRecipients($a_recipients,$a_type)
00795 {
00796 global $rbacsystem,$rbacreview;
00797
00798 $wrong_rcps = '';
00799
00800 $tmp_rcp = $this->explodeRecipients($a_recipients);
00801
00802 foreach ($tmp_rcp as $rcp)
00803 {
00804 if (empty($rcp))
00805 {
00806 continue;
00807 }
00808
00809 if (substr($rcp,0,1) != '#')
00810 {
00811
00812 if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
00813 !ilUtil::is_email($rcp))
00814 {
00815 $wrong_rcps .= "<BR/>".$rcp;
00816 continue;
00817 }
00818
00819
00820 if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00821 {
00822 if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
00823 {
00824 $wrong_rcps .= "<BR/>".$rcp." (".$this->lng->txt("user_cant_receive_mail").")";
00825 continue;
00826 }
00827 }
00828 }
00829 elseif (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
00830 {
00831 continue;
00832 }
00833 elseif(!$rbacreview->roleExists(addslashes(substr($rcp,1))))
00834 {
00835 $wrong_rcps .= "<BR/>".$rcp." (".$this->lng->txt("mail_no_valid_group_role").")";
00836 continue;
00837 }
00838
00839 }
00840
00841 return $wrong_rcps;
00842 }
00843
00858 function savePostData($a_user_id,
00859 $a_attachments,
00860 $a_rcp_to,
00861 $a_rcp_cc,
00862 $a_rcp_bcc,
00863 $a_m_type,
00864 $a_m_email,
00865 $a_m_subject,
00866 $a_m_message)
00867 {
00868 $query = "DELETE FROM $this->table_mail_saved ".
00869 "WHERE user_id = '".$this->user_id."'";
00870 $res = $this->ilias->db->query($query);
00871
00872 $query = "INSERT INTO $this->table_mail_saved ".
00873 "SET user_id = '".$a_user_id."',".
00874 "attachments = '".addslashes(serialize($a_attachments))."',".
00875 "rcp_to = '".addslashes($a_rcp_to)."',".
00876 "rcp_cc = '".addslashes($a_rcp_cc)."',".
00877 "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00878 "m_type = '".addslashes(serialize($a_m_type))."',".
00879 "m_email = '',".
00880 "m_subject = '".addslashes($a_m_subject)."',".
00881 "m_message = '".addslashes($a_m_message)."'";
00882
00883 $res = $this->ilias->db->query($query);
00884
00885 return true;
00886 }
00887
00893 function getSavedData()
00894 {
00895 $query = "SELECT * FROM $this->table_mail_saved ".
00896 "WHERE user_id = '".$this->user_id."'";
00897
00898 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00899
00900 return $this->mail_data;
00901 }
00902
00916 function sendMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$a_type)
00917 {
00918 global $lng,$rbacsystem;
00919
00920 $error_message = '';
00921 $message = '';
00922
00923 if (in_array("system",$a_type))
00924 {
00925 $this->__checkSystemRecipients($a_rcp_to);
00926 $a_type = array('system');
00927 }
00928
00929 if ($a_attachment)
00930 {
00931 if (!$this->mfile->checkFilesExist($a_attachment))
00932 {
00933 return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
00934 }
00935 }
00936
00937 if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
00938 {
00939 return $error_message;
00940 }
00941
00942 if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
00943 {
00944 $message .= $error_message;
00945 }
00946
00947 if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
00948 {
00949 $message .= $error_message;
00950 }
00951
00952 if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
00953 {
00954 $message .= $error_message;
00955 }
00956
00957 if (!empty($message))
00958 {
00959 return $this->lng->txt("mail_following_rcp_not_valid").$message;
00960 }
00961
00962
00963 if (in_array('system',$a_type))
00964 {
00965 if (!empty($a_attachment))
00966 {
00967 return $lng->txt("mail_no_attach_allowed");
00968 }
00969 }
00970
00971
00972 $a_rcp_to = $this->__substituteRecipients($a_rcp_to,"substitute");
00973 $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc,"substitute");
00974 $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc,"substitute");
00975
00976 $c_emails = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,true);
00977 $c_rcp = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,false);
00978
00979 if (count($c_emails))
00980 {
00981 if (!$this->getEmailOfSender())
00982 {
00983 return $lng->txt("mail_check_your_email_addr");
00984 }
00985 }
00986
00987
00988
00989 $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
00990 $a_m_subject,$a_m_message);
00991 if ($a_attachment)
00992 {
00993 $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
00994
00995 if ($c_emails < $c_rcp)
00996 {
00997 if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
00998 {
00999 return $error;
01000 }
01001 }
01002 }
01003
01004
01005
01006 if ($c_emails)
01007 {
01008 if (!$rbacsystem->checkAccess("smtp_mail",$this->getMailObjectReferenceId()))
01009 {
01010 return $lng->txt("mail_no_permissions_write_smtp");
01011 }
01012 $this->sendMimeMail($this->__getEmailRecipients($a_rcp_to),
01013 $this->__getEmailRecipients($a_rcp_cc),
01014 $this->__getEmailRecipients($a_rcp_bc),
01015 $a_m_subject,
01016 $a_m_message,
01017 $a_attachment);
01018 }
01019
01020 if (in_array('system',$a_type))
01021 {
01022 if (!$this->distributeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$sent_id,$a_type,'system'))
01023 {
01024 return $lng->txt("mail_send_error");
01025 }
01026 }
01027
01028 if (in_array('normal',$a_type))
01029 {
01030
01031 if (!$this->distributeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$sent_id,$a_type,'normal'))
01032 {
01033 return $lng->txt("mail_send_error");
01034 }
01035 }
01036
01037
01038 if (!$this->getSaveInSentbox())
01039 {
01040 $this->deleteMails(array($sent_id));
01041 }
01042
01043 return '';
01044 }
01045
01058 function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
01059 $a_m_subject,$a_m_message)
01060 {
01061 include_once "classes/class.ilMailbox.php";
01062
01063 $mbox = new ilMailbox($this->user_id);
01064 $sent_id = $mbox->getSentFolder();
01065
01066 return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
01067 $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id);
01068 }
01069
01070
01071
01077 function addFullname($a_email)
01078 {
01079 include_once 'classes/class.ilMimeMail.php';
01080
01081 global $ilUser;
01082
01083 return ilMimeMail::_mimeEncode($ilUser->getFullname()).'<'.$a_email.'>';
01084 }
01085
01098 function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
01099 {
01100 #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
01101
01102 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01103 $a_m_subject = "[".$inst_name."] ".$a_m_subject;
01104
01105 if($this->isSOAPEnabled())
01106 {
01107
01108 include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
01109
01110 $soap_client = new ilSoapClient();
01111 $soap_client->setTimeout(1);
01112 $soap_client->setResponseTimeout(1);
01113 $soap_client->enableWSDL(true);
01114 $soap_client->init();
01115
01116 $attachments = array();
01117 foreach($a_attachments as $attachment)
01118 {
01119 $attachments[] = $this->mfile->getAbsolutePath($attachment);
01120 }
01121 $attachments = implode(',',$attachments);
01122
01123 $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'],
01124 $a_rcp_to,
01125 $a_rcp_cc,
01126 $a_rcp_bcc,
01127 $this->addFullname($this->getEmailOfSender()),
01128 $a_m_subject,
01129 $a_m_message,
01130 $attachments));
01131
01132 return true;
01133 }
01134 else
01135 {
01136
01137 include_once "classes/class.ilMimeMail.php";
01138
01139 $sender = $this->addFullname($this->getEmailOfSender());
01140
01141 $mmail = new ilMimeMail();
01142 $mmail->autoCheck(false);
01143 $mmail->From($sender);
01144 $mmail->To($a_rcp_to);
01145
01146 $mmail->Subject($a_m_subject);
01147 $mmail->Body($a_m_message);
01148
01149 if ($a_rcp_cc)
01150 {
01151 $mmail->Cc($a_rcp_cc);
01152 }
01153
01154 if ($a_rcp_bcc)
01155 {
01156 $mmail->Bcc($a_rcp_bcc);
01157 }
01158
01159 foreach ($a_attachments as $attachment)
01160 {
01161 $mmail->Attach($this->mfile->getAbsolutePath($attachment));
01162 }
01163
01164 $mmail->Send();
01165 }
01166 }
01172 function getEmailOfSender()
01173 {
01174 $umail = new ilObjUser($this->user_id);
01175 $sender = $umail->getEmail();
01176
01177 if (ilUtil::is_email($sender))
01178 {
01179 return $sender;
01180 }
01181 else
01182 {
01183 return '';
01184 }
01185 }
01186
01193 function saveAttachments($a_attachments)
01194 {
01195 $query = "UPDATE $this->table_mail_saved ".
01196 "SET attachments = '".addslashes(serialize($a_attachments))."' ".
01197 "WHERE user_id = '".$this->user_id."'";
01198
01199 $res = $this->ilias->db->query($query);
01200
01201 return true;
01202 }
01203
01209 function getAttachments()
01210 {
01211 return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
01212 }
01213
01220 function explodeRecipients($a_recipients)
01221 {
01222 $a_recipients = trim($a_recipients);
01223
01224
01225 #$a_recipients = preg_replace("/ /",",",$a_recipients);
01226 $a_recipients = preg_replace("/;/",",",$a_recipients);
01227
01228
01229 foreach(explode(',',$a_recipients) as $tmp_rec)
01230 {
01231 if($tmp_rec)
01232 {
01233 $rcps[] = trim($tmp_rec);
01234 }
01235 }
01236 return is_array($rcps) ? $rcps : array();
01237
01238 }
01239
01240 function __getCountRecipient($rcp,$a_only_email = true)
01241 {
01242 $counter = 0;
01243
01244 foreach ($this->explodeRecipients($rcp) as $to)
01245 {
01246 if ($a_only_email)
01247 {
01248 if (strpos($to,'@'))
01249 {
01250 ++$counter;
01251 }
01252 }
01253 else
01254 {
01255 ++$counter;
01256 }
01257 }
01258
01259 return $counter;
01260 }
01261
01262
01263 function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
01264 {
01265 return $this->__getCountRecipient($a_to,$a_only_email)
01266 + $this->__getCountRecipient($a_cc,$a_only_email)
01267 + $this->__getCountRecipient($a_bcc,$a_only_email);
01268 }
01269
01270 function __getEmailRecipients($a_rcp)
01271 {
01272 foreach ($this->explodeRecipients($a_rcp) as $to)
01273 {
01274 if(strpos($to,'@'))
01275 {
01276 $rcp[] = $to;
01277 }
01278 }
01279 return implode(',',$rcp ? $rcp : array());
01280 }
01281
01282 function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
01283 {
01284 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01285
01286 $message = $inst_name." To:".$rcp_to."\n";
01287
01288 if ($rcp_cc)
01289 {
01290 $message .= "Cc: ".$rcp_cc;
01291 }
01292
01293 $message .= "\n\n";
01294 $message .= $a_m_message;
01295
01296 return $message;
01297 }
01298
01299 function __checkSystemRecipients(&$a_rcp_to)
01300 {
01301 if (preg_match("/@all/",$a_rcp_to))
01302 {
01303
01304 $all = ilObjUser::_getAllUserLogins($this->ilias);
01305 $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
01306 }
01307
01308 return;
01309 }
01310
01311 function __substituteRecipients($a_rcp,$direction)
01312 {
01313 $new_name = array();
01314
01315 $tmp_names = $this->explodeRecipients($a_rcp);
01316
01317
01318 foreach($tmp_names as $name)
01319 {
01320 if(strpos($name,"#") === 0)
01321 {
01322 $new_name[] = $name;
01323 continue;
01324 }
01325 switch($direction)
01326 {
01327 case "substitute":
01328 if(strpos($name,"@") and loginExists($name))
01329 {
01330 $new_name[] = preg_replace("/@/","�#�",$name);
01331 }
01332 else
01333 {
01334 $new_name[] = $name;
01335 }
01336 break;
01337
01338 case "resubstitute":
01339 if(stristr($name,"�#�"))
01340 {
01341 $new_name[] = preg_replace("/�#�/","@",$name);
01342 }
01343 else
01344 {
01345 $new_name[] = $name;
01346 }
01347 break;
01348 }
01349 }
01350 return implode(",",$new_name);
01351 }
01352 }
01353 ?>