/**
  * @param \RainLoop\Model\Account $oAccount
  * @param string $sQuery
  * @param int $iLimit = 20
  *
  * @return array
  */
 public function Process($oAccount, $sQuery, $iLimit = 20)
 {
     $iInputLimit = $iLimit;
     $aResult = array();
     $sQuery = \trim($sQuery);
     try {
         if ('' === $sQuery || !$oAccount || !\RainLoop\Utils::IsOwnCloudLoggedIn() || !\class_exists('OCP\\Contacts') || !\OCP\Contacts::isEnabled()) {
             return $aResult;
         }
         $aSearchResult = \OCP\Contacts::search($sQuery, array('FN', 'EMAIL'));
         //$this->oLogger->WriteDump($aSearchResult);
         $aHashes = array();
         if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) {
             foreach ($aSearchResult as $aContact) {
                 if (0 >= $iLimit) {
                     break;
                 }
                 $sUid = empty($aContact['UID']) ? '' : $aContact['UID'];
                 if (!empty($sUid)) {
                     $sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : '';
                     $mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : '';
                     if (!\is_array($mEmails)) {
                         $mEmails = array($mEmails);
                     }
                     foreach ($mEmails as $sEmail) {
                         $sHash = '"' . $sFullName . '" <' . $sEmail . '>';
                         if (!isset($aHashes[$sHash])) {
                             $aHashes[$sHash] = true;
                             $aResult[] = array($sEmail, $sFullName);
                             $iLimit--;
                         }
                     }
                 }
             }
             $aResult = \array_slice($aResult, 0, $iInputLimit);
         }
         unset($aSearchResult, $aHashes);
     } catch (\Exception $oException) {
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $aResult;
 }
 /**
  * @return array
  *
  * @throws \MailSo\Base\Exceptions\Exception
  */
 public function DoAttachmentsActions()
 {
     if (!$this->GetCapa(false, \RainLoop\Enumerations\Capa::ATTACHMENTS_ACTIONS)) {
         return $this->FalseResponse(__FUNCTION__);
     }
     $oAccount = $this->initMailClientConnection();
     $sAction = $this->GetActionParam('Do', '');
     $aHashes = $this->GetActionParam('Hashes', null);
     $mResult = false;
     $bError = false;
     $aData = false;
     if (\is_array($aHashes) && 0 < \count($aHashes)) {
         $aData = array();
         foreach ($aHashes as $sZipHash) {
             $aResult = $this->getMimeFileByHash($oAccount, $sZipHash);
             if (\is_array($aResult) && !empty($aResult['FileHash'])) {
                 $aData[] = $aResult;
             } else {
                 $bError = true;
                 break;
             }
         }
     }
     $oFilesProvider = $this->FilesProvider();
     if (!empty($sAction) && !$bError && \is_array($aData) && 0 < \count($aData) && $oFilesProvider && $oFilesProvider->IsActive()) {
         $bError = false;
         switch (\strtolower($sAction)) {
             case 'zip':
                 if (\class_exists('ZipArchive')) {
                     $sZipHash = \MailSo\Base\Utils::Md5Rand();
                     $sZipFileName = $oFilesProvider->GenerateLocalFullFileName($oAccount, $sZipHash);
                     if (!empty($sZipFileName)) {
                         $oZip = new \ZipArchive();
                         $oZip->open($sZipFileName, \ZIPARCHIVE::CREATE | \ZIPARCHIVE::OVERWRITE);
                         $oZip->setArchiveComment('RainLoop/' . APP_VERSION);
                         foreach ($aData as $aItem) {
                             $sFileName = (string) (isset($aItem['FileName']) ? $aItem['FileName'] : 'file.dat');
                             $sFileHash = (string) (isset($aItem['FileHash']) ? $aItem['FileHash'] : '');
                             if (!empty($sFileHash)) {
                                 $sFullFileNameHash = $oFilesProvider->GetFileName($oAccount, $sFileHash);
                                 if (!$oZip->addFile($sFullFileNameHash, $sFileName)) {
                                     $bError = true;
                                 }
                             }
                         }
                         if (!$bError) {
                             $bError = !$oZip->close();
                         } else {
                             $oZip->close();
                         }
                     }
                     foreach ($aData as $aItem) {
                         $sFileHash = (string) (isset($aItem['FileHash']) ? $aItem['FileHash'] : '');
                         if (!empty($sFileHash)) {
                             $oFilesProvider->Clear($oAccount, $sFileHash);
                         }
                     }
                     if (!$bError) {
                         $mResult = array('Files' => array(array('FileName' => 'attachments.zip', 'Hash' => \RainLoop\Utils::EncodeKeyValuesQ(array('V' => APP_VERSION, 'Account' => $oAccount ? \md5($oAccount->Hash()) : '', 'FileName' => 'attachments.zip', 'MimeType' => 'application/zip', 'FileHash' => $sZipHash)))));
                     }
                 }
                 break;
             case 'owncloud':
                 $mResult = false;
                 if (\RainLoop\Utils::IsOwnCloudLoggedIn() && \class_exists('OCP\\Files')) {
                     $sSaveFolder = $this->Config()->Get('labs', 'owncloud_save_folder', '');
                     if (empty($sSaveFolder)) {
                         $sSaveFolder = 'Attachments';
                     }
                     $oFiles = \OCP\Files::getStorage('files');
                     if ($oFilesProvider && $oFiles && $oFilesProvider->IsActive() && \method_exists($oFiles, 'file_put_contents')) {
                         if (!$oFiles->is_dir($sSaveFolder)) {
                             $oFiles->mkdir($sSaveFolder);
                         }
                         $mResult = true;
                         foreach ($aData as $aItem) {
                             $sSavedFileName = isset($aItem['FileName']) ? $aItem['FileName'] : 'file.dat';
                             $sSavedFileHash = !empty($aItem['FileHash']) ? $aItem['FileHash'] : '';
                             if (!empty($sSavedFileHash)) {
                                 $fFile = $oFilesProvider->GetFile($oAccount, $sSavedFileHash, 'rb');
                                 if (\is_resource($fFile)) {
                                     $sSavedFileNameFull = \MailSo\Base\Utils::SmartFileExists($sSaveFolder . '/' . $sSavedFileName, function ($sPath) use($oFiles) {
                                         return $oFiles->file_exists($sPath);
                                     });
                                     if (!$oFiles->file_put_contents($sSavedFileNameFull, $fFile)) {
                                         $mResult = false;
                                     }
                                     if (\is_resource($fFile)) {
                                         @\fclose($fFile);
                                     }
                                 }
                             }
                         }
                     }
                 }
                 foreach ($aData as $aItem) {
                     $sFileHash = (string) (isset($aItem['FileHash']) ? $aItem['FileHash'] : '');
                     if (!empty($sFileHash)) {
                         $oFilesProvider->Clear($oAccount, $sFileHash);
                     }
                 }
                 break;
             case 'dropbox':
                 $mResult = array('ShortLife' => '_' . $this->GetShortLifeSpecAuthToken(), 'Url' => \preg_replace('/\\?(.*)$/', '', $this->Http()->GetFullUrl()), 'Files' => array());
                 foreach ($aData as $aItem) {
                     $mResult['Files'][] = array('FileName' => isset($aItem['FileName']) ? $aItem['FileName'] : 'file.dat', 'Hash' => \RainLoop\Utils::EncodeKeyValuesQ($aItem));
                 }
                 break;
         }
     } else {
         $bError = true;
     }
     $this->requestSleep();
     return $this->DefaultResponse(__FUNCTION__, $bError ? false : $mResult);
 }