Esempio n. 1
0
 /**
  * @param Folder $folders
  * @param DbStorage $dbStorage
  * @return bool
  */
 function _synchronizeFolderWithOpenDbConnection(&$folder, &$dbStorage)
 {
     $result = true;
     if ($folder->SyncType == FOLDERSYNC_DontSync || $folder->SyncType == FOLDERSYNC_DirectMode) {
         return true;
     }
     //get uids from pop3 server
     $pop3Uids =& $this->_getPop3Uids(true);
     //get uids from DB
     $dbUids =& $dbStorage->SelectReadsRecords();
     $dbUids = array_keys($dbUids);
     //get only new messages from pop3 server
     $newUids = array_diff($pop3Uids, $dbUids);
     //get deletd uids from pop3 server
     $uidsToDelete = array_diff($dbUids, $pop3Uids);
     //get Array sizes all messages on pop3 server
     $pop3Sizes =& $this->_getPop3Sizes(true);
     //get size all messages in DB
     $mailBoxesSize = $dbStorage->SelectMailboxesSize();
     if ($this->DownloadedMessagesHandler != null) {
         ShowDownloadedMessageNumber($folder->Name, count($newUids));
         call_user_func($this->DownloadedMessagesHandler);
     }
     foreach ($newUids as $newUid) {
         //get id message from uid pop3 server
         $index = $this->_getMessageIndexFromUid($pop3Uids, $newUid);
         $indexArray = array($index);
         $mailBoxesSize += $pop3Sizes[$index];
         if (!$this->_settings->EnableMailboxSizeLimit || $this->Account->MailboxLimit > $mailBoxesSize) {
             //Check sync mode
             if ($folder->SyncType == FOLDERSYNC_NewEntireMessages || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
                 //Entire Message
                 $mailMessageCollection =& $this->LoadMessages($indexArray, false, $folder);
             } elseif ($folder->SyncType == FOLDERSYNC_NewHeadersOnly || $folder->SyncType == FOLDERSYNC_AllHeadersOnly) {
                 //Entire Header
                 $mailMessageCollection =& $this->_loadMessageHeaders($indexArray, false, $folder);
             }
             //Apply filters and save message in DB
             if (!$this->ApplyFilters($mailMessageCollection, $dbStorage, $folder)) {
                 return false;
                 //$result = false;
                 //break;
             }
             //Check mailmode to delete from server
             if ($this->Account->MailMode == MAILMODE_DeleteMessagesFromServer) {
                 //Delete received messages from server
                 $this->_pop3Mail->delete_mail($index);
             }
             //Save uid to reads table
             $dbStorage->InsertReadsRecords(array($newUid));
         } else {
             $result = false;
             setGlobalError(ErrorGetMailLimit);
             break;
         }
     }
     //delete from DB
     if (count($uidsToDelete) > 0) {
         if ($folder->SyncType == FOLDERSYNC_AllHeadersOnly || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
             $result &= $dbStorage->DeleteMessages($uidsToDelete, true, $folder);
         }
     }
     if (($this->Account->MailMode == MAILMODE_KeepMessagesOnServer || $this->Account->MailMode == MAILMODE_KeepMessagesOnServerAndDeleteMessageWhenItsRemovedFromTrash) && $this->Account->MailsOnServerDays > 0) {
         $expiredUids =& $dbStorage->SelectExpiredMessageUids();
         $result &= $this->DeleteMessages($expiredUids, true, $folder);
     }
     if (!empty($uidsToDelete)) {
         $result &= $dbStorage->DeleteReadsRecordsByUids($uidsToDelete);
     }
     $result &= $dbStorage->UpdateMailboxSize();
     return $result;
 }
Esempio n. 2
0
 /**
  * @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;
 }