/** * @param \RainLoop\Plugins\Manager $oPlugins * @param \MailSo\Mail\MailClient $oMailClient * @param \RainLoop\Application $oConfig * * @return bool */ public function IncConnectAndLoginHelper($oPlugins, $oMailClient, $oConfig) { $bLogin = false; $aImapCredentials = array('UseConnect' => true, 'UseAuth' => true, 'Host' => $this->DomainIncHost(), 'Port' => $this->DomainIncPort(), 'Secure' => $this->DomainIncSecure(), 'Login' => $this->IncLogin(), 'Password' => $this->Password(), 'ProxyAuthUser' => $this->ProxyAuthUser(), 'ProxyAuthPassword' => $this->ProxyAuthPassword(), 'VerifySsl' => !!$oConfig->Get('ssl', 'verify_certificate', false), 'AllowSelfSigned' => !!$oConfig->Get('ssl', 'allow_self_signed', true), 'UseAuthPlainIfSupported' => !!$oConfig->Get('labs', 'use_imap_auth_plain')); $oPlugins->RunHook('filter.imap-credentials', array($this, &$aImapCredentials)); $oPlugins->RunHook('event.imap-pre-connect', array($this, $aImapCredentials['UseConnect'], $aImapCredentials)); if ($aImapCredentials['UseConnect']) { $oMailClient->Connect($aImapCredentials['Host'], $aImapCredentials['Port'], $aImapCredentials['Secure'], $aImapCredentials['VerifySsl'], $aImapCredentials['AllowSelfSigned']); } $oPlugins->RunHook('event.imap-pre-login', array($this, $aImapCredentials['UseAuth'], $aImapCredentials)); if ($aImapCredentials['UseAuth']) { if (0 < \strlen($aImapCredentials['ProxyAuthUser']) && 0 < \strlen($aImapCredentials['ProxyAuthPassword'])) { $oMailClient->Login($aImapCredentials['ProxyAuthUser'], $aImapCredentials['ProxyAuthPassword'], $aImapCredentials['Login'], $aImapCredentials['UseAuthPlainIfSupported']); } else { $iGatLen = \strlen(APP_GOOGLE_ACCESS_TOKEN_PREFIX); $sPassword = $aImapCredentials['Password']; if (APP_GOOGLE_ACCESS_TOKEN_PREFIX === \substr($sPassword, 0, $iGatLen)) { $oMailClient->LoginWithXOauth2(\base64_encode('user='******'Login'] . "" . 'auth=Bearer ' . \substr($sPassword, $iGatLen) . "")); } else { $oMailClient->Login($aImapCredentials['Login'], $aImapCredentials['Password'], '', $aImapCredentials['UseAuthPlainIfSupported']); } } $bLogin = true; } $oPlugins->RunHook('event.imap-post-login', array($this, $aImapCredentials['UseAuth'], $bLogin, $aImapCredentials)); return $bLogin; }
/** * @param string $sHash * * @return int * * @throws \MailSo\Base\Exceptions\Exception */ public function getAccountUnredCountFromHash($sHash) { $iResult = 0; $oAccount = $this->GetAccountFromCustomToken($sHash, false); if ($oAccount) { try { $oMailClient = \MailSo\Mail\MailClient::NewInstance(); $oMailClient->SetLogger($this->Logger()); $oAccount->IncConnectAndLoginHelper($this->Plugins(), $oMailClient, $this->Config()); $iResult = $oMailClient->InboxUnreadCount(); $oMailClient->LogoutAndDisconnect(); } catch (\Exception $oException) { $this->Logger()->WriteException($oException); } } return $iResult; }
<?php include '../lib/MailSo/MailSo.php'; echo '<pre>'; $oLogger = \MailSo\Log\Logger::SingletonInstance()->Add(\MailSo\Log\Drivers\Inline::NewInstance("\r\n", true)); $oData = null; try { $oMailClient = \MailSo\Mail\MailClient::NewInstance()->SetLogger($oLogger); $oData = $oMailClient->Connect('imap.gmail.com', 993, \MailSo\Net\Enumerations\ConnectionSecurityType::SSL)->Login('*****@*****.**', 'test')->MessageList('INBOX'); $oMailClient->LogoutAndDisconnect(); } catch (Exception $e) { var_dump($e); } $oLogger->WriteDump($oData); $oLogger->WriteDump(\MailSo\Base\Loader::Statistic());
/** * @param mixed $mResponse * @param string $sParent * @param array $aParameters = array() * * @return mixed */ protected function responseObject($mResponse, $sParent = '', $aParameters = array()) { $mResult = $mResponse; if (\is_object($mResponse)) { $bHook = true; $sClassName = \get_class($mResponse); $bHasSimpleJsonFunc = \method_exists($mResponse, 'ToSimpleJSON'); $bThumb = $this->GetCapa(false, \RainLoop\Enumerations\Capa::ATTACHMENT_THUMBNAILS); if ($bHasSimpleJsonFunc) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), $mResponse->ToSimpleJSON(true)); } else { if ('MailSo\\Mail\\Message' === $sClassName) { $oAccount = $this->getAccountFromToken(false); $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('Folder' => $mResponse->Folder(), 'Uid' => (string) $mResponse->Uid(), 'Subject' => \trim(\MailSo\Base\Utils::Utf8Clear($mResponse->Subject())), 'MessageId' => $mResponse->MessageId(), 'Size' => $mResponse->Size(), 'DateTimeStampInUTC' => !!$this->Config()->Get('labs', 'date_from_headers', false) ? $mResponse->HeaderTimeStampInUTC() : $mResponse->InternalTimeStampInUTC(), 'ReplyTo' => $this->responseObject($mResponse->ReplyTo(), $sParent, $aParameters), 'From' => $this->responseObject($mResponse->From(), $sParent, $aParameters), 'To' => $this->responseObject($mResponse->To(), $sParent, $aParameters), 'Cc' => $this->responseObject($mResponse->Cc(), $sParent, $aParameters), 'Bcc' => $this->responseObject($mResponse->Bcc(), $sParent, $aParameters), 'Sender' => $this->responseObject($mResponse->Sender(), $sParent, $aParameters), 'DeliveredTo' => $this->responseObject($mResponse->DeliveredTo(), $sParent, $aParameters), 'Priority' => $mResponse->Priority(), 'Threads' => $mResponse->Threads(), 'ThreadsLen' => $mResponse->ThreadsLen(), 'ParentThread' => $mResponse->ParentThread(), 'Sensitivity' => $mResponse->Sensitivity(), 'ExternalProxy' => false, 'ReadReceipt' => '')); $mResult['SubjectParts'] = $this->explodeSubject($mResult['Subject']); $oAttachments = $mResponse->Attachments(); $iAttachmentsCount = $oAttachments ? $oAttachments->Count() : 0; $mResult['HasAttachments'] = 0 < $iAttachmentsCount; $mResult['AttachmentsMainType'] = ''; if (0 < $iAttachmentsCount) { switch (true) { case $iAttachmentsCount === $oAttachments->ImageCount(): $mResult['AttachmentsMainType'] = 'image'; break; case $iAttachmentsCount === $oAttachments->ArchiveCount(): $mResult['AttachmentsMainType'] = 'archive'; break; case $iAttachmentsCount === $oAttachments->PdfCount(): $mResult['AttachmentsMainType'] = 'pdf'; break; case $iAttachmentsCount === $oAttachments->DocCount(): $mResult['AttachmentsMainType'] = 'doc'; break; case $iAttachmentsCount === $oAttachments->CertificateCount(): $mResult['AttachmentsMainType'] = 'certificate'; break; } } $sSubject = $mResult['Subject']; $mResult['Hash'] = \md5($mResult['Folder'] . $mResult['Uid']); $mResult['RequestHash'] = \RainLoop\Utils::EncodeKeyValues(array('V' => APP_VERSION, 'Account' => $oAccount ? \md5($oAccount->Hash()) : '', 'Folder' => $mResult['Folder'], 'Uid' => $mResult['Uid'], 'MimeType' => 'message/rfc822', 'FileName' => (0 === \strlen($sSubject) ? 'message-' . $mResult['Uid'] : \MailSo\Base\Utils::ClearXss($sSubject)) . '.eml')); // Flags $aFlags = $mResponse->FlagsLowerCase(); $mResult['IsSeen'] = \in_array('\\seen', $aFlags); $mResult['IsFlagged'] = \in_array('\\flagged', $aFlags); $mResult['IsAnswered'] = \in_array('\\answered', $aFlags); $mResult['IsDeleted'] = \in_array('\\deleted', $aFlags); $sForwardedFlag = $this->Config()->Get('labs', 'imap_forwarded_flag', ''); $sReadReceiptFlag = $this->Config()->Get('labs', 'imap_read_receipt_flag', ''); $mResult['IsForwarded'] = 0 < \strlen($sForwardedFlag) && \in_array(\strtolower($sForwardedFlag), $aFlags); $mResult['IsReadReceipt'] = 0 < \strlen($sReadReceiptFlag) && \in_array(\strtolower($sReadReceiptFlag), $aFlags); $mResult['TextPartIsTrimmed'] = false; if ('Message' === $sParent) { $oAttachments = $mResponse->Attachments(); $bHasExternals = false; $mFoundedCIDs = array(); $aContentLocationUrls = array(); $mFoundedContentLocationUrls = array(); if ($oAttachments && 0 < $oAttachments->Count()) { $aList =& $oAttachments->GetAsArray(); foreach ($aList as $oAttachment) { if ($oAttachment) { $sContentLocation = $oAttachment->ContentLocation(); if ($sContentLocation && 0 < \strlen($sContentLocation)) { $aContentLocationUrls[] = $oAttachment->ContentLocation(); } } } } $sPlain = ''; $sHtml = \trim($mResponse->Html()); if (0 === \strlen($sHtml)) { $sPlain = \trim($mResponse->Plain()); } $mResult['DraftInfo'] = $mResponse->DraftInfo(); $mResult['InReplyTo'] = $mResponse->InReplyTo(); $mResult['References'] = $mResponse->References(); $fAdditionalExternalFilter = null; if (!!$this->Config()->Get('labs', 'use_local_proxy_for_external_images', false)) { $fAdditionalExternalFilter = function ($sUrl) { return './?/ProxyExternal/' . \RainLoop\Utils::EncodeKeyValues(array('Rnd' => \md5(\microtime(true)), 'Token' => \RainLoop\Utils::GetConnectionToken(), 'Url' => $sUrl)) . '/'; }; } $mResult['Html'] = 0 === \strlen($sHtml) ? '' : \MailSo\Base\HtmlUtils::ClearHtml($sHtml, $bHasExternals, $mFoundedCIDs, $aContentLocationUrls, $mFoundedContentLocationUrls, false, false, $fAdditionalExternalFilter); $mResult['ExternalProxy'] = null !== $fAdditionalExternalFilter; $mResult['Plain'] = $sPlain; // $mResult['Plain'] = 0 === \strlen($sPlain) ? '' : \MailSo\Base\HtmlUtils::ConvertPlainToHtml($sPlain); $mResult['TextHash'] = \md5($mResult['Html'] . $mResult['Plain']); $mResult['TextPartIsTrimmed'] = $mResponse->TextPartIsTrimmed(); $mResult['PgpSigned'] = $mResponse->PgpSigned(); $mResult['PgpEncrypted'] = $mResponse->PgpEncrypted(); $mResult['PgpSignature'] = $mResponse->PgpSignature(); unset($sHtml, $sPlain); $mResult['HasExternals'] = $bHasExternals; $mResult['HasInternals'] = \is_array($mFoundedCIDs) && 0 < \count($mFoundedCIDs) || \is_array($mFoundedContentLocationUrls) && 0 < \count($mFoundedContentLocationUrls); $mResult['FoundedCIDs'] = $mFoundedCIDs; $mResult['Attachments'] = $this->responseObject($oAttachments, $sParent, \array_merge($aParameters, array('FoundedCIDs' => $mFoundedCIDs, 'FoundedContentLocationUrls' => $mFoundedContentLocationUrls))); $mResult['ReadReceipt'] = $mResponse->ReadReceipt(); if (0 < \strlen($mResult['ReadReceipt']) && !$mResult['IsReadReceipt']) { if (0 < \strlen($mResult['ReadReceipt'])) { try { $oReadReceipt = \MailSo\Mime\Email::Parse($mResult['ReadReceipt']); if (!$oReadReceipt) { $mResult['ReadReceipt'] = ''; } } catch (\Exception $oException) { unset($oException); } } if (0 < \strlen($mResult['ReadReceipt']) && '1' === $this->Cacher($oAccount)->Get(\RainLoop\KeyPathHelper::ReadReceiptCache($oAccount->Email(), $mResult['Folder'], $mResult['Uid']), '0')) { $mResult['ReadReceipt'] = ''; } } } } else { if ('MailSo\\Mime\\Email' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('Name' => \MailSo\Base\Utils::Utf8Clear($mResponse->GetDisplayName()), 'Email' => \MailSo\Base\Utils::Utf8Clear($mResponse->GetEmail(true)), 'DkimStatus' => $mResponse->GetDkimStatus(), 'DkimValue' => $mResponse->GetDkimValue())); } else { if ('RainLoop\\Providers\\AddressBook\\Classes\\Contact' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('IdContact' => $mResponse->IdContact, 'Display' => \MailSo\Base\Utils::Utf8Clear($mResponse->Display), 'ReadOnly' => $mResponse->ReadOnly, 'IdPropertyFromSearch' => $mResponse->IdPropertyFromSearch, 'Properties' => $this->responseObject($mResponse->Properties, $sParent, $aParameters))); } else { if ('RainLoop\\Providers\\AddressBook\\Classes\\Tag' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('IdContactTag' => $mResponse->IdContactTag, 'Name' => \MailSo\Base\Utils::Utf8Clear($mResponse->Name), 'ReadOnly' => $mResponse->ReadOnly)); } else { if ('RainLoop\\Providers\\AddressBook\\Classes\\Property' === $sClassName) { // Simple hack if ($mResponse && $mResponse->IsWeb()) { $mResponse->Value = \preg_replace('/(skype|ftp|http[s]?)\\\\:\\/\\//i', '$1://', $mResponse->Value); } $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('IdProperty' => $mResponse->IdProperty, 'Type' => $mResponse->Type, 'TypeStr' => $mResponse->TypeStr, 'Value' => \MailSo\Base\Utils::Utf8Clear($mResponse->Value))); } else { if ('MailSo\\Mail\\Attachment' === $sClassName) { $oAccount = $this->getAccountFromToken(false); $mFoundedCIDs = isset($aParameters['FoundedCIDs']) && \is_array($aParameters['FoundedCIDs']) && 0 < \count($aParameters['FoundedCIDs']) ? $aParameters['FoundedCIDs'] : null; $mFoundedContentLocationUrls = isset($aParameters['FoundedContentLocationUrls']) && \is_array($aParameters['FoundedContentLocationUrls']) && 0 < \count($aParameters['FoundedContentLocationUrls']) ? $aParameters['FoundedContentLocationUrls'] : null; if ($mFoundedCIDs || $mFoundedContentLocationUrls) { $mFoundedCIDs = \array_merge($mFoundedCIDs ? $mFoundedCIDs : array(), $mFoundedContentLocationUrls ? $mFoundedContentLocationUrls : array()); $mFoundedCIDs = 0 < \count($mFoundedCIDs) ? $mFoundedCIDs : null; } $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('Folder' => $mResponse->Folder(), 'Uid' => (string) $mResponse->Uid(), 'Framed' => false, 'MimeIndex' => (string) $mResponse->MimeIndex(), 'MimeType' => $mResponse->MimeType(), 'FileName' => \MailSo\Base\Utils::ClearFileName(\MailSo\Base\Utils::ClearXss($mResponse->FileName(true))), 'EstimatedSize' => $mResponse->EstimatedSize(), 'CID' => $mResponse->Cid(), 'ContentLocation' => $mResponse->ContentLocation(), 'IsInline' => $mResponse->IsInline(), 'IsThumbnail' => $bThumb, 'IsLinked' => $mFoundedCIDs && \in_array(\trim(\trim($mResponse->Cid()), '<>'), $mFoundedCIDs) || $mFoundedContentLocationUrls && \in_array(\trim($mResponse->ContentLocation()), $mFoundedContentLocationUrls))); $mResult['Framed'] = $this->isFileHasFramedPreview($mResult['FileName']); if ($mResult['IsThumbnail']) { $mResult['IsThumbnail'] = $this->isFileHasThumbnail($mResult['FileName']); } $mResult['Download'] = \RainLoop\Utils::EncodeKeyValues(array('V' => APP_VERSION, 'Account' => $oAccount ? \md5($oAccount->Hash()) : '', 'Folder' => $mResult['Folder'], 'Uid' => $mResult['Uid'], 'MimeIndex' => $mResult['MimeIndex'], 'MimeType' => $mResult['MimeType'], 'FileName' => $mResult['FileName'], 'Framed' => $mResult['Framed'])); } else { if ('MailSo\\Mail\\Folder' === $sClassName) { $aExtended = null; $mStatus = $mResponse->Status(); if (\is_array($mStatus) && isset($mStatus['MESSAGES'], $mStatus['UNSEEN'], $mStatus['UIDNEXT'])) { $aExtended = array('MessageCount' => (int) $mStatus['MESSAGES'], 'MessageUnseenCount' => (int) $mStatus['UNSEEN'], 'UidNext' => (string) $mStatus['UIDNEXT'], 'Hash' => \MailSo\Mail\MailClient::GenerateHash($mResponse->FullNameRaw(), $mStatus['MESSAGES'], $mStatus['UNSEEN'], $mStatus['UIDNEXT'])); } $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('Name' => $mResponse->Name(), 'FullName' => $mResponse->FullName(), 'FullNameRaw' => $mResponse->FullNameRaw(), 'FullNameHash' => $this->hashFolderFullName($mResponse->FullNameRaw(), $mResponse->FullName()), 'Delimiter' => (string) $mResponse->Delimiter(), 'HasVisibleSubFolders' => $mResponse->HasVisibleSubFolders(), 'IsSubscribed' => $mResponse->IsSubscribed(), 'IsExists' => $mResponse->IsExists(), 'IsSelectable' => $mResponse->IsSelectable(), 'Flags' => $mResponse->FlagsLowerCase(), 'Extended' => $aExtended, 'SubFolders' => $this->responseObject($mResponse->SubFolders(), $sParent, $aParameters))); } else { if ('MailSo\\Mail\\MessageCollection' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('MessageCount' => $mResponse->MessageCount, 'MessageUnseenCount' => $mResponse->MessageUnseenCount, 'MessageResultCount' => $mResponse->MessageResultCount, 'Folder' => $mResponse->FolderName, 'FolderHash' => $mResponse->FolderHash, 'UidNext' => $mResponse->UidNext, 'NewMessages' => $this->responseObject($mResponse->NewMessages), 'LastCollapsedThreadUids' => $mResponse->LastCollapsedThreadUids, 'Offset' => $mResponse->Offset, 'Limit' => $mResponse->Limit, 'Search' => $mResponse->Search)); } else { if ('MailSo\\Mail\\AttachmentCollection' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('InlineCount' => $mResponse->InlineCount())); } else { if ('MailSo\\Mail\\FolderCollection' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('Namespace' => $mResponse->GetNamespace(), 'FoldersHash' => isset($mResponse->FoldersHash) ? $mResponse->FoldersHash : '', 'IsThreadsSupported' => $mResponse->IsThreadsSupported, 'Optimized' => $mResponse->Optimized, 'SystemFolders' => isset($mResponse->SystemFolders) && \is_array($mResponse->SystemFolders) ? $mResponse->SystemFolders : array())); } else { if ($mResponse instanceof \MailSo\Base\Collection) { $aList =& $mResponse->GetAsArray(); if (100 < \count($aList) && $mResponse instanceof \MailSo\Mime\EmailCollection) { $aList = \array_slice($aList, 0, 100); } $mResult = $this->responseObject($aList, $sParent, $aParameters); $bHook = false; } else { if ('RainLoop\\Providers\\AddressBook\\Classes\\Group' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('IdGroup' => $mResponse->IdGroup, 'Name' => $mResponse->Name, 'Contacts' => $this->responseObject($mResponse->Contacts, $sParent, $aParameters))); } else { if ('RainLoop\\Providers\\AddressBook\\Classes\\GroupContact' === $sClassName) { $mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array('IdContact' => $mResponse->IdContact, 'IdGroup' => $mResponse->IdGroup, 'Name' => $mResponse->Name, 'Email' => $mResponse->Email, 'Phone' => $mResponse->Phone)); } else { $mResult = '["' . \get_class($mResponse) . '"]'; $bHook = false; } } } } } } } } } } } } } } if ($bHook) { $this->Plugins()->RunHook('filter.response-object', array($sClassName, $mResult), false); } } else { if (\is_array($mResponse)) { foreach ($mResponse as $iKey => $oItem) { $mResponse[$iKey] = $this->responseObject($oItem, $sParent, $aParameters); } $mResult = $mResponse; } } unset($mResponse); return $mResult; }
/** * @param \RainLoop\Plugins\Manager $oPlugins * @param \MailSo\Mail\MailClient $oMailClient * @param \RainLoop\Application $oConfig * * @return bool */ public function IncConnectAndLoginHelper($oPlugins, $oMailClient, $oConfig) { $bLogin = false; $aImapCredentials = array('UseConnect' => true, 'UseAuth' => true, 'Host' => $this->DomainIncHost(), 'Port' => $this->DomainIncPort(), 'Secure' => $this->DomainIncSecure(), 'Login' => $this->IncLogin(), 'Password' => $this->Password(), 'ProxyAuthUser' => $this->ProxyAuthUser(), 'ProxyAuthPassword' => $this->ProxyAuthPassword(), 'VerifySsl' => !!$oConfig->Get('ssl', 'verify_certificate', false), 'AllowSelfSigned' => !!$oConfig->Get('ssl', 'allow_self_signed', true), 'UseAuthPlainIfSupported' => !!$oConfig->Get('labs', 'use_imap_auth_plain')); $oPlugins->RunHook('filter.imap-credentials', array($this, &$aImapCredentials)); $oPlugins->RunHook('event.imap-pre-connect', array($this, $aImapCredentials['UseConnect'], $aImapCredentials)); if ($aImapCredentials['UseConnect']) { $oMailClient->Connect($aImapCredentials['Host'], $aImapCredentials['Port'], $aImapCredentials['Secure'], $aImapCredentials['VerifySsl'], $aImapCredentials['AllowSelfSigned']); } $oPlugins->RunHook('event.imap-pre-login', array($this, $aImapCredentials['UseAuth'], $aImapCredentials)); if ($aImapCredentials['UseAuth']) { if (0 < \strlen($aImapCredentials['ProxyAuthUser']) && 0 < \strlen($aImapCredentials['ProxyAuthPassword'])) { $oMailClient->Login($aImapCredentials['ProxyAuthUser'], $aImapCredentials['ProxyAuthPassword'], $aImapCredentials['Login'], $aImapCredentials['UseAuthPlainIfSupported']); } else { $oMailClient->Login($aImapCredentials['Login'], $aImapCredentials['Password'], '', $aImapCredentials['UseAuthPlainIfSupported']); } $bLogin = true; } $oPlugins->RunHook('event.imap-post-login', array($this, $aImapCredentials['UseAuth'], $bLogin, $aImapCredentials)); return $bLogin; }