• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilMail.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
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 
00136         function ilMail($a_user_id)
00137         {
00138                 require_once "classes/class.ilFileDataMail.php";
00139                 require_once "classes/class.ilMailOptions.php";
00140 
00141                 global $ilias, $lng;
00142 
00143                 $lng->loadLanguageModule("mail");
00144 
00145                 // Initiate variables
00146                 $this->ilias =& $ilias;
00147                 $this->lng   =& $lng;
00148                 $this->table_mail = 'mail';
00149                 $this->table_mail_saved = 'mail_saved';
00150                 $this->user_id = $a_user_id;
00151                 $this->mfile =& new ilFileDataMail($this->user_id);
00152                 $this->mail_options =& new ilMailOptions($a_user_id);
00153 
00154                 // DEFAULT: sent mail aren't stored insentbox of user.
00155                 $this->setSaveInSentbox(false);
00156 
00157                 // GET REFERENCE ID OF MAIL OBJECT
00158                 $this->readMailObjectReferenceId();
00159 
00160         }
00161 
00162         function setSaveInSentbox($a_save_in_sentbox)
00163         {
00164                 $this->save_in_sentbox = $a_save_in_sentbox;
00165         }
00166 
00167         function getSaveInSentbox()
00168         {
00169                 return $this->save_in_sentbox;
00170         }
00171 
00177         function setMailSendType($a_types)
00178         {
00179                 $this->mail_send_type = $a_types;
00180         }
00181 
00187         function setMailRcpTo($a_rcp_to)
00188         {
00189                 $this->mail_rcp_to = $a_rcp_to;
00190         }
00191 
00197         function setMailRcpCc($a_rcp_cc)
00198         {
00199                 $this->mail_rcp_cc = $a_rcp_cc;
00200         }
00201 
00207         function setMailRcpBc($a_rcp_bc)
00208         {
00209                 $this->mail_rcp_bc = $a_rcp_bc;
00210         }
00211 
00217         function setMailSubject($a_subject)
00218         {
00219                 $this->mail_subject = $a_subject;
00220         }
00221 
00227         function setMailMessage($a_message)
00228         {
00229                 $this->mail_message = $a_message;
00230         }
00231 
00236         function readMailObjectReferenceId()
00237         {
00238                 // mail settings id is set by a constant in ilias.ini. Keep the select for some time until everyone has updated his ilias.ini
00239                 if (!MAIL_SETTINGS_ID)
00240                 {
00241                         $query = "SELECT object_reference.ref_id FROM object_reference,tree,object_data ".
00242                                         "WHERE tree.parent = '".SYSTEM_FOLDER_ID."' ".
00243                                         "AND object_data.type = 'mail' ".
00244                                         "AND object_reference.ref_id = tree.child ".
00245                                         "AND object_reference.obj_id = object_data.obj_id";
00246                         $res = $this->ilias->db->query($query);
00247 
00248                         while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00249                         {
00250                                 $this->mail_obj_ref_id = $row["ref_id"];
00251                         }
00252                 }
00253                 else
00254                 {
00255                         $this->mail_obj_ref_id = MAIL_SETTINGS_ID;
00256                 }
00257         }
00258 
00264         function getMailObjectReferenceId()
00265         {
00266                 return $this->mail_obj_ref_id;
00267         }
00268 
00275         function getMailsOfFolder($a_folder_id)
00276         {
00277                 $this->mail_counter = array();
00278                 $this->mail_counter["read"] = 0;
00279                 $this->mail_counter["unread"] = 0;
00280 
00281                 $query = "SELECT * FROM $this->table_mail ".
00282                         "WHERE user_id = $this->user_id ".
00283                         "AND folder_id = '".$a_folder_id."' ORDER BY send_time DESC";
00284                 
00285                 $res = $this->ilias->db->query($query);
00286 
00287                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00288                 {
00289                         if($row->sender_id and !ilObjectFactory::ObjectIdExists($row->sender_id))
00290                         {
00291                                 continue;
00292                         }
00293                         $tmp = $this->fetchMailData($row);
00294 
00295                         if ($tmp["m_status"] == 'read')
00296                         {
00297                                 ++$this->mail_counter["read"];
00298                         }
00299 
00300                         if ($tmp["m_status"] == 'unread')
00301                         {
00302                                 ++$this->mail_counter["unread"];
00303                         }
00304 
00305                         $output[] = $tmp;
00306                 }
00307 
00308                 $this->mail_counter["total"] = count($output);
00309 
00310                 return $output ? $output : array();
00311         }
00312 
00319         function getMailCounterData()
00320         {
00321                 return is_array($this->mail_counter) ? $this->mail_counter : array(
00322                         "total"  => 0,
00323                         "read"   => 0,
00324                         "unread" => 0);
00325         }
00326 
00333         function getMail($a_mail_id)
00334         {
00335                 $query = "SELECT * FROM $this->table_mail ".
00336                         "WHERE user_id = $this->user_id ".
00337                         "AND mail_id = '".$a_mail_id."'";
00338                 
00339                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00340                 
00341                 return $this->mail_data; 
00342         }
00343 
00350         function markRead($a_mail_ids)
00351         {
00352                 // CREATE IN STATEMENT
00353                 $in = "(". implode(",",$a_mail_ids) . ")";
00354                 
00355                 $query = "UPDATE $this->table_mail ".
00356                         "SET m_status = 'read' ".
00357                         "WHERE user_id = '".$this->user_id."' ".
00358                         "AND mail_id IN $in";
00359 
00360                 $res = $this->ilias->db->query($query);
00361 
00362                 return true;
00363         }
00364 
00371         function markUnread($a_mail_ids)
00372         {
00373                 // CREATE IN STATEMENT
00374                 $in = "(". implode(",",$a_mail_ids) . ")";
00375                 
00376                 $query = "UPDATE $this->table_mail ".
00377                         "SET m_status = 'unread' ".
00378                         "WHERE user_id = '".$this->user_id."' ".
00379                         "AND mail_id IN $in";
00380 
00381                 $res = $this->ilias->db->query($query);
00382 
00383                 return true;
00384         }
00385 
00393         function moveMailsToFolder($a_mail_ids,$a_folder_id)
00394         {
00395                 // CREATE IN STATEMENT
00396                 $in = "(". implode(",",$a_mail_ids) . ")";
00397 
00398                 $query = "UPDATE $this->table_mail ".
00399                         "SET folder_id = '".$a_folder_id."' ".
00400                         "WHERE user_id = '".$this->user_id."' ".
00401                         "AND mail_id IN $in";
00402 
00403                 $res = $this->ilias->db->query($query);
00404 
00405                 return true;
00406         }
00407 
00414         function deleteMails($a_mail_ids)
00415         {
00416 
00417                 foreach ($a_mail_ids as $id)
00418                 {
00419                         $query = "DELETE FROM $this->table_mail ".
00420                                 "WHERE user_id = '".$this->user_id."' ".
00421                                 "AND mail_id = '".$id."'";
00422                         $res = $this->ilias->db->query($query);
00423                         $this->mfile->deassignAttachmentFromDirectory($id);
00424                 }
00425 
00426                 return true;
00427         }
00428 
00435         function fetchMailData($a_row)
00436         {
00437                 return array(
00438                         "mail_id"         => $a_row->mail_id,
00439                         "user_id"         => $a_row->user_id,
00440                         "folder_id"       => $a_row->folder_id,
00441                         "sender_id"       => $a_row->sender_id,
00442                         "attachments"     => unserialize(stripslashes($a_row->attachments)), 
00443                         "send_time"       => $a_row->send_time,
00444                         "rcp_to"          => stripslashes($a_row->rcp_to),
00445                         "rcp_cc"          => stripslashes($a_row->rcp_cc),
00446                         "rcp_bcc"         => stripslashes($a_row->rcp_bcc),
00447                         "m_status"        => $a_row->m_status,
00448                         "m_type"          => unserialize(stripslashes($a_row->m_type)),
00449                         "m_email"         => $a_row->m_email,
00450                         "m_subject"       => stripslashes($a_row->m_subject),
00451                         "m_message"       => stripslashes($a_row->m_message),
00452                         "import_name"     => stripslashes($a_row->import_name));
00453         }
00454 
00455         function updateDraft($a_folder_id,
00456                                                  $a_attachments,
00457                                                  $a_rcp_to,
00458                                                  $a_rcp_cc,
00459                                                  $a_rcp_bcc,
00460                                                  $a_m_type,
00461                                                  $a_m_email,
00462                                                  $a_m_subject,
00463                                                  $a_m_message,
00464                                                  $a_draft_id = 0)
00465         {
00466                 $query = "UPDATE $this->table_mail ".
00467                         "SET folder_id = '".$a_folder_id."',".
00468                         "attachments = '".addslashes(serialize($a_attachments))."',".
00469                         "send_time = now(),".
00470                         "rcp_to = '".addslashes($a_rcp_to)."',".
00471                         "rcp_cc = '".addslashes($a_rcp_cc)."',".
00472                         "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00473                         "m_status = 'read',".
00474                         "m_type = '".addslashes(serialize($a_m_type))."',".
00475                         "m_email = '".$a_m_email."',".
00476                         "m_subject = '".addslashes($a_m_subject)."',".
00477                         "m_message = '".addslashes($a_m_message)."' ".
00478                         "WHERE mail_id = '".$a_draft_id."'";
00479                         
00480 
00481                 $res = $this->ilias->db->query($query);
00482 
00483                 return $a_draft_id;
00484         }
00485 
00503         function sendInternalMail($a_folder_id,
00504                                                           $a_sender_id,
00505                                                           $a_attachments,
00506                                                           $a_rcp_to,
00507                                                           $a_rcp_cc,
00508                                                           $a_rcp_bcc,
00509                                                           $a_status,
00510                                                           $a_m_type,
00511                                                           $a_m_email,
00512                                                           $a_m_subject,
00513                                                           $a_m_message,
00514                                                           $a_user_id = 0)
00515         {
00516                 $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
00517 
00518                 $query = "INSERT INTO $this->table_mail ".
00519                         "SET user_id = '".$a_user_id."',".
00520                         "folder_id = '".$a_folder_id."',".
00521                         "sender_id = '".$a_sender_id."',".
00522                         "attachments = '".addslashes(serialize($a_attachments))."',".
00523                         "send_time = now(),".
00524                         "rcp_to = '".addslashes($a_rcp_to)."',".
00525                         "rcp_cc = '".addslashes($a_rcp_cc)."',".
00526                         "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00527                         "m_status = '".$a_status."',".
00528                         "m_type = '".addslashes(serialize($a_m_type))."',".
00529                         "m_email = '".$a_m_email."',".
00530                         "m_subject = '".addslashes($a_m_subject)."',".
00531                         "m_message = '".addslashes($a_m_message)."'";
00532 
00533                 $res = $this->ilias->db->query($query);
00534                 $query = "SELECT LAST_INSERT_ID() as id FROM $this->table_mail";
00535                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_ASSOC);
00536 
00537                 return $row["id"];
00538         }
00552         function distributeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_subject,$a_message,$a_attachments,$sent_mail_id,$a_type,$a_action)
00553         {
00554                 include_once "classes/class.ilMailbox.php";
00555                 include_once "./classes/class.ilObjUser.php";
00556 
00557                 // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
00558                 $a_rcp_to = $this->__substituteRecipients($a_rcp_to,"resubstitute");
00559                 $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc,"resubstitute");
00560                 $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc,"resubstitute");
00561 
00562 
00563                 $as_email = array();
00564 
00565                 $mbox =& new ilMailbox();
00566 
00567                 $rcp_ids = $this->getUserIds(trim($a_rcp_to).",".trim($a_rcp_cc).",".trim($a_rcp_bcc));
00568 
00569                 foreach($rcp_ids as $id)
00570                 {
00571                         $tmp_mail_options =& new ilMailOptions($id);
00572 
00573                         // CONTINUE IF USER WNATS HIS MAIL SEND TO EMAIL
00574                         if ($tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
00575                         {
00576                                 $as_email[] = $id;
00577                                 continue;
00578                         }
00579 
00580                         if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
00581                         {
00582                                 $as_email[] = $id;
00583                         }
00584 
00585                         if ($a_action == 'system')
00586                         {
00587                                 $inbox_id = 0;
00588                         }
00589                         else
00590                         {
00591                                 $mbox->setUserId($id);
00592                                 $inbox_id = $mbox->getInboxFolder();
00593                         }
00594                         $mail_id = $this->sendInternalMail($inbox_id,$this->user_id,
00595                                                                   $a_attachments,$a_rcp_to,
00596                                                                   $a_rcp_cc,'','unread',$a_type,
00597                                                                   0,$a_subject,$a_message,$id);
00598                         if ($a_attachments)
00599                         {
00600                                 $this->mfile->assignAttachmentsToDirectory($mail_id,$sent_mail_id,$a_attachments);
00601                         }
00602                 }
00603 
00604                 // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
00605                 foreach ($as_email as $id)
00606                 {
00607                         $tmp_user =& new ilObjUser($id);
00608                         $this->sendMimeMail('','',$tmp_user->getEmail(),$a_subject,$a_message,$a_attachments);
00609                 }
00610                 
00611                 return true;
00612         }
00613         
00614 
00620         function getUserIds($a_recipients)
00621         {
00622                 $tmp_names = $this->explodeRecipients($a_recipients);
00623                 
00624                 for ($i = 0;$i < count($tmp_names); $i++)
00625                 {
00626                         if (substr($tmp_names[$i],0,1) == '#')
00627                         {
00628                                 include_once("./classes/class.ilObjectFactory.php");
00629 
00630                                 // GET GROUP MEMBER IDS
00631                                 $grp_data = ilUtil::searchGroups(substr($tmp_names[$i],1));
00632 
00633                                 // INSTATIATE GROUP OBJECT
00634                                 foreach ($grp_data as $grp)
00635                                 {
00636                                         $grp_object = ilObjectFactory::getInstanceByRefId($grp["ref_id"]);
00637                                         break;
00638                                 }
00639                                 // STORE MEMBER IDS IN $ids
00640                                 foreach ($grp_object->getGroupMemberIds() as $id)
00641                                 {
00642                                         $ids[] = $id;
00643                                 } 
00644                         }
00645                         else if (!empty($tmp_names[$i]))
00646                         {
00647                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
00648                                 {
00649                                         $ids[] = $id;
00650                                 }
00651 #                               else if ($id = ilObjUser::getUserIdByEmail(addslashes($tmp_names[$i])))
00652 #                               {
00653 #                                       $ids[] = $id;
00654 #                               }
00655                         }
00656                 }
00657 
00658                 return is_array($ids) ? $ids : array();
00659         }
00670         function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
00671         {
00672                 $error_message = '';
00673 
00674                 if (empty($a_m_subject))
00675                 {
00676                         $error_message .= $error_message ? "<br>" : '';
00677                         $error_message .= $this->lng->txt("mail_add_subject");
00678                 }
00679 
00680                 if (empty($a_rcp_to))
00681                 {
00682                         $error_message .= $error_message ? "<br>" : '';
00683                         $error_message .= $this->lng->txt("mail_add_recipient");
00684                 }
00685 
00686                 return $error_message;
00687         }
00688 
00695         function getEmailsOfRecipients($a_rcp)
00696         {
00697                 $addresses = array();
00698 
00699                 $tmp_rcp = $this->explodeRecipients($a_rcp);
00700 
00701                 foreach ($tmp_rcp as $rcp)
00702                 {
00703                         // NO GROUP
00704                         if (substr($rcp,0,1) != '#')
00705                         {
00706                                 if (strpos($rcp,'@'))
00707                                 {
00708                                         $addresses[] = $rcp;
00709                                         continue;
00710                                 }
00711 
00712                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00713                                 {
00714                                         $tmp_user = new ilObjUser($id);
00715                                         $addresses[] = $tmp_user->getEmail();
00716                                         continue;
00717                                 }
00718                         }
00719                         else
00720                         {
00721                                 // GROUP THINGS
00722                                 include_once("./classes/class.ilObjectFactory.php");
00723 
00724                                 // GET GROUP MEMBER IDS
00725                                 $grp_data = ilUtil::searchGroups(substr($rcp,1));
00726 
00727                                 // INSTATIATE GROUP OBJECT
00728                                 foreach ($grp_data as $grp)
00729                                 {
00730                                         $grp_object = ilObjectFactory::getInstanceByRefId($grp["ref_id"]);
00731                                         break;
00732                                 }
00733                                 // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
00734                                 foreach ($grp_object->getGroupMemberIds() as $id)
00735                                 {
00736                                         $tmp_user = new ilObjUser($id);
00737                                         $addresses[] = $tmp_user->getEmail();
00738                                 } 
00739                         }
00740                 }
00741 
00742                 return $addresses;
00743         }
00744                 
00751         function checkRecipients($a_recipients,$a_type)
00752         {
00753                 global $rbacsystem;
00754                 
00755                 $wrong_rcps = '';
00756 
00757                 $tmp_rcp = $this->explodeRecipients($a_recipients);
00758 
00759                 foreach ($tmp_rcp as $rcp)
00760                 {
00761                         if (empty($rcp))
00762                         {
00763                                 continue;
00764                         }
00765                         // NO GROUP
00766                         if (substr($rcp,0,1) != '#')
00767                         {
00768                                 // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
00769                                 if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
00770                                         !ilUtil::is_email($rcp))
00771                                 {
00772                                         $wrong_rcps .= "<BR/>".$rcp;
00773                                         continue;
00774                                 }
00775                                 
00776                                 // CHECK IF USER CAN RECEIVE MAIL
00777                                 if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00778                                 {
00779                                         if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
00780                                         {
00781                                                 $wrong_rcps .= "<BR/>".$rcp." (".$this->lng->txt("user_cant_receive_mail").")";
00782                                                 continue;
00783                                         }
00784                                 }
00785                         }
00786                         else
00787                         {
00788                                 if (!ilUtil::groupNameExists(addslashes(substr($rcp,1))))
00789                                 {
00790                                         $wrong_rcps .= "<BR/>".$rcp;
00791                                         continue;
00792                                 }
00793                         }
00794                 }
00795 
00796                 return $wrong_rcps;
00797         }
00798 
00813         function savePostData($a_user_id,
00814                                                   $a_attachments,
00815                                                   $a_rcp_to,
00816                                                   $a_rcp_cc,
00817                                                   $a_rcp_bcc,
00818                                                   $a_m_type,
00819                                                   $a_m_email,
00820                                                   $a_m_subject,
00821                                                   $a_m_message)
00822         {
00823                 $query = "DELETE FROM $this->table_mail_saved ".
00824                         "WHERE user_id = '".$this->user_id."'";
00825                 $res = $this->ilias->db->query($query);
00826 
00827                 $query = "INSERT INTO $this->table_mail_saved ".
00828                         "SET user_id = '".$a_user_id."',".
00829                         "attachments = '".addslashes(serialize($a_attachments))."',".
00830                         "rcp_to = '".addslashes($a_rcp_to)."',".
00831                         "rcp_cc = '".addslashes($a_rcp_cc)."',".
00832                         "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00833                         "m_type = '".addslashes(serialize($a_m_type))."',".
00834                         "m_email = '',".
00835                         "m_subject = '".addslashes($a_m_subject)."',".
00836                         "m_message = '".addslashes($a_m_message)."'";
00837 
00838                 $res = $this->ilias->db->query($query);
00839 
00840                 return true;
00841         }
00842 
00848         function getSavedData()
00849         {
00850                 $query = "SELECT * FROM $this->table_mail_saved ".
00851                         "WHERE user_id = '".$this->user_id."'";
00852 
00853                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00854 
00855                 return $this->mail_data;
00856         }
00857 
00871         function sendMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$a_type)
00872         {
00873                 global $lng,$rbacsystem;
00874 
00875                 $error_message = '';
00876                 $message = '';
00877 
00878 
00879                 if (in_array("system",$a_type))
00880                 {
00881                         $this->__checkSystemRecipients($a_rcp_to);
00882                         $a_type = array('system');
00883                 }
00884 
00885                 if ($a_attachment)
00886                 {
00887                         if (!$this->mfile->checkFilesExist($a_attachment))
00888                         {
00889                                 return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
00890                         }
00891                 }
00892                 // CHECK NECESSARY MAIL DATA FOR ALL TYPES
00893                 if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
00894                 {
00895                         return $error_message;
00896                 }
00897                 // check recipients
00898                 if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
00899                 {
00900                         $message .= $error_message;
00901                 }
00902 
00903                 if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
00904                 {
00905                         $message .= $error_message;
00906                 }
00907 
00908                 if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
00909                 {
00910                         $message .= $error_message;
00911                 }
00912                 // if there was an error
00913                 if (!empty($message))
00914                 {
00915                         return $this->lng->txt("mail_following_rcp_not_valid").$message;
00916                 }
00917 
00918                 // CHECK FOR SYSTEM MAIL
00919                 if (in_array('system',$a_type))
00920                 {
00921                         if (!empty($a_attachment))
00922                         {
00923                                 return $lng->txt("mail_no_attach_allowed");
00924                         }
00925                 }
00926 
00927                 // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
00928                 $a_rcp_to = $this->__substituteRecipients($a_rcp_to,"substitute");
00929                 $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc,"substitute");
00930                 $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc,"substitute");
00931 
00932                 // COUNT EMAILS
00933                 $c_emails = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,true);
00934                 $c_rcp = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,false);
00935 
00936                 if (count($c_emails))
00937                 {
00938                         if (!$this->getEmailOfSender())
00939                         {
00940                                 return $lng->txt("mail_check_your_email_addr");
00941                         }
00942                         
00943                 }
00944 
00945                 // ACTIONS FOR ALL TYPES
00946                 // save mail in sent box
00947                 $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
00948                                                                                 $a_m_subject,$a_m_message);
00949                 if ($a_attachment)
00950                 {
00951                         $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
00952                         // ARE THERE INTERNAL MAILS
00953                         if ($c_emails < $c_rcp)
00954                         {
00955                                 if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
00956                                 {
00957                                         return $error;
00958                                 }
00959                         }
00960                 }
00961 
00962                 // FILTER EMAILS
00963                 // IF EMAIL RECIPIENT
00964                 if ($c_emails)
00965                 {
00966                         if (!$rbacsystem->checkAccess("smtp_mail",$this->getMailObjectReferenceId()))
00967                         {
00968                                 return $lng->txt("mail_no_permissions_write_smtp");
00969                         }
00970                         
00971                         //IF ONLY EMAIL
00972                         if ( $c_rcp == $c_emails)
00973                         {
00974                                 // SEND IT
00975                                 $this->sendMimeMail($a_rcp_to,
00976                                                                         $a_rcp_cc,
00977                                                                         $a_rcp_bc,
00978                                                                         $a_m_subject,$a_m_message,$a_attachment);
00979                         }
00980                         else
00981                         {
00982                                 // SET ALL EMAIL RECIPIENTS BCC AND CREATE A LINE ('to','cc') in Message body
00983                                 $new_bcc = array_merge($this->__getEmailRecipients($a_rcp_to),
00984                                                                            $this->__getEmailRecipients($a_rcp_cc),
00985                                                                            $this->__getEmailRecipients($a_rcp_bcc));
00986                                 $this->sendMimeMail("",
00987                                                                         "",
00988                                                                         $new_bcc,
00989                                                                         $a_m_subject,
00990                                                                         $this->__prependMessage($a_m_message,$a_rcp_to,$a_rcp_cc),
00991                                                                         $a_attachment);
00992                         }
00993                 }
00994 
00995                 if (in_array('system',$a_type))
00996                 {
00997                         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'))
00998                         {
00999                                 return $lng->txt("mail_send_error");
01000                         }
01001                 }
01002                 // ACTIONS FOR TYPE SYSTEM AND NORMAL
01003                 if (in_array('normal',$a_type))
01004                 {
01005                         // TRY BOTH internal and email (depends on user settings)
01006                         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'))
01007                         {
01008                                 return $lng->txt("mail_send_error");
01009                         }
01010                 }
01011 
01012                 // Temporary bugfix
01013                 if (!$this->getSaveInSentbox())
01014                 {
01015                         $this->deleteMails(array($sent_id));
01016                 }
01017 
01018                 return '';
01019         }
01020 
01033         function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
01034                                                    $a_m_subject,$a_m_message)
01035         {
01036                 include_once "classes/class.ilMailbox.php";
01037 
01038                 $mbox = new ilMailbox($this->user_id);
01039                 $sent_id = $mbox->getSentFolder();
01040 
01041                 return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
01042                                                                                 $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id);
01043         }
01044 
01050         function addFullname($a_email)
01051         {
01052                 global $ilUser;
01053                 
01054                 return $ilUser->getFullname().'<'.$a_email.'>';
01055         }
01056 
01068         function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
01069         {
01070                 include_once "classes/class.ilMimeMail.php";
01071 
01072                 $sender = $this->addFullname($this->getEmailOfSender());
01073 
01074                 $mmail = new ilMimeMail();
01075                 $mmail->autoCheck(false);
01076                 $mmail->From($sender);
01077                 $mmail->To($a_rcp_to);
01078                 // Add installation name to subject
01079                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01080                 $a_m_subject = "[".$inst_name."] ".$a_m_subject;
01081                 $mmail->Subject($a_m_subject);
01082                 $mmail->Body($a_m_message);
01083 
01084                 if ($a_rcp_cc)
01085                 {
01086                         $mmail->Cc($a_rcp_cc);
01087                 }
01088 
01089                 if ($a_rcp_bcc)
01090                 {
01091                         $mmail->Bcc($a_rcp_bcc);
01092                 }
01093 
01094                 foreach ($a_attachments as $attachment)
01095                 {
01096                         $mmail->Attach($this->mfile->getAbsolutePath($attachment));
01097                 }
01098 
01099                 $mmail->Send();
01100         }
01101 
01107         function getEmailOfSender()
01108         {
01109                 $umail = new ilObjUser($this->user_id);
01110                 $sender = $umail->getEmail();
01111 
01112                 if (ilUtil::is_email($sender))
01113                 {
01114                         return $sender;
01115                 }
01116                 else
01117                 {
01118                         return '';
01119                 }
01120         }
01121 
01128         function saveAttachments($a_attachments)
01129         {
01130                 $query = "UPDATE $this->table_mail_saved ".
01131                         "SET attachments = '".addslashes(serialize($a_attachments))."' ".
01132                         "WHERE user_id = '".$this->user_id."'";
01133 
01134                 $res = $this->ilias->db->query($query);
01135 
01136                 return true;
01137         }
01138 
01144         function getAttachments()
01145         {
01146                 return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
01147         }
01148         
01155         function explodeRecipients($a_recipients)
01156         {
01157                 $a_recipients = trim($a_recipients);
01158 
01159                 // WHITESPACE IS NOT ALLOWED AS SEPERATOR
01160                 #$a_recipients = preg_replace("/ /",",",$a_recipients);
01161                 $a_recipients = preg_replace("/;/",",",$a_recipients);
01162                 $rcps = explode(',',$a_recipients);
01163 
01164                 if (count($rcps))
01165                 {
01166                         for ($i = 0; $i < count($rcps); ++ $i)
01167                         {
01168                                 $rcps[$i] = trim($rcps[$i]);
01169                         }
01170                 }
01171         
01172                 return is_array($rcps) ? $rcps : array();
01173                 
01174         }
01175 
01176         function __getCountRecipient($rcp,$a_only_email = true)
01177         {
01178                 $counter = 0;
01179 
01180                 foreach ($this->explodeRecipients($rcp) as $to)
01181                 {
01182                         if ($a_only_email)
01183                         {
01184                                 if (strpos($to,'@'))
01185                                 {
01186                                         ++$counter;
01187                                 }
01188                         }
01189                         else
01190                         {
01191                                 ++$counter;
01192                         }
01193                 }
01194 
01195                 return $counter;
01196         }
01197                         
01198 
01199         function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
01200         {
01201                 return $this->__getCountRecipient($a_to,$a_only_email) 
01202                         + $this->__getCountRecipient($a_cc,$a_only_email) 
01203                         + $this->__getCountRecipient($a_bcc,$a_only_email);
01204         }
01205 
01206         function __getEmailRecipients($a_rcp)
01207         {
01208                 foreach ($this->explodeRecipients($a_rcp) as $to)
01209                 {
01210                         if (strpos($to,'@'))
01211                         {
01212                                 $rcp[] = $to;
01213                         }
01214                 }
01215 
01216                 return $rcp ? $rcp : array();
01217         }
01218 
01219         function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
01220         {
01221                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01222 
01223                 $message = $inst_name." To:".$rcp_to."\n";
01224 
01225                 if ($rcp_cc)
01226                 {
01227                         $message .= "Cc: ".$rcp_cc;
01228                 }
01229 
01230                 $message .= "\n\n";
01231                 $message .= $a_m_message;
01232 
01233                 return $message;
01234         }
01235 
01236         function __checkSystemRecipients(&$a_rcp_to)
01237         {
01238                 if (preg_match("/@all/",$a_rcp_to))
01239                 {
01240                         // GET ALL LOGINS
01241                         $all = ilObjUser::_getAllUserLogins($this->ilias);
01242                         $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
01243                 }
01244 
01245                 return;
01246         }
01247 
01248         function __substituteRecipients($a_rcp,$direction)
01249         {
01250                 $new_name = array();
01251 
01252                 $tmp_names = $this->explodeRecipients($a_rcp);
01253 
01254 
01255                 foreach($tmp_names as $name)
01256                 {
01257                         if(strpos($name,"#") === 0)
01258                         {
01259                                 $new_name[] = $name;
01260                                 continue;
01261                         }
01262                         switch($direction)
01263                         {
01264                                 case "substitute":
01265                                         if(strpos($name,"@") and loginExists($name))
01266                                         {
01267                                                 $new_name[] = preg_replace("/@/","�#�",$name);
01268                                         }
01269                                         else
01270                                         {
01271                                                 $new_name[] = $name;
01272                                         }
01273                                         break;
01274                                         
01275                                 case "resubstitute":
01276                                         if(stristr($name,"�#�"))
01277                                         {
01278                                                 $new_name[] = preg_replace("/�#�/","@",$name);
01279                                         }
01280                                         else
01281                                         {
01282                                                 $new_name[] = $name;
01283                                         }
01284                                         break;
01285                         }
01286                 }
01287                 return implode(",",$new_name);
01288         }
01289 } // END class.ilMail
01290 ?>

Generated on Fri Dec 13 2013 09:06:34 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1