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;
 }
Exemple #2
0
 /**
  * @param int $iIdTenant
  *
  * @return bool
  */
 public function startMailboxMonitorPrev($iIdTenant)
 {
     $aData = $this->getHelpdeskMainSettings($iIdTenant);
     if (!empty($aData['AdminEmailAccount']) && 0 < $aData['HelpdeskFetcherType']) {
         $oApiUsers = $this->_getApiUsers();
         $oApiMail = $this->_getApiMail();
         $oApiFileCache = \CApi::Manager('filecache');
         $oApiFilestorage = \CApi::Manager('filestorage');
         if ($oApiUsers && $oApiMail && $oApiFileCache) {
             $oAccount = $oApiUsers->getAccountByEmail($aData['AdminEmailAccount']);
             if ($oAccount) {
                 $iPrevLastUid = $this->getHelpdeskMailboxLastUid($iIdTenant, \strtolower($oAccount->Email));
                 $iLastUid = 0;
                 $aData = $oApiMail->getMessagesForHelpdeskSynch($oAccount, 0 < $iPrevLastUid ? $iPrevLastUid + 1 : 0, $iLastUid);
                 if (0 < $iLastUid) {
                     $this->setHelpdeskMailboxLastUid($iIdTenant, \strtolower($oAccount->Email), $iLastUid);
                 }
                 if (is_array($aData) && 0 < count($aData)) {
                     foreach ($aData as $oMessage) {
                         $aMatch = array();
                         $oFrom = $oMessage->getFrom();
                         $aFrom = $oFrom ? $oFrom->GetAsArray() : array();
                         $oAttachments = $oMessage->getAttachments();
                         $aAttachments = $oAttachments ? $oAttachments->GetAsArray() : array();
                         $sSubject = $oMessage->getSubject();
                         if (is_array($aFrom) && 0 < count($aFrom) && !empty($sSubject) && preg_match('/\\[#([a-zA-Z0-9]+)#\\]/', $sSubject, $aMatch) && !empty($aMatch[1])) {
                             $oEmail = $aFrom[0];
                             $sEmail = $oEmail ? $oEmail->GetEmail() : '';
                             if (0 < \strlen($sEmail)) {
                                 $oHelpdeskUser = $this->getUserByEmail($iIdTenant, $sEmail);
                                 if ($oHelpdeskUser) {
                                     $sThreadHash = (string) $aMatch[1];
                                     if (!empty($sThreadHash)) {
                                         $iThreadID = $this->getThreadIdByHash($iIdTenant, $sThreadHash);
                                         if (0 < $iThreadID) {
                                             $oThread = $this->getThreadById($oHelpdeskUser, $iThreadID);
                                             if ($oThread) {
                                                 $sText = trim($oMessage->getHtml());
                                                 if (0 === strlen($sText)) {
                                                     $sText = trim($oMessage->getPlain());
                                                 } else {
                                                     $sText = \MailSo\Base\HtmlUtils::ConvertHtmlToPlain($sText);
                                                 }
                                                 $oPost = new \CHelpdeskPost();
                                                 $oPost->IdTenant = $oHelpdeskUser->IdTenant;
                                                 $oPost->IdOwner = $oHelpdeskUser->IdHelpdeskUser;
                                                 $oPost->IdHelpdeskThread = $oThread->IdHelpdeskThread;
                                                 $oPost->Type = \EHelpdeskPostType::Normal;
                                                 $oPost->SystemType = \EHelpdeskPostSystemType::None;
                                                 $oPost->Text = $sText;
                                                 $aResultAttachment = array();
                                                 if (is_array($aAttachments) && 0 < count($aAttachments)) {
                                                     foreach ($aAttachments as $oAttachment) {
                                                         $sUploadName = $oAttachment->getFileName(true);
                                                         $sTempName = md5($sUploadName . rand(1000, 9999));
                                                         $oApiMail->directMessageToStream($oAccount, function ($rResource, $sContentType, $sFileName, $sMimeIndex = '') use($oHelpdeskUser, &$sTempName, $oApiFileCache) {
                                                             if (!$oApiFileCache->putFile($oHelpdeskUser, $sTempName, $rResource)) {
                                                                 $sTempName = '';
                                                             }
                                                         }, $oAttachment->getFolder(), $oAttachment->getUid(), $oAttachment->MimeIndex());
                                                         $rData = 0 < \strlen($sTempName) ? $oApiFileCache->getFile($oHelpdeskUser, $sTempName) : null;
                                                         if ($rData) {
                                                             $iFileSize = $oApiFileCache->fileSize($oHelpdeskUser, $sTempName);
                                                             $sThreadID = (string) $oThread->IdHelpdeskThread;
                                                             $sThreadID = str_pad($sThreadID, 2, '0', STR_PAD_LEFT);
                                                             $sThreadIDSubFolder = substr($sThreadID, 0, 2);
                                                             $sThreadFolderName = API_HELPDESK_PUBLIC_NAME . '/' . $sThreadIDSubFolder . '/' . $sThreadID;
                                                             $oApiFilestorage->createFolder($oHelpdeskUser, \EFileStorageType::Corporate, '', $sThreadFolderName);
                                                             $oApiFilestorage->createFile($oHelpdeskUser, \EFileStorageTypeStr::Corporate, $sThreadFolderName, $sUploadName, $rData, false);
                                                             if (is_resource($rData)) {
                                                                 @fclose($rData);
                                                             }
                                                             $oAttachment = new \CHelpdeskAttachment();
                                                             $oAttachment->IdHelpdeskThread = $oThread->IdHelpdeskThread;
                                                             $oAttachment->IdHelpdeskPost = $oPost->IdHelpdeskPost;
                                                             $oAttachment->IdOwner = $oHelpdeskUser->IdHelpdeskUser;
                                                             $oAttachment->IdTenant = $oHelpdeskUser->IdTenant;
                                                             $oAttachment->FileName = $sUploadName;
                                                             $oAttachment->SizeInBytes = $iFileSize;
                                                             $oAttachment->encodeHash($oHelpdeskUser, $sThreadFolderName);
                                                             $oApiFileCache->clear($oHelpdeskUser, $sTempName);
                                                             $aResultAttachment[] = $oAttachment;
                                                         }
                                                     }
                                                     if (is_array($aResultAttachment) && 0 < count($aResultAttachment)) {
                                                         $oPost->Attachments = $aResultAttachment;
                                                     }
                                                 }
                                                 $this->createPost($oHelpdeskUser, $oThread, $oPost, false, false);
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         unset($oMessage);
                     }
                 }
             }
         }
     }
     return true;
 }
 /**
  * @param \RainLoop\Model\Account $oAccount
  * @param bool $bWithDraftInfo = true
  *
  * @return \MailSo\Mime\Message
  */
 private function buildMessage($oAccount, $bWithDraftInfo = true)
 {
     $sIdentityID = $this->GetActionParam('IdentityID', '');
     $sTo = $this->GetActionParam('To', '');
     $sCc = $this->GetActionParam('Cc', '');
     $sBcc = $this->GetActionParam('Bcc', '');
     $sReplyTo = $this->GetActionParam('ReplyTo', '');
     $sSubject = $this->GetActionParam('Subject', '');
     $bTextIsHtml = '1' === $this->GetActionParam('TextIsHtml', '0');
     $bReadReceiptRequest = '1' === $this->GetActionParam('ReadReceiptRequest', '0');
     $bMarkAsImportant = '1' === $this->GetActionParam('MarkAsImportant', '0');
     $sText = $this->GetActionParam('Text', '');
     $aAttachments = $this->GetActionParam('Attachments', null);
     $aDraftInfo = $this->GetActionParam('DraftInfo', null);
     $sInReplyTo = $this->GetActionParam('InReplyTo', '');
     $sReferences = $this->GetActionParam('References', '');
     $oMessage = \MailSo\Mime\Message::NewInstance();
     $oMessage->RegenerateMessageId();
     $oMessage->SetXMailer('RainLoop/' . APP_VERSION);
     $oFromIdentity = $this->GetIdentityByID($oAccount, $sIdentityID);
     if ($oFromIdentity) {
         $oMessage->SetFrom(\MailSo\Mime\Email::NewInstance($oFromIdentity->Email(), $oFromIdentity->Name()));
     } else {
         $oMessage->SetFrom(\MailSo\Mime\Email::Parse($oAccount->Email()));
     }
     if (!empty($sReplyTo)) {
         $oReplyTo = \MailSo\Mime\EmailCollection::NewInstance($sReplyTo);
         if ($oReplyTo && 0 < $oReplyTo->Count()) {
             $oMessage->SetReplyTo($oReplyTo);
         }
     }
     if ($bReadReceiptRequest) {
         $oMessage->SetReadReceipt($oAccount->Email());
     }
     if ($bMarkAsImportant) {
         $oMessage->SetPriority(\MailSo\Mime\Enumerations\MessagePriority::HIGH);
     }
     $oMessage->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);
     }
     $aFoundedCids = array();
     $mFoundDataURL = array();
     $aFoundedContentLocationUrls = array();
     $sTextToAdd = $bTextIsHtml ? \MailSo\Base\HtmlUtils::BuildHtml($sText, $aFoundedCids, $mFoundDataURL, $aFoundedContentLocationUrls) : $sText;
     $this->Plugins()->RunHook($bTextIsHtml ? 'filter.message-html' : 'filter.message-plain', array($oAccount, &$oMessage, &$sTextToAdd));
     if ($bTextIsHtml && 0 < \strlen($sTextToAdd)) {
         $sTextConverted = \MailSo\Base\HtmlUtils::ConvertHtmlToPlain($sTextToAdd);
         $this->Plugins()->RunHook('filter.message-plain', array($oAccount, &$oMessage, &$sTextConverted));
         $oMessage->AddText($sTextConverted, false);
     }
     $oMessage->AddText($sTextToAdd, $bTextIsHtml);
     if (\is_array($aAttachments)) {
         foreach ($aAttachments as $sTempName => $aData) {
             $sFileName = (string) $aData[0];
             $bIsInline = (bool) $aData[1];
             $sCID = (string) $aData[2];
             $sContentLocation = isset($aData[3]) ? (string) $aData[3] : '';
             $rResource = $this->FilesProvider()->GetFile($oAccount, $sTempName);
             if (\is_resource($rResource)) {
                 $iFileSize = $this->FilesProvider()->FileSize($oAccount, $sTempName);
                 $oMessage->Attachments()->Add(\MailSo\Mime\Attachment::NewInstance($rResource, $sFileName, $iFileSize, $bIsInline, \in_array(trim(trim($sCID), '<>'), $aFoundedCids), $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]+);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', '.', $aMatch[1]);
                     $rResource = \MailSo\Base\ResourceRegistry::CreateMemoryResourceFromString($sRaw);
                     $sRaw = '';
                     unset($sRaw);
                     unset($aMatch);
                     $oMessage->Attachments()->Add(\MailSo\Mime\Attachment::NewInstance($rResource, $sFileName, $iFileSize, true, true, $sCID));
                 }
             }
         }
     }
     $this->Plugins()->RunHook('filter.build-message', array(&$oMessage));
     $this->Plugins()->RunHook('filter.build-message[2]', array(&$oMessage, $oAccount));
     return $oMessage;
 }