Exemple #1
0
 /**
  * @return bool
  */
 private function rawCallback($sRawKey, $fCallback, $bCache = true, &$oAccount = null, &$oHelpdeskUser = null)
 {
     $aValues = \CApi::DecodeKeyValues($sRawKey);
     $sFolder = '';
     $iUid = 0;
     $sMimeIndex = '';
     $oAccount = null;
     $oHelpdeskUser = null;
     $oHelpdeskUserFromAttachment = null;
     if (isset($aValues['HelpdeskUserID'], $aValues['HelpdeskTenantID'])) {
         $oAccount = null;
         $oHelpdeskUser = $this->getHelpdeskAccountFromParam($oAccount);
         if ($oHelpdeskUser && $oHelpdeskUser->IdTenant === $aValues['HelpdeskTenantID']) {
             $oApiHelpdesk = $this->ApiHelpdesk();
             if ($oApiHelpdesk) {
                 if ($oHelpdeskUser->IdHelpdeskUser === $aValues['HelpdeskUserID']) {
                     $oHelpdeskUserFromAttachment = $oHelpdeskUser;
                 } else {
                     if ($oHelpdeskUser->IsAgent) {
                         $oHelpdeskUserFromAttachment = $oApiHelpdesk->GetUserById($aValues['HelpdeskTenantID'], $aValues['HelpdeskUserID']);
                     }
                 }
             }
         }
     } else {
         if (isset($aValues['AccountID'])) {
             $oAccount = $this->getAccountFromParam(true, !(isset($aValues['Iframed'], $aValues['Time']) && $aValues['Iframed'] && $aValues['Time'] > \ProjectSeven\Base\Utils::iframedTimestamp()));
             if (!$oAccount || $aValues['AccountID'] !== $oAccount->IdAccount) {
                 return false;
             }
         }
     }
     if ($oHelpdeskUserFromAttachment && isset($aValues['FilestorageFile'], $aValues['StorageType'], $aValues['Path'], $aValues['Name'])) {
         if ($bCache) {
             $this->verifyCacheByKey($sRawKey);
         }
         $bResult = false;
         $mResult = false;
         if ($this->oApiFilestorage->FileExists($oHelpdeskUserFromAttachment, $aValues['StorageType'], $aValues['Path'], $aValues['Name'])) {
             $mResult = $this->oApiFilestorage->GetFile($oHelpdeskUserFromAttachment, $aValues['StorageType'], $aValues['Path'], $aValues['Name']);
         }
         if (is_resource($mResult)) {
             if ($bCache) {
                 $this->cacheByKey($sRawKey);
             }
             $bResult = true;
             $sFileName = $aValues['Name'];
             $sContentType = empty($sFileName) ? 'text/plain' : \MailSo\Base\Utils::MimeContentType($sFileName);
             $sFileName = $this->clearFileName($sFileName, $sContentType);
             call_user_func_array($fCallback, array($oAccount, $sContentType, $sFileName, $mResult, $oHelpdeskUser));
         }
         return $bResult;
     } else {
         if (isset($aValues['TempFile'], $aValues['TempName'], $aValues['Name']) && ($oHelpdeskUserFromAttachment || $oAccount)) {
             if ($bCache) {
                 $this->verifyCacheByKey($sRawKey);
             }
             $bResult = false;
             $mResult = $this->ApiFileCache()->GetFile($oHelpdeskUserFromAttachment ? $oHelpdeskUserFromAttachment : $oAccount, $aValues['TempName']);
             if (is_resource($mResult)) {
                 if ($bCache) {
                     $this->cacheByKey($sRawKey);
                 }
                 $bResult = true;
                 $sFileName = $aValues['Name'];
                 $sContentType = empty($sFileName) ? 'text/plain' : \MailSo\Base\Utils::MimeContentType($sFileName);
                 $sFileName = $this->clearFileName($sFileName, $sContentType);
                 call_user_func_array($fCallback, array($oAccount, $sContentType, $sFileName, $mResult, $oHelpdeskUser));
             }
             return $bResult;
         } else {
             $sFolder = isset($aValues['Folder']) ? $aValues['Folder'] : '';
             $iUid = (int) (isset($aValues['Uid']) ? $aValues['Uid'] : 0);
             $sMimeIndex = (string) (isset($aValues['MimeIndex']) ? $aValues['MimeIndex'] : '');
         }
     }
     if ($bCache && 0 < strlen($sFolder) && 0 < $iUid) {
         $this->verifyCacheByKey($sRawKey);
     }
     $sContentTypeIn = (string) (isset($aValues['MimeType']) ? $aValues['MimeType'] : '');
     $sFileNameIn = (string) (isset($aValues['FileName']) ? $aValues['FileName'] : '');
     if (!$oAccount) {
         return false;
     }
     $self = $this;
     return $this->oApiMail->MessageMimeStream($oAccount, function ($rResource, $sContentType, $sFileName, $sMimeIndex = '') use($self, $oAccount, $fCallback, $sRawKey, $bCache, $sContentTypeIn, $sFileNameIn) {
         if (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[0]) {
                 $sFileNameOut = $sFileName;
             }
             $sFileNameOut = $self->clearFileName($sFileNameOut, $sContentType, $sMimeIndex);
             if ($bCache) {
                 $self->cacheByKey($sRawKey);
             }
             call_user_func_array($fCallback, array($oAccount, $sContentTypeOut, $sFileNameOut, $rResource));
         }
     }, $sFolder, $iUid, $sMimeIndex);
 }