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

Services/Mail/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 
00128 require_once "Services/User/classes/class.ilObjUser.php";
00129 
00130 class ilMail
00131 {
00138         var $ilias;
00139 
00145         var $lng;
00146 
00152         var $mfile;
00153 
00154         var $mail_options;
00155 
00161         var $user_id;
00162 
00168         var $table_mail;
00169 
00175         var $table_mail_saved;
00176 
00182         var $mail_counter;
00183 
00189         var $mail_data;
00190 
00191 
00197         var $mail_obj_ref_id;
00198 
00204         var $mail_send_type;
00205 
00211         var $save_in_sentbox;
00212 
00218         var $mail_rcp_to;
00219         var $mail_rcp_cc;
00220         var $mail_rcp_bc;
00221         var $mail_subject;
00222         var $mail_message;
00223         var $mail_use_placeholders = 0;
00224 
00225         var $soap_enabled = true;
00226         
00227         private $mlists = null;
00228 
00235         function ilMail($a_user_id)
00236         {
00237                 require_once "classes/class.ilFileDataMail.php";
00238                 require_once "Services/Mail/classes/class.ilMailOptions.php";
00239                 require_once "Services/Mail/classes/class.ilMailingLists.php";
00240 
00241                 global $ilias, $lng, $ilUser;
00242 
00243                 $lng->loadLanguageModule("mail");
00244 
00245                 // Initiate variables
00246                 $this->ilias =& $ilias;
00247                 $this->lng   =& $lng;
00248                 $this->table_mail = 'mail';
00249                 $this->table_mail_saved = 'mail_saved';
00250                 $this->user_id = $a_user_id;
00251                 $this->mfile =& new ilFileDataMail($this->user_id);
00252                 $this->mail_options =& new ilMailOptions($a_user_id);
00253                 $this->mlists = new ilMailingLists($ilUser);
00254 
00255                 // DEFAULT: sent mail aren't stored insentbox of user.
00256                 $this->setSaveInSentbox(false);
00257 
00258                 // GET REFERENCE ID OF MAIL OBJECT
00259                 $this->readMailObjectReferenceId();
00260         }
00261         
00262         public function doesRecipientStillExists($a_recipient, $a_existing_recipients)
00263         {               
00264                 if(self::_usePearMail())
00265                 {
00266                         $recipients = $this->explodeRecipients($a_existing_recipients);
00267                         if(is_a($recipients, 'PEAR_Error'))
00268                         {
00269                                 return false;                           
00270                         }
00271                         else
00272                         {
00273                                 foreach($recipients as $rcp)
00274                                 {
00275                                         if (substr($rcp->mailbox, 0, 1) != '#')
00276                                         {
00277                                                 if(trim($rcp->mailbox) == trim($a_recipient) ||
00278                                                    trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
00279                                                 {
00280                                                         return true;
00281                                                 }
00282                                         }
00283                                         else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
00284                                         {
00285                                                 if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
00286                                                 {
00287                                                         return true;
00288                                                 }
00289                                         }
00290                                         else
00291                                         {
00292                                                 if(trim($rcp->mailbox.'@'.$rcp->host) == trim($a_recipient))
00293                                                 {
00294                                                         return true;
00295                                                 }
00296                                         }                                       
00297                                 }
00298                         }
00299                 }
00300                 else
00301                 {               
00302                         $recipients = $this->explodeRecipients($a_existing_recipients);                 
00303                         if(count($recipients))
00304                         {
00305                                 foreach($recipients as $recipient)
00306                                 {
00307                                         if(trim($recipient) == trim($a_recipient))
00308                                         {
00309                                                 return true;
00310                                         }
00311                                 }
00312                         }
00313                 }
00314                 
00315                 return false;
00316         }
00317 
00325         function enableSOAP($a_status)
00326         {
00327                 $this->soap_enabled = $a_status;
00328         }
00329         function isSOAPEnabled()
00330         {
00331                 if(!extension_loaded('curl'))
00332                 {
00333                         return false;
00334                 }
00335                 return (bool) $this->soap_enabled;
00336         }
00337 
00338 
00339         function setSaveInSentbox($a_save_in_sentbox)
00340         {
00341                 $this->save_in_sentbox = $a_save_in_sentbox;
00342         }
00343 
00344         function getSaveInSentbox()
00345         {
00346                 return $this->save_in_sentbox;
00347         }
00348 
00354         function setMailSendType($a_types)
00355         {
00356                 $this->mail_send_type = $a_types;
00357         }
00358 
00364         function setMailRcpTo($a_rcp_to)
00365         {
00366                 $this->mail_rcp_to = $a_rcp_to;
00367         }
00368 
00374         function setMailRcpCc($a_rcp_cc)
00375         {
00376                 $this->mail_rcp_cc = $a_rcp_cc;
00377         }
00378 
00384         function setMailRcpBc($a_rcp_bc)
00385         {
00386                 $this->mail_rcp_bc = $a_rcp_bc;
00387         }
00388 
00394         function setMailSubject($a_subject)
00395         {
00396                 $this->mail_subject = $a_subject;
00397         }
00398 
00404         function setMailMessage($a_message)
00405         {
00406                 $this->mail_message = $a_message;
00407         }
00408 
00413         function readMailObjectReferenceId()
00414         {
00415                 global $ilDB;
00416                 
00417                 // mail settings id is set by a constant in ilias.ini. Keep the select for some time until everyone has updated his ilias.ini
00418                 if (!MAIL_SETTINGS_ID)
00419                 {
00420                         $query = "SELECT object_reference.ref_id FROM object_reference,tree,object_data ".
00421                                         "WHERE tree.parent = ".$ilDB->quote(SYSTEM_FOLDER_ID)." ".
00422                                         "AND object_data.type = 'mail' ".
00423                                         "AND object_reference.ref_id = tree.child ".
00424                                         "AND object_reference.obj_id = object_data.obj_id";
00425                         $res = $this->ilias->db->query($query);
00426 
00427                         while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00428                         {
00429                                 $this->mail_obj_ref_id = $row["ref_id"];
00430                         }
00431                 }
00432                 else
00433                 {
00434                         $this->mail_obj_ref_id = MAIL_SETTINGS_ID;
00435                 }
00436         }
00437 
00438         function getMailObjectReferenceId()
00439         {
00440                 return $this->mail_obj_ref_id;
00441         }
00442         
00443         function formatNotificationSubject()
00444         {
00445                 return $this->lng->txt('mail_notification_subject');
00446         }
00447                 
00448         function formatNotificationMessage($user_id, $mail_data = array())
00449         {
00450                 global $tpl, $lng;
00451                 
00452                 $tpl =& new ilTemplate('tpl.mail_notifications.html', true, true, 'Services/Mail');
00453                 
00454                 $tpl->setVariable('TXT_RECEIVED_MAILS', sprintf($this->lng->txt('mail_received_x_new_mails'), count($mail_data)));
00455                                 
00456                 $counter = 0;
00457                 foreach ($mail_data as $mail)
00458                 {
00459                         $tpl->setCurrentBlock('mails');
00460                         $tpl->setVariable('NR', $counter + 1);                  
00461                         $tpl->setVariable('TXT_SENT', $this->lng->txt('sent'));
00462                         $tpl->setVariable('SEND_TIME', ilFormat::formatDate($mail['send_time']));
00463                         $tpl->setVariable('TXT_SUBJECT', $this->lng->txt('subject'));
00464                         $tpl->setVariable('SUBJECT', $mail['m_subject']);               
00465                         $tpl->parseCurrentBlock();
00466                         
00467                         ++$counter;     
00468                 }               
00469                 
00470                 $message = $this->replacePlaceholders($tpl->get(), $user_id);           
00471                 
00472                 return $message;
00473         }
00474         
00485         public function formatNamesForOutput($users = '')
00486         {
00487                 $users = trim($users);
00488                 if($users)
00489                 {
00490                         if(strstr($users, ','))
00491                         {
00492                                 $rcp_to_array = array();
00493                                 
00494                                 $recipients = explode(',', $users);
00495                                 foreach($recipients as $recipient)
00496                                 {
00497                                         $recipient = trim($recipient);
00498                                         if($uid = ilObjUser::_lookupId($recipient))
00499                                         {
00500                                                 $tmp_obj = new ilObjUser($uid);
00501 
00502                                                 if(ilObjUser::_lookupPref($uid, 'public_profile') == 'y')
00503                                                 {                                                               
00504                                                         $rcp_to_array[] = $tmp_obj->getFullname().' ['.$recipient.']';
00505                                                 }
00506                                                 else
00507                                                 {
00508                                                         $rcp_to_array[] = $recipient;
00509                                                 }
00510                                                 unset($tmp_obj);                
00511                                         }
00512                                         else
00513                                         {
00514                                                 $rcp_to_array[] = $recipient;
00515                                         }                                                       
00516                                 }
00517 
00518                                 return trim(implode(', ', $rcp_to_array));
00519                         }
00520                         else
00521                         {
00522                                 if($uid = ilObjUser::_lookupId($users))
00523                                 {
00524                                         $tmp_obj = new ilObjUser($uid);
00525                                         if(ilObjUser::_lookupPref($uid, 'public_profile') == 'y')
00526                                         {
00527                                                 return $tmp_obj->getFullname().' ['.$users.']';
00528                                         }
00529                                         else
00530                                         {
00531                                                 unset($tmp_obj);
00532                                                 return $users;
00533                                         }                                       
00534                                 }
00535                                 else
00536                                 {
00537                                         return $users;
00538                                 }
00539                         }
00540                 }
00541                 else
00542                 {
00543                         return $this->lng->txt('not_available');
00544                 }
00545         }
00546         
00547         function getPreviousMail($a_mail_id)
00548         {
00549                 global $ilDB;
00550                 
00551                 $query = "SELECT b.* FROM " . $this->table_mail ." AS a ".
00552                                  "INNER JOIN ".$this->table_mail ." AS b ON b.folder_id = a.folder_id AND b.user_id = a.user_id AND b.send_time > a.send_time ".
00553                                  "WHERE a.user_id = ".$ilDB->quote($this->user_id) ." ".                                 
00554                                  "AND a.mail_id = ".$ilDB->quote($a_mail_id)." ORDER BY b.send_time ASC LIMIT 1";
00555                 
00556                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00557                 
00558                 return $this->mail_data; 
00559         }
00560         
00561         function getNextMail($a_mail_id)
00562         {
00563                 global $ilDB;
00564                 
00565                 $query = "SELECT b.* FROM " . $this->table_mail ." AS a ".
00566                                  "INNER JOIN ".$this->table_mail ." AS b ON b.folder_id = a.folder_id AND b.user_id = a.user_id AND b.send_time < a.send_time ".
00567                                  "WHERE a.user_id = ".$ilDB->quote($this->user_id) ." ".                                 
00568                                  "AND a.mail_id = ".$ilDB->quote($a_mail_id)." ORDER BY b.send_time DESC LIMIT 1";
00569                 
00570                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00571                 
00572                 return $this->mail_data;
00573         }
00574 
00581         function getMailsOfFolder($a_folder_id)
00582         {
00583                 global $ilDB;
00584                 
00585                 $this->mail_counter = array();
00586                 $this->mail_counter["read"] = 0;
00587                 $this->mail_counter["unread"] = 0;
00588 
00589                 $query = "SELECT * FROM $this->table_mail ".
00590                         "WHERE user_id = ".$ilDB->quote($this->user_id) ." ".
00591                         "AND folder_id = ".$ilDB->quote($a_folder_id)." ORDER BY send_time DESC";
00592                 
00593                 $res = $this->ilias->db->query($query);
00594 
00595                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00596                 {
00597                         if($row->sender_id and !ilObjectFactory::ObjectIdExists($row->sender_id))
00598                         {
00599                                 continue;
00600                         }
00601                         $tmp = $this->fetchMailData($row);
00602 
00603                         if ($tmp["m_status"] == 'read')
00604                         {
00605                                 ++$this->mail_counter["read"];
00606                         }
00607 
00608                         if ($tmp["m_status"] == 'unread')
00609                         {
00610                                 ++$this->mail_counter["unread"];
00611                         }
00612 
00613                         $output[] = $tmp;
00614                 }
00615 
00616                 $this->mail_counter["total"] = count($output);
00617 
00618                 return $output ? $output : array();
00619         }
00620         
00627         function countMailsOfFolder($a_folder_id)
00628         {
00629                 global $ilDB;           
00630 
00631                 $query = "SELECT COUNT(*) FROM $this->table_mail ".
00632                         "WHERE user_id = ".$ilDB->quote($this->user_id) ." ".
00633                         "AND folder_id = ".$ilDB->quote($a_folder_id)." ";
00634                 
00635                 if (is_object($res = $this->ilias->db->query($query)))
00636                 {
00637                         return $res->numRows(); 
00638                 }
00639                 
00640                 return 0;
00641         }
00642         
00649         function deleteMailsOfFolder($a_folder_id)
00650         {
00651                 if ($a_folder_id)
00652                 {               
00653                         global $ilDB;
00654         
00655                         $query = "DELETE FROM $this->table_mail ".
00656                                 "WHERE user_id = ".$ilDB->quote($this->user_id) ." ".
00657                                 "AND folder_id = ".$ilDB->quote($a_folder_id)." ";
00658                         
00659                         $res = $this->ilias->db->query($query);
00660                         
00661                         return true;
00662                 }
00663                 
00664                 return false;
00665         }
00666 
00673         function getMailCounterData()
00674         {
00675                 return is_array($this->mail_counter) ? $this->mail_counter : array(
00676                         "total"  => 0,
00677                         "read"   => 0,
00678                         "unread" => 0);
00679         }
00680 
00687         function getMail($a_mail_id)
00688         {
00689                 global $ilDB;
00690                 
00691                 $query = "SELECT * FROM $this->table_mail ".
00692                         "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00693                         "AND mail_id = ".$ilDB->quote($a_mail_id)." ";
00694                 
00695                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT));
00696                 
00697                 return $this->mail_data; 
00698         }
00699 
00706         function markRead($a_mail_ids)
00707         {
00708                 global $ilDB;
00709                 // CREATE IN STATEMENT
00710                 $in = "(". implode(",",ilUtil::quoteArray($a_mail_ids)) . ")";
00711                 
00712                 $query = "UPDATE $this->table_mail ".
00713                         "SET m_status = 'read' ".
00714                         "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00715                         "AND mail_id IN $in";
00716 
00717                 $res = $this->ilias->db->query($query);
00718 
00719                 return true;
00720         }
00721 
00728         function markUnread($a_mail_ids)
00729         {
00730                 global $ilDB;
00731                 // CREATE IN STATEMENT
00732                 $in = "(". implode(",",ilUtil::quoteArray($a_mail_ids)) . ")";
00733                 
00734                 $query = "UPDATE $this->table_mail ".
00735                         "SET m_status = 'unread' ".
00736                         "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00737                         "AND mail_id IN $in";
00738 
00739                 $res = $this->ilias->db->query($query);
00740 
00741                 return true;
00742         }
00743 
00751         function moveMailsToFolder($a_mail_ids,$a_folder_id)
00752         {
00753                 global $ilDB;
00754                 // CREATE IN STATEMENT
00755                 $in = "(". implode(",",ilUtil::quoteArray($a_mail_ids)) . ")";
00756 
00757                 $query = "UPDATE $this->table_mail ".
00758                         "SET folder_id = ".$ilDB->quote($a_folder_id)." ".
00759                         "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00760                         "AND mail_id IN $in";
00761 
00762                 $res = $this->ilias->db->query($query);
00763 
00764                 return true;
00765         }
00766 
00773         function deleteMails($a_mail_ids)
00774         {
00775                 global $ilDB;
00776                 
00777                 foreach ($a_mail_ids as $id)
00778                 {
00779                         $query = "DELETE FROM $this->table_mail ".
00780                                 "WHERE user_id = ".$ilDB->quote($this->user_id)." ".
00781                                 "AND mail_id = ".$ilDB->quote($id)." ";
00782                         $res = $this->ilias->db->query($query);
00783                         $this->mfile->deassignAttachmentFromDirectory($id);
00784                 }
00785 
00786                 return true;
00787         }
00788 
00795         function fetchMailData($a_row)
00796         {
00797                 if (!$a_row) return;
00798                 
00799                 return array(
00800                         "mail_id"         => $a_row->mail_id,
00801                         "user_id"         => $a_row->user_id,
00802                         "folder_id"       => $a_row->folder_id,
00803                         "sender_id"       => $a_row->sender_id,
00804                         "attachments"     => unserialize(stripslashes($a_row->attachments)), 
00805                         "send_time"       => $a_row->send_time,
00806                         "rcp_to"          => stripslashes($a_row->rcp_to),
00807                         "rcp_cc"          => stripslashes($a_row->rcp_cc),
00808                         "rcp_bcc"         => stripslashes($a_row->rcp_bcc),
00809                         "m_status"        => $a_row->m_status,
00810                         "m_type"          => unserialize(stripslashes($a_row->m_type)),
00811                         "m_email"         => $a_row->m_email,
00812                         "m_subject"       => stripslashes($a_row->m_subject),
00813                         "m_message"       => stripslashes($a_row->m_message),                   
00814                         "import_name"     => stripslashes($a_row->import_name),
00815                         "use_placeholders"=> $a_row->use_placeholders);
00816         }
00817 
00818         function updateDraft($a_folder_id,
00819                                                  $a_attachments,
00820                                                  $a_rcp_to,
00821                                                  $a_rcp_cc,
00822                                                  $a_rcp_bcc,
00823                                                  $a_m_type,
00824                                                  $a_m_email,
00825                                                  $a_m_subject,
00826                                                  $a_m_message,
00827                                                  $a_draft_id = 0, $a_use_placeholders = 0)
00828         {
00829                 global $ilDB;
00830                 
00831                 $query = "UPDATE $this->table_mail ".
00832                         "SET folder_id = ".$ilDB->quote($a_folder_id).",".
00833                         "attachments = '".addslashes(serialize($a_attachments))."',".
00834                         "send_time = now(),".
00835                         "rcp_to = ".$ilDB->quote($a_rcp_to).",".
00836                         "rcp_cc = ".$ilDB->quote($a_rcp_cc).",".
00837                         "rcp_bcc = ".$ilDB->quote($a_rcp_bcc).",".
00838                         "m_status = 'read',".
00839                         "m_type = '".addslashes(serialize($a_m_type))."',".
00840                         "m_email = ".$ilDB->quote($a_m_email).",".
00841                         "m_subject = ".$ilDB->quote($a_m_subject).",".
00842                         "m_message = ".$ilDB->quote($a_m_message).",".
00843                         "use_placeholders = ".$ilDB->quote($a_use_placeholders)." ".
00844                         "WHERE mail_id = ".$ilDB->quote($a_draft_id)."";
00845                         
00846                 $res = $this->ilias->db->query($query);
00847 
00848                 return $a_draft_id;
00849         }
00850 
00868         function sendInternalMail($a_folder_id,
00869                                                           $a_sender_id,
00870                                                           $a_attachments,
00871                                                           $a_rcp_to,
00872                                                           $a_rcp_cc,
00873                                                           $a_rcp_bcc,
00874                                                           $a_status,
00875                                                           $a_m_type,
00876                                                           $a_m_email,
00877                                                           $a_m_subject,
00878                                                           $a_m_message,
00879                                                           $a_user_id = 0, $a_use_placeholders = 0)
00880         {
00881                 $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
00882 
00883                 global $ilDB, $log;
00884                 //$log->write('class.ilMail->sendInternalMail to user_id:'.$a_rcp_to.' '.$a_m_message);
00885 
00886                 if ($a_use_placeholders) $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
00887 
00888                 $query = "INSERT INTO $this->table_mail ".
00889                         "SET user_id = ".$ilDB->quote($a_user_id).",".
00890                         "folder_id = ".$ilDB->quote($a_folder_id).",".
00891                         "sender_id = ".$ilDB->quote($a_sender_id).",".
00892                         "attachments = '".addslashes(serialize($a_attachments))."',".
00893                         "send_time = now(),".
00894                         "rcp_to = ".$ilDB->quote($a_rcp_to).",".
00895                         "rcp_cc = ".$ilDB->quote($a_rcp_cc).",".
00896                         "rcp_bcc = ".$ilDB->quote($a_rcp_bcc).",".
00897                         "m_status = ".$ilDB->quote($a_status).",".
00898                         "m_type = '".addslashes(serialize($a_m_type))."',".
00899                         "m_email = ".$ilDB->quote($a_m_email).",".
00900                         "m_subject = ".$ilDB->quote($a_m_subject).",".
00901                         "m_message = ".$ilDB->quote($a_m_message)." ";
00902 
00903                 $res = $this->ilias->db->query($query);
00904                 $query = "SELECT LAST_INSERT_ID() as id";
00905                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_ASSOC);
00906 
00907                 return $row["id"];
00908         }
00909         
00910         function replacePlaceholders($a_message, $a_user_id)
00911         {               
00912                 global $lng;
00913                 
00914                 $user = new ilObjUser($a_user_id);              
00915                 
00916                 // determine salutation         
00917                 switch ($user->getGender())
00918                 {
00919                         case 'f':       $gender_salut = $lng->txt('salutation_f');
00920                                                 break;
00921                         case 'm':       $gender_salut = $lng->txt('salutation_m');
00922                                                 break;
00923         }
00924 
00925                 $a_message = str_replace('[MAIL_SALUTATION]', $gender_salut, $a_message);
00926                 $a_message = str_replace('[LOGIN]', $user->getLogin(), $a_message);
00927                 $a_message = str_replace('[FIRST_NAME]', $user->getFirstname(), $a_message);
00928                 $a_message = str_replace('[LAST_NAME]', $user->getLastname(), $a_message);
00929                 $a_message = str_replace('[ILIAS_URL]', ILIAS_HTTP_PATH.'/login.php?client_id='.CLIENT_ID, $a_message);
00930                 $a_message = str_replace('[CLIENT_NAME]', CLIENT_NAME, $a_message);
00931                 
00932                 unset($user);
00933         
00934                 return $a_message;
00935         }
00936         
00950         function distributeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_subject,$a_message,$a_attachments,$sent_mail_id,$a_type,$a_action, $a_use_placeholders = 0)
00951         {
00952                 global $log;
00953 
00954                 include_once 'Services/Mail/classes/class.ilMailbox.php';
00955                 include_once './Services/User/classes/class.ilObjUser.php';
00956 
00957                 if (!ilMail::_usePearMail())
00958                 {
00959                         // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
00960                         $a_rcp_to = $this->__substituteRecipients($a_rcp_to, 'resubstitute');
00961                         $a_rcp_cc = $this->__substituteRecipients($a_rcp_cc, 'resubstitute');
00962                         $a_rcp_bc = $this->__substituteRecipients($a_rcp_bc, 'resubstitute');
00963                 }
00964 
00965                 $mbox =& new ilMailbox();
00966                 
00967                 if (!$a_use_placeholders) # No Placeholders
00968                 {                       
00969                         $rcp_ids = $this->getUserIds(trim($a_rcp_to).','.trim($a_rcp_cc).','.trim($a_rcp_bcc));
00970                         
00971                         $as_email = array();
00972                                                 
00973                         foreach($rcp_ids as $id)
00974                         {
00975                                 $tmp_mail_options =& new ilMailOptions($id);
00976         
00977                                 // DETERMINE IF THE USER CAN READ INTERNAL MAILS
00978                                 $tmp_user =& new ilObjUser($id);
00979                                 $tmp_user->read();
00980                                 $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() && 
00981                                                                                             $tmp_user->getActive() && 
00982                                                                                             $tmp_user->checkTimeLimit();
00983         
00984                                 // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
00985                                 if (in_array('system', $a_type) && !$user_can_read_internal_mails)
00986                                 {
00987                                         continue;
00988                                 }
00989         
00990                                 // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS/HER MAIL
00991                                 // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
00992                                 if (!$user_can_read_internal_mails ||
00993                                         $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
00994                                 {
00995                                         $as_email[] = $id;
00996                                         continue;
00997                                 }
00998         
00999                                 if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
01000                                 {
01001                                         $as_email[] = $id;
01002                                 }
01003         
01004                                 $mbox->setUserId($id);
01005                                 $inbox_id = $mbox->getInboxFolder();
01006         
01007                                 $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
01008                                                                           $a_attachments, $a_rcp_to,
01009                                                                           $a_rcp_cc, '', 'unread', $a_type,
01010                                                                           0, $a_subject, $a_message, $id, 0);
01011                                 if ($a_attachments)
01012                                 {
01013                                         $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
01014                                 }
01015                         }
01016                         
01017                         // SEND EMAIL TO ALL USERS WHO DECIDED 'email' or 'both'
01018                         $to = array();
01019                         $bcc = array();
01020                         
01021                         if (count($as_email) == 1)
01022                         {
01023                                 $to[] = ilObjUser::_lookupEmail($as_email[0]); 
01024                         }
01025                         else
01026                         {
01027                                 foreach ($as_email as $id)
01028                                 {
01029                                         $bcc[] = ilObjUser::_lookupEmail($id);
01030                                 }
01031                         }
01032                         
01033                         if(count($to) > 0 || count($bcc) > 0)
01034                         {
01035                                 $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $a_message, $a_attachments);
01036                         }
01037                 }
01038                 else # Use Placeholders
01039                 {
01040                         // to
01041                         $rcp_ids_replace = $this->getUserIds(trim($a_rcp_to));
01042                         
01043                         // cc / bcc
01044                         $rcp_ids_no_replace = $this->getUserIds(trim($a_rcp_cc).','.trim($a_rcp_bcc));
01045                         
01046                         $as_email = array();                    
01047                         
01048                         // to
01049                         foreach($rcp_ids_replace as $id)
01050                         {
01051                                 $tmp_mail_options =& new ilMailOptions($id);
01052         
01053                                 // DETERMINE IF THE USER CAN READ INTERNAL MAILS
01054                                 $tmp_user =& new ilObjUser($id);
01055                                 $tmp_user->read();
01056                                 $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() &&
01057                                                                                             $tmp_user->getActive() &&
01058                                                                                             $tmp_user->checkTimeLimit();
01059         
01060                                 // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
01061                                 if (in_array('system', $a_type) && !$user_can_read_internal_mails)
01062                                 {
01063                                         continue;
01064                                 }
01065         
01066                                 // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
01067                                 // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
01068                                 if (!$user_can_read_internal_mails ||
01069                                         $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
01070                                 {
01071                                         $as_email[] = $id;
01072                                         continue;
01073                                 }
01074         
01075                                 if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
01076                                 {
01077                                         $as_email[] = $id;
01078                                 }
01079         
01080                                 $mbox->setUserId($id);
01081                                 $inbox_id = $mbox->getInboxFolder();
01082         
01083                                 $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
01084                                                                           $a_attachments, $a_rcp_to,
01085                                                                           $a_rcp_cc, '', 'unread', $a_type,
01086                                                                           0, $a_subject, $a_message, $id, 1);
01087                                 if ($a_attachments)
01088                                 {
01089                                         $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
01090                                 }
01091                         }
01092                         
01093                         if (count($as_email))
01094                         {
01095                                 foreach ($as_email as $id)
01096                                 {                                       
01097                                         $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $this->replacePlaceholders($a_message, $id), $a_attachments);
01098                                 }
01099                         }
01100                         
01101                         $as_email = array();
01102                         
01103                         // cc / bcc
01104                         foreach($rcp_ids_no_replace as $id)
01105                         {
01106                                 $tmp_mail_options =& new ilMailOptions($id);
01107         
01108                                 // DETERMINE IF THE USER CAN READ INTERNAL MAILS
01109                                 $tmp_user =& new ilObjUser($id);
01110                                 $tmp_user->read();
01111                                 $user_can_read_internal_mails = $tmp_user->hasAcceptedUserAgreement() 
01112                                         && $tmp_user->getActive() && $tmp_user->checkTimeLimit();
01113         
01114                                 // CONTINUE IF SYSTEM MESSAGE AND USER CAN'T READ INTERNAL MAILS
01115                                 if (in_array('system', $a_type) && !$user_can_read_internal_mails)
01116                                 {
01117                                         continue;
01118                                 }
01119         
01120                                 // CONTINUE IF USER CAN'T READ INTERNAL MAILS OR IF HE/SHE WANTS HIS MAIL
01121                                 // SENT TO HIS/HER EXTERNAL E-MAIL ADDRESS ONLY
01122                                 if (!$user_can_read_internal_mails ||
01123                                         $tmp_mail_options->getIncomingType() == $this->mail_options->EMAIL)
01124                                 {
01125                                         $as_email[] = $id;
01126                                         continue;
01127                                 }
01128         
01129                                 if ($tmp_mail_options->getIncomingType() == $this->mail_options->BOTH)
01130                                 {
01131                                         $as_email[] = $id;
01132                                 }
01133         
01134                                 $mbox->setUserId($id);
01135                                 $inbox_id = $mbox->getInboxFolder();
01136         
01137                                 $mail_id = $this->sendInternalMail($inbox_id, $this->user_id,
01138                                                                           $a_attachments, $a_rcp_to,
01139                                                                           $a_rcp_cc, '', 'unread', $a_type,
01140                                                                           0, $a_subject, $a_message, $id, 0);
01141                                 if ($a_attachments)
01142                                 {
01143                                         $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id, $a_attachments);
01144                                 }
01145                         }
01146                         
01147                         if (count($as_email))
01148                         {
01149                                 foreach ($as_email as $id)
01150                                 {                                       
01151                                         $this->sendMimeMail(ilObjUser::_lookupEmail($id), '', '', $a_subject, $a_message, $a_attachments);
01152                                 }
01153                         }
01154                 }       
01155                 
01156                 return true;
01157         }       
01158 
01164         function getUserIds($a_recipients)
01165         {
01166                 global $log, $rbacreview;
01167                 $ids = array();
01168 
01169                 if (ilMail::_usePearMail())
01170                 {
01171                         $tmp_names = $this->explodeRecipients($a_recipients);
01172                         if (! is_a($tmp_names, 'PEAR_Error'))
01173                         {
01174                                 for ($i = 0;$i < count($tmp_names); $i++)
01175                                 {                                       
01176                                         if (substr($tmp_names[$i]->mailbox,0,1) === '#')
01177                                         {
01178                                                 $role_ids = $rbacreview->searchRolesByMailboxAddressList($tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
01179                                                 foreach($role_ids as $role_id)
01180                                                 {
01181                                                         foreach($rbacreview->assignedUsers($role_id) as $usr_id)
01182                                                         {
01183                                                                 $ids[] = $usr_id;
01184                                                         }
01185                                                 }
01186                                         }
01187                                         else if (strtolower($tmp_names[$i]->host) == 'ilias')
01188                                         {
01189                                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i]->mailbox)))
01190                                                 {
01191                                                         //$log->write('class.ilMail->getUserIds() recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host.' user_id:'.$id);
01192                                                         $ids[] = $id;
01193                                                 }
01194                                                 else
01195                                                 {
01196                                                         //$log->write('class.ilMail->getUserIds() no user account found for recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
01197                                                 }
01198                                         }
01199                                         else
01200                                         {
01201                                                 //$log->write('class.ilMail->getUserIds() external recipient:'.$tmp_names[$i]->mailbox.'@'.$tmp_names[$i]->host);
01202                                         }
01203                                 }
01204                         }
01205                         else
01206                         {
01207                                 //$log->write('class.ilMail->getUserIds() illegal recipients:'.$a_recipients);
01208                         }
01209                 }
01210                 else
01211                 {
01212                         $tmp_names = $this->explodeRecipients($a_recipients);
01213                         for ($i = 0;$i < count($tmp_names); $i++)
01214                         {
01215                                 if (substr($tmp_names[$i],0,1) == '#')
01216                                 {
01217                                         if(ilUtil::groupNameExists(addslashes(substr($tmp_names[$i],1))))
01218                                         {
01219                                                 include_once("./classes/class.ilObjectFactory.php");
01220                                                 include_once('./classes/class.ilObjGroup.php');
01221                                                 
01222                                                 foreach(ilObject::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
01223                                                 {
01224                                                         $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
01225                                                         break;
01226                                                 }
01227                                                 // STORE MEMBER IDS IN $ids
01228                                                 foreach ($grp_object->getGroupMemberIds() as $id)
01229                                                 {
01230                                                         $ids[] = $id;
01231                                                 }
01232                                         }
01233                                         // is role: get role ids
01234                                         elseif($role_id = $rbacreview->roleExists(addslashes(substr($tmp_names[$i],1))))
01235                                         {
01236                                                 foreach($rbacreview->assignedUsers($role_id) as $usr_id)
01237                                                 {
01238                                                         $ids[] = $usr_id;
01239                                                 }
01240                                         }
01241                                         
01242                                 }
01243                                 else if (!empty($tmp_names[$i]))
01244                                 {
01245                                         if ($id = ilObjUser::getUserIdByLogin(addslashes($tmp_names[$i])))
01246                                         {
01247                                                 $ids[] = $id;
01248                                         }
01249                                 }
01250                         }
01251                 }
01252                 return array_unique($ids);
01253         }
01254 
01265         function checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_type)
01266         {
01267                 $error_message = '';
01268 
01269                 if (empty($a_m_subject))
01270                 {
01271                         $error_message .= $error_message ? "<br>" : '';
01272                         $error_message .= $this->lng->txt("mail_add_subject");
01273                 }
01274 
01275                 if (empty($a_rcp_to))
01276                 {
01277                         $error_message .= $error_message ? "<br>" : '';
01278                         $error_message .= $this->lng->txt("mail_add_recipient");
01279                 }
01280 
01281                 return $error_message;
01282         }
01283 
01290         function getEmailsOfRecipients($a_rcp)
01291         {
01292                 $addresses = array();
01293 
01294                 if (ilMail::_usePearMail())
01295                 {
01296                         $tmp_rcp = $this->explodeRecipients($a_rcp);
01297                         if (! is_a($tmp_rcp, 'PEAR_Error'))
01298                         {
01299                                 foreach ($tmp_rcp as $rcp)
01300                                 {
01301                                         // NO GROUP
01302                                         if (substr($rcp->mailbox,0,1) != '#')
01303                                         {
01304                                                 if (strtolower($rcp->host) != 'ilias')
01305                                                 {
01306                                                         $addresses[] = $rcp->mailbox.'@'.$rcp->host;
01307                                                         continue;
01308                                                 }
01309                 
01310                                                 if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)))
01311                                                 {
01312                                                         $tmp_user = new ilObjUser($id);
01313                                                         $addresses[] = $tmp_user->getEmail();
01314                                                         continue;
01315                                                 }
01316                                         }
01317                                         else
01318                                         {
01319                                                 // Roles
01320                                                 $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
01321                                                 foreach($role_ids as $role_id)
01322                                                 {
01323                                                         foreach($rbacreview->assignedUsers($role_id) as $usr_id)
01324                                                         {
01325                                                                 $tmp_user = new ilObjUser($usr_id);
01326                                                                 $addresses[] = $tmp_user->getEmail();
01327                                                         }
01328                                                 }
01329                                         }
01330                                 }
01331                         }
01332                 }
01333                 else
01334                 {
01335                         $tmp_rcp = $this->explodeRecipients($a_rcp);
01336         
01337                         foreach ($tmp_rcp as $rcp)
01338                         {
01339                                 // NO GROUP
01340                                 if (substr($rcp,0,1) != '#')
01341                                 {
01342                                         if (strpos($rcp,'@'))
01343                                         {
01344                                                 $addresses[] = $rcp;
01345                                                 continue;
01346                                         }
01347         
01348                                         if ($id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
01349                                         {
01350                                                 $tmp_user = new ilObjUser($id);
01351                                                 $addresses[] = $tmp_user->getEmail();
01352                                                 continue;
01353                                         }
01354                                 }
01355                                 else
01356                                 {
01357                                         // GROUP THINGS
01358                                         include_once("./classes/class.ilObjectFactory.php");
01359                                         include_once('./classes/class.ilObjGroup.php');
01360         
01361                                         // Fix 
01362                                         foreach(ilObjGroup::_getAllReferences(ilObjGroup::_lookupIdByTitle(addslashes(substr($tmp_names[$i],1)))) as $ref_id)
01363                                         {
01364                                                 $grp_object = ilObjectFactory::getInstanceByRefId($ref_id);
01365                                                 break;
01366                                         }
01367                                         // GET EMAIL OF MEMBERS AND STORE THEM IN $addresses
01368                                         foreach ($grp_object->getGroupMemberIds() as $id)
01369                                         {
01370                                                 $tmp_user = new ilObjUser($id);
01371                                                 $addresses[] = $tmp_user->getEmail();
01372                                         } 
01373                                 }
01374                         }
01375                 }
01376 
01377                 return $addresses;
01378         }
01379                 
01387         function checkRecipients($a_recipients,$a_type)
01388         {
01389                 global $rbacsystem,$rbacreview;
01390                 $wrong_rcps = '';
01391 
01392                 if (ilMail::_usePearMail())
01393                 {
01394                         $tmp_rcp = $this->explodeRecipients($a_recipients);
01395                         if (is_a($tmp_rcp, 'PEAR_Error'))
01396                         {
01397                                 $colon_pos = strpos($tmp_rcp->message, ':');
01398                                 $wrong_rcps = '<br />'.(($colon_pos === false) ? $tmp_rcp->message : substr($tmp_rcp->message, $colon_pos+2));
01399                         }
01400                         else
01401                         {
01402                                 foreach ($tmp_rcp as $rcp)
01403                                 {
01404                                         // NO ROLE MAIL ADDRESS
01405                                         if (substr($rcp->mailbox,0,1) != '#')
01406                                         {
01407                                                 // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
01408                                                 $user_id = ($rcp->host == 'ilias') ? ilObjUser::getUserIdByLogin(addslashes($rcp->mailbox)) : false;
01409                                                 if ($user_id == false && $rcp->host == 'ilias')
01410                                                 {
01411                                                         $wrong_rcps .= "<br />".htmlentities($rcp->mailbox);
01412                                                         continue;
01413                                                 }
01414                                                 
01415                                                 // CHECK IF USER CAN RECEIVE MAIL
01416                                                 if ($user_id)
01417                                                 {
01418                                                         if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
01419                                                         {
01420                                                                 $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).
01421                                                                         " (".$this->lng->txt("user_cant_receive_mail").")";
01422                                                                 continue;
01423                                                         }
01424                                                 }
01425                                         }
01426                                         else if (substr($rcp->mailbox, 0, 7) == '#il_ml_')
01427                                         {
01428                                                 if (!$this->mlists->mailingListExists($rcp->mailbox))
01429                                                 {                                       
01430                                                         $wrong_rcps .= "<br />".htmlentities($rcp->mailbox).    
01431                                                                 " (".$this->lng->txt("mail_no_valid_mailing_list").")";
01432                                                 }
01433                                                 
01434                                                 continue;
01435                                         }
01436                                         else
01437                                         {
01438                                                 $role_ids = $rbacreview->searchRolesByMailboxAddressList($rcp->mailbox.'@'.$rcp->host);
01439                                                 if (count($role_ids) == 0)
01440                                                 {
01441                                                         $wrong_rcps .= '<br />'.htmlentities($rcp->mailbox).
01442                                                                 ' ('.$this->lng->txt('mail_no_recipient_found').')';
01443                                                         continue;
01444                                                 }
01445                                                 else if (count($role_ids) > 1)
01446                                                 {
01447                                                         $wrong_rcps .= '<br/>'.htmlentities($rcp->mailbox).
01448                                                                 ' ('.sprintf($this->lng->txt('mail_multiple_recipients_found'), implode(',', $role_ids)).')';
01449                                                 }
01450                                         }
01451                                 }
01452                         }
01453                 }
01454                 else
01455                 {
01456                         $tmp_rcp = $this->explodeRecipients($a_recipients);
01457 
01458                         foreach ($tmp_rcp as $rcp)
01459                         {
01460                                 if (empty($rcp))
01461                                 {
01462                                         continue;
01463                                 }
01464                                 // NO GROUP
01465                                 if (substr($rcp,0,1) != '#')
01466                                 {
01467                                         // ALL RECIPIENTS MUST EITHER HAVE A VALID LOGIN OR A VALID EMAIL
01468                                         if (!ilObjUser::getUserIdByLogin(addslashes($rcp)) and
01469                                                 !ilUtil::is_email($rcp))
01470                                         {
01471                                                 $wrong_rcps .= "<br />".htmlentities($rcp);
01472                                                 continue;
01473                                         }
01474 
01475                                         // CHECK IF USER CAN RECEIVE MAIL
01476                                         if ($user_id = ilObjUser::getUserIdByLogin(addslashes($rcp)))
01477                                         {
01478                                                 if(!$rbacsystem->checkAccessOfUser($user_id, "mail_visible", $this->getMailObjectReferenceId()))
01479                                                 {
01480                                                         $wrong_rcps .= "<br />".htmlentities($rcp).
01481                                                                 " (".$this->lng->txt("user_cant_receive_mail").")";
01482                                                         continue;
01483                                                 }
01484                                         }
01485                                 }
01486                                 else if (substr($rcp, 0, 7) == '#il_ml_')
01487                                 {
01488                                         if (!$this->mlists->mailingListExists($rcp))
01489                                         {                                       
01490                                                 $wrong_rcps .= "<br />".htmlentities($rcp).     
01491                                                         " (".$this->lng->txt("mail_no_valid_mailing_list").")";
01492                                         }
01493                                         
01494                                         continue;
01495                                 }
01496                                 else if (ilUtil::groupNameExists(addslashes(substr($rcp,1))))
01497                                 {
01498                                         continue;
01499                                 }
01500                                 else if (!$rbacreview->roleExists(addslashes(substr($rcp,1))))
01501                                 {
01502                                         $wrong_rcps .= "<br />".htmlentities($rcp).     
01503                                                 " (".$this->lng->txt("mail_no_valid_group_role").")";
01504                                         continue;
01505                                 }                               
01506                         }
01507                 }
01508                 return $wrong_rcps;
01509         }
01510 
01526         function savePostData($a_user_id,
01527                                                   $a_attachments,
01528                                                   $a_rcp_to,
01529                                                   $a_rcp_cc,
01530                                                   $a_rcp_bcc,
01531                                                   $a_m_type,
01532                                                   $a_m_email,
01533                                                   $a_m_subject,
01534                                                   $a_m_message,
01535                                                   $a_use_placeholders)
01536         {
01537                 global $ilDB;
01538                 
01539                 $query = "DELETE FROM $this->table_mail_saved ".
01540                         "WHERE user_id = ".$ilDB->quote($this->user_id)." ";
01541                 $res = $this->ilias->db->query($query);
01542 
01543                 $query = "INSERT INTO $this->table_mail_saved ".
01544                         "SET user_id = ".$ilDB->quote($a_user_id).",".
01545                         "attachments = '".addslashes(serialize($a_attachments))."',".
01546                         "rcp_to = ".$ilDB->quote($a_rcp_to).",".
01547                         "rcp_cc = ".$ilDB->quote($a_rcp_cc).",".
01548                         "rcp_bcc = ".$ilDB->quote($a_rcp_bcc).",".
01549                         "m_type = '".addslashes(serialize($a_m_type))."',".
01550                         "m_email = '',".
01551                         "m_subject = ".$ilDB->quote($a_m_subject).",".
01552                         "m_message = ".$ilDB->quote($a_m_message).",".
01553                         "use_placeholders = ".$ilDB->quote($a_use_placeholders)."";
01554                 $res = $this->ilias->db->query($query);
01555 
01556                 $this->getSavedData();
01557 
01558                 return true;
01559         }
01560 
01566         function getSavedData()
01567         {
01568                 global $ilDB;
01569                 
01570                 $query = "SELECT * FROM $this->table_mail_saved ".
01571                         "WHERE user_id = ".$ilDB->quote($this->user_id)." ";
01572 
01573                 $this->mail_data = $this->fetchMailData($this->ilias->db->getRow($query, DB_FETCHMODE_OBJECT));
01574 
01575                 return $this->mail_data;
01576         }
01577 
01591         function sendMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$a_type, $a_use_placeholders = 0)
01592         {
01593                 global $lng,$rbacsystem,$log;
01594                 //$log->write('class.ilMail.sendMail '.$a_rcp_to.' '.$a_m_subject);
01595                 $error_message = '';
01596                 $message = '';
01597 
01598                 if (in_array("system",$a_type))
01599                 {
01600                         $this->__checkSystemRecipients($a_rcp_to);
01601                         $a_type = array('system');
01602                 }
01603 
01604                 if ($a_attachment)
01605                 {
01606                         if (!$this->mfile->checkFilesExist($a_attachment))
01607                         {
01608                                 return "YOUR LIST OF ATTACHMENTS IS NOT VALID, PLEASE EDIT THE LIST";
01609                         }
01610                 }
01611                 // CHECK NECESSARY MAIL DATA FOR ALL TYPES
01612                 if ($error_message = $this->checkMail($a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_m_subject,$a_m_message,$a_type))
01613                 {
01614                         return $error_message;
01615                 }
01616                 // check recipients
01617                 if ($error_message = $this->checkRecipients($a_rcp_to,$a_type))
01618                 {
01619                         $message .= $error_message;
01620                 }
01621 
01622                 if ($error_message = $this->checkRecipients($a_rcp_cc,$a_type))
01623                 {
01624                         $message .= $error_message;
01625                 }
01626 
01627                 if ($error_message = $this->checkRecipients($a_rcp_bc,$a_type))
01628                 {
01629                         $message .= $error_message;
01630                 }
01631                 // if there was an error
01632                 if (!empty($message))
01633                 {
01634                         return $this->lng->txt("mail_following_rcp_not_valid").$message;
01635                 }
01636 
01637                 // CHECK FOR SYSTEM MAIL
01638                 if (in_array('system',$a_type))
01639                 {
01640                         if (!empty($a_attachment))
01641                         {
01642                                 return $lng->txt("mail_no_attach_allowed");
01643                         }
01644                 }
01645                 
01646                 // ACTIONS FOR ALL TYPES
01647                 // GET RCPT OF MAILING LISTS
01648                 $rcp_to = $this->parseRcptOfMailingLists($a_rcp_to);
01649                 $rcp_cc = $this->parseRcptOfMailingLists($a_rcp_cc);
01650                 $rcp_bc = $this->parseRcptOfMailingLists($a_rcp_bc);
01651 
01652                 if (! ilMail::_usePearMail())
01653                 {
01654                         // REPLACE ALL LOGIN NAMES WITH '@' BY ANOTHER CHARACTER
01655                         $rcp_to = $this->__substituteRecipients($rcp_to,"substitute");
01656                         $rcp_cc = $this->__substituteRecipients($rcp_cc,"substitute");
01657                         $rcp_bc = $this->__substituteRecipients($rcp_bc,"substitute");
01658                 }
01659 
01660                 // COUNT EMAILS
01661                 $c_emails = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,true);
01662                 $c_rcp = $this->__getCountRecipients($rcp_to,$rcp_cc,$rcp_bc,false);
01663 
01664                 // currently disabled..
01665                 /*
01666                 if (count($c_emails))
01667                 {
01668                         if (!$this->getEmailOfSender())
01669                         {
01670                                 return $lng->txt("mail_check_your_email_addr");
01671                         }
01672                 }
01673                 */
01674                 
01675                 // check smtp permission
01676                 if($c_emails && $this->user_id != ANONYMOUS_USER_ID && 
01677                    !$rbacsystem->checkAccess('smtp_mail', $this->mail_obj_ref_id))
01678                 {
01679                         return $this->lng->txt('mail_no_permissions_write_smtp');
01680                 }                       
01681                         
01682                 // save mail in sent box
01683                 $sent_id = $this->saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bc,$a_type,
01684                                                                                 $a_m_subject,$a_m_message);
01685                                                                                 
01686                 if($a_attachment)
01687                 {
01688                         $this->mfile->assignAttachmentsToDirectory($sent_id,$sent_id);
01689 
01690                         if ($error = $this->mfile->saveFiles($sent_id,$a_attachment))
01691                         {
01692                                 return $error;
01693                         }
01694                 }
01695 
01696                 // FILTER EMAILS
01697                 // IF EMAIL RECIPIENT
01698                 if($c_emails)
01699                 {               
01700                         $this->sendMimeMail($this->__getEmailRecipients($rcp_to),
01701                                                                 $this->__getEmailRecipients($rcp_cc),
01702                                                                 $this->__getEmailRecipients($rcp_bc),
01703                                                                 $a_m_subject,
01704                                                                 $a_m_message,
01705                                                                 $a_attachment,
01706                                                                 0);
01707                 }
01708 
01709                 if (in_array('system',$a_type))
01710                 {
01711                         if (!$this->distributeMail($rcp_to,$rcp_cc,$rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$sent_id,$a_type,'system', $a_use_placeholders))
01712                         {
01713                                 return $lng->txt("mail_send_error");
01714                         }
01715                 }
01716                 // ACTIONS FOR TYPE SYSTEM AND NORMAL
01717                 if (in_array('normal',$a_type))
01718                 {
01719                         // TRY BOTH internal and email (depends on user settings)
01720                         if (!$this->distributeMail($rcp_to,$rcp_cc,$rcp_bc,$a_m_subject,$a_m_message,$a_attachment,$sent_id,$a_type,'normal', $a_use_placeholders))
01721                         {
01722                                 return $lng->txt("mail_send_error");
01723                         }
01724                 }
01725 
01726                 // Temporary bugfix
01727                 if (!$this->getSaveInSentbox())
01728                 {
01729                         $this->deleteMails(array($sent_id));
01730                 }
01731 
01732                 return '';
01733         }
01734         
01735         function parseRcptOfMailingLists($rcpt = '')
01736         {
01737                 if ($rcpt == '') return $rcpt;
01738                 
01739                 $arrRcpt = $this->explodeRecipients(trim($rcpt));
01740                 if (!is_array($arrRcpt) || empty($arrRcpt)) return $rcpt;
01741                 
01742                 $new_rcpt = array();
01743                 
01744                 foreach ($arrRcpt as $item)
01745                 {
01746                         if (ilMail::_usePearMail())
01747                         {
01748                                 if (substr($item->mailbox, 0, 7) == '#il_ml_')
01749                                 {
01750                                         if ($this->mlists->mailingListExists($item->mailbox))
01751                                         {
01752                                                 foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
01753                                                 {
01754                                                         $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
01755                                                 }
01756                                         }
01757                                 }
01758                                 else
01759                                 {
01760                                         $new_rcpt[] = $item->mailbox.'@'.$item->host;
01761                                 }
01762                         }
01763                         else
01764                         {
01765                                 if (substr($item, 0, 7) == '#il_ml_')
01766                                 {
01767                                         if ($this->mlists->mailingListExists($item))
01768                                         {
01769                                                 foreach ($this->mlists->getCurrentMailingList()->getAssignedEntries() as $entry)
01770                                                 {
01771                                                         $new_rcpt[] = ($entry['login'] != '' ? $entry['login'] : $entry['email']);
01772                                                 }
01773                                         }       
01774                                 }
01775                                 else
01776                                 {
01777                                         $new_rcpt[] = $item;
01778                                 }
01779                         }               
01780                 }               
01781                 
01782                 return implode(',', $new_rcpt);
01783         }
01784 
01797         function saveInSentbox($a_attachment,$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_type,
01798                                                    $a_m_subject,$a_m_message)
01799         {
01800                 include_once "Services/Mail/classes/class.ilMailbox.php";
01801 
01802                 $mbox = new ilMailbox($this->user_id);
01803                 $sent_id = $mbox->getSentFolder();
01804 
01805                 return $this->sendInternalMail($sent_id,$this->user_id,$a_attachment,$a_rcp_to,$a_rcp_cc,
01806                                                                                 $a_rcp_bcc,'read',$a_type,$a_as_email,$a_m_subject,$a_m_message,$this->user_id, 0);
01807         }
01808 
01809 
01810         
01816         function addFullname($a_email)
01817         {
01818                 include_once 'Services/Mail/classes/class.ilMimeMail.php';
01819                 
01820                 global $ilUser;
01821 
01822                 return ilMimeMail::_mimeEncode($ilUser->getFullname()).'<'.$a_email.'>';
01823         }
01824 
01837         function sendMimeMail($a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments)
01838         {
01839                 #var_dump("<pre>",$a_rcp_to,$a_rcp_cc,$a_rcp_bcc,$a_m_subject,$a_m_message,$a_attachments,"<pre>");
01840                 
01841                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
01842                 $a_m_subject = "[".$inst_name."] ".$a_m_subject;
01843                 
01844                 if($this->user_id != ANONYMOUS_USER_ID)
01845                 {
01846                         $sender = $this->addFullname($this->getEmailOfSender());        
01847                 }
01848                 else
01849                 {
01850                         if(trim($this->ilias->getSetting('mail_external_sender_noreply')) != '')
01851                         {
01852                                 include_once "Services/Mail/classes/class.ilMimeMail.php";
01853                                 $sender = ilMimeMail::_mimeEncode(self::_getAnonymousName()).
01854                                                   '<noreply@'.trim($this->ilias->getSetting('mail_external_sender_noreply')).'>';
01855                         }
01856                         else
01857                         {
01858                                 include_once "Services/Mail/classes/class.ilMimeMail.php";
01859                                 $sender = ilMimeMail::_mimeEncode(self::_getAnonymousName()).
01860                                                   '<noreply@'.$_SERVER['SERVER_NAME'].'>';
01861                         }
01862                 }
01863 
01864                 if($this->isSOAPEnabled())
01865                 {
01866                         // Send per soap
01867                         include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
01868 
01869                         $soap_client = new ilSoapClient();
01870                         $soap_client->setTimeout(1);
01871                         $soap_client->setResponseTimeout(1);
01872                         $soap_client->enableWSDL(true);
01873                         $soap_client->init();
01874 
01875                         $attachments = array();
01876                         $a_attachments = $a_attachments ? $a_attachments : array();
01877                         foreach($a_attachments as $attachment)
01878                         {
01879                                 $attachments[] = $this->mfile->getAbsolutePath($attachment);
01880                         }
01881                         $attachments = implode(',',$attachments);                       
01882 
01883                         $soap_client->call('sendMail',array($_COOKIE['PHPSESSID'].'::'.$_COOKIE['ilClientId'],  // session id
01884                                                                                                 $a_rcp_to,
01885                                                                                                 $a_rcp_cc,
01886                                                                                                 $a_rcp_bcc,
01887                                                                                                 $sender,
01888                                                                                                 $a_m_subject,
01889                                                                                                 $a_m_message,
01890                                                                                                 $attachments));
01891                 
01892                         return true;
01893                 }
01894                 else
01895                 {
01896                         // send direct
01897                         include_once "Services/Mail/classes/class.ilMimeMail.php";
01898                         
01899                         $mmail = new ilMimeMail();
01900                         $mmail->autoCheck(false);
01901                         $mmail->From($sender);
01902                         $mmail->To($a_rcp_to);
01903                         // Add installation name to subject
01904                         $mmail->Subject($a_m_subject);
01905                         $mmail->Body($a_m_message);
01906 
01907                         if ($a_rcp_cc)
01908                         {
01909                                 $mmail->Cc($a_rcp_cc);
01910                         }
01911 
01912                         if ($a_rcp_bcc)
01913                         {
01914                                 $mmail->Bcc($a_rcp_bcc);
01915                         }
01916 
01917                         if (is_array($a_attachments))
01918                         {       
01919                                 foreach ($a_attachments as $attachment)
01920                                 {
01921                                         $mmail->Attach($this->mfile->getAbsolutePath($attachment));
01922                                 }
01923                         }
01924 
01925                         $mmail->Send();
01926                 }
01927         }
01933         function getEmailOfSender()
01934         {
01935                 $umail = new ilObjUser($this->user_id);
01936                 $sender = $umail->getEmail();
01937 
01938                 if (ilUtil::is_email($sender))
01939                 {
01940                         return $sender;
01941                 }
01942                 else
01943                 {
01944                         return '';
01945                 }
01946         }
01947 
01954         function saveAttachments($a_attachments)
01955         {
01956                 global $ilDB;
01957                 
01958                 $query = "UPDATE $this->table_mail_saved ".
01959                         "SET attachments = '".addslashes(serialize($a_attachments))."' ".
01960                         "WHERE user_id = ".$ilDB->quote($this->user_id)." ";
01961 
01962                 $res = $this->ilias->db->query($query);
01963 
01964                 return true;
01965         }
01966 
01972         function getAttachments()
01973         {
01974                 return $this->mail_data["attachments"] ? $this->mail_data["attachments"] : array();
01975         }
01976         
01990         function explodeRecipients($a_recipients)
01991         {
01992                 if (ilMail::_usePearMail())
01993                 {
01994                         if (strlen(trim($a_recipients)) > 0)
01995                         {
01996                                 require_once 'Mail/RFC822.php';
01997                                 $parser = &new Mail_RFC822();
01998                                 return $parser->parseAddressList($a_recipients, "ilias", false, true);
01999                         } else {
02000                                 return array();
02001                         }
02002                 }
02003                 else
02004                 {
02005                         $a_recipients = trim($a_recipients);
02006         
02007                         // WHITESPACE IS NOT ALLOWED AS SEPERATOR
02008                         #$a_recipients = preg_replace("/ /",",",$a_recipients);
02009                         $a_recipients = preg_replace("/;/",",",$a_recipients);
02010                         
02011         
02012                         foreach(explode(',',$a_recipients) as $tmp_rec)
02013                         {
02014                                 if($tmp_rec)
02015                                 {
02016                                         $rcps[] = trim($tmp_rec);
02017                                 }
02018                         }
02019                         return is_array($rcps) ? $rcps : array();
02020                 }
02021         }
02022 
02023         function __getCountRecipient($rcp,$a_only_email = true)
02024         {
02025                 $counter = 0;
02026 
02027                 if (ilMail::_usePearMail())
02028                 {
02029                         $tmp_rcp = $this->explodeRecipients($rcp);
02030                         if (! is_a($tmp_rcp, 'PEAR_Error'))
02031                         {
02032                                 foreach ($tmp_rcp as $to)
02033                                 {
02034                                         if ($a_only_email)
02035                                         {
02036                                                 // Addresses which aren't on the ilias host, and 
02037                                                 // which have a mailbox which does not start with '#',
02038                                                 // are external e-mail addresses
02039                                                 if ($to->host != 'ilias' && substr($to->mailbox,0,1) != '#')
02040                                                 {
02041                                                         ++$counter;
02042                                                 }
02043                                         }
02044                                         else
02045                                         {
02046                                                 ++$counter;
02047                                         }
02048                                 }
02049                         }
02050                 }
02051                 else
02052                 {
02053                         foreach ($this->explodeRecipients($rcp) as $to)
02054                         {
02055                                 if ($a_only_email)
02056                                 {
02057                                         if (strpos($to,'@'))
02058                                         {
02059                                                 ++$counter;
02060                                         }
02061                                 }
02062                                 else
02063                                 {
02064                                         ++$counter;
02065                                 }
02066                         }
02067                 }
02068                 return $counter;
02069         }
02070                         
02071 
02072         function __getCountRecipients($a_to,$a_cc,$a_bcc,$a_only_email = true)
02073         {
02074                 return $this->__getCountRecipient($a_to,$a_only_email) 
02075                         + $this->__getCountRecipient($a_cc,$a_only_email) 
02076                         + $this->__getCountRecipient($a_bcc,$a_only_email);
02077         }
02078 
02079         function __getEmailRecipients($a_rcp)
02080         {
02081                 if (ilMail::_usePearMail())
02082                 {
02083                         $rcp = array();
02084                         $tmp_rcp = $this->explodeRecipients($a_rcp);
02085                         if (! is_a($tmp_rcp, 'PEAR_Error'))
02086                         {
02087                                 foreach ($tmp_rcp as $to)
02088                                 {
02089                                         if(substr($to->mailbox,0,1) != '#' && $to->host != 'ilias')
02090                                         {
02091                                                 $rcp[] = $to->mailbox.'@'.$to->host;
02092                                         }
02093                                 }
02094                         }
02095                         return implode(',',$rcp);
02096                 }
02097                 else
02098                 {
02099                         foreach ($this->explodeRecipients($a_rcp) as $to)
02100                         {
02101                                 if(strpos($to,'@'))
02102                                 {
02103                                         $rcp[] = $to;
02104                                 }
02105                         }
02106                         return implode(',',$rcp ? $rcp : array());
02107                 }
02108         }
02109 
02110         function __prependMessage($a_m_message,$rcp_to,$rcp_cc)
02111         {
02112                 $inst_name = $this->ilias->getSetting("inst_name") ? $this->ilias->getSetting("inst_name") : "ILIAS 3";
02113 
02114                 $message = $inst_name." To:".$rcp_to."\n";
02115 
02116                 if ($rcp_cc)
02117                 {
02118                         $message .= "Cc: ".$rcp_cc;
02119                 }
02120 
02121                 $message .= "\n\n";
02122                 $message .= $a_m_message;
02123 
02124                 return $message;
02125         }
02126 
02127         function __checkSystemRecipients(&$a_rcp_to)
02128         {
02129                 if (preg_match("/@all/",$a_rcp_to))
02130                 {
02131                         // GET ALL LOGINS
02132                         $all = ilObjUser::_getAllUserLogins($this->ilias);
02133                         $a_rcp_to = preg_replace("/@all/",implode(',',$all),$a_rcp_to);
02134                 }
02135 
02136                 return;
02137         }
02138 
02146         function __substituteRecipients($a_rcp,$direction)
02147         {
02148                 $new_name = array();
02149 
02150                 $tmp_names = $this->explodeRecipients($a_rcp);
02151 
02152 
02153                 foreach($tmp_names as $name)
02154                 {
02155                         if(strpos($name,"#") === 0)
02156                         {
02157                                 $new_name[] = $name;
02158                                 continue;
02159                         }
02160                         switch($direction)
02161                         {
02162                                 case "substitute":
02163                                         if(strpos($name,"@") and ilObjUser::_loginExists($name))
02164                                         {
02165                                                 $new_name[] = preg_replace("/@/","�#�",$name);
02166                                         }
02167                                         else
02168                                         {
02169                                                 $new_name[] = $name;
02170                                         }
02171                                         break;
02172                                         
02173                                 case "resubstitute":
02174                                         if(stristr($name,"�#�"))
02175                                         {
02176                                                 $new_name[] = preg_replace("/�#�/","@",$name);
02177                                         }
02178                                         else
02179                                         {
02180                                                 $new_name[] = $name;
02181                                         }
02182                                         break;
02183                         }
02184                 }
02185                 return implode(",",$new_name);
02186         }
02187 
02203         public static function _getUserInternalMailboxAddress($usr_id, $login=null, $firstname=null, $lastname=null) {
02204                 if (ilMail::_usePearMail())
02205                 {
02206                         if ($login == null)
02207                         {
02208                                 require_once './Services/User/classes/class.ilObjUser.php';
02209                                 $usr_obj = new ilObjUser($usr_id);
02210                                 $usr_obj->read();
02211                                 $login = $usr_obj->getLogin();
02212                                 $firstname = $usr_obj->getFirstname();
02213                                 $lastname = $usr_obj->getLastname();
02214                         }
02215                         return preg_replace('/[()<>@,;:\\".\[\]]/','',$firstname.' '.$lastname).' <'.$login.'>';
02216                 }
02217                 else
02218                 {
02219                         return $login;
02220                 }
02221         }
02228         public static function _usePearMail() {
02229                 global $ilias;
02230 
02231                 $result = false;
02232                 if ($ilias->getSetting('pear_mail_enable') == true)
02233                 {
02234                         // Note: We use the include statement to determine whether PEAR MAIL is
02235                         //      installed. We use the @ operator to prevent PHP from issuing a
02236                         //      warning while we test for PEAR MAIL.
02237                         $is_pear_mail_installed = @include_once 'Mail/RFC822.php';
02238                         if ($is_pear_mail_installed) {
02239                                 $result = true;
02240                         } else {
02241                                 // Disable Pear Mail, when we detect that it is not
02242                                 // installed
02243                                 global $log;
02244                                 $log->write("WARNING: ilMail::_userPearMail disabled Pear Mail support, because include 'Mail/RFC822.php' failed.");
02245                                 $ilias->setSetting('pear_mail_enable', false);
02246                         }
02247                 }
02248                 return $result;
02249         }
02250         
02251         public static function _getAnonymousName()
02252         {               
02253                 return 'ILIAS';
02254         }
02255         
02256 } // END class.ilMail
02257 ?>

Generated on Fri Dec 13 2013 17:56:57 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1