Esempio n. 1
0
 /**
  * Parse date string formated as "2011-06-14 23:59:59 +0400"
  *
  * @param string $sDateTime
  *
  * @return int
  */
 public static function ParseDateStringType1($sDateTime)
 {
     $oDateTime = \DateTime::createFromFormat('Y-m-d H:i:s O', \trim($sDateTime), \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject());
     return $oDateTime ? $oDateTime->getTimestamp() : 0;
 }
Esempio n. 2
0
 /**
  * @param string $sDate
  * @param int $iTimeZoneOffset
  *
  * @return int
  */
 private function parseSearchDate($sDate, $iTimeZoneOffset)
 {
     $iResult = 0;
     if (0 < \strlen($sDate)) {
         $oDateTime = \DateTime::createFromFormat('Y.m.d', $sDate, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject());
         return $oDateTime ? $oDateTime->getTimestamp() - $iTimeZoneOffset : 0;
     }
     return $iResult;
 }
Esempio n. 3
0
 /**
  * @param string $sFolder
  * @param \MailSo\Imap\FetchResponse $oFetchResponse
  * @param \MailSo\Imap\BodyStructure $oBodyStructure = null
  *
  * @return \MailSo\Mail\Message
  */
 public function InitByFetchResponse($sFolder, $oFetchResponse, $oBodyStructure = null)
 {
     if (!$oBodyStructure) {
         $oBodyStructure = $oFetchResponse->GetFetchBodyStructure();
     }
     $sUid = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::UID);
     $sSize = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::RFC822_SIZE);
     $sInternalDate = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::INTERNALDATE);
     $aFlags = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::FLAGS);
     $this->sFolder = $sFolder;
     $this->iUid = \is_numeric($sUid) ? (int) $sUid : 0;
     $this->iSize = \is_numeric($sSize) ? (int) $sSize : 0;
     $this->aFlags = \is_array($aFlags) ? $aFlags : array();
     $this->aFlagsLowerCase = \array_map('strtolower', $this->aFlags);
     $this->iInternalTimeStampInUTC = \MailSo\Base\DateTimeHelper::ParseInternalDateString($sInternalDate);
     $sCharset = $oBodyStructure ? $oBodyStructure->SearchCharset() : '';
     $sCharset = \MailSo\Base\Utils::NormalizeCharset($sCharset);
     $sHeaders = $oFetchResponse->GetHeaderFieldsValue();
     if (0 < \strlen($sHeaders)) {
         $oHeaders = \MailSo\Mime\HeaderCollection::NewInstance()->Parse($sHeaders, false, $sCharset);
         $sContentTypeCharset = $oHeaders->ParameterValue(\MailSo\Mime\Enumerations\Header::CONTENT_TYPE, \MailSo\Mime\Enumerations\Parameter::CHARSET);
         if (0 < \strlen($sContentTypeCharset)) {
             $sCharset = $sContentTypeCharset;
             $sCharset = \MailSo\Base\Utils::NormalizeCharset($sCharset);
         }
         if (0 < \strlen($sCharset)) {
             $oHeaders->SetParentCharset($sCharset);
         }
         $bCharsetAutoDetect = 0 === \strlen($sCharset);
         $this->sSubject = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::SUBJECT, $bCharsetAutoDetect);
         $this->sMessageId = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::MESSAGE_ID);
         $this->sContentType = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::CONTENT_TYPE);
         $this->oFrom = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::FROM_, $bCharsetAutoDetect);
         $this->oTo = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::TO_, $bCharsetAutoDetect);
         $this->oCc = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::CC, $bCharsetAutoDetect);
         $this->oBcc = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::BCC, $bCharsetAutoDetect);
         $this->oSender = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::SENDER, $bCharsetAutoDetect);
         $this->oReplyTo = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::REPLY_TO, $bCharsetAutoDetect);
         $this->oDeliveredTo = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::DELIVERED_TO, $bCharsetAutoDetect);
         $this->sInReplyTo = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::IN_REPLY_TO);
         $this->sReferences = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::REFERENCES);
         $sHeaderDate = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::DATE);
         $this->sHeaderDate = $sHeaderDate;
         $this->iHeaderTimeStampInUTC = \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sHeaderDate);
         // Sensitivity
         $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::NOTHING;
         $sSensitivity = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::SENSITIVITY);
         switch (\strtolower($sSensitivity)) {
             case 'personal':
                 $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::PERSONAL;
                 break;
             case 'private':
                 $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::PRIVATE_;
                 break;
             case 'company-confidential':
                 $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::CONFIDENTIAL;
                 break;
         }
         // Priority
         $this->iPriority = \MailSo\Mime\Enumerations\MessagePriority::NORMAL;
         $sPriority = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_MSMAIL_PRIORITY);
         if (0 === \strlen($sPriority)) {
             $sPriority = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::IMPORTANCE);
         }
         if (0 === \strlen($sPriority)) {
             $sPriority = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_PRIORITY);
         }
         if (0 < \strlen($sPriority)) {
             switch (\str_replace(' ', '', \strtolower($sPriority))) {
                 case 'high':
                 case '1(highest)':
                 case '2(high)':
                 case '1':
                 case '2':
                     $this->iPriority = \MailSo\Mime\Enumerations\MessagePriority::HIGH;
                     break;
                 case 'low':
                 case '4(low)':
                 case '5(lowest)':
                 case '4':
                 case '5':
                     $this->iPriority = \MailSo\Mime\Enumerations\MessagePriority::LOW;
                     break;
             }
         }
         // Delivery Receipt
         $this->sDeliveryReceipt = \trim($oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::RETURN_RECEIPT_TO));
         // Read Receipt
         $this->sReadReceipt = \trim($oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::DISPOSITION_NOTIFICATION_TO));
         if (empty($this->sReadReceipt)) {
             $this->sReadReceipt = \trim($oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_CONFIRM_READING_TO));
         }
         $sDraftInfo = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_DRAFT_INFO);
         if (0 < \strlen($sDraftInfo)) {
             $sType = '';
             $sFolder = '';
             $sUid = '';
             \MailSo\Mime\ParameterCollection::NewInstance($sDraftInfo)->ForeachList(function ($oParameter) use(&$sType, &$sFolder, &$sUid) {
                 switch (\strtolower($oParameter->Name())) {
                     case 'type':
                         $sType = $oParameter->Value();
                         break;
                     case 'uid':
                         $sUid = $oParameter->Value();
                         break;
                     case 'folder':
                         $sFolder = \base64_decode($oParameter->Value());
                         break;
                 }
             });
             if (0 < \strlen($sType) && 0 < \strlen($sFolder) && 0 < \strlen($sUid)) {
                 $this->aDraftInfo = array($sType, $sUid, $sFolder);
             }
         }
     } else {
         if ($oFetchResponse->GetEnvelope()) {
             if (0 === \strlen($sCharset) && $oBodyStructure) {
                 $sCharset = $oBodyStructure->SearchCharset();
                 $sCharset = \MailSo\Base\Utils::NormalizeCharset($sCharset);
             }
             if (0 === \strlen($sCharset)) {
                 $sCharset = \MailSo\Base\Enumerations\Charset::ISO_8859_1;
             }
             // date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, message-id
             $this->sMessageId = $oFetchResponse->GetFetchEnvelopeValue(9, '');
             $this->sSubject = \MailSo\Base\Utils::DecodeHeaderValue($oFetchResponse->GetFetchEnvelopeValue(1, ''), $sCharset);
             $this->oFrom = $oFetchResponse->GetFetchEnvelopeEmailCollection(2, $sCharset);
             $this->oSender = $oFetchResponse->GetFetchEnvelopeEmailCollection(3, $sCharset);
             $this->oReplyTo = $oFetchResponse->GetFetchEnvelopeEmailCollection(4, $sCharset);
             $this->oTo = $oFetchResponse->GetFetchEnvelopeEmailCollection(5, $sCharset);
             $this->oCc = $oFetchResponse->GetFetchEnvelopeEmailCollection(6, $sCharset);
             $this->oBcc = $oFetchResponse->GetFetchEnvelopeEmailCollection(7, $sCharset);
             $this->sInReplyTo = $oFetchResponse->GetFetchEnvelopeValue(8, '');
         }
     }
     $aTextParts = $oBodyStructure ? $oBodyStructure->SearchHtmlOrPlainParts() : null;
     if (\is_array($aTextParts) && 0 < \count($aTextParts)) {
         if (0 === \strlen($sCharset)) {
             $sCharset = \MailSo\Base\Enumerations\Charset::UTF_8;
         }
         $sHtmlParts = array();
         $sPlainParts = array();
         foreach ($aTextParts as $oPart) {
             $sText = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY . '[' . $oPart->PartID() . ']');
             if (null === $sText) {
                 $sText = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY . '[' . $oPart->PartID() . ']<0>');
                 if (\is_string($sText) && 0 < \strlen($sText)) {
                     $this->bTextPartIsTrimmed = true;
                 }
             }
             if (\is_string($sText) && 0 < \strlen($sText)) {
                 $sTextCharset = $oPart->Charset();
                 if (empty($sTextCharset)) {
                     $sTextCharset = $sCharset;
                 }
                 $sTextCharset = \MailSo\Base\Utils::NormalizeCharset($sTextCharset, true);
                 $sText = \MailSo\Base\Utils::DecodeEncodingValue($sText, $oPart->MailEncodingName());
                 $sText = \MailSo\Base\Utils::ConvertEncoding($sText, $sTextCharset, \MailSo\Base\Enumerations\Charset::UTF_8);
                 $sText = \MailSo\Base\Utils::Utf8Clear($sText);
                 if ('text/html' === $oPart->ContentType()) {
                     $sHtmlParts[] = $sText;
                 } else {
                     $sPlainParts[] = $sText;
                 }
             }
         }
         if (0 < \count($sHtmlParts)) {
             $this->sHtml = \implode('<br />', $sHtmlParts);
         } else {
             $this->sPlain = \trim(\implode("\n", $sPlainParts));
         }
         $aMatch = array();
         if (\preg_match('/-----BEGIN PGP SIGNATURE-----(.+)-----END PGP SIGNATURE-----/ism', $this->sPlain, $aMatch) && !empty($aMatch[0])) {
             $this->sPgpSignature = \trim($aMatch[0]);
             $this->bPgpSigned = true;
         }
         $aMatch = array();
         if (\preg_match('/-----BEGIN PGP MESSAGE-----/ism', $this->sPlain, $aMatch) && !empty($aMatch[0])) {
             $this->bPgpEncrypted = true;
         }
         unset($sHtmlParts, $sPlainParts, $aMatch);
     }
     //		if (empty($this->sPgpSignature) && 'multipart/signed' === \strtolower($this->sContentType) &&
     //			'application/pgp-signature' === \strtolower($oHeaders->ParameterValue(
     //				\MailSo\Mime\Enumerations\Header::CONTENT_TYPE,
     //				\MailSo\Mime\Enumerations\Parameter::PROTOCOL
     //			)))
     //		{
     //			$aPgpSignatureParts = $oBodyStructure ? $oBodyStructure->SearchByContentType('application/pgp-signature') : null;
     //			if (\is_array($aPgpSignatureParts) && 0 < \count($aPgpSignatureParts) && isset($aPgpSignatureParts[0]))
     //			{
     //				$sPgpSignatureText = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY.'['.$aPgpSignatureParts[0]->PartID().']');
     //				if (\is_string($sPgpSignatureText) && 0 < \strlen($sPgpSignatureText) && 0 < \strpos($sPgpSignatureText, 'BEGIN PGP SIGNATURE'))
     //				{
     //					$this->sPgpSignature = \trim($sPgpSignatureText);
     //					$this->bPgpSigned = true;
     //				}
     //			}
     //		}
     if ($oBodyStructure) {
         $aAttachmentsParts = $oBodyStructure->SearchAttachmentsParts();
         if ($aAttachmentsParts && 0 < count($aAttachmentsParts)) {
             $this->oAttachments = AttachmentCollection::NewInstance();
             foreach ($aAttachmentsParts as $oAttachmentItem) {
                 $this->oAttachments->Add(\MailSo\Mail\Attachment::NewBodyStructureInstance($this->sFolder, $this->iUid, $oAttachmentItem));
             }
         }
     }
     return $this;
 }
