• 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         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                 // Initiate variables
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                 // DEFAULT: sent mail aren't stored insentbox of user.
00157                 $this->setSaveInSentbox(false);
00158 
00159                 // GET REFERENCE ID OF MAIL OBJECT
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                 // mail settings id is set by a constant in ilias.ini. Keep the select for some time until everyone has updated his ilias.ini
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                 // CREATE IN STATEMENT
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                 // CREATE IN STATEMENT
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                 // CREATE IN STATEMENT
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 FROM $this->table_mail";
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                 // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
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                         // CONTINUE IF USER WNATS HIS MAIL SEND TO EMAIL
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                 // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
00628                 $counter = 0;
00629                 $to = array();
00630                 $bcc = array();
00631                 foreach ($as_email as $id)
00632                 {
00633                         if(!$counter++)
00634                         {
00635                                 $to[] = ilObjUser::_lookupEmail($id);
00636                         }
00637                         else
00638                         {
00639                                 $bcc[] = ilObjUser::_lookupEmail($id);
00640                         }
00641                 }
00642                 if($to)
00643                 {
00644                         $this->sendMimeMail(implode(',',$to),'',implode(',',$bcc),$a_subject,$a_message,$a_attachments);
00645                 }
00646                 
00647                 return true;
00648         }
00649         
00650 
00656         function getUserIds($a_recipients)
00657         {
00658                 global $rbacreview;
00659 
00660                 $tmp_names = $this->explodeRecipients($a_recipients);
00661                 $ids = array();
00662                 for ($i = 0;$i < count($tmp_names); $i++)
00663                 {
00664                         if (substr($tmp_names[$i],0,1) == '#')
00665                         {
00666                                 if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
00667                                 {
00668                                         include_once("./classes/class.ilObjectFactory.php");
00669                                         include_once('./classes/class.ilObjGroup.php');
00670                                         
00671                                         foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
00672                                         {
00673                                                 $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
00674                                                 break;
00675                                         }
00676                                         // STORE MEMBER IDS IN $ids
00677                                         foreach ($grp_object->getGroupMemberIds() as $id)
00678                                         {
00679                                                 $ids[] = $id;
00680                                         }
00681                                 }
00682                                 // is role: get role ids
00683                                 elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
00684                                 {
00685                                         foreach($rbacreview->assignedUsers($role_id) as $usr_id)
00686                                         {
00687                                                 $ids[] = $usr_id;
00688                                         }
00689                                 }
00690                                 
00691                         }
00692                         else if (!empty($tmp_names[$i]))
00693                         {
00694                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
00695                                 {
00696                                         $ids[] = $id;
00697                                 }
00698 #                               else if ($id = ilObjUser::getUserIdByEmail(addslashes($tmp_names[$i])))
00699 #                               {
00700 #                                       $ids[] = $id;
00701 #                               }
00702                         }
00703                 }
00704                 return array_unique($ids);
00705         }
00716         function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
00717         {
00718                 $error_message = '';
00719 
00720                 if (empty($a_m_subject))
00721                 {
00722                         $error_message .= $error_message ? "<br>" : '';
00723                         $error_message .= $this->lng->txt("mail_add_subject");
00724                 }
00725 
00726                 if (empty($a_rcp_to))
00727                 {
00728                         $error_message .= $error_message ? "<br>" : '';
00729                         $error_message .= $this->lng->txt("mail_add_recipient");
00730                 }
00731 
00732                 return $error_message;
00733         }
00734 
00741         function getEmailsOfRecipients($a_rcp)
00742         {
00743                 $addresses = array();
00744 
00745                 $tmp_rcp = $this->explodeRecipients($a_rcp);
00746 
00747                 foreach ($tmp_rcp as $rcp)
00748                 {
00749                         // NO GROUP
00750                         if (substr($rcp,0,1) != '#')
00751                         {
00752                                 if (strpos($rcp,'@'))
00753                                 {
00754                                         $addresses[] = $rcp;
00755                                         continue;
00756                                 }
00757 
00758                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00759                                 {
00760                                         $tmp_user = new ilObjUser($id);
00761                                         $addresses[] = $tmp_user->getEmail();
00762                                         continue;
00763                                 }
00764                         }
00765                         else
00766                         {
00767                                 // GROUP THINGS
00768                                 include_once("./classes/class.ilObjectFactory.php");
00769                                 include_once('./classes/class.ilObjGroup.php');
00770 
00771                                 // Fix 
00772                                 foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
00773                                 {
00774                                         $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
00775                                         break;
00776                                 }
00777                                 // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
00778                                 foreach ($grp_object->getGroupMemberIds() as $id)
00779                                 {
00780                                         $tmp_user = new ilObjUser($id);
00781                                         $addresses[] = $tmp_user->getEmail();
00782                                 } 
00783                         }
00784                 }
00785 
00786                 return $addresses;
00787         }
00788                 
00795         function checkRecipients($a_recipients,$a_type)
00796         {
00797                 global $rbacsystem,$rbacreview;
00798                 
00799                 $wrong_rcps = '';
00800 
00801                 $tmp_rcp = $this->explodeRecipients($a_recipients);
00802 
00803                 foreach ($tmp_rcp as $rcp)
00804                 {
00805                         if (empty($rcp))
00806                         {
00807                                 continue;
00808                         }
00809                         // NO GROUP
00810                         if (substr($rcp,0,1) != '#')
00811                         {
00812                                 // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
00813                                 if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
00814                                         !ilUtil::is_email($rcp))
00815                                 {
00816                                         $wrong_rcps .= "<BR/>".$rcp;
00817                                         continue;
00818                                 }
00819                                 
00820                                 // CHECK IF USER CAN RECEIVE MAIL
00821                                 if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
00822                                 {
00823                                         if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
00824                                         {
00825                                                 $wrong_rcps .= "<BR/>".$rcp." (".$this->lng->txt("user_cant_receive_mail").")";
00826                                                 continue;
00827                                         }
00828                                 }
00829                         }
00830                         elseif (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
00831                         {
00832                                 continue;
00833                         }
00834                         elseif(!$rbacreview->roleExists(addslashes(substr($rcp,1))))
00835                         {
00836                                 $wrong_rcps .= "<BR/>".$rcp." (".$this->lng->txt("mail_no_valid_group_role").")";
00837                                 continue;
00838                         }
00839 
00840                 }
00841 
00842                 return $wrong_rcps;
00843         }
00844 
00859         function savePostData($a_user_id,
00860                                                   $a_attachments,
00861                                                   $a_rcp_to,
00862                                                   $a_rcp_cc,
00863                                                   $a_rcp_bcc,
00864                                                   $a_m_type,
00865                                                   $a_m_email,
00866                                                   $a_m_subject,
00867                                                   $a_m_message)
00868         {
00869                 $query = "DELETE FROM $this->table_mail_saved ".
00870                         "WHERE user_id = '".$this->user_id."'";
00871                 $res = $this->ilias->db->query($query);
00872 
00873                 $query = "INSERT INTO $this->table_mail_saved ".
00874                         "SET user_id = '".$a_user_id."',".
00875                         "attachments = '".addslashes(serialize($a_attachments))."',".
00876                         "rcp_to = '".addslashes($a_rcp_to)."',".
00877                         "rcp_cc = '".addslashes($a_rcp_cc)."',".
00878                         "rcp_bcc = '".addslashes($a_rcp_bcc)."',".
00879                         "m_type = '".addslashes(serialize($a_m_type))."',".
00880                         "m_email = '',".
00881                         "m_subject = '".addslashes($a_m_subject)."',".
00882                         "m_message = '".addslashes($a_m_message)."'";
00883 
00884                 $res = $this->ilias->db->query($query);
00885 
00886                 return true;
00887         }
00888 
00894         function getSavedData()
00895         {
00896                 $query = "SELECT * FROM $this->table_mail_saved ".
00897                         "WHERE user_id = '".$this->user_id."'";
00898 
00899                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00900 
00901                 return $this->mail_data;
00902         }
00903 
00917         function sendMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$a_type)
00918         {
00919                 global $lng,$rbacsystem;
00920 
00921                 $error_message = '';
00922                 $message = '';
00923 
00924                 if (in_array("system",$a_type))
00925                 {
00926                         $this->__checkSystemRecipients($a_rcp_to);
00927                         $a_type = array('system');
00928                 }
00929 
00930                 if ($a_attachment)
00931                 {
00932                         if (!$this->mfile->checkFilesExist($a_attachment))
00933                         {
00934                                 return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
00935                         }
00936                 }
00937                 // CHECK NECESSARY MAIL DATA FOR ALL TYPES
00938                 if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
00939                 {
00940                         return $error_message;
00941                 }
00942                 // check recipients
00943                 if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
00944                 {
00945                         $message .= $error_message;
00946                 }
00947 
00948                 if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
00949                 {
00950                         $message .= $error_message;
00951                 }
00952 
00953                 if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
00954                 {
00955                         $message .= $error_message;
00956                 }
00957                 // if there was an error
00958                 if (!empty($message))
00959                 {
00960                         return $this->lng->txt("mail_following_rcp_not_valid").$message;
00961                 }
00962 
00963                 // CHECK FOR SYSTEM MAIL
00964                 if (in_array('system',$a_type))
00965                 {
00966                         if (!empty($a_attachment))
00967                         {
00968                                 return $lng->txt("mail_no_attach_allowed");
00969                         }
00970                 }
00971 
00972                 // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
00973                 $a_rcp_to = $this->__substituteRecipients($a_rcp_to,"substitute");
00974                 $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc,"substitute");
00975                 $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc,"substitute");
00976                 // COUNT EMAILS
00977                 $c_emails = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,true);
00978                 $c_rcp = $this->__getCountRecipients($a_rcp_to,$a_rcp_cc,$a_rcp_bc,false);
00979 
00980                 if (count($c_emails))
00981                 {
00982                         if (!$this->getEmailOfSender())
00983                         {
00984                                 return $lng->txt("mail_check_your_email_addr");
00985                         }
00986                 }
00987 
00988                 // ACTIONS FOR ALL TYPES
00989                 // save mail in sent box
00990                 $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
00991                                                                                 $a_m_subject,$a_m_message);
00992                 if ($a_attachment)
00993                 {
00994                         $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
00995                         // ARE THERE INTERNAL MAILS
00996                         if ($c_emails < $c_rcp)
00997                         {
00998                                 if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
00999                                 {
01000                                         return $error;
01001                                 }
01002                         }
01003                 }
01004 
01005                 // FILTER EMAILS
01006                 // IF EMAIL RECIPIENT
01007                 if ($c_emails)
01008                 {
01009                         if (!$rbacsystem->checkAccess("smtp_mail",$this->getMailObjectReferenceId()))
01010                         {
01011                                 return $lng->txt("mail_no_permissions_write_smtp");
01012                         }
01013                         $this->sendMimeMail($this->__getEmailRecipients($a_rcp_to),
01014                                                                 $this->__getEmailRecipients($a_rcp_cc),
01015                                                                 $this->__getEmailRecipients($a_rcp_bc),
01016                                                                 $a_m_subject,
01017                                                                 $a_m_message,
01018                                                                 $a_attachment);
01019                 }
01020 
01021                 if (in_array('system',$a_type))
01022                 {
01023                         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'))
01024                         {
01025                                 return $lng->txt("mail_send_error");
01026                         }
01027                 }
01028                 // ACTIONS FOR TYPE SYSTEM AND NORMAL
01029                 if (in_array('normal',$a_type))
01030                 {
01031                         // TRY BOTH internal and email (depends on user settings)
01032                         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'))
01033                         {
01034                                 return $lng->txt("mail_send_error");
01035                         }
01036                 }
01037 
01038                 // Temporary bugfix
01039                 if (!$this->getSaveInSentbox())
01040                 {
01041                         $this->deleteMails(array($sent_id));
01042                 }
01043 
01044                 return '';
01045         }
01046 
01059         function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
01060                                                    $a_m_subject,$a_m_message)
01061         {
01062                 include_once "classes/class.ilMailbox.php";
01063 
01064                 $mbox = new ilMailbox($this->user_id);
01065                 $sent_id = $mbox->getSentFolder();
01066 
01067                 return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
01068                                                                                 $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id);
01069         }
01070 
01071 
01072         
01078         function addFullname($a_email)
01079         {
01080                 include_once 'classes/class.ilMimeMail.php';
01081                 
01082                 global $ilUser;
01083 
01084                 return ilMimeMail::_mimeEncode($ilUser->getFullname()).'<'.$a_email.'>';
01085         }
01086 
01099         function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
01100         {
01101                 #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
01102                 
01103                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01104                 $a_m_subject = "[".$inst_name."] ".$a_m_subject;
01105 
01106 
01107                 if($this->isSOAPEnabled())
01108                 {
01109                         // Send per soap
01110                         include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
01111 
01112                         $soap_client = new ilSoapClient();
01113                         $soap_client->setTimeout(1);
01114                         $soap_client->setResponseTimeout(1);
01115                         $soap_client->enableWSDL(true);
01116                         $soap_client->init();
01117 
01118                         $attachments = array();
01119                         foreach($a_attachments as $attachment)
01120                         {
01121                                 $attachments[] = $this->mfile->getAbsolutePath($attachment);
01122                         }
01123                         $attachments = implode(',',$attachments);
01124 
01125                         $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'],  // session id
01126                                                                                                 $a_rcp_to,
01127                                                                                                 $a_rcp_cc,
01128                                                                                                 $a_rcp_bcc,
01129                                                                                                 $this->addFullname($this->getEmailOfSender()),
01130                                                                                                 $a_m_subject,
01131                                                                                                 $a_m_message,
01132                                                                                                 $attachments));
01133                 
01134                         return true;
01135                 }
01136                 else
01137                 {
01138                         // send direct
01139                         include_once "classes/class.ilMimeMail.php";
01140 
01141                         $sender = $this->addFullname($this->getEmailOfSender());
01142 
01143                         $mmail = new ilMimeMail();
01144                         $mmail->autoCheck(false);
01145                         $mmail->From($sender);
01146                         $mmail->To($a_rcp_to);
01147                         // Add installation name to subject
01148                         $mmail->Subject($a_m_subject);
01149                         $mmail->Body($a_m_message);
01150 
01151                         if ($a_rcp_cc)
01152                         {
01153                                 $mmail->Cc($a_rcp_cc);
01154                         }
01155 
01156                         if ($a_rcp_bcc)
01157                         {
01158                                 $mmail->Bcc($a_rcp_bcc);
01159                         }
01160 
01161                         foreach ($a_attachments as $attachment)
01162                         {
01163                                 $mmail->Attach($this->mfile->getAbsolutePath($attachment));
01164                         }
01165 
01166                         $mmail->Send();
01167                 }
01168         }
01174         function getEmailOfSender()
01175         {
01176                 $umail = new ilObjUser($this->user_id);
01177                 $sender = $umail->getEmail();
01178 
01179                 if (ilUtil::is_email($sender))
01180                 {
01181                         return $sender;
01182                 }
01183                 else
01184                 {
01185                         return '';
01186                 }
01187         }
01188 
01195         function saveAttachments($a_attachments)
01196         {
01197                 $query = "UPDATE $this->table_mail_saved ".
01198                         "SET attachments = '".addslashes(serialize($a_attachments))."' ".
01199                         "WHERE user_id = '".$this->user_id."'";
01200 
01201                 $res = $this->ilias->db->query($query);
01202 
01203                 return true;
01204         }
01205 
01211         function getAttachments()
01212         {
01213                 return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
01214         }
01215         
01222         function explodeRecipients($a_recipients)
01223         {
01224                 $a_recipients = trim($a_recipients);
01225 
01226                 // WHITESPACE IS NOT ALLOWED AS SEPERATOR
01227                 #$a_recipients = preg_replace("/ /",",",$a_recipients);
01228                 $a_recipients = preg_replace("/;/",",",$a_recipients);
01229                 
01230 
01231                 foreach(explode(',',$a_recipients) as $tmp_rec)
01232                 {
01233                         if($tmp_rec)
01234                         {
01235                                 $rcps[] = trim($tmp_rec);
01236                         }
01237                 }
01238                 return is_array($rcps) ? $rcps : array();
01239                 
01240         }
01241 
01242         function __getCountRecipient($rcp,$a_only_email = true)
01243         {
01244                 $counter = 0;
01245 
01246                 foreach ($this->explodeRecipients($rcp) as $to)
01247                 {
01248                         if ($a_only_email)
01249                         {
01250                                 if (strpos($to,'@'))
01251                                 {
01252                                         ++$counter;
01253                                 }
01254                         }
01255                         else
01256                         {
01257                                 ++$counter;
01258                         }
01259                 }
01260 
01261                 return $counter;
01262         }
01263                         
01264 
01265         function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
01266         {
01267                 return $this->__getCountRecipient($a_to,$a_only_email) 
01268                         + $this->__getCountRecipient($a_cc,$a_only_email) 
01269                         + $this->__getCountRecipient($a_bcc,$a_only_email);
01270         }
01271 
01272         function __getEmailRecipients($a_rcp)
01273         {
01274                 foreach ($this->explodeRecipients($a_rcp) as $to)
01275                 {
01276                         if(strpos($to,'@'))
01277                         {
01278                                 $rcp[] = $to;
01279                         }
01280                 }
01281                 return implode(',',$rcp ? $rcp : array());
01282         }
01283 
01284         function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
01285         {
01286                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01287 
01288                 $message = $inst_name." To:".$rcp_to."\n";
01289 
01290                 if ($rcp_cc)
01291                 {
01292                         $message .= "Cc: ".$rcp_cc;
01293                 }
01294 
01295                 $message .= "\n\n";
01296                 $message .= $a_m_message;
01297 
01298                 return $message;
01299         }
01300 
01301         function __checkSystemRecipients(&$a_rcp_to)
01302         {
01303                 if (preg_match("/@all/",$a_rcp_to))
01304                 {
01305                         // GET ALL LOGINS
01306                         $all = ilObjUser::_getAllUserLogins($this->ilias);
01307                         $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
01308                 }
01309 
01310                 return;
01311         }
01312 
01313         function __substituteRecipients($a_rcp,$direction)
01314         {
01315                 $new_name = array();
01316 
01317                 $tmp_names = $this->explodeRecipients($a_rcp);
01318 
01319 
01320                 foreach($tmp_names as $name)
01321                 {
01322                         if(strpos($name,"#") === 0)
01323                         {
01324                                 $new_name[] = $name;
01325                                 continue;
01326                         }
01327                         switch($direction)
01328                         {
01329                                 case "substitute":
01330                                         if(strpos($name,"@") and loginExists($name))
01331                                         {
01332                                                 $new_name[] = preg_replace("/@/","�#�",$name);
01333                                         }
01334                                         else
01335                                         {
01336                                                 $new_name[] = $name;
01337                                         }
01338                                         break;
01339                                         
01340                                 case "resubstitute":
01341                                         if(stristr($name,"�#�"))
01342                                         {
01343                                                 $new_name[] = preg_replace("/�#�/","@",$name);
01344                                         }
01345                                         else
01346                                         {
01347                                                 $new_name[] = $name;
01348                                         }
01349                                         break;
01350                         }
01351                 }
01352                 return implode(",",$new_name);
01353         }
01354 } // END class.ilMail
01355 ?>

Generated on Fri Dec 13 2013 11:57:54 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1