• 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 
00535                 $query = "SELECT LAST_INSERT_ID() FROM $this->table_mail";
00536                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_ASSOC);
00537 
00538                 return $row["last_insert_id()"];
00539         }
00553         function distributeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_subject,$a_message,$a_attachments,$sent_mail_id,$a_type,$a_action)
00554         {
00555                 include_once "classes/class.ilMailbox.php";
00556                 include_once "./classes/class.ilObjUser.php";
00557 
00558                 // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
00559                 $a_rcp_to = $this->__substituteRecipients($a_rcp_to,"resubstitute");
00560                 $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc,"resubstitute");
00561                 $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc,"resubstitute");
00562 
00563 
00564                 $as_email = array();
00565 
00566                 $mbox =& new ilMailbox();
00567 
00568                 $rcp_ids = $this->getUserIds(trim($a_rcp_to).",".trim($a_rcp_cc).",".trim($a_rcp_bcc));
00569 
00570                 foreach($rcp_ids as $id)
00571                 {
00572                         $tmp_mail_options =& new ilMailOptions($id);
00573 
00574                         // CONTINUE IF USER WNATS HIS MAIL SEND TO EMAIL
00575                         if ($tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
00576                         {
00577                                 $as_email[] = $id;
00578                                 continue;
00579                         }
00580 
00581                         if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
00582                         {
00583                                 $as_email[] = $id;
00584                         }
00585 
00586                         if ($a_action == 'system')
00587                         {
00588                                 $inbox_id = 0;
00589                         }
00590                         else
00591                         {
00592                                 $mbox->setUserId($id);
00593                                 $inbox_id = $mbox->getInboxFolder();
00594                         }
00595                         $mail_id = $this->sendInternalMail($inbox_id,$this->user_id,
00596                                                                   $a_attachments,$a_rcp_to,
00597                                                                   $a_rcp_cc,'','unread',$a_type,
00598                                                                   0,$a_subject,$a_message,$id);
00599                         if ($a_attachments)
00600                         {
00601                                 $this->mfile->assignAttachmentsToDirectory($mail_id,$sent_mail_id,$a_attachments);
00602                         }
00603                 }
00604 
00605                 // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
00606                 foreach ($as_email as $id)
00607                 {
00608                         $tmp_user =& new ilObjUser($id);
00609                         $this->sendMimeMail('','',$tmp_user->getEmail(),$a_subject,$a_message,$a_attachments);
00610                 }
00611                 
00612                 return true;
00613         }
00614         
00615 
00621         function getUserIds($a_recipients)
00622         {
00623                 $tmp_names = $this->explodeRecipients($a_recipients);
00624                 
00625                 for ($i = 0;$i < count($tmp_names); $i++)
00626                 {
00627                         if (substr($tmp_names[$i],0,1) == '#')
00628                         {
00629                                 include_once("./classes/class.ilObjectFactory.php");
00630 
00631                                 // GET GROUP MEMBER IDS
00632                                 $grp_data = ilUtil::searchGroups(substr($tmp_names[$i],1));
00633 
00634                                 // INSTATIATE GROUP OBJECT
00635                                 foreach ($grp_data as $grp)
00636                                 {
00637                                         $grp_object = ilObjectFactory::getInstanceByRefId($grp["ref_id"]);
00638                                         break;
00639                                 }
00640                                 // STORE MEMBER IDS IN $ids
00641                                 foreach ($grp_object->getGroupMemberIds() as $id)
00642                                 {
00643                                         $ids[] = $id;
00644                                 } 
00645                         }
00646                         else if (!empty($tmp_names[$i]))
00647                         {
00648                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
00649                                 {
00650                                         $ids[] = $id;
00651                                 }
00652 #                               else if ($id = ilObjUser::getUserIdByEmail(addslashes($tmp_names[$i])))
00653 #                               {
00654 #                                       $ids[] = $id;
00655 #                               }
00656                         }
00657                 }
00658 
00659                 return is_array($ids) ? $ids : array();
00660         }
00671         function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
00672         {
00673                 $error_message = '';
00674 
00675                 if (empty($a_m_subject))
00676                 {
00677                         $error_message .= $error_message ? "<br>" : '';
00678                         $error_message .= $this->lng->txt("mail_add_subject");
00679                 }
00680 
00681                 if (empty($a_rcp_to))
00682                 {
00683                         $error_message .= $error_message ? "<br>" : '';
00684                         $error_message .= $this->lng->txt("mail_add_recipient");
00685                 }
00686 
00687                 return $error_message;
00688         }
00689 
00696         function getEmailsOfRecipients($a_rcp)
00697         {
00698                 $addresses = array();
00699 
00700                 $tmp_rcp = $this->explodeRecipients($a_rcp);
00701 
00702                 foreach ($tmp_rcp as $rcp)
00703                 {
00704                         // NO GROUP
00705                         if (substr($rcp,0,1) != '#')
00706                         {
00707                                 if (strpos($rcp,'@'))
00708                                 {
00709                                         $addresses[] = $rcp;
00710                                         continue;
00711                                 }
00712 
00713                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00714                                 {
00715                                         $tmp_user = new ilObjUser($id);
00716                                         $addresses[] = $tmp_user->getEmail();
00717                                         continue;
00718                                 }
00719                         }
00720                         else
00721                         {
00722                                 // GROUP THINGS
00723                                 include_once("./classes/class.ilObjectFactory.php");
00724 
00725                                 // GET GROUP MEMBER IDS
00726                                 $grp_data = ilUtil::searchGroups(substr($rcp,1));
00727 
00728                                 // INSTATIATE GROUP OBJECT
00729                                 foreach ($grp_data as $grp)
00730                                 {
00731                                         $grp_object = ilObjectFactory::getInstanceByRefId($grp["ref_id"]);
00732                                         break;
00733                                 }
00734                                 // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
00735                                 foreach ($grp_object->getGroupMemberIds() as $id)
00736                                 {
00737                                         $tmp_user = new ilObjUser($id);
00738                                         $addresses[] = $tmp_user->getEmail();
00739                                 } 
00740                         }
00741                 }
00742 
00743                 return $addresses;
00744         }
00745                 
00752         function checkRecipients($a_recipients,$a_type)
00753         {
00754                 global $rbacsystem;
00755                 
00756                 $wrong_rcps = '';
00757 
00758                 $tmp_rcp = $this->explodeRecipients($a_recipients);
00759 
00760                 foreach ($tmp_rcp as $rcp)
00761                 {
00762                         if (empty($rcp))
00763                         {
00764                                 continue;
00765                         }
00766                         // NO GROUP
00767                         if (substr($rcp,0,1) != '#')
00768                         {
00769                                 // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
00770                                 if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
00771                                         !ilUtil::is_email($rcp))
00772                                 {
00773                                         $wrong_rcps .= "<BR/>".$rcp;
00774                                         continue;
00775                                 }
00776                                 
00777                                 // CHECK IF USER CAN RECEIVE MAIL
00778                                 if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00779                                 {
00780                                         if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
00781                                         {
00782                                                 $wrong_rcps .= "<BR/>".$rcp." (".$this->lng->txt("user_cant_receive_mail").")";
00783                                                 continue;
00784                                         }
00785                                 }
00786                         }
00787                         else
00788                         {
00789                                 if (!ilUtil::groupNameExists(addslashes(substr($rcp,1))))
00790                                 {
00791                                         $wrong_rcps .= "<BR/>".$rcp;
00792                                         continue;
00793                                 }
00794                         }
00795                 }
00796 
00797                 return $wrong_rcps;
00798         }
00799 
00814         function savePostData($a_user_id,
00815                                                   $a_attachments,
00816                                                   $a_rcp_to,
00817                                                   $a_rcp_cc,
00818                                                   $a_rcp_bcc,
00819                                                   $a_m_type,
00820                                                   $a_m_email,
00821                                                   $a_m_subject,
00822                                                   $a_m_message)
00823         {
00824                 $query = "DELETE FROM $this->table_mail_saved ".
00825                         "WHERE user_id = '".$this->user_id."'";
00826                 $res = $this->ilias->db->query($query);
00827 
00828                 $query = "INSERT INTO $this->table_mail_saved ".
00829                         "SET user_id = '".$a_user_id."',".
00830                         "attachments = '".addslashes(serialize($a_attachments))."',".
00831                         "rcp_to = '".addslashes($a_rcp_to)."',".
00832                         "rcp_cc = '".addslashes($a_rcp_cc)."',".
00833                         "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00834                         "m_type = '".addslashes(serialize($a_m_type))."',".
00835                         "m_email = '',".
00836                         "m_subject = '".addslashes($a_m_subject)."',".
00837                         "m_message = '".addslashes($a_m_message)."'";
00838 
00839                 $res = $this->ilias->db->query($query);
00840 
00841                 return true;
00842         }
00843 
00849         function getSavedData()
00850         {
00851                 $query = "SELECT * FROM $this->table_mail_saved ".
00852                         "WHERE user_id = '".$this->user_id."'";
00853 
00854                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00855 
00856                 return $this->mail_data;
00857         }
00858 
00872         function sendMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$a_type)
00873         {
00874                 global $lng,$rbacsystem;
00875 
00876                 $error_message = '';
00877                 $message = '';
00878 
00879 
00880                 if (in_array("system",$a_type))
00881                 {
00882                         $this->__checkSystemRecipients($a_rcp_to);
00883                         $a_type = array('system');
00884                 }
00885 
00886                 if ($a_attachment)
00887                 {
00888                         if (!$this->mfile->checkFilesExist($a_attachment))
00889                         {
00890                                 return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
00891                         }
00892                 }
00893                 // CHECK NECESSARY MAIL DATA FOR ALL TYPES
00894                 if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
00895                 {
00896                         return $error_message;
00897                 }
00898                 // check recipients
00899                 if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
00900                 {
00901                         $message .= $error_message;
00902                 }
00903 
00904                 if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
00905                 {
00906                         $message .= $error_message;
00907                 }
00908 
00909                 if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
00910                 {
00911                         $message .= $error_message;
00912                 }
00913                 // if there was an error
00914                 if (!empty($message))
00915                 {
00916                         return $this->lng->txt("mail_following_rcp_not_valid").$message;
00917                 }
00918 
00919                 // CHECK FOR SYSTEM MAIL
00920                 if (in_array('system',$a_type))
00921                 {
00922                         if (!empty($a_attachment))
00923                         {
00924                                 return $lng->txt("mail_no_attach_allowed");
00925                         }
00926                 }
00927 
00928                 // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
00929                 $a_rcp_to = $this->__substituteRecipients($a_rcp_to,"substitute");
00930                 $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc,"substitute");
00931                 $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc,"substitute");
00932 
00933                 // COUNT EMAILS
00934                 $c_emails = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,true);
00935                 $c_rcp = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,false);
00936 
00937                 if (count($c_emails))
00938                 {
00939                         if (!$this->getEmailOfSender())
00940                         {
00941                                 return $lng->txt("mail_check_your_email_addr");
00942                         }
00943                         
00944                 }
00945 
00946                 // ACTIONS FOR ALL TYPES
00947                 // save mail in sent box
00948                 $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
00949                                                                                 $a_m_subject,$a_m_message);
00950                 if ($a_attachment)
00951                 {
00952                         $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
00953                         // ARE THERE INTERNAL MAILS
00954                         if ($c_emails < $c_rcp)
00955                         {
00956                                 if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
00957                                 {
00958                                         return $error;
00959                                 }
00960                         }
00961                 }
00962 
00963                 // FILTER EMAILS
00964                 // IF EMAIL RECIPIENT
00965                 if ($c_emails)
00966                 {
00967                         if (!$rbacsystem->checkAccess("smtp_mail",$this->getMailObjectReferenceId()))
00968                         {
00969                                 return $lng->txt("mail_no_permissions_write_smtp");
00970                         }
00971                         
00972                         //IF ONLY EMAIL
00973                         if ( $c_rcp == $c_emails)
00974                         {
00975                                 // SEND IT
00976                                 $this->sendMimeMail($a_rcp_to,
00977                                                                         $a_rcp_cc,
00978                                                                         $a_rcp_bc,
00979                                                                         $a_m_subject,$a_m_message,$a_attachment);
00980                         }
00981                         else
00982                         {
00983                                 // SET ALL EMAIL RECIPIENTS BCC AND CREATE A LINE ('to','cc') in Message body
00984                                 $new_bcc = array_merge($this->__getEmailRecipients($a_rcp_to),
00985                                                                            $this->__getEmailRecipients($a_rcp_cc),
00986                                                                            $this->__getEmailRecipients($a_rcp_bcc));
00987                                 $this->sendMimeMail("",
00988                                                                         "",
00989                                                                         $new_bcc,
00990                                                                         $a_m_subject,
00991                                                                         $this->__prependMessage($a_m_message,$a_rcp_to,$a_rcp_cc),
00992                                                                         $a_attachment);
00993                         }
00994                 }
00995 
00996                 if (in_array('system',$a_type))
00997                 {
00998                         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'))
00999                         {
01000                                 return $lng->txt("mail_send_error");
01001                         }
01002                 }
01003                 // ACTIONS FOR TYPE SYSTEM AND NORMAL
01004                 if (in_array('normal',$a_type))
01005                 {
01006                         // TRY BOTH internal and email (depends on user settings)
01007                         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'))
01008                         {
01009                                 return $lng->txt("mail_send_error");
01010                         }
01011                 }
01012 
01013                 // Temporary bugfix
01014                 if (!$this->getSaveInSentbox())
01015                 {
01016                         $this->deleteMails(array($sent_id));
01017                 }
01018 
01019                 return '';
01020         }
01021 
01034         function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
01035                                                    $a_m_subject,$a_m_message)
01036         {
01037                 include_once "classes/class.ilMailbox.php";
01038 
01039                 $mbox = new ilMailbox($this->user_id);
01040                 $sent_id = $mbox->getSentFolder();
01041 
01042                 return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
01043                                                                                 $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id);
01044         }
01045 
01057         function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
01058         {
01059                 include_once "classes/class.ilMimeMail.php";
01060 
01061                 $sender = $this->getEmailOfSender();
01062 
01063                 $mmail = new ilMimeMail();
01064                 $mmail->autoCheck(false);
01065                 $mmail->From($sender);
01066                 $mmail->To($a_rcp_to);
01067                 // Add installation name to subject
01068                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01069                 $a_m_subject = "[".$inst_name."] ".$a_m_subject;
01070                 $mmail->Subject($a_m_subject);
01071                 $mmail->Body($a_m_message);
01072 
01073                 if ($a_rcp_cc)
01074                 {
01075                         $mmail->Cc($a_rcp_cc);
01076                 }
01077 
01078                 if ($a_rcp_bcc)
01079                 {
01080                         $mmail->Bcc($a_rcp_bcc);
01081                 }
01082 
01083                 foreach ($a_attachments as $attachment)
01084                 {
01085                         $mmail->Attach($this->mfile->getAbsolutePath($attachment));
01086                 }
01087 
01088                 $mmail->Send();
01089         }
01090 
01096         function getEmailOfSender()
01097         {
01098                 $umail = new ilObjUser($this->user_id);
01099                 $sender = $umail->getEmail();
01100 
01101                 if (ilUtil::is_email($sender))
01102                 {
01103                         return $sender;
01104                 }
01105                 else
01106                 {
01107                         return '';
01108                 }
01109         }
01110 
01117         function saveAttachments($a_attachments)
01118         {
01119                 $query = "UPDATE $this->table_mail_saved ".
01120                         "SET attachments = '".addslashes(serialize($a_attachments))."' ".
01121                         "WHERE user_id = '".$this->user_id."'";
01122 
01123                 $res = $this->ilias->db->query($query);
01124 
01125                 return true;
01126         }
01127 
01133         function getAttachments()
01134         {
01135                 return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
01136         }
01137         
01144         function explodeRecipients($a_recipients)
01145         {
01146                 $a_recipients = trim($a_recipients);
01147 
01148                 // WHITESPACE IS NOT ALLOWED AS SEPERATOR
01149                 #$a_recipients = preg_replace("/ /",",",$a_recipients);
01150                 $a_recipients = preg_replace("/;/",",",$a_recipients);
01151                 $rcps = explode(',',$a_recipients);
01152 
01153                 if (count($rcps))
01154                 {
01155                         for ($i = 0; $i < count($rcps); ++ $i)
01156                         {
01157                                 $rcps[$i] = trim($rcps[$i]);
01158                         }
01159                 }
01160         
01161                 return is_array($rcps) ? $rcps : array();
01162                 
01163         }
01164 
01165         function __getCountRecipient($rcp,$a_only_email = true)
01166         {
01167                 $counter = 0;
01168 
01169                 foreach ($this->explodeRecipients($rcp) as $to)
01170                 {
01171                         if ($a_only_email)
01172                         {
01173                                 if (strpos($to,'@'))
01174                                 {
01175                                         ++$counter;
01176                                 }
01177                         }
01178                         else
01179                         {
01180                                 ++$counter;
01181                         }
01182                 }
01183 
01184                 return $counter;
01185         }
01186                         
01187 
01188         function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
01189         {
01190                 return $this->__getCountRecipient($a_to,$a_only_email) 
01191                         + $this->__getCountRecipient($a_cc,$a_only_email) 
01192                         + $this->__getCountRecipient($a_bcc,$a_only_email);
01193         }
01194 
01195         function __getEmailRecipients($a_rcp)
01196         {
01197                 foreach ($this->explodeRecipients($a_rcp) as $to)
01198                 {
01199                         if (strpos($to,'@'))
01200                         {
01201                                 $rcp[] = $to;
01202                         }
01203                 }
01204 
01205                 return $rcp ? $rcp : array();
01206         }
01207 
01208         function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
01209         {
01210                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01211 
01212                 $message = $inst_name." To:".$rcp_to."\n";
01213 
01214                 if ($rcp_cc)
01215                 {
01216                         $message .= "Cc: ".$rcp_cc;
01217                 }
01218 
01219                 $message .= "\n\n";
01220                 $message .= $a_m_message;
01221 
01222                 return $message;
01223         }
01224 
01225         function __checkSystemRecipients(&$a_rcp_to)
01226         {
01227                 if (preg_match("/@all/",$a_rcp_to))
01228                 {
01229                         // GET ALL LOGINS
01230                         $all = ilObjUser::_getAllUserLogins($this->ilias);
01231                         $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
01232                 }
01233 
01234                 return;
01235         }
01236 
01237         function __substituteRecipients($a_rcp,$direction)
01238         {
01239                 $new_name = array();
01240 
01241                 $tmp_names = $this->explodeRecipients($a_rcp);
01242 
01243 
01244                 foreach($tmp_names as $name)
01245                 {
01246                         if(strpos($name,"#") === 0)
01247                         {
01248                                 $new_name[] = $name;
01249                                 continue;
01250                         }
01251                         switch($direction)
01252                         {
01253                                 case "substitute":
01254                                         if(strpos($name,"@") and loginExists($name))
01255                                         {
01256                                                 $new_name[] = preg_replace("/@/","�#�",$name);
01257                                         }
01258                                         else
01259                                         {
01260                                                 $new_name[] = $name;
01261                                         }
01262                                         break;
01263                                         
01264                                 case "resubstitute":
01265                                         if(stristr($name,"�#�"))
01266                                         {
01267                                                 $new_name[] = preg_replace("/�#�/","@",$name);
01268                                         }
01269                                         else
01270                                         {
01271                                                 $new_name[] = $name;
01272                                         }
01273                                         break;
01274                         }
01275                 }
01276                 return implode(",",$new_name);
01277         }
01278 } // END class.ilMail
01279 ?>

Generated on Fri Dec 13 2013 08:00:14 for ILIAS Release_3_3_x_branch .rev 46803 by  doxygen 1.7.1