/**
  * @param WebMailMessageCollection $messageCollection
  * @param DbStorage $dbStorage
  * @param Folder $folder
  * @return bool
  */
 function ApplyFilters(&$messageCollection, &$dbStorage, &$folder)
 {
     $filters =& $dbStorage->SelectFilters($this->Account->Id);
     $lastIdMsg = $dbStorage->SelectLastIdMsg();
     $mailBoxesSize = $dbStorage->SelectMailboxesSize();
     $result = true;
     foreach (array_keys($messageCollection->Instance()) as $key) {
         $message =& $messageCollection->Get($key);
         $needToSave = true;
         if ($this->_settings->EnableMailboxSizeLimit && $this->Account->MailboxLimit < $mailBoxesSize + $message->Size) {
             setGlobalError(ErrorGetMailLimit);
             return false;
         }
         $mailBoxesSize += $message->Size;
         $lastIdMsg += rand(1, 10);
         $message->IdMsg = $lastIdMsg;
         if ($folder->SyncType == FOLDERSYNC_NewEntireMessages || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
             $result &= $dbStorage->SaveMessage($message, $folder);
         } elseif ($folder->SyncType == FOLDERSYNC_NewHeadersOnly || $folder->SyncType == FOLDERSYNC_AllHeadersOnly) {
             $result &= $dbStorage->SaveMessageHeader($message, $folder, false);
         }
     }
     if ($folder->Type == FOLDERTYPE_Inbox && $result && isset($GLOBALS['useFilters'])) {
         $mailProcessor = new MailProcessor($this->Account);
         foreach (array_keys($filters->Instance()) as $key) {
             $filter =& $filters->Get($key);
             $action = $filter->GetActionToApply($message);
             $uidArray = array($message->Uid);
             $messageIdUidSet[$message->IdMsg] = $message->Uid;
             switch ($action) {
                 case FILTERACTION_DeleteFromServerImmediately:
                     $result &= $mailProcessor->DeleteMessages($messageIdUidSet, $folder);
                     break;
                 case FILTERACTION_MoveToFolder:
                     $toFolder =& new Folder($filter->IdAcct, $filter->IdFolder, '');
                     $dbStorage->GetFolderInfo($toFolder);
                     $result &= $mailProcessor->MoveMessages($messageIdUidSet, $folder, $toFolder);
                     break;
                 case FILTERACTION_MarkGrey:
                     $result &= $mailProcessor->SetFlags($messageIdUidSet, $folder, MESSAGEFLAGS_Grayed, ACTION_Set);
                     break;
             }
         }
     }
     return $result;
 }
 /**
  * @param WebMailMessage $message
  * @param DbStorage $dbStorage
  * @param Folder $folder
  * @return bool
  */
 function ApplyFilters(&$message, &$dbStorage, &$folder, &$filters)
 {
     $result = true;
     $needToSave = true;
     if ($folder->Type == FOLDERTYPE_Inbox && $result && isset($GLOBALS['useFilters'])) {
         $mailProcessor = null;
         if (null == $this->mailproc) {
             $this->mailproc = new MailProcessor($this->Account);
         }
         $mailProcessor =& $this->mailproc;
         $messageIdUidSet = array($message->IdMsg => $message->Uid);
         $filtersKeys = array_keys($filters->Instance());
         foreach ($filtersKeys as $key) {
             $filter =& $filters->Get($key);
             $action = $filter->GetActionToApply($message);
             switch ($action) {
                 case FILTERACTION_DeleteFromServerImmediately:
                     $result &= $mailProcessor->DeleteFromServerImmediately($messageIdUidSet, $folder);
                     $needToSave = false;
                     break 2;
                 case FILTERACTION_MoveToSpamFolder:
                     $folders = $mailProcessor->GetFolders();
                     $spamFolder = $folders->GetFolderByType(FOLDERTYPE_Spam);
                     if ($spamFolder && $spamFolder->IdDb) {
                         $filter->IdAcct = $this->Account->Id;
                         $filter->IdFolder = $spamFolder->IdDb;
                     } else {
                         break;
                     }
                 case FILTERACTION_MoveToFolder:
                     if ($filter->IdFolder != $folder->IdDb) {
                         if ($folder->SyncType == FOLDERSYNC_NewEntireMessages || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
                             $result &= $dbStorage->SaveMessage($message, $folder);
                         } else {
                             if ($folder->SyncType == FOLDERSYNC_NewHeadersOnly || $folder->SyncType == FOLDERSYNC_AllHeadersOnly) {
                                 $result &= $dbStorage->SaveMessageHeader($message, $folder, false);
                             }
                         }
                         $messageIdUidSet = array($message->IdMsg => $message->Uid);
                         if ($result) {
                             $needToSave = false;
                             $toFolder = new Folder($filter->IdAcct, $filter->IdFolder, '');
                             $dbStorage->GetFolderInfo($toFolder);
                             $_tDowmloaded = $this->DownloadedMessagesHandler;
                             $_tDeleting = $this->ShowDeletingMessageNumber;
                             $this->DownloadedMessagesHandler = $this->ShowDeletingMessageNumber = null;
                             $result &= $mailProcessor->MoveMessages($messageIdUidSet, $folder, $toFolder);
                             $this->DownloadedMessagesHandler = $_tDowmloaded;
                             $this->ShowDeletingMessageNumber = $_tDeleting;
                             if ($this->UpdateFolderHandler != null) {
                                 call_user_func_array($this->UpdateFolderHandler, array($toFolder->IdDb, $toFolder->FullName));
                             }
                         } else {
                             if ($this->UpdateFolderHandler != null) {
                                 call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
                             }
                         }
                     }
                     break 2;
                 case FILTERACTION_MarkGrey:
                     $result &= $mailProcessor->SetFlags($messageIdUidSet, $folder, MESSAGEFLAGS_Grayed, ACTION_Set, false);
                     $message->Flags |= MESSAGEFLAGS_Grayed;
                     break;
             }
             unset($filter);
         }
     }
     if ($needToSave) {
         if ($folder->SyncType == FOLDERSYNC_NewEntireMessages || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
             $result &= $dbStorage->SaveMessage($message, $folder);
             if ($this->UpdateFolderHandler != null) {
                 call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
             }
         } else {
             if ($folder->SyncType == FOLDERSYNC_NewHeadersOnly || $folder->SyncType == FOLDERSYNC_AllHeadersOnly) {
                 $result &= $dbStorage->SaveMessageHeader($message, $folder, false);
                 if ($this->UpdateFolderHandler != null) {
                     call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
                 }
             }
         }
     }
     return $result;
 }