/**
  * @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;
 }
Example #2
0
 /**
  * @param Folder $folders
  * @param DbStorage $dbStorage
  * @return bool
  */
 function _synchronizeFolderWithOpenDbConnection(&$folder, &$dbStorage)
 {
     $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;
     }
     if ($this->DownloadedMessagesHandler != null) {
         call_user_func_array($this->DownloadedMessagesHandler, array($folder->GetFolderName($this->Account), 0));
     }
     //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 && count($newUids) > 0) {
         call_user_func_array($this->DownloadedMessagesHandler, array($folder->GetFolderName($this->Account), count($newUids)));
     }
     $_filters =& $dbStorage->SelectFilters($this->Account->Id, true);
     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 == 0 || $this->Account->MailboxLimit > $mailBoxesSize) {
             if ($this->DownloadedMessagesHandler != null) {
                 call_user_func($this->DownloadedMessagesHandler);
             }
             $mailMessageCollection = null;
             //Check sync mode
             if ($folder->SyncType == FOLDERSYNC_NewEntireMessages || $folder->SyncType == FOLDERSYNC_AllEntireMessages) {
                 //Entire Message
                 $mailMessageCollection =& $this->LoadMessages($indexArray, false, $folder);
             } else {
                 if ($folder->SyncType == FOLDERSYNC_NewHeadersOnly || $folder->SyncType == FOLDERSYNC_AllHeadersOnly) {
                     //Entire Header
                     $mailMessageCollection =& $this->_loadMessageHeaders($indexArray, false, $folder);
                 }
             }
             /* write to DB */
             if ($mailMessageCollection != null && $mailMessageCollection->Count() > 0) {
                 $message =& $mailMessageCollection->Get(0);
                 if (!$this->ApplyFilters($message, $dbStorage, $folder, $_filters)) {
                     return 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->UpdateFolderHandler != null) {
                 call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
             }
         }
     }
     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 ($this->UpdateFolderHandler != null) {
             call_user_func_array($this->UpdateFolderHandler, array($folder->IdDb, $folder->FullName));
         }
     }
     if (!empty($uidsToDelete)) {
         $result &= $dbStorage->DeleteReadsRecordsByUids($uidsToDelete);
     }
     $result &= $dbStorage->UpdateMailboxSize();
     return $result;
 }
Example #3
0
 /**
  * @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;
 }