/** * @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; }
/** * @param Folder $folders * @param DbStorage $dbStorage * @param int $lastIdMsg * @return bool */ function _synchronizeFolderWithOpenDbConnection(&$folder, &$dbStorage) { $log =& CLog::CreateInstance(); $result = true; if ($folder->SyncType == FOLDERSYNC_DontSync || $folder->SyncType == FOLDERSYNC_DirectMode || $folder->Hide) { if ($this->UpdateFolderHandler != null && $folder->SyncType == FOLDERSYNC_DirectMode) { call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName)); } return true; } $foldername = ''; if ($this->DownloadedMessagesHandler != null) { $foldername = $folder->GetFolderName($this->Account); call_user_func_array($this->DownloadedMessagesHandler, array($foldername, 0)); } if (!$this->_imapMail->open_mailbox($folder->FullName, false, true)) { return true; } $_isAllUpdate = $folder->SyncType == FOLDERSYNC_AllHeadersOnly || $folder->SyncType == FOLDERSYNC_AllEntireMessages; $_isUidsOnly = $folder->SyncType == FOLDERSYNC_NewHeadersOnly; /* get uid, flags and size from IMAP4 Server */ if ($log->Enabled) { $start = getmicrotime(); } $paramsMessages = $this->_imapMail->getParamsMessages(); if ($log->Enabled) { $log->WriteLine('IMAP4: getParamsMessages()=' . (getmicrotime() - $start)); } if (!is_array($paramsMessages)) { return false; } $imapUids = $imapSizes = $imapUidFlags = $imapUidSizes = array(); $this->_imapArrayForeach($paramsMessages, $imapUids, $imapSizes, $imapUidFlags, $imapUidSizes); unset($paramsMessages); $dbUidsIdMsgsFlags =& $dbStorage->SelectIdMsgAndUidByIdMsgDesc($folder); $dbUids = $dbUidsFlag = array(); foreach ($dbUidsIdMsgsFlags as $value) { $dbUids[] = $value[1]; $dbUidsFlag[$value[1]] = $value[2]; } unset($dbUidsIdMsgsFlags); /* array need added to DB */ //$newUids = array_diff($imapUids, $dbUids); $newUids = array(); foreach ($imapUids as $_imUid) { if (!isset($dbUidsFlag[$_imUid])) { $newUids[] = $_imUid; } } if ($this->DownloadedMessagesHandler != null && count($newUids) > 0) { call_user_func_array($this->DownloadedMessagesHandler, array($foldername, count($newUids))); } if ($_isAllUpdate) { /* update flags */ $_flags4Update = array(); /* intersect uids */ foreach ($imapUids as $_imUid) { if (isset($dbUidsFlag[$_imUid])) { $flagBD = (int) $dbUidsFlag[$_imUid]; $flagImap = (int) $this->getIntFlags($imapUidFlags[$_imUid]); /* update messages whith different flags */ if ($flagBD != $flagImap) { $_flags4Update[$flagImap][] = $_imUid; } } } if (count($_flags4Update) > 0) { foreach ($_flags4Update as $_flag => $_uidArray) { if (is_array($_uidArray)) { $dbStorage->UpdateMessageFlags($_uidArray, true, $folder, $_flag, $this->Account); } } if ($this->UpdateFolderHandler != null) { call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName)); } } /* delete from DB */ //$uidsToDelete = array_diff($dbUids, $imapUids); $uidsToDelete = array(); foreach ($dbUids as $_dbUid) { if (!isset($imapUidFlags[$_dbUid])) { //$dbUidsFlag[$_dbUid] = $value[2]; $uidsToDelete[] = $_dbUid; } } if (count($uidsToDelete) > 0) { if ($this->UpdateFolderHandler != null) { call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName)); } // $result &= $dbStorage->SetMessagesFlags($uidsToDelete, true, $folder, MESSAGEFLAGS_Deleted, ACTION_Set); $result &= $dbStorage->DeleteMessages($uidsToDelete, true, $folder); $result &= $dbStorage->UpdateMailboxSize(); } } $maxEnvelopesPerSession = 1; /* get size all messages in DB */ $mailBoxesSize = $dbStorage->SelectMailboxesSize(); $filters =& $dbStorage->SelectFilters($this->Account->Id, true); if ($folder->SyncType == FOLDERSYNC_NewHeadersOnly || $folder->SyncType == FOLDERSYNC_AllHeadersOnly) { $syncCycles = ceil(count($newUids) / MAX_ENVELOPES_PER_SESSION); for ($q = 0; $q < $syncCycles; $q++) { if (!$this->_imapMail->open_mailbox($folder->FullName)) { return true; } $cyclesNewUids = array_slice($newUids, $q * MAX_ENVELOPES_PER_SESSION, MAX_ENVELOPES_PER_SESSION); $mailMessageCollection = null; $mailMessageCollection =& $this->LoadMessageHeadersInOneRequest($folder, $cyclesNewUids, true); if ($mailMessageCollection) { for ($i = 0, $c = $mailMessageCollection->Count(); $i < $c; $i++) { if ($this->DownloadedMessagesHandler != null && function_exists($this->DownloadedMessagesHandler)) { call_user_func($this->DownloadedMessagesHandler); } $message =& $mailMessageCollection->Get($i); $mailBoxesSize += $message->Size; if ($this->_settings->EnableMailboxSizeLimit && $this->Account->MailboxLimit > 0 && $this->Account->MailboxLimit < $mailBoxesSize) { $result = false; setGlobalError(ErrorGetMailLimit); break 2; } if (!$this->ApplyFilters($message, $dbStorage, $folder, $filters)) { $result = false; } } } } } else { $syncCycles = ceil(count($newUids) / $maxEnvelopesPerSession); for ($i = 0; $i < $syncCycles; $i++) { $mailBoxesSize += $imapSizes[$i + 1]; if ($this->_settings->EnableMailboxSizeLimit && $this->Account->MailboxLimit > 0 && $this->Account->MailboxLimit < $mailBoxesSize) { $result = false; setGlobalError(ErrorGetMailLimit); break; } if (!$this->_imapMail->open_mailbox($folder->FullName)) { return true; } $listPartToDownload = $i != $syncCycles - 1 ? array_slice($newUids, $i * $maxEnvelopesPerSession, $maxEnvelopesPerSession) : array_slice($newUids, $i * $maxEnvelopesPerSession); if ($this->DownloadedMessagesHandler != null && function_exists($this->DownloadedMessagesHandler)) { call_user_func($this->DownloadedMessagesHandler); } $mailMessageCollection = null; $mailMessageCollection =& $this->LoadMessages($listPartToDownload, true, $folder, $imapUids, $imapUidFlags, $imapUidSizes); if ($mailMessageCollection && $mailMessageCollection->Count() > 0) { $message =& $mailMessageCollection->Get(0); if (!$this->ApplyFilters($message, $dbStorage, $folder, $filters)) { $result = false; break; } } } } $result &= $dbStorage->UpdateMailboxSize(); return $result; }