/** * @param string $sPrevFolderFullNameRaw * @param string $sNextFolderNameInUtf * @param bool $bRenameOrMove * @param bool $bSubscribeOnModify * * @return \MailSo\Mail\MailClient * * @throws \MailSo\Base\Exceptions\InvalidArgumentException */ public function folderModify($sPrevFolderFullNameRaw, $sNextFolderNameInUtf, $bRenameOrMove, $bSubscribeOnModify) { if (0 === \strlen($sPrevFolderFullNameRaw) || 0 === \strlen($sNextFolderNameInUtf)) { throw new \MailSo\Base\Exceptions\InvalidArgumentException(); } $aFolders = $this->oImapClient->FolderList('', $sPrevFolderFullNameRaw); if (!\is_array($aFolders) || !isset($aFolders[0])) { // TODO throw new \MailSo\Mail\Exceptions\RuntimeException('Cannot rename non-existen folder'); } $sDelimiter = $aFolders[0]->Delimiter(); $iLast = \strrpos($sPrevFolderFullNameRaw, $sDelimiter); $mSubscribeFolders = null; if ($bSubscribeOnModify) { $mSubscribeFolders = $this->oImapClient->FolderSubscribeList($sPrevFolderFullNameRaw, '*'); if (\is_array($mSubscribeFolders) && 0 < count($mSubscribeFolders)) { foreach ($mSubscribeFolders as $oFolder) { $this->oImapClient->FolderUnSubscribe($oFolder->FullNameRaw()); } } } $sNewFolderFullNameRaw = \MailSo\Base\Utils::ConvertEncoding($sNextFolderNameInUtf, \MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::UTF_7_IMAP); if ($bRenameOrMove) { if (0 < \strlen($sDelimiter) && false !== \strpos($sNewFolderFullNameRaw, $sDelimiter)) { // TODO throw new \MailSo\Mail\Exceptions\RuntimeException('New folder name contain delimiter'); } $sFolderParentFullNameRaw = false === $iLast ? '' : \substr($sPrevFolderFullNameRaw, 0, $iLast + 1); $sNewFolderFullNameRaw = $sFolderParentFullNameRaw . $sNewFolderFullNameRaw; } $this->oImapClient->FolderRename($sPrevFolderFullNameRaw, $sNewFolderFullNameRaw); if (\is_array($mSubscribeFolders) && 0 < count($mSubscribeFolders)) { foreach ($mSubscribeFolders as $oFolder) { $sFolderFullNameRawForResubscrine = $oFolder->FullNameRaw(); if (0 === \strpos($sFolderFullNameRawForResubscrine, $sPrevFolderFullNameRaw)) { $sNewFolderFullNameRawForResubscrine = $sNewFolderFullNameRaw . \substr($sFolderFullNameRawForResubscrine, \strlen($sPrevFolderFullNameRaw)); $this->oImapClient->FolderSubscribe($sNewFolderFullNameRawForResubscrine); } } } return $this; }
/** * @param \RainLoop\Providers\Filters\Classes\Filter $oFilter * @param array $aCapa * * @return string */ private function filterToSieveScript($oFilter, &$aCapa) { $sNL = \RainLoop\Providers\Filters\SieveStorage::NEW_LINE; $sTab = ' '; $bAll = false; $aResult = array(); // Conditions $aConditions = $oFilter->Conditions(); if (\is_array($aConditions)) { if (1 < \count($aConditions)) { if (\RainLoop\Providers\Filters\Enumerations\ConditionsType::ANY === $oFilter->ConditionsType()) { $aResult[] = 'if anyof('; $bTrim = false; foreach ($aConditions as $oCond) { $bTrim = true; $sCons = $this->conditionToSieveScript($oCond); if (!empty($sCons)) { $aResult[] = $sTab . $sCons . ','; } } if ($bTrim) { $aResult[\count($aResult) - 1] = \rtrim($aResult[\count($aResult) - 1], ','); } $aResult[] = ')'; } else { $aResult[] = 'if allof('; foreach ($aConditions as $oCond) { $aResult[] = $sTab . $this->conditionToSieveScript($oCond) . ','; } $aResult[\count($aResult) - 1] = \rtrim($aResult[\count($aResult) - 1], ','); $aResult[] = ')'; } } else { if (1 === \count($aConditions)) { $aResult[] = 'if ' . $this->conditionToSieveScript($aConditions[0]) . ''; } else { $bAll = true; } } } // actions if (!$bAll) { $aResult[] = '{'; } else { $sTab = ''; } if ($oFilter->MarkAsRead() && \in_array($oFilter->ActionType(), array(\RainLoop\Providers\Filters\Enumerations\ActionType::NONE, \RainLoop\Providers\Filters\Enumerations\ActionType::MOVE_TO, \RainLoop\Providers\Filters\Enumerations\ActionType::FORWARD))) { $aCapa['imap4flags'] = true; $aResult[] = $sTab . 'addflag "\\\\Seen";'; } switch ($oFilter->ActionType()) { case \RainLoop\Providers\Filters\Enumerations\ActionType::NONE: $aResult[] = $sTab . 'stop;'; break; case \RainLoop\Providers\Filters\Enumerations\ActionType::DISCARD: $aResult[] = $sTab . 'discard;'; $aResult[] = $sTab . 'stop;'; break; case \RainLoop\Providers\Filters\Enumerations\ActionType::VACATION: $sValue = \trim($oFilter->ActionValue()); $sValueSecond = \trim($oFilter->ActionValueSecond()); $sValueThird = \trim($oFilter->ActionValueThird()); if (0 < \strlen($sValue)) { $aCapa['vacation'] = true; $iDays = 1; $sSubject = ''; if (0 < \strlen($sValueSecond)) { $sSubject = ':subject "' . $this->quote(\MailSo\Base\Utils::StripSpaces($sValueSecond)) . '" '; } if (0 < \strlen($sValueThird) && \is_numeric($sValueThird) && 1 < (int) $sValueThird) { $iDays = (int) $sValueThird; } $aResult[] = $sTab . 'vacation :days ' . $iDays . ' ' . $sSubject . '"' . $this->quote($sValue) . '";'; if ($oFilter->Stop()) { $aResult[] = $sTab . 'stop;'; } } else { $aResult[] = $sTab . '# @Error (vacation): empty action value'; } break; case \RainLoop\Providers\Filters\Enumerations\ActionType::REJECT: $sValue = \trim($oFilter->ActionValue()); if (0 < \strlen($sValue)) { $aCapa['reject'] = true; $aResult[] = $sTab . 'reject "' . $this->quote($sValue) . '";'; $aResult[] = $sTab . 'stop;'; } else { $aResult[] = $sTab . '# @Error (reject): empty action value'; } break; case \RainLoop\Providers\Filters\Enumerations\ActionType::FORWARD: $sValue = $oFilter->ActionValue(); if (0 < \strlen($sValue)) { if ($oFilter->Keep()) { $aCapa['fileinto'] = true; $aResult[] = $sTab . 'fileinto "INBOX";'; } $aResult[] = $sTab . 'redirect "' . $this->quote($sValue) . '";'; $aResult[] = $sTab . 'stop;'; } else { $aResult[] = $sTab . '# @Error (redirect): empty action value'; } break; case \RainLoop\Providers\Filters\Enumerations\ActionType::MOVE_TO: $sValue = $oFilter->ActionValue(); if (0 < \strlen($sValue)) { $sFolderName = $sValue; // utf7-imap if ($this->bUtf8FolderName) { $sFolderName = \MailSo\Base\Utils::ConvertEncoding($sFolderName, \MailSo\Base\Enumerations\Charset::UTF_7_IMAP, \MailSo\Base\Enumerations\Charset::UTF_8); } $aCapa['fileinto'] = true; $aResult[] = $sTab . 'fileinto "' . $this->quote($sFolderName) . '";'; $aResult[] = $sTab . 'stop;'; } else { $aResult[] = $sTab . '# @Error (fileinto): empty action value'; } break; } if (!$bAll) { $aResult[] = '}'; } return \implode($sNL, $aResult); }
/** * @param string $sEncodedValue * @param string $sIncomingCharset = '' * @param string $sForcedIncomingCharset = '' * * @return string */ public static function DecodeHeaderValue($sEncodedValue, $sIncomingCharset = '', $sForcedIncomingCharset = '') { $sValue = $sEncodedValue; if (0 < \strlen($sIncomingCharset)) { $sIncomingCharset = \MailSo\Base\Utils::NormalizeCharsetByValue($sIncomingCharset, $sValue); $sValue = \MailSo\Base\Utils::ConvertEncoding($sValue, $sIncomingCharset, \MailSo\Base\Enumerations\Charset::UTF_8); } $sValue = \preg_replace('/\\?=[\\n\\r\\t\\s]{1,5}=\\?/m', '?==?', $sValue); $sValue = \preg_replace('/[\\r\\n\\t]+/m', ' ', $sValue); $aEncodeArray = array(''); $aMatch = array(); \preg_match_all('/=\\?[^\\?]+\\?[q|b|Q|B]\\?[^\\?]*(\\?=)/', $sValue, $aMatch); if (isset($aMatch[0]) && \is_array($aMatch[0])) { for ($iIndex = 0, $iLen = \count($aMatch[0]); $iIndex < $iLen; $iIndex++) { if (isset($aMatch[0][$iIndex])) { $iPos = @\strpos($aMatch[0][$iIndex], '*'); if (false !== $iPos) { $aMatch[0][$iIndex][0] = \substr($aMatch[0][$iIndex][0], 0, $iPos); } } } $aEncodeArray = $aMatch[0]; } $aParts = array(); $sMainCharset = ''; $bOneCharset = true; for ($iIndex = 0, $iLen = \count($aEncodeArray); $iIndex < $iLen; $iIndex++) { $aTempArr = array('', $aEncodeArray[$iIndex]); if ('=?' === \substr(\trim($aTempArr[1]), 0, 2)) { $iPos = \strpos($aTempArr[1], '?', 2); $aTempArr[0] = \substr($aTempArr[1], 2, $iPos - 2); $sEncType = \strtoupper($aTempArr[1][$iPos + 1]); switch ($sEncType) { case 'Q': $sHeaderValuePart = \str_replace('_', ' ', $aTempArr[1]); $aTempArr[1] = \quoted_printable_decode(\substr($sHeaderValuePart, $iPos + 3, \strlen($sHeaderValuePart) - $iPos - 5)); break; case 'B': $sHeaderValuePart = $aTempArr[1]; $aTempArr[1] = \MailSo\Base\Utils::Base64Decode(\substr($sHeaderValuePart, $iPos + 3, \strlen($sHeaderValuePart) - $iPos - 5)); break; } } if (0 < \strlen($aTempArr[0])) { $sCharset = 0 === \strlen($sForcedIncomingCharset) ? $aTempArr[0] : $sForcedIncomingCharset; $sCharset = \MailSo\Base\Utils::NormalizeCharset($sCharset, true); if ('' === $sMainCharset) { $sMainCharset = $sCharset; } else { if ($sMainCharset !== $sCharset) { $bOneCharset = false; } } } $aParts[] = array($aEncodeArray[$iIndex], $aTempArr[1], $sCharset); unset($aTempArr); } for ($iIndex = 0, $iLen = \count($aParts); $iIndex < $iLen; $iIndex++) { if ($bOneCharset) { $sValue = \str_replace($aParts[$iIndex][0], $aParts[$iIndex][1], $sValue); } else { $aParts[$iIndex][2] = \MailSo\Base\Utils::NormalizeCharsetByValue($aParts[$iIndex][2], $aParts[$iIndex][1]); $sValue = \str_replace($aParts[$iIndex][0], \MailSo\Base\Utils::ConvertEncoding($aParts[$iIndex][1], $aParts[$iIndex][2], \MailSo\Base\Enumerations\Charset::UTF_8), $sValue); } } if ($bOneCharset && 0 < \strlen($sMainCharset)) { $sMainCharset = \MailSo\Base\Utils::NormalizeCharsetByValue($sMainCharset, $sValue); $sValue = \MailSo\Base\Utils::ConvertEncoding($sValue, $sMainCharset, \MailSo\Base\Enumerations\Charset::UTF_8); } return $sValue; }
/** * @param array $aParams * @param string $sParamName * @param string $sCharset = \MailSo\Base\Enumerations\Charset::UTF_8 * * @return string */ private static function decodeAttrParamenter($aParams, $sParamName, $sCharset = \MailSo\Base\Enumerations\Charset::UTF_8) { $sResult = ''; if (isset($aParams[$sParamName])) { $sResult = \MailSo\Base\Utils::DecodeHeaderValue($aParams[$sParamName], $sCharset); } else { if (isset($aParams[$sParamName . '*'])) { $aValueParts = \explode('\'\'', $aParams[$sParamName . '*'], 2); if (\is_array($aValueParts) && 2 === \count($aValueParts)) { $sCharset = isset($aValueParts[0]) ? $aValueParts[0] : \MailSo\Base\Enumerations\Charset::UTF_8; $sResult = \MailSo\Base\Utils::ConvertEncoding(\urldecode($aValueParts[1]), $sCharset, \MailSo\Base\Enumerations\Charset::UTF_8); } else { $sResult = \urldecode($aParams[$sParamName . '*']); } } else { if (isset($aParams[$sParamName . '*0*'])) { $sCharset = ''; $aFileNames = array(); foreach ($aParams as $sName => $sValue) { $aMatches = array(); if ($sParamName . '*0*' === $sName) { if (0 === \strlen($sCharset)) { $aValueParts = \explode('\'\'', $sValue, 2); if (\is_array($aValueParts) && 2 === \count($aValueParts) && 0 < \strlen($aValueParts[0])) { $sCharset = $aValueParts[0]; $sValue = $aValueParts[1]; } } $aFileNames[0] = $sValue; } else { if ($sParamName . '*0*' !== $sName && \preg_match('/^' . \preg_quote($sParamName, '/') . '\\*([0-9]+)\\*$/i', $sName, $aMatches) && 0 < \strlen($aMatches[1])) { $aFileNames[(int) $aMatches[1]] = $sValue; } } } if (0 < \count($aFileNames)) { \ksort($aFileNames, SORT_NUMERIC); $sResult = \implode(\array_values($aFileNames)); $sResult = \urldecode($sResult); if (0 < \strlen($sCharset)) { $sResult = \MailSo\Base\Utils::ConvertEncoding($sResult, $sCharset, \MailSo\Base\Enumerations\Charset::UTF_8); } } } } } return $sResult; }
/** * @return void */ private function reParseParameters() { $aDataToReParse = $this->CloneAsArray(); $sCharset = \MailSo\Base\Enumerations\Charset::UTF_8; $this->clear(); $aPreParams = array(); foreach ($aDataToReParse as $oParam) { $aMatch = array(); $sParamName = $oParam->Name(); if (preg_match('/([^\\*]+)\\*([\\d]{1,2})\\*/', $sParamName, $aMatch) && isset($aMatch[1], $aMatch[2]) && 0 < strlen($aMatch[1]) && is_numeric($aMatch[2])) { if (!isset($aPreParams[$aMatch[1]])) { $aPreParams[$aMatch[1]] = array(); } $sValue = $oParam->Value(); $aValueParts = explode('\'\'', $sValue, 2); if (is_array($aValueParts) && 2 === count($aValueParts) && 0 < strlen($aValueParts[1])) { $sCharset = $aValueParts[0]; $sValue = $aValueParts[1]; } $aPreParams[$aMatch[1]][(int) $aMatch[2]] = $sValue; } else { if (preg_match('/([^\\*]+)\\*/', $sParamName, $aMatch) && isset($aMatch[1])) { if (!isset($aPreParams[$aMatch[1]])) { $aPreParams[$aMatch[1]] = array(); } $sValue = $oParam->Value(); $aValueParts = explode('\'\'', $sValue, 2); if (is_array($aValueParts) && 2 === count($aValueParts) && 0 < strlen($aValueParts[1])) { $sCharset = $aValueParts[0]; $sValue = $aValueParts[1]; } $aPreParams[$aMatch[1]][0] = $sValue; } else { $this->Add($oParam); } } } foreach ($aPreParams as $sName => $aValues) { ksort($aValues); $sResult = implode(array_values($aValues)); $sResult = urldecode($sResult); if (0 < strlen($sCharset)) { $sResult = \MailSo\Base\Utils::ConvertEncoding($sResult, $sCharset, \MailSo\Base\Enumerations\Charset::UTF_8); } $this->Add(Parameter::NewInstance($sName, $sResult)); } }
/** * @return string */ public function ParentFullName() { return BaseUtils::ConvertEncoding($this->sParentFullNameRaw, \MailSo\Base\Enumerations\Charset::UTF_7_IMAP, \MailSo\Base\Enumerations\Charset::UTF_8); }
/** * @return bool */ private function raw($bDownload = true, $bThumbnail = false) { $self = $this; return $this->rawCallback((string) $this->getParamValue('RawKey', ''), function ($oAccount, $sContentType, $sFileName, $rResource, $oHelpdeskUser = null) use($self, $bDownload, $bThumbnail) { $self->RawOutputHeaders($bDownload, $sContentType, $sFileName); if (!$bDownload && 'text/html' === $sContentType) { $sHtml = stream_get_contents($rResource); if ($sHtml) { $sCharset = ''; $aMacth = array(); if (preg_match('/charset[\\s]?=[\\s]?([^\\s"\']+)/i', $sHtml, $aMacth) && !empty($aMacth[1])) { $sCharset = $aMacth[1]; } if ('' !== $sCharset && \MailSo\Base\Enumerations\Charset::UTF_8 !== $sCharset) { $sHtml = \MailSo\Base\Utils::ConvertEncoding($sHtml, \MailSo\Base\Utils::NormalizeCharset($sCharset, true), \MailSo\Base\Enumerations\Charset::UTF_8); } include_once PSEVEN_APP_ROOT_PATH . 'libraries/other/CssToInlineStyles.php'; $oCssToInlineStyles = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles($sHtml); $oCssToInlineStyles->setEncoding('utf-8'); $oCssToInlineStyles->setUseInlineStylesBlock(true); echo '<html><head></head><body>' . \MailSo\Base\HtmlUtils::ClearHtmlSimple($oCssToInlineStyles->convert(), true, true) . '</body></html>'; } } else { if ($bThumbnail && !$bDownload) { $self->thumbResource($oAccount ? $oAccount : $oHelpdeskUser, $rResource, $sFileName); } else { \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource); } } }, !$bDownload); }
/** * Changes folder's name. * * @param CAccount $oAccount Account object. * @param string $sPrevFolderFullNameRaw Raw full name of the folder. * @param string $sNewTopFolderNameInUtf8 = ''. New name for the folder in utf8. * * @return string * * @throws CApiInvalidArgumentException * @throws CApiBaseException */ public function renameFolder($oAccount, $sPrevFolderFullNameRaw, $sNewTopFolderNameInUtf8) { $sNewTopFolderNameInUtf8 = trim($sNewTopFolderNameInUtf8); if (0 === strlen($sPrevFolderFullNameRaw) || 0 === strlen($sNewTopFolderNameInUtf8)) { throw new CApiInvalidArgumentException(); } $oImapClient =& $this->_getImapClient($oAccount); $aFolders = $oImapClient->FolderList('', $sPrevFolderFullNameRaw); if (!is_array($aFolders) || !isset($aFolders[0])) { // TODO throw new CApiBaseException(Errs::Mail_CannotRenameNonExistenFolder); } $sDelimiter = $aFolders[0]->Delimiter(); $iLast = strrpos($sPrevFolderFullNameRaw, $sDelimiter); $sFolderParentFullNameRaw = false === $iLast ? '' : substr($sPrevFolderFullNameRaw, 0, $iLast + 1); $aSubscribeFolders = $oImapClient->FolderSubscribeList($sPrevFolderFullNameRaw, '*'); if (is_array($aSubscribeFolders) && 0 < count($aSubscribeFolders)) { foreach ($aSubscribeFolders as $oFolder) { $oImapClient->FolderUnSubscribe($oFolder->FullNameRaw()); } } $sNewFolderFullNameRaw = \MailSo\Base\Utils::ConvertEncoding($sNewTopFolderNameInUtf8, \MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::UTF_7_IMAP); if (0 < strlen($sDelimiter) && false !== strpos($sNewFolderFullNameRaw, $sDelimiter)) { // TODO throw new CApiBaseException(Errs::Mail_FolderNameContainDelimiter); } $sNewFolderFullNameRaw = $sFolderParentFullNameRaw . $sNewFolderFullNameRaw; $oImapClient->FolderRename($sPrevFolderFullNameRaw, $sNewFolderFullNameRaw); if (is_array($aSubscribeFolders) && 0 < count($aSubscribeFolders)) { foreach ($aSubscribeFolders as $oFolder) { $sFolderFullNameRawForResubscrine = $oFolder->FullNameRaw(); if (0 === strpos($sFolderFullNameRawForResubscrine, $sPrevFolderFullNameRaw)) { $sNewFolderFullNameRawForResubscrine = $sNewFolderFullNameRaw . substr($sFolderFullNameRawForResubscrine, strlen($sPrevFolderFullNameRaw)); $oImapClient->FolderSubscribe($sNewFolderFullNameRawForResubscrine); } } } $aOrders = $this->oStorage->getFoldersOrder($oAccount); if (is_array($aOrders)) { foreach ($aOrders as &$sName) { if ($sPrevFolderFullNameRaw === $sName) { $sName = $sNewFolderFullNameRaw; $this->oStorage->updateFoldersOrder($oAccount, $aOrders); break; } } } return $sNewFolderFullNameRaw; }
/** * @param bool $bDownload * @param bool $bThumbnail = false * * @return bool */ private function rawSmart($bDownload, $bThumbnail = false) { $sRawKey = (string) $this->GetActionParam('RawKey', ''); $aValues = $this->getDecodedRawKeyValue($sRawKey); $sFolder = isset($aValues['Folder']) ? $aValues['Folder'] : ''; $iUid = isset($aValues['Uid']) ? (int) $aValues['Uid'] : 0; $sMimeIndex = isset($aValues['MimeIndex']) ? (string) $aValues['MimeIndex'] : ''; $sContentTypeIn = isset($aValues['MimeType']) ? (string) $aValues['MimeType'] : ''; $sFileNameIn = isset($aValues['FileName']) ? (string) $aValues['FileName'] : ''; $sFileHashIn = isset($aValues['FileHash']) ? (string) $aValues['FileHash'] : ''; $bDetectImageOrientation = !!$this->Config()->Get('labs', 'detect_image_exif_orientation', true); if (!empty($sFileHashIn)) { $this->verifyCacheByKey($sRawKey); $oAccount = $this->getAccountFromToken(); $sContentTypeOut = empty($sContentTypeIn) ? \MailSo\Base\Utils::MimeContentType($sFileNameIn) : $sContentTypeIn; $sFileNameOut = $this->MainClearFileName($sFileNameIn, $sContentTypeIn, $sMimeIndex); $rResource = $this->FilesProvider()->GetFile($oAccount, $sFileHashIn); if (\is_resource($rResource)) { $sFileNameOut = \MailSo\Base\Utils::ConvertEncoding($sFileNameOut, \MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::CP858); \header('Content-Type: ' . $sContentTypeOut); \header('Content-Disposition: attachment; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true); \header('Accept-Ranges: none', true); \header('Content-Transfer-Encoding: binary'); \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource); return true; } return false; } else { if (!empty($sFolder) && 0 < $iUid) { $this->verifyCacheByKey($sRawKey); } } $oAccount = $this->initMailClientConnection(); $self = $this; return $this->MailClient()->MessageMimeStream(function ($rResource, $sContentType, $sFileName, $sMimeIndex = '') use($self, $oAccount, $sRawKey, $sContentTypeIn, $sFileNameIn, $bDownload, $bThumbnail, $bDetectImageOrientation) { if ($oAccount && \is_resource($rResource)) { $sContentTypeOut = $sContentTypeIn; if (empty($sContentTypeOut)) { $sContentTypeOut = $sContentType; if (empty($sContentTypeOut)) { $sContentTypeOut = empty($sFileName) ? 'text/plain' : \MailSo\Base\Utils::MimeContentType($sFileName); } } $sFileNameOut = $sFileNameIn; if (empty($sFileNameOut)) { $sFileNameOut = $sFileName; } $sFileNameOut = $self->MainClearFileName($sFileNameOut, $sContentTypeOut, $sMimeIndex); $self->cacheByKey($sRawKey); $bDone = false; if ($bThumbnail && !$bDownload) { try { $oImagine = new \Imagine\Gd\Imagine(); $bDone = true; $oImage = $oImagine->load(\stream_get_contents($rResource)); $oImage = $this->correctImageOrientation($oImage, $bDetectImageOrientation, 60); \header('Content-Disposition: inline; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut . '_thumb60x60.png')), true); $oImage->show('png'); } catch (\Exception $oException) { $self->Logger()->WriteExceptionShort($oException); } } if (!$bDone && !$bDownload && $bDetectImageOrientation && \in_array($sContentTypeOut, array('image/png', 'image/jpeg', 'image/jpg')) && \MailSo\Base\Utils::FunctionExistsAndEnabled('gd_info')) { try { $oImagine = new \Imagine\Gd\Imagine(); $bDone = true; $oImage = $oImagine->load(\stream_get_contents($rResource)); $oImage = $this->correctImageOrientation($oImage, $bDetectImageOrientation); \header('Content-Disposition: inline; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true); $oImage->show($sContentTypeOut === 'image/png' ? 'png' : 'jpg'); } catch (\Exception $oException) { $self->Logger()->WriteExceptionShort($oException); } } if (!$bDone) { \header('Content-Type: ' . $sContentTypeOut); \header('Content-Disposition: ' . ($bDownload ? 'attachment' : 'inline') . '; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true); \header('Accept-Ranges: none', true); \header('Content-Transfer-Encoding: binary'); \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource); } } }, $sFolder, $iUid, true, $sMimeIndex); }
/** * @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; }
/** * @param bool $bDownload * @param bool $bThumbnail = false * * @return bool */ private function rawSmart($bDownload, $bThumbnail = false) { $sRawKey = (string) $this->GetActionParam('RawKey', ''); $aValues = $this->getDecodedRawKeyValue($sRawKey); $sFolder = isset($aValues['Folder']) ? $aValues['Folder'] : ''; $iUid = isset($aValues['Uid']) ? (int) $aValues['Uid'] : 0; $sMimeIndex = isset($aValues['MimeIndex']) ? (string) $aValues['MimeIndex'] : ''; $sContentTypeIn = isset($aValues['MimeType']) ? (string) $aValues['MimeType'] : ''; $sFileNameIn = isset($aValues['FileName']) ? (string) $aValues['FileName'] : ''; $sFileHashIn = isset($aValues['FileHash']) ? (string) $aValues['FileHash'] : ''; if (!empty($sFileHashIn)) { $this->verifyCacheByKey($sRawKey); $oAccount = $this->getAccountFromToken(); $sContentTypeOut = empty($sContentTypeIn) ? \MailSo\Base\Utils::MimeContentType($sFileNameIn) : $sContentTypeIn; $sFileNameOut = $this->MainClearFileName($sFileNameIn, $sContentTypeIn, $sMimeIndex); $rResource = $this->FilesProvider()->GetFile($oAccount, $sFileHashIn); if (\is_resource($rResource)) { $sFileNameOut = \MailSo\Base\Utils::ConvertEncoding($sFileNameOut, \MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::CP858); \header('Content-Type: ' . $sContentTypeOut); \header('Content-Disposition: attachment; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true); \header('Accept-Ranges: none', true); \header('Content-Transfer-Encoding: binary'); \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource); return true; } return false; } else { if (!empty($sFolder) && 0 < $iUid) { $this->verifyCacheByKey($sRawKey); } } $oAccount = $this->initMailClientConnection(); $self = $this; return $this->MailClient()->MessageMimeStream(function ($rResource, $sContentType, $sFileName, $sMimeIndex = '') use($self, $oAccount, $sRawKey, $sContentTypeIn, $sFileNameIn, $bDownload, $bThumbnail) { if ($oAccount && \is_resource($rResource)) { $sContentTypeOut = $sContentTypeIn; if (empty($sContentTypeOut)) { $sContentTypeOut = $sContentType; if (empty($sContentTypeOut)) { $sContentTypeOut = empty($sFileName) ? 'text/plain' : \MailSo\Base\Utils::MimeContentType($sFileName); } } $sFileNameOut = $sFileNameIn; if (empty($sFileNameOut)) { $sFileNameOut = $sFileName; } $sFileNameOut = $self->MainClearFileName($sFileNameOut, $sContentTypeOut, $sMimeIndex); $self->cacheByKey($sRawKey); $bDone = false; if ($bThumbnail && !$bDownload) { $sFileName = ''; $rTempResource = \MailSo\Base\StreamWrappers\TempFile::CreateStream(\MailSo\Base\Utils::Md5Rand($sFileNameOut), $sFileName); if (@\is_resource($rTempResource)) { $bDone = true; \MailSo\Base\Utils::MultipleStreamWriter($rResource, array($rTempResource)); @\fclose($rTempResource); try { $oThumb = new \PHPThumb\GD($sFileName); if ($oThumb) { $oThumb->adaptiveResize(60, 60)->show(); } } catch (\Exception $oException) { $self->Logger()->WriteExceptionShort($oException); } } } if (!$bDone) { \header('Content-Type: ' . $sContentTypeOut); \header('Content-Disposition: ' . ($bDownload ? 'attachment' : 'inline') . '; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true); \header('Accept-Ranges: none', true); \header('Content-Transfer-Encoding: binary'); \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource); } } }, $sFolder, $iUid, true, $sMimeIndex); }
/** * @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; }
/** * @param string $sEncodedString * @param string $sEndBuffer * * @return string */ public static function InlineConvertDecode($sEncodedString, &$sEndBuffer, $sFromEncoding, $sToEncoding) { $sEndBuffer = ''; $sQuotedPrintableLen = strlen($sEncodedString); $iLastSpace = strrpos($sEncodedString, ' '); if (false !== $iLastSpace && $iLastSpace + 1 < $sQuotedPrintableLen) { $sEndBuffer = substr($sEncodedString, $iLastSpace + 1); $sEncodedString = substr($sEncodedString, 0, $iLastSpace + 1); } return \MailSo\Base\Utils::ConvertEncoding($sEncodedString, $sFromEncoding, $sToEncoding); }
/** * @return array */ public function DoFolders() { $sRawKey = (string) $this->GetActionParam('RawKey', ''); if (0 < \strlen($sRawKey)) { $this->verifyCacheByKey($sRawKey); } $oAccount = $this->initMailClientConnection(); $oFolderCollection = null; $this->Plugins()->RunHook('filter.folders-before', array($oAccount, &$oFolderCollection)); if (null === $oFolderCollection) { $oFolderCollection = $this->MailClient()->Folders('', '*', !!$this->Config()->Get('labs', 'use_imap_list_subscribe', true), (int) $this->Config()->Get('labs', 'imap_folder_list_limit', 200)); } $this->Plugins()->RunHook('filter.folders-post', array($oAccount, &$oFolderCollection)); if ($oFolderCollection instanceof \MailSo\Mail\FolderCollection) { $aSystemFolders = array(); $this->recFoldersTypes($oAccount, $oFolderCollection, $aSystemFolders); $oFolderCollection->SystemFolders = $aSystemFolders; if ($this->Config()->Get('labs', 'autocreate_system_folders', true)) { $bDoItAgain = false; $sNamespace = $oFolderCollection->GetNamespace(); $sParent = empty($sNamespace) ? '' : \substr($sNamespace, 0, -1); $sDelimiter = $oFolderCollection->FindDelimiter(); $aList = array(); $aMap = $this->systemFoldersNames($oAccount); if ('' === $this->GetActionParam('SentFolder', '')) { $aList[] = \MailSo\Imap\Enumerations\FolderType::SENT; } if ('' === $this->GetActionParam('DraftFolder', '')) { $aList[] = \MailSo\Imap\Enumerations\FolderType::DRAFTS; } if ('' === $this->GetActionParam('SpamFolder', '')) { $aList[] = \MailSo\Imap\Enumerations\FolderType::JUNK; } if ('' === $this->GetActionParam('TrashFolder', '')) { $aList[] = \MailSo\Imap\Enumerations\FolderType::TRASH; } if ('' === $this->GetActionParam('ArchiveFolder', '')) { $aList[] = \MailSo\Imap\Enumerations\FolderType::ALL; } $this->Plugins()->RunHook('filter.folders-system-types', array($oAccount, &$aList)); foreach ($aList as $iType) { if (!isset($aSystemFolders[$iType])) { $mFolderNameToCreate = \array_search($iType, $aMap); if (!empty($mFolderNameToCreate)) { $iPos = \strrpos($mFolderNameToCreate, $sDelimiter); if (false !== $iPos) { $mNewParent = \substr($mFolderNameToCreate, 0, $iPos); $mNewFolderNameToCreate = \substr($mFolderNameToCreate, $iPos + 1); if (0 < \strlen($mNewFolderNameToCreate)) { $mFolderNameToCreate = $mNewFolderNameToCreate; } if (0 < \strlen($mNewParent)) { $sParent = 0 < \strlen($sParent) ? $sParent . $sDelimiter . $mNewParent : $mNewParent; } } $sFullNameToCheck = \MailSo\Base\Utils::ConvertEncoding($mFolderNameToCreate, \MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::UTF_7_IMAP); if (0 < \strlen(\trim($sParent))) { $sFullNameToCheck = $sParent . $sDelimiter . $sFullNameToCheck; } if (!$oFolderCollection->GetByFullNameRaw($sFullNameToCheck)) { try { if ($this->MailClient()->FolderCreate($mFolderNameToCreate, $sParent, true, $sDelimiter)) { $bDoItAgain = true; } } catch (\Exception $oException) { $this->Logger()->WriteException($oException); } } } } } if ($bDoItAgain) { $oFolderCollection = $this->MailClient()->Folders('', '*', !!$this->Config()->Get('labs', 'use_imap_list_subscribe', true), (int) $this->Config()->Get('labs', 'imap_folder_list_limit', 200)); if ($oFolderCollection) { $aSystemFolders = array(); $this->recFoldersTypes($oAccount, $oFolderCollection, $aSystemFolders); $oFolderCollection->SystemFolders = $aSystemFolders; } } } if ($oFolderCollection) { $oFolderCollection->FoldersHash = \md5(\implode("", $this->recFoldersNames($oFolderCollection))); $this->cacheByKey($sRawKey); } } $this->Plugins()->RunHook('filter.folders-complete', array($oAccount, &$oFolderCollection)); return $this->DefaultResponse(__FUNCTION__, $oFolderCollection); }