Esempio n. 4
0
 /**
  * @param string $sEmail
  * @param string $sUrl
  * @param string $sUser
  * @param string $sPassword
  * @param string $sProxy = ''
  *
  * @return bool
  */
 public function Sync($sEmail, $sUrl, $sUser, $sPassword, $sProxy = '')
 {
     $this->SyncDatabase();
     $iUserID = $this->getUserId($sEmail);
     if (0 >= $iUserID) {
         return false;
     }
     $aUrl = \parse_url($sUrl);
     if (!\is_array($aUrl)) {
         $aUrl = array();
     }
     $aUrl['scheme'] = isset($aUrl['scheme']) ? $aUrl['scheme'] : 'http';
     $aUrl['host'] = isset($aUrl['host']) ? $aUrl['host'] : 'localhost';
     $aUrl['port'] = isset($aUrl['port']) ? $aUrl['port'] : 80;
     $aUrl['path'] = isset($aUrl['path']) ? \rtrim($aUrl['path'], '\\/') . '/' : '/';
     $aSettings = array('baseUri' => $aUrl['scheme'] . '://' . $aUrl['host'] . ('80' === (string) $aUrl['port'] ? '' : ':' . $aUrl['port']), 'userName' => $sUser, 'password' => $sPassword);
     $this->oLogger->AddSecret($sPassword);
     if (!empty($sProxy)) {
         $aSettings['proxy'] = $sProxy;
     }
     $sPath = $aUrl['path'];
     if (!\class_exists('Sabre\\DAV\\Client')) {
         return false;
     }
     $oClient = new \Sabre\DAV\Client($aSettings);
     $oClient->setVerifyPeer(false);
     $this->oLogger->Write('User: '******'userName'] . ', Url: ' . $sUrl, \MailSo\Log\Enumerations\Type::INFO, 'DAV');
     $aRemoteSyncData = $this->prepearRemoteSyncData($oClient, $sPath);
     if (false === $aRemoteSyncData) {
         return false;
     }
     $aDatabaseSyncData = $this->prepearDatabaseSyncData($iUserID);
     //		$this->oLogger->WriteDump($aDatabaseSyncData);
     //		$this->oLogger->WriteDump($aRemoteSyncData);
     //+++del (from carddav)
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if ($aData['deleted'] && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf'])) {
             $this->davClientRequest($oClient, 'DELETE', $sPath . $aRemoteSyncData[$sKey]['vcf']);
         }
     }
     //---del
     //+++del (from db)
     $aIdsForDeletedion = array();
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if (!$aData['deleted'] && !empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) {
             $aIdsForDeletedion[] = $aData['id_contact'];
         }
     }
     if (0 < \count($aIdsForDeletedion)) {
         $this->DeleteContacts($sEmail, $aIdsForDeletedion, false);
     }
     //---del
     $this->flushDeletedContacts($iUserID);
     //+++new or newer (from db)
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if (!$aData['deleted'] && (empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) || !empty($aData['etag']) && isset($aRemoteSyncData[$sKey]) && $aRemoteSyncData[$sKey]['etag'] !== $aData['etag'] && $aRemoteSyncData[$sKey]['changed'] < $aData['changed']) {
             $mID = $aData['id_contact'];
             $oContact = $this->GetContactByID($sEmail, $mID, false);
             if ($oContact) {
                 $sExsistensBody = '';
                 $mExsistenRemoteID = isset($aRemoteSyncData[$sKey]['vcf']) && !empty($aData['etag']) ? $aRemoteSyncData[$sKey]['vcf'] : '';
                 if (0 < \strlen($mExsistenRemoteID)) {
                     $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $mExsistenRemoteID);
                     if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) {
                         $sExsistensBody = \trim($oResponse['body']);
                     }
                 }
                 $oResponse = $this->davClientRequest($oClient, 'PUT', $sPath . $oContact->CardDavNameUri(), $oContact->ToVCard($sExsistensBody));
                 if ($oResponse && isset($oResponse['headers'], $oResponse['headers']['etag'])) {
                     $sEtag = \trim(\trim($oResponse['headers']['etag']), '"\'');
                     $sDate = !empty($oResponse['headers']['date']) ? \trim($oResponse['headers']['date']) : '';
                     if (!empty($sEtag)) {
                         $iChanged = empty($sDate) ? \time() : \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate);
                         $this->updateContactEtagAndTime($iUserID, $mID, $sEtag, $iChanged);
                     }
                 }
             }
             unset($oContact);
         }
     }
     //---new
     //+++new or newer (from carddav)
     foreach ($aRemoteSyncData as $sKey => $aData) {
         if (!isset($aDatabaseSyncData[$sKey]) || $aDatabaseSyncData[$sKey]['etag'] !== $aData['etag'] && $aDatabaseSyncData[$sKey]['changed'] < $aData['changed']) {
             $mExsistenContactID = isset($aDatabaseSyncData[$sKey]['id_contact']) ? $aDatabaseSyncData[$sKey]['id_contact'] : '';
             $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $aData['vcf']);
             if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) {
                 $sBody = \trim($oResponse['body']);
                 if (!empty($sBody)) {
                     $oContact = null;
                     if ($mExsistenContactID) {
                         $oContact = $this->GetContactByID($sEmail, $mExsistenContactID);
                     }
                     if (!$oContact) {
                         $oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
                     }
                     $oContact->PopulateByVCard($sBody, !empty($oResponse['headers']['etag']) ? \trim(\trim($oResponse['headers']['etag']), '"\'') : '');
                     $this->ContactSave($sEmail, $oContact);
                     unset($oContact);
                 }
             }
         }
     }
     return true;
 }
