/**
  * @param Folder $folders
  * @param DbStorage $dbStorage
  * @param int $lastIdMsg
  * @return bool
  */
 function _synchronizeFolderWithOpenDbConnection(&$folder, &$dbStorage, $lastIdMsg)
 {
     $result = true;
     if ($folder->SyncType == FOLDERSYNC_DontSync || $folder->SyncType == FOLDERSYNC_DirectMode || $folder->Hide) {
         return true;
     }
     if (!$this->_imapMail->examine_mailbox($folder->FullName)) {
         return false;
     }
     //Get uid, flags and size from imap Server
     $paramsMessages = $this->_imapMail->getParamsMessages();
     $imapFlags = array();
     $imapUids = array();
     $imapSizes = array();
     $dbUids = array();
     $imapUidFlags = array();
     if (!is_array($paramsMessages)) {
         return false;
     }
     foreach ($paramsMessages as $key => $value) {
         $imapFlags[$key] = $value["flag"];
         $imapUids[$key] = $value["uid"];
         $imapSizes[$key] = $value["size"];
         $imapUidFlags[$value["uid"]] = $value["flag"];
         $imapUidSizes[$value["uid"]] = $value["size"];
     }
     $dbUidsIdMsgsFlags =& $dbStorage->SelectIdMsgAndUidByIdMsgDesc($folder);
     foreach ($dbUidsIdMsgsFlags as $value) {
         $dbUidsFlag[$value[1]] = $value[2];
         $dbUids[] = $value[1];
     }
     //Array need added to DB
     $newUids = array_diff($imapUids, $dbUids);
     //Array delete from DB
     $uidsToDelete = array_diff($dbUids, $imapUids);
     //Intersect uids
     $currentUids = array_intersect($imapUids, $dbUids);
     if ($folder->SyncType == FOLDERSYNC_AllHeadersOnly || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
         //Update messages whith different flags
         foreach ($currentUids as $currentUid) {
             $flagBD = $dbUidsFlag[$currentUid];
             $flagImap = $this->getIntFlags($imapUidFlags[$currentUid]);
             if ($flagBD != $flagImap) {
                 $dbStorage->UpdateMessageFlags(array($currentUid), true, $folder, $flagImap, $this->Account);
             }
         }
     }
     if ($this->DownloadedMessagesHandler != null) {
         if (ConvertUtils::IsLatin($folder->Name)) {
             $foldername = ConvertUtils::ConvertEncoding($folder->Name, CPAGE_UTF7_Imap, $this->Account->GetUserCharset());
         } else {
             $foldername = ConvertUtils::ConvertEncoding($folder->Name, $this->Account->DefaultIncCharset, $this->Account->GetUserCharset());
         }
         ShowDownloadedMessageNumber($foldername, count($newUids));
         ShowDownloadedMessageNumber();
     }
     //Delete from DB
     if (count($uidsToDelete) > 0 && ($folder->SyncType == FOLDERSYNC_AllHeadersOnly || $folder->SyncType == FOLDERSYNC_AllEntireMessages)) {
         //$result &= $dbStorage->DeleteMessages($uidsToDelete, true, $folder);
         $result &= $dbStorage->SetMessagesFlags($uidsToDelete, true, $folder, MESSAGEFLAGS_Deleted, ACTION_Set);
     }
     $result &= $dbStorage->UpdateMailboxSize();
     $maxEnvelopesPerSession = 1;
     //Get size all messages in DB
     $mailBoxesSize = $dbStorage->SelectMailboxesSize();
     $syncCycles = ceil(count($newUids) / $maxEnvelopesPerSession);
     for ($i = 0; $i < $syncCycles; $i++) {
         $mailBoxesSize += $imapSizes[$i + 1];
         if ($this->_settings->EnableMailboxSizeLimit && $this->Account->MailboxLimit < $mailBoxesSize) {
             $result = false;
             setGlobalError(ErrorGetMailLimit);
             break;
         }
         $listPartToDownload = $i != $syncCycles - 1 ? array_slice($newUids, $i * $maxEnvelopesPerSession, $maxEnvelopesPerSession) : array_slice($newUids, $i * $maxEnvelopesPerSession);
         //Synchronize
         if ($folder->SyncType == FOLDERSYNC_NewEntireMessages || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
             $mailMessageCollection =& $this->LoadMessages($listPartToDownload, true, $folder, $imapUids, $imapUidFlags, $imapUidSizes);
         } elseif ($folder->SyncType == FOLDERSYNC_NewHeadersOnly || $folder->SyncType == FOLDERSYNC_AllHeadersOnly) {
             $mailMessageCollection =& $this->_loadMessageHeaders($listPartToDownload, $imapUids, $imapUidFlags, $imapUidSizes);
         }
         //Write to DB
         if ($mailMessageCollection != null && $mailMessageCollection->Count() > 0) {
             if (!$this->ApplyFilters($mailMessageCollection, $dbStorage, $folder)) {
                 $result = false;
                 break;
             }
         }
     }
     $result &= $dbStorage->UpdateMailboxSize();
     return $result;
 }