Exemple #1
0
 /**
  * @param \CAccount $oAccount
  * @param \CFetcher $oFetcher = null
  * @param bool $bWithDraftInfo = true
  * @param \CIdentity $oIdentity = null
  *
  * @return \MailSo\Mime\Message
  */
 private function buildMessage($oAccount, $oFetcher = null, $bWithDraftInfo = true, $oIdentity = null)
 {
     $sTo = $this->getParamValue('To', '');
     $sCc = $this->getParamValue('Cc', '');
     $sBcc = $this->getParamValue('Bcc', '');
     $sSubject = $this->getParamValue('Subject', '');
     $bTextIsHtml = '1' === $this->getParamValue('IsHtml', '0');
     $sText = $this->getParamValue('Text', '');
     $aAttachments = $this->getParamValue('Attachments', null);
     $aDraftInfo = $this->getParamValue('DraftInfo', null);
     $sInReplyTo = $this->getParamValue('InReplyTo', '');
     $sReferences = $this->getParamValue('References', '');
     $sImportance = $this->getParamValue('Importance', '');
     // 1 3 5
     $sSensitivity = $this->getParamValue('Sensitivity', '');
     // 0 1 2 3 4
     $bReadingConfirmation = '1' === $this->getParamValue('ReadingConfirmation', '0');
     $oMessage = \MailSo\Mime\Message::NewInstance();
     $oMessage->RegenerateMessageId();
     $sXMailer = \CApi::GetConf('webmail.xmailer-value', '');
     if (0 < strlen($sXMailer)) {
         $oMessage->SetXMailer($sXMailer);
     }
     if ($oIdentity) {
         $oFrom = \MailSo\Mime\Email::NewInstance($oIdentity->Email, $oIdentity->FriendlyName);
     } else {
         $oFrom = $oFetcher ? \MailSo\Mime\Email::NewInstance($oFetcher->Email, $oFetcher->Name) : \MailSo\Mime\Email::NewInstance($oAccount->Email, $oAccount->FriendlyName);
     }
     $oMessage->SetFrom($oFrom)->SetSubject($sSubject);
     $oToEmails = \MailSo\Mime\EmailCollection::NewInstance($sTo);
     if ($oToEmails && $oToEmails->Count()) {
         $oMessage->SetTo($oToEmails);
     }
     $oCcEmails = \MailSo\Mime\EmailCollection::NewInstance($sCc);
     if ($oCcEmails && $oCcEmails->Count()) {
         $oMessage->SetCc($oCcEmails);
     }
     $oBccEmails = \MailSo\Mime\EmailCollection::NewInstance($sBcc);
     if ($oBccEmails && $oBccEmails->Count()) {
         $oMessage->SetBcc($oBccEmails);
     }
     if ($bWithDraftInfo && is_array($aDraftInfo) && !empty($aDraftInfo[0]) && !empty($aDraftInfo[1]) && !empty($aDraftInfo[2])) {
         $oMessage->SetDraftInfo($aDraftInfo[0], $aDraftInfo[1], $aDraftInfo[2]);
     }
     if (0 < strlen($sInReplyTo)) {
         $oMessage->SetInReplyTo($sInReplyTo);
     }
     if (0 < strlen($sReferences)) {
         $oMessage->SetReferences($sReferences);
     }
     if (0 < strlen($sImportance) && in_array((int) $sImportance, array(\MailSo\Mime\Enumerations\MessagePriority::HIGH, \MailSo\Mime\Enumerations\MessagePriority::NORMAL, \MailSo\Mime\Enumerations\MessagePriority::LOW))) {
         $oMessage->SetPriority((int) $sImportance);
     }
     if (0 < strlen($sSensitivity) && in_array((int) $sSensitivity, array(\MailSo\Mime\Enumerations\Sensitivity::NOTHING, \MailSo\Mime\Enumerations\Sensitivity::CONFIDENTIAL, \MailSo\Mime\Enumerations\Sensitivity::PRIVATE_, \MailSo\Mime\Enumerations\Sensitivity::PERSONAL))) {
         $oMessage->SetSensitivity((int) $sSensitivity);
     }
     if ($bReadingConfirmation) {
         $oMessage->SetReadConfirmation($oFetcher ? $oFetcher->Email : $oAccount->Email);
     }
     $aFoundCids = array();
     \CApi::Plugin()->RunHook('webmail.message-text-html-raw', array($oAccount, &$oMessage, &$sText, &$bTextIsHtml));
     if ($bTextIsHtml) {
         $sTextConverted = \MailSo\Base\HtmlUtils::ConvertHtmlToPlain($sText);
         \CApi::Plugin()->RunHook('webmail.message-plain-part', array($oAccount, &$oMessage, &$sTextConverted));
         $oMessage->AddText($sTextConverted, false);
     }
     $mFoundDataURL = array();
     $aFoundedContentLocationUrls = array();
     $sTextConverted = $bTextIsHtml ? \MailSo\Base\HtmlUtils::BuildHtml($sText, $aFoundCids, $mFoundDataURL, $aFoundedContentLocationUrls) : $sText;
     \CApi::Plugin()->RunHook($bTextIsHtml ? 'webmail.message-html-part' : 'webmail.message-plain-part', array($oAccount, &$oMessage, &$sTextConverted));
     $oMessage->AddText($sTextConverted, $bTextIsHtml);
     if (is_array($aAttachments)) {
         foreach ($aAttachments as $sTempName => $aData) {
             if (is_array($aData) && isset($aData[0], $aData[1], $aData[2], $aData[3])) {
                 $sFileName = (string) $aData[0];
                 $sCID = (string) $aData[1];
                 $bIsInline = '1' === (string) $aData[2];
                 $bIsLinked = '1' === (string) $aData[3];
                 $sContentLocation = isset($aData[4]) ? (string) $aData[4] : '';
                 $rResource = $this->ApiFileCache()->getFile($oAccount, $sTempName);
                 if (is_resource($rResource)) {
                     $iFileSize = $this->ApiFileCache()->fileSize($oAccount, $sTempName);
                     $sCID = trim(trim($sCID), '<>');
                     $bIsFounded = 0 < strlen($sCID) ? in_array($sCID, $aFoundCids) : false;
                     if (!$bIsLinked || $bIsFounded) {
                         $oMessage->Attachments()->Add(\MailSo\Mime\Attachment::NewInstance($rResource, $sFileName, $iFileSize, $bIsInline, $bIsLinked, $bIsLinked ? '<' . $sCID . '>' : '', array(), $sContentLocation));
                     }
                 }
             }
         }
     }
     if ($mFoundDataURL && \is_array($mFoundDataURL) && 0 < \count($mFoundDataURL)) {
         foreach ($mFoundDataURL as $sCidHash => $sDataUrlString) {
             $aMatch = array();
             $sCID = '<' . $sCidHash . '>';
             if (\preg_match('/^data:(image\\/[a-zA-Z0-9]+\\+?[a-zA-Z0-9]+);base64,(.+)$/i', $sDataUrlString, $aMatch) && !empty($aMatch[1]) && !empty($aMatch[2])) {
                 $sRaw = \MailSo\Base\Utils::Base64Decode($aMatch[2]);
                 $iFileSize = \strlen($sRaw);
                 if (0 < $iFileSize) {
                     $sFileName = \preg_replace('/[^a-z0-9]+/i', '.', \MailSo\Base\Utils::NormalizeContentType($aMatch[1]));
                     // fix bug #68532 php < 5.5.21 or php < 5.6.5
                     $sRaw = $this->FixBase64EncodeOmitsPaddingBytes($sRaw);
                     $rResource = \MailSo\Base\ResourceRegistry::CreateMemoryResourceFromString($sRaw);
                     $sRaw = '';
                     unset($sRaw);
                     unset($aMatch);
                     $oMessage->Attachments()->Add(\MailSo\Mime\Attachment::NewInstance($rResource, $sFileName, $iFileSize, true, true, $sCID));
                 }
             }
         }
     }
     \CApi::Plugin()->RunHook('webmail.build-message', array(&$oMessage));
     return $oMessage;
 }