/**
  * @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;
 }