Пример #1
0
 /**
  * @param CAccount $oAccount
  * @param string $sFolderFullNameRaw
  * @param int $iUid
  * @param string $sRfc822SubMimeIndex = ''
  * @param bool $bParseICalAndVcard = false
  * @param bool $bParseAsc = false
  * @param int $iBodyTextLimit = 0
  *
  * @return CApiMailMessage
  *
  * @throws CApiInvalidArgumentException
  */
 public function Message($oAccount, $sFolderFullNameRaw, $iUid, $sRfc822SubMimeIndex = '', $bParseICalAndVcard = false, $bParseAsc = false, $iBodyTextLimit = 0)
 {
     if (0 === strlen($sFolderFullNameRaw) || !is_numeric($iUid) || 0 >= (int) $iUid) {
         throw new CApiInvalidArgumentException();
     }
     $iUid = (int) $iUid;
     $oImapClient =& $this->getImapClient($oAccount);
     $oImapClient->FolderExamine($sFolderFullNameRaw);
     $oMessage = false;
     $sICalMimeIndex = '';
     $sVCardMimeIndex = '';
     $aTextMimeIndexes = array();
     $aAscPartsIds = array();
     $aFetchResponse = $oImapClient->Fetch(array(\MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE), $iUid, true);
     $oBodyStructure = 0 < count($aFetchResponse) ? $aFetchResponse[0]->GetFetchBodyStructure($sRfc822SubMimeIndex) : null;
     if ($oBodyStructure) {
         $aTextParts = $oBodyStructure->SearchHtmlOrPlainParts();
         if (is_array($aTextParts) && 0 < count($aTextParts)) {
             foreach ($aTextParts as $oPart) {
                 $aTextMimeIndexes[] = array($oPart->PartID(), $oPart->Size());
             }
         }
         if ($bParseICalAndVcard) {
             $aICalPart = $oBodyStructure->SearchByContentType('text/calendar');
             $oICalPart = is_array($aICalPart) && 0 < count($aICalPart) ? $aICalPart[0] : null;
             $sICalMimeIndex = $oICalPart ? $oICalPart->PartID() : '';
             $aVCardPart = $oBodyStructure->SearchByContentType('text/vcard');
             $aVCardPart = $aVCardPart ? $aVCardPart : $oBodyStructure->SearchByContentType('text/x-vcard');
             $oVCardPart = is_array($aVCardPart) && 0 < count($aVCardPart) ? $aVCardPart[0] : null;
             $sVCardMimeIndex = $oVCardPart ? $oVCardPart->PartID() : '';
         }
         if ($bParseAsc) {
             $aAscParts = $oBodyStructure->SearchByCallback(function ($oPart) {
                 return '.asc' === \strtolower(\substr(\trim($oPart->FileName()), -4));
             });
             if (is_array($aAscParts) && 0 < count($aAscParts)) {
                 foreach ($aAscParts as $oPart) {
                     $aAscPartsIds[] = $oPart->PartID();
                 }
             }
         }
     }
     $aFetchItems = array(\MailSo\Imap\Enumerations\FetchType::INDEX, \MailSo\Imap\Enumerations\FetchType::UID, \MailSo\Imap\Enumerations\FetchType::RFC822_SIZE, \MailSo\Imap\Enumerations\FetchType::INTERNALDATE, \MailSo\Imap\Enumerations\FetchType::FLAGS, 0 < strlen($sRfc822SubMimeIndex) ? \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sRfc822SubMimeIndex . '.HEADER]' : \MailSo\Imap\Enumerations\FetchType::BODY_HEADER_PEEK);
     if (0 < count($aTextMimeIndexes)) {
         if (0 < strlen($sRfc822SubMimeIndex) && is_numeric($sRfc822SubMimeIndex)) {
             $sLine = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $aTextMimeIndexes[0][0] . '.1]';
             if (\is_numeric($iBodyTextLimit) && 0 < $iBodyTextLimit && $iBodyTextLimit < $aTextMimeIndexes[0][1]) {
                 $sLine .= '<0.' . (int) $iBodyTextLimit . '>';
             }
             $aFetchItems[] = $sLine;
         } else {
             foreach ($aTextMimeIndexes as $aTextMimeIndex) {
                 $sLine = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $aTextMimeIndex[0] . ']';
                 if (\is_numeric($iBodyTextLimit) && 0 < $iBodyTextLimit && $iBodyTextLimit < $aTextMimeIndex[1]) {
                     $sLine .= '<0.' . (int) $iBodyTextLimit . '>';
                 }
                 $aFetchItems[] = $sLine;
             }
         }
     }
     if (0 < strlen($sICalMimeIndex)) {
         $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sICalMimeIndex . ']';
     }
     if (0 < strlen($sVCardMimeIndex)) {
         $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sVCardMimeIndex . ']';
     }
     if (0 < count($aAscPartsIds)) {
         foreach ($aAscPartsIds as $sPartID) {
             $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODY_PEEK . '[' . $sPartID . ']';
         }
     }
     if (!$oBodyStructure) {
         $aFetchItems[] = \MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE;
     }
     $aFetchResponse = $oImapClient->Fetch($aFetchItems, $iUid, true);
     if (0 < count($aFetchResponse)) {
         $oMessage = CApiMailMessage::NewFetchResponseInstance($sFolderFullNameRaw, $aFetchResponse[0], $oBodyStructure, $sRfc822SubMimeIndex, $aAscPartsIds);
     }
     if ($oMessage) {
         $sFromEmail = '';
         $oFromCollection = $oMessage->From();
         if ($oFromCollection && 0 < $oFromCollection->Count()) {
             $oFrom =& $oFromCollection->GetByIndex(0);
             if ($oFrom) {
                 $sFromEmail = trim($oFrom->GetEmail());
             }
         }
         if (0 < strlen($sFromEmail)) {
             $oApiUsersManager = CApi::Manager('users');
             $oMessage->SetSafety($oApiUsersManager->GetSafetySender($oAccount->IdUser, $sFromEmail, true));
         }
         if ($bParseAsc && 0 < count($aAscPartsIds)) {
         }
         if ($bParseICalAndVcard) {
             $oApiCapa = CApi::Manager('capability');
             $oApiFileCache = CApi::Manager('filecache');
             // ICAL
             $sICal = $oMessage->GetExtend('ICAL_RAW');
             if (!empty($sICal) && $oApiCapa->IsCalendarSupported($oAccount)) {
                 $oApiCalendarManager = CApi::Manager('calendar');
                 if ($oApiCalendarManager) {
                     $mResult = $oApiCalendarManager->ProcessICS($oAccount, trim($sICal), $sFromEmail);
                     if (is_array($mResult) && !empty($mResult['Action']) && !empty($mResult['Body'])) {
                         $sTemptFile = md5($mResult['Body']) . '.ics';
                         if ($oApiFileCache && $oApiFileCache->Put($oAccount, $sTemptFile, $mResult['Body'])) {
                             $oIcs = CApiMailIcs::NewInstance();
                             $oIcs->Uid = $mResult['UID'];
                             $oIcs->File = $sTemptFile;
                             $oIcs->Attendee = isset($mResult['Attendee']) ? $mResult['Attendee'] : null;
                             $oIcs->Type = $mResult['Action'];
                             $oIcs->Location = !empty($mResult['Location']) ? $mResult['Location'] : '';
                             $oIcs->Description = !empty($mResult['Description']) ? $mResult['Description'] : '';
                             $oIcs->When = !empty($mResult['When']) ? $mResult['When'] : '';
                             $oIcs->CalendarId = !empty($mResult['CalendarId']) ? $mResult['CalendarId'] : '';
                             if (!$oApiCapa->IsCalendarAppointmentsSupported($oAccount)) {
                                 $oIcs->Type = '';
                             }
                             // TODO
                             //								$oIcs->Calendars = array();
                             //								if (isset($mResult['Calendars']) && is_array($mResult['Calendars']) && 0 < count($mResult['Calendars']))
                             //								{
                             //									foreach ($mResult['Calendars'] as $sUid => $sName)
                             //									{
                             //										$oIcs->Calendars[$sUid] = $sName;
                             //									}
                             //								}
                             $oMessage->AddExtend('ICAL', $oIcs);
                         } else {
                             CApi::Log('Can\'t save temp file "' . $sTemptFile . '"', ELogLevel::Error);
                         }
                     }
                 }
             }
             // VCARD
             $sVCard = $oMessage->GetExtend('VCARD_RAW');
             if (!empty($sVCard) && $oApiCapa->IsContactsSupported($oAccount)) {
                 $oApiContactsManager = CApi::Manager('contacts');
                 $oContact = new CContact();
                 $oContact->InitFromVCardStr($oAccount->IdUser, $sVCard);
                 $oContact->InitBeforeChange();
                 $oContact->IdContact = 0;
                 $bContactExists = false;
                 if (0 < strlen($oContact->ViewEmail)) {
                     if ($oApiContactsManager) {
                         $oLocalContact = $oApiContactsManager->GetContactByEmail($oAccount->IdUser, $oContact->ViewEmail);
                         if ($oLocalContact) {
                             $oContact->IdContact = $oLocalContact->IdContact;
                             $bContactExists = true;
                         }
                     }
                 }
                 $sTemptFile = md5($sVCard) . '.vcf';
                 if ($oApiFileCache && $oApiFileCache->Put($oAccount, $sTemptFile, $sVCard)) {
                     $oVcard = CApiMailVcard::NewInstance();
                     $oVcard->Uid = $oContact->IdContact;
                     $oVcard->File = $sTemptFile;
                     $oVcard->Exists = !!$bContactExists;
                     $oVcard->Name = $oContact->FullName;
                     $oVcard->Email = $oContact->ViewEmail;
                     $oMessage->AddExtend('VCARD', $oVcard);
                 } else {
                     CApi::Log('Can\'t save temp file "' . $sTemptFile . '"', ELogLevel::Error);
                 }
             }
         }
     }
     return $oMessage;
 }