Esempio n. 5
0
 /**
  * @param string $sRawFolderFullName
  * @param \MailSo\Imap\FetchResponse $oFetchResponse
  * @param \MailSo\Imap\BodyStructure $oBodyStructure = null
  * @param array $aAscPartsIds = array()
  *
  * @return CApiMailMessage
  */
 public function InitByFetchResponse($sRawFolderFullName, $oFetchResponse, $oBodyStructure = null, $sRfc822SubMimeIndex = '', $aAscPartsIds = array())
 {
     if (!$oBodyStructure) {
         $oBodyStructure = $oFetchResponse->GetFetchBodyStructure();
     }
     $aTextParts = $oBodyStructure ? $oBodyStructure->SearchHtmlOrPlainParts() : array();
     $aICalPart = $oBodyStructure ? $oBodyStructure->SearchByContentType('text/calendar') : null;
     $oICalPart = is_array($aICalPart) && 0 < count($aICalPart) ? $aICalPart[0] : null;
     $aVCardPart = $oBodyStructure ? $oBodyStructure->SearchByContentType('text/vcard') : null;
     $aVCardPart = $aVCardPart ? $aVCardPart : ($oBodyStructure ? $oBodyStructure->SearchByContentType('text/x-vcard') : null);
     $oVCardPart = is_array($aVCardPart) && 0 < count($aVCardPart) ? $aVCardPart[0] : null;
     $sUid = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::UID);
     $sSize = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::RFC822_SIZE);
     $sInternalDate = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::INTERNALDATE);
     $aFlags = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::FLAGS);
     $this->sFolder = $sRawFolderFullName;
     $this->iUid = is_numeric($sUid) ? (int) $sUid : 0;
     $this->iSize = is_numeric($sSize) ? (int) $sSize : 0;
     $this->iTextSize = 0;
     $this->aFlags = is_array($aFlags) ? $aFlags : array();
     $this->aFlagsLowerCase = array_map('strtolower', $this->aFlags);
     $this->iInternalTimeStampInUTC = \MailSo\Base\DateTimeHelper::ParseInternalDateString($sInternalDate);
     if ($oICalPart) {
         $sICal = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY . '[' . $oICalPart->PartID() . ']');
         if (!empty($sICal)) {
             $sICal = \MailSo\Base\Utils::DecodeEncodingValue($sICal, $oICalPart->MailEncodingName());
             $sICal = \MailSo\Base\Utils::ConvertEncoding($sICal, \MailSo\Base\Utils::NormalizeCharset($oICalPart->Charset(), true), \MailSo\Base\Enumerations\Charset::UTF_8);
             if (!empty($sICal) && false !== strpos($sICal, 'BEGIN:VCALENDAR')) {
                 $sICal = preg_replace('/(.*)(BEGIN[:]VCALENDAR(.+)END[:]VCALENDAR)(.*)/ms', '$2', $sICal);
             } else {
                 $sICal = '';
             }
             if (!empty($sICal)) {
                 $this->AddExtend('ICAL_RAW', $sICal);
             }
         }
     }
     if ($oVCardPart) {
         $sVCard = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY . '[' . $oVCardPart->PartID() . ']');
         if (!empty($sVCard)) {
             $sVCard = \MailSo\Base\Utils::DecodeEncodingValue($sVCard, $oVCardPart->MailEncodingName());
             $sVCard = \MailSo\Base\Utils::ConvertEncoding($sVCard, \MailSo\Base\Utils::NormalizeCharset($oVCardPart->Charset(), true), \MailSo\Base\Enumerations\Charset::UTF_8);
             if (!empty($sVCard) && false !== strpos($sVCard, 'BEGIN:VCARD')) {
                 $sVCard = preg_replace('/(.*)(BEGIN\\:VCARD(.+)END\\:VCARD)(.*)/ms', '$2', $sVCard);
             } else {
                 $sVCard = '';
             }
             if (!empty($sVCard)) {
                 $this->AddExtend('VCARD_RAW', $sVCard);
             }
         }
     }
     $sCharset = $oBodyStructure ? $oBodyStructure->SearchCharset() : '';
     $sCharset = \MailSo\Base\Utils::NormalizeCharset($sCharset);
     $this->sHeaders = trim($oFetchResponse->GetHeaderFieldsValue($sRfc822SubMimeIndex));
     if (!empty($this->sHeaders)) {
         $oHeaders = \MailSo\Mime\HeaderCollection::NewInstance()->Parse($this->sHeaders, false, $sCharset);
         $sContentTypeCharset = $oHeaders->ParameterValue(\MailSo\Mime\Enumerations\Header::CONTENT_TYPE, \MailSo\Mime\Enumerations\Parameter::CHARSET);
         if (!empty($sContentTypeCharset)) {
             $sCharset = $sContentTypeCharset;
             $sCharset = \MailSo\Base\Utils::NormalizeCharset($sCharset);
         }
         if (!empty($sCharset)) {
             $oHeaders->SetParentCharset($sCharset);
         }
         $bCharsetAutoDetect = 0 === \strlen($sCharset);
         $this->sSubject = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::SUBJECT, $bCharsetAutoDetect);
         $this->sMessageId = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::MESSAGE_ID);
         $this->sContentType = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::CONTENT_TYPE);
         $aReceived = $oHeaders->ValuesByName(\MailSo\Mime\Enumerations\Header::RECEIVED);
         $sReceived = !empty($aReceived[0]) ? trim($aReceived[0]) : '';
         $sDate = '';
         if (!empty($sReceived)) {
             $aParts = explode(';', $sReceived);
             if (0 < count($aParts)) {
                 $aParts = array_reverse($aParts);
                 foreach ($aParts as $sReceiveLine) {
                     $sReceiveLine = trim($sReceiveLine);
                     if (preg_match('/[\\d]{4} [\\d]{2}:[\\d]{2}:[\\d]{2} /', $sReceiveLine)) {
                         $sDate = $sReceiveLine;
                         break;
                     }
                 }
             }
         }
         if (empty($sDate)) {
             $sDate = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::DATE);
         }
         if (!empty($sDate)) {
             $this->iReceivedOrDateTimeStampInUTC = \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate);
         }
         $this->oFrom = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::FROM_, $bCharsetAutoDetect);
         $this->oTo = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::TO_, $bCharsetAutoDetect);
         $this->oCc = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::CC, $bCharsetAutoDetect);
         $this->oBcc = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::BCC, $bCharsetAutoDetect);
         $this->oSender = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::SENDER, $bCharsetAutoDetect);
         $this->oReplyTo = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::REPLY_TO, $bCharsetAutoDetect);
         $this->sInReplyTo = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::IN_REPLY_TO);
         $this->sReferences = \preg_replace('/[\\s]+/', ' ', $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::REFERENCES));
         // Sensitivity
         $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::NOTHING;
         $sSensitivity = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::SENSITIVITY);
         switch (strtolower($sSensitivity)) {
             case 'personal':
                 $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::PERSONAL;
                 break;
             case 'private':
                 $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::PRIVATE_;
                 break;
             case 'company-confidential':
                 $this->iSensitivity = \MailSo\Mime\Enumerations\Sensitivity::CONFIDENTIAL;
                 break;
         }
         // Priority
         $this->iPriority = \MailSo\Mime\Enumerations\MessagePriority::NORMAL;
         $sPriority = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_MSMAIL_PRIORITY);
         if (0 === strlen($sPriority)) {
             $sPriority = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::IMPORTANCE);
         }
         if (0 === strlen($sPriority)) {
             $sPriority = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_PRIORITY);
         }
         if (0 < strlen($sPriority)) {
             switch (str_replace(' ', '', strtolower($sPriority))) {
                 case 'high':
                 case '1(highest)':
                 case '2(high)':
                 case '1':
                 case '2':
                     $this->iPriority = \MailSo\Mime\Enumerations\MessagePriority::HIGH;
                     break;
                 case 'low':
                 case '4(low)':
                 case '5(lowest)':
                 case '4':
                 case '5':
                     $this->iPriority = \MailSo\Mime\Enumerations\MessagePriority::LOW;
                     break;
             }
         }
         // ReadingConfirmation
         $this->sReadingConfirmation = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::DISPOSITION_NOTIFICATION_TO);
         if (0 === strlen($this->sReadingConfirmation)) {
             $this->sReadingConfirmation = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_CONFIRM_READING_TO);
         }
         $this->sReadingConfirmation = trim($this->sReadingConfirmation);
         $sDraftInfo = $oHeaders->ValueByName(\MailSo\Mime\Enumerations\Header::X_DRAFT_INFO);
         if (0 < strlen($sDraftInfo)) {
             $sType = '';
             $sFolder = '';
             $sUid = '';
             \MailSo\Mime\ParameterCollection::NewInstance($sDraftInfo)->ForeachList(function ($oParameter) use(&$sType, &$sFolder, &$sUid) {
                 switch (strtolower($oParameter->Name())) {
                     case 'type':
                         $sType = $oParameter->Value();
                         break;
                     case 'uid':
                         $sUid = $oParameter->Value();
                         break;
                     case 'folder':
                         $sFolder = base64_decode($oParameter->Value());
                         break;
                 }
             });
             if (0 < strlen($sType) && 0 < strlen($sFolder) && 0 < strlen($sUid)) {
                 $this->aDraftInfo = array($sType, $sUid, $sFolder);
             }
         }
         \CApi::Plugin()->RunHook('api-mail-message-headers-parse', array(&$this, $oHeaders));
     }
     if (is_array($aTextParts) && 0 < count($aTextParts)) {
         if (0 === \strlen($sCharset)) {
             $sCharset = \MailSo\Base\Enumerations\Charset::UTF_8;
         }
         $sHtmlParts = array();
         $sPlainParts = array();
         $iHtmlSize = 0;
         $iPlainSize = 0;
         foreach ($aTextParts as $oPart) {
             if ($oPart) {
                 if ('text/html ' === $oPart->ContentType()) {
                     $iHtmlSize += $oPart->EstimatedSize();
                 } else {
                     $iPlainSize += $oPart->EstimatedSize();
                 }
             }
             $sText = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY . '[' . $oPart->PartID() . ('' !== $sRfc822SubMimeIndex && is_numeric($sRfc822SubMimeIndex) ? '.1' : '') . ']');
             //				if (null === $sText)
             //				{
             //					$sText = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY.'['.$oPart->PartID().
             //						('' !== $sRfc822SubMimeIndex && is_numeric($sRfc822SubMimeIndex) ? '.1' : '').']<0>');
             //				}
             if (is_string($sText) && 0 < strlen($sText)) {
                 $sTextCharset = $oPart->Charset();
                 if (empty($sTextCharset)) {
                     $sTextCharset = $sCharset;
                 }
                 $sTextCharset = \MailSo\Base\Utils::NormalizeCharset($sTextCharset, true);
                 $sText = \MailSo\Base\Utils::DecodeEncodingValue($sText, $oPart->MailEncodingName());
                 $sText = \MailSo\Base\Utils::ConvertEncoding($sText, $sTextCharset, \MailSo\Base\Enumerations\Charset::UTF_8);
                 $sText = \MailSo\Base\Utils::Utf8Clear($sText);
                 if ('text/html' === $oPart->ContentType()) {
                     $sHtmlParts[] = $sText;
                 } else {
                     $sPlainParts[] = $sText;
                 }
             }
         }
         if (0 < count($sHtmlParts)) {
             $this->sHtml = trim(implode('<br />', $sHtmlParts));
             $this->iTextSize = strlen($this->sHtml);
         } else {
             $this->sPlain = trim(implode("\n", $sPlainParts));
             $this->iTextSize = strlen($this->sPlain);
         }
         if (0 === $this->iTextSize) {
             $this->iTextSize = 0 < $iHtmlSize ? $iHtmlSize : $iPlainSize;
         }
         unset($sHtmlParts, $sPlainParts);
     }
     if ($oBodyStructure) {
         $aAttachmentsParts = $oBodyStructure->SearchAttachmentsParts();
         if ($aAttachmentsParts && 0 < count($aAttachmentsParts)) {
             $this->oAttachments = CApiMailAttachmentCollection::NewInstance();
             foreach ($aAttachmentsParts as $oAttachmentItem) {
                 $this->oAttachments->Add(CApiMailAttachment::NewBodyStructureInstance($this->sFolder, $this->iUid, $oAttachmentItem));
             }
             $this->oAttachments->ForeachList(function ($oAttachment) use($aAscPartsIds, $oFetchResponse) {
                 if ($oAttachment && in_array($oAttachment->MimeIndex(), $aAscPartsIds)) {
                     $mContent = $oFetchResponse->GetFetchValue(\MailSo\Imap\Enumerations\FetchType::BODY . '[' . $oAttachment->MimeIndex() . ']');
                     if (is_string($mContent)) {
                         $oAttachment->SetContent(\MailSo\Base\Utils::DecodeEncodingValue($mContent, $oAttachment->ContentTransferEncoding()));
                     }
                 }
             });
         }
     }
     \CApi::Plugin()->RunHook('api-mail-message-parse', array(&$this, $oFetchResponse, $oBodyStructure, $sRfc822SubMimeIndex));
     return $this;
 }
Esempio n. 6
0
 /**
  * Parse date string formated as "2015-05-08T14:32:18.483-07:00"
  *
  * @param string $sDateTime
  *
  * @return int
  */
 public static function TryToParseSpecEtagFormat($sDateTime)
 {
     $sDateTime = \trim(\preg_replace('/ \\([a-zA-Z0-9]+\\)$/', '', \trim($sDateTime)));
     $sDateTime = \trim(\preg_replace('/(:[\\d]{2})\\.[\\d]{3}/', '$1', \trim($sDateTime)));
     $sDateTime = \trim(\preg_replace('/(-[\\d]{2})T([\\d]{2}:)/', '$1 $2', \trim($sDateTime)));
     $sDateTime = \trim(\preg_replace('/([\\-+][\\d]{2}):([\\d]{2})$/', ' $1$2', \trim($sDateTime)));
     return \MailSo\Base\DateTimeHelper::ParseDateStringType1($sDateTime);
 }
 /**
  * @param string $sEmail
  * @param string $sUrl
  * @param string $sUser
  * @param string $sPassword
  * @param string $sProxy = ''
  *
  * @return bool
  */
 public function Sync($sEmail, $sUrl, $sUser, $sPassword, $sProxy = '')
 {
     $this->SyncDatabase();
     $iUserID = $this->getUserId($sEmail);
     if (0 >= $iUserID) {
         return false;
     }
     $oClient = $this->getDavClient($sUrl, $sUser, $sPassword, $sProxy);
     if (!$oClient) {
         return false;
     }
     $sPath = $oClient->__UrlPath__;
     $aRemoteSyncData = $this->prepearRemoteSyncData($oClient, $sPath);
     if (false === $aRemoteSyncData) {
         return false;
     }
     $aDatabaseSyncData = $this->prepearDatabaseSyncData($iUserID);
     //		$this->oLogger->WriteDump($aRemoteSyncData);
     //		$this->oLogger->WriteDump($aDatabaseSyncData);
     //+++del (from carddav)
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if ($aData['deleted'] && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf'])) {
             $this->davClientRequest($oClient, 'DELETE', $sPath . $aRemoteSyncData[$sKey]['vcf']);
         }
     }
     //---del
     //+++del (from db)
     $aIdsForDeletedion = array();
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if (!$aData['deleted'] && !empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) {
             $aIdsForDeletedion[] = $aData['id_contact'];
         }
     }
     if (0 < \count($aIdsForDeletedion)) {
         $this->DeleteContacts($sEmail, $aIdsForDeletedion, false);
     }
     //---del
     $this->flushDeletedContacts($iUserID);
     //+++new or newer (from db)
     foreach ($aDatabaseSyncData as $sKey => $aData) {
         if (!$aData['deleted'] && (empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) || !empty($aData['etag']) && isset($aRemoteSyncData[$sKey]) && $aRemoteSyncData[$sKey]['etag'] !== $aData['etag'] && $aRemoteSyncData[$sKey]['changed'] < $aData['changed']) {
             $mID = $aData['id_contact'];
             $oContact = $this->GetContactByID($sEmail, $mID, false);
             if ($oContact) {
                 $sExsistensBody = '';
                 $mExsistenRemoteID = isset($aRemoteSyncData[$sKey]['vcf']) && !empty($aData['etag']) ? $aRemoteSyncData[$sKey]['vcf'] : '';
                 if (0 < \strlen($mExsistenRemoteID)) {
                     $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $mExsistenRemoteID);
                     if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) {
                         $sExsistensBody = \trim($oResponse['body']);
                     }
                     //						$this->oLogger->WriteDump($sExsistensBody);
                 }
                 $oResponse = $this->davClientRequest($oClient, 'PUT', $sPath . (0 < \strlen($mExsistenRemoteID) ? $mExsistenRemoteID : $oContact->CardDavNameUri()), $oContact->ToVCard($sExsistensBody, $this->oLogger) . "\r\n\r\n");
                 if ($oResponse && isset($oResponse['headers'], $oResponse['headers']['etag'])) {
                     $sEtag = \trim(\trim($oResponse['headers']['etag']), '"\'');
                     $sDate = !empty($oResponse['headers']['date']) ? \trim($oResponse['headers']['date']) : '';
                     if (!empty($sEtag)) {
                         $iChanged = empty($sDate) ? \time() : \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate);
                         $this->updateContactEtagAndTime($iUserID, $mID, $sEtag, $iChanged);
                     }
                 }
             }
             unset($oContact);
         }
     }
     //---new
     //+++new or newer (from carddav)
     foreach ($aRemoteSyncData as $sKey => $aData) {
         if (!isset($aDatabaseSyncData[$sKey]) || $aDatabaseSyncData[$sKey]['etag'] !== $aData['etag'] && $aDatabaseSyncData[$sKey]['changed'] < $aData['changed']) {
             $mExsistenContactID = isset($aDatabaseSyncData[$sKey]['id_contact']) ? $aDatabaseSyncData[$sKey]['id_contact'] : '';
             $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $aData['vcf']);
             if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) {
                 $sBody = \trim($oResponse['body']);
                 if (!empty($sBody)) {
                     $oContact = null;
                     if ($mExsistenContactID) {
                         $oContact = $this->GetContactByID($sEmail, $mExsistenContactID);
                     }
                     if (!$oContact) {
                         $oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
                     }
                     $oContact->PopulateByVCard($aData['uid'], $sBody, !empty($oResponse['headers']['etag']) ? \trim(\trim($oResponse['headers']['etag']), '"\'') : '', $this->oLogger);
                     $this->ContactSave($sEmail, $oContact);
                     unset($oContact);
                 }
             }
         }
     }
     return true;
 }