/** * Sets up the fixture. * This method is called before a test is executed. * * @access protected */ protected function setUp() { $this->_account = Felamimail_Controller_Account::getInstance()->search()->getFirstRecord(); $this->_controller = Felamimail_Controller_Folder::getInstance(); $this->_imap = Felamimail_Backend_ImapFactory::factory($this->_account); // fill folder cache first $this->_controller->search($this->_getFolderFilter('')); }
/** * Deletes entries * * @param string|integer|Tinebase_Record_Interface|array $_id * @return void * @return int The number of affected rows. */ public function delete($_id) { if (is_array($_id)) { foreach ($_id as $id) { $decodedIds = self::decodeMessageId($id); $globalname = Felamimail_Backend_Cache_Imap_Folder::decodeFolderUid($decodedIds['folderId']); $accountId = $decodedIds['accountId']; $imap = Felamimail_Backend_ImapFactory::factory($accountId); $imap->expunge($globalname['globalName']); $return = count($_id); } } return $return; }
/** * check if folder is selectable: try to select folder on imap server if isSelectable is false/not set * - courier imap servers subfolder have isSelectable = 0 but they still can be selected * @see http://www.tine20.org/bugtracker/view.php?id=2736 * * @param array $_folderData * @param Felamimail_Model_Account $_account * @return boolean */ protected function _isSelectable($_folderData, $_account) { $result = TRUE; if (!$_folderData['isSelectable'] == '1') { if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Folder ' . $_folderData['globalName'] . ' is not selectable.'); } $imap = Felamimail_Backend_ImapFactory::factory($_account); try { $folderData = $imap->examineFolder(Felamimail_Model_Folder::encodeFolderName($_folderData['globalName'])); } catch (Zend_Mail_Storage_Exception $zmse) { if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Could not select folder. Skipping it.'); } $result = FALSE; } } return $result; }
/** * Sets up the fixture. * This method is called before a test is executed. * * @access protected */ protected function setUp() { Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb()); // get (or create) test accout $this->_account = Felamimail_Controller_Account::getInstance()->search()->getFirstRecord(); $this->_oldSieveVacationActiveState = $this->_account->sieve_vacation_active; try { $this->_oldSieveData = new Felamimail_Sieve_Backend_Sql($this->_account); } catch (Tinebase_Exception_NotFound $tenf) { // do nothing } $this->_json = new Felamimail_Frontend_Json(); $this->_imap = Felamimail_Backend_ImapFactory::factory($this->_account); foreach (array($this->_testFolderName, $this->_account->sent_folder, $this->_account->trash_folder) as $folderToCreate) { // create folder if it does not exist $this->_getFolder($folderToCreate); } $config = TestServer::getInstance()->getConfig(); $this->_mailDomain = $config->maildomain ? $config->maildomain : 'tine20.org'; $this->_frontend = new Tinebase_Frontend_Json(); }
/** * append mail to send folder * * @param Felamimail_Transport $_transport * @param Felamimail_Model_Account $_account * @param array $_additionalHeaders * @return void */ protected function _saveInSent(Felamimail_Transport $_transport, Felamimail_Model_Account $_account, $_additionalHeaders = array()) { try { $mailAsString = $_transport->getRawMessage(NULL, $_additionalHeaders); $sentFolder = Felamimail_Controller_Account::getInstance()->getSystemFolder($_account, Felamimail_Model_Folder::FOLDER_SENT); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' About to save message in sent folder (' . $sentFolder->globalname . ') ...'); } Felamimail_Backend_ImapFactory::factory($_account)->appendMessage($mailAsString, Felamimail_Model_Folder::encodeFolderName($sentFolder->globalname)); Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Saved sent message in "' . $sentFolder->globalname . '".'); } catch (Zend_Mail_Protocol_Exception $zmpe) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not save sent message in "' . $sentFolder->globalname . '".' . ' Please check if a folder with this name exists.' . '(' . $zmpe->getMessage() . ')'); } catch (Zend_Mail_Storage_Exception $zmse) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not save sent message in "' . $sentFolder->globalname . '".' . ' Please check if a folder with this name exists.' . '(' . $zmse->getMessage() . ')'); } }
/** * get imap backend and catch exceptions * * @param Felamimail_Model_Account $_account * @param boolean $_throwException * @return boolean|Felamimail_Backend_ImapProxy * @throws Felamimail_Exception_IMAP|Felamimail_Exception_IMAPInvalidCredentials */ protected function _getIMAPBackend(Felamimail_Model_Account $_account, $_throwException = FALSE) { $result = FALSE; try { $result = Felamimail_Backend_ImapFactory::factory($_account); } catch (Zend_Mail_Storage_Exception $zmse) { $message = 'Wrong user credentials (' . $zmse->getMessage() . ')'; } catch (Zend_Mail_Protocol_Exception $zmpe) { $message = 'No connection to imap server (' . $zmpe->getMessage() . ')'; } catch (Felamimail_Exception_IMAPInvalidCredentials $feiic) { $message = 'Wrong user credentials (' . $feiic->getMessage() . ')'; } if (!$result) { $message .= ' for account ' . $_account->name; if ($_throwException) { throw isset($feiic) ? $feiic : new Felamimail_Exception_IMAP($message); } else { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $message); } } return $result; }
/** * get folder cache counter like total and unseen * * @param string $_folderId the folderid * @return array */ public function getFolderCounter($_folderId) { if ($_folderId instanceof Felamimail_Model_Folder) { $exists = $_folderId->cache_totalcount; $unseen = $_folderId->cache_unreadcount; } else { $folder = self::decodeFolderUid($_folderId); $imap = Felamimail_Backend_ImapFactory::factory($folder['accountId']); $counter = $imap->examineFolder($folder['globalName']); $exists = $counter['exists']; $unseen = $counter['unseen']; } return array('cache_totalcount' => $exists, 'cache_unreadcount' => $unseen); }
/** * append message (from given filename) to folder * * @param string $_filename * @param string $_folder */ protected function _appendMessage($_filename, $_folder) { $mailAsString = file_get_contents(dirname(dirname(dirname(__FILE__))) . '/files/' . $_filename); Felamimail_Backend_ImapFactory::factory($this->_account->getId())->appendMessage($mailAsString, $_folder); }
/** * get imap backend and folder (and select folder) * * @param string $_folderId * @param Felamimail_Backend_Folder &$_folder * @param boolean $_select * @param Felamimail_Backend_ImapProxy $_imapBackend * @throws Felamimail_Exception_IMAPServiceUnavailable * @throws Felamimail_Exception_IMAPFolderNotFound * @return Felamimail_Backend_ImapProxy */ protected function _getBackendAndSelectFolder($_folderId = NULL, &$_folder = NULL, $_select = TRUE, Felamimail_Backend_ImapProxy $_imapBackend = NULL) { if ($_folder === NULL || empty($_folder)) { $folderBackend = new Felamimail_Backend_Folder(); $_folder = $folderBackend->get($_folderId); } try { $imapBackend = $_imapBackend === NULL ? Felamimail_Backend_ImapFactory::factory($_folder->account_id) : $_imapBackend; if ($_select) { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Select folder ' . $_folder->globalname); } $imapBackend->selectFolder(Felamimail_Model_Folder::encodeFolderName($_folder->globalname)); } } catch (Zend_Mail_Storage_Exception $zmse) { // @todo remove the folder from cache if it could not be found on the IMAP server? throw new Felamimail_Exception_IMAPFolderNotFound($zmse->getMessage()); } catch (Zend_Mail_Protocol_Exception $zmpe) { throw new Felamimail_Exception_IMAPServiceUnavailable($zmpe->getMessage()); } return $imapBackend; }
/** * test change pw + credential cache */ public function testChangePasswordAndUpdateCredentialCache() { $this->markTestSkipped('FIXME 0009250: fix test testChangePasswordAndUpdateCredentialCache'); $testConfig = Zend_Registry::get('testConfig'); $account = clone $this->_account; unset($account->id); $account->type = Felamimail_Model_Account::TYPE_USER; $account->user = $testConfig->username; $imapConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::IMAP, new Tinebase_Config_Struct())->toArray(); if (isset($imapConfig['domain']) && !empty($imapConfig['domain'])) { $account->user .= '@' . $imapConfig['domain']; } $account->password = $testConfig->password; $account = $this->_controller->create($account); $testPw = 'testpwd'; // change pw & update credential cache $this->_setCredentials($testConfig->username, $testPw); $account = $this->_controller->get($account->getId()); // try to connect to imap $loginSuccessful = TRUE; try { $imap = Felamimail_Backend_ImapFactory::factory($account); } catch (Felamimail_Exception_IMAPInvalidCredentials $e) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' config: ' . print_r($imapAccountConfig, true) . ' / exception:' . $e); } $loginSuccessful = FALSE; } $this->assertTrue($loginSuccessful, 'wrong credentials'); }
/** * move messages from one folder to another folder within the same email account * * @param Tinebase_Record_RecordSet $_messages * @param Felamimail_Model_Folder $_targetFolder */ protected function _moveMessagesInFolderOnSameAccount(Tinebase_Record_RecordSet $_messages, Felamimail_Model_Folder $_targetFolder) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Move ' . count($_messages) . ' message(s) to ' . $_targetFolder->globalname); } $firstMessage = $_messages->getFirstRecord(); $folder = Felamimail_Controller_Folder::getInstance()->get($firstMessage->folder_id); $imapBackend = Felamimail_Backend_ImapFactory::factory($firstMessage->account_id); $imapBackend->selectFolder(Felamimail_Model_Folder::encodeFolderName($folder->globalname)); $imapMessageUids = array(); foreach ($_messages as $message) { $imapMessageUids[] = $message->messageuid; if (count($imapMessageUids) >= 50) { $this->_moveBatchOfMessages($imapMessageUids, $_targetFolder->globalname, $imapBackend); $imapMessageUids = array(); } } // the rest if (count($imapMessageUids) > 0) { $this->_moveBatchOfMessages($imapMessageUids, $_targetFolder->globalname, $imapBackend); } }
/** * test change pw + credential cache */ public function testChangePasswordAndUpdateCredentialCache() { $testConfig = Zend_Registry::get('testConfig'); $account = clone $this->_account; unset($account->id); $account->type = Felamimail_Model_Account::TYPE_USER; $account->user = $testConfig->username; $imapConfig = Tinebase_Config::getInstance()->getConfigAsArray(Tinebase_Config::IMAP); if (isset($imapConfig['domain']) && !empty($imapConfig['domain'])) { $account->user .= '@' . $imapConfig['domain']; } $account->password = $testConfig->password; $account = $this->_controller->create($account); $testPw = 'testpwd'; // change pw & update credential cache $this->_setCredentials($testConfig->username, $testPw); $account = $this->_controller->get($account->getId()); // try to connect to imap $loginSuccessful = TRUE; try { $imap = Felamimail_Backend_ImapFactory::factory($account); $imapAccountConfig = $account->getImapConfig(); $imap->connectAndLogin((object) $imapAccountConfig); } catch (Felamimail_Exception_IMAPInvalidCredentials $e) { $loginSuccessful = FALSE; } $this->assertTrue($loginSuccessful, 'wrong credentials'); }
/** * update/synchronize flags * * @param string|Felamimail_Model_Folder $_folder * @param integer $_time * @return Felamimail_Model_Folder * * @todo only get flags of current batch of messages from imap? * @todo add status/progress to start at later messages when this is called next time? */ public function updateFlags($_folder, $_time = 60) { // always read folder from database $folder = Felamimail_Controller_Folder::getInstance()->get($_folder); if ($folder->cache_status !== Felamimail_Model_Folder::CACHE_STATUS_COMPLETE) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Do not update flags of incomplete folder ' . $folder->globalname); } return $folder; } if ($this->_availableUpdateTime == 0) { $this->_availableUpdateTime = $_time; $this->_timeStart = microtime(true); } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating flags of folder ' . $folder->globalname . ' / start time: ' . Tinebase_DateTime::now()->toString() . ' / available seconds: ' . ($this->_availableUpdateTime - $this->_timeElapsed)); } // get all flags for folder $imap = Felamimail_Backend_ImapFactory::factory($folder->account_id); $imap->selectFolder(Felamimail_Model_Folder::encodeFolderName($folder->globalname)); $flags = $imap->getFlags(1, INF); for ($i = $folder->cache_totalcount; $i > 0; $i -= $this->_flagSyncCountPerStep) { $firstMessageSequence = $i - $this->_flagSyncCountPerStep >= 0 ? $i - $this->_flagSyncCountPerStep : 0; $messagesWithFlags = $this->_backend->getFlagsForFolder($folder->getId(), $firstMessageSequence, $this->_flagSyncCountPerStep); $this->_setFlagsOnCache($messagesWithFlags, $flags, $folder->getId()); if (!$this->_timeLeft()) { break; } } $updatedCounters = Felamimail_Controller_Cache_Folder::getInstance()->getCacheFolderCounter($_folder); $folder = Felamimail_Controller_Folder::getInstance()->updateFolderCounter($folder, array('cache_unreadcount' => $updatedCounters['cache_unreadcount'])); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' New unreadcount after flags update: ' . $updatedCounters['cache_unreadcount']); } return $folder; }
/** * get folder status/values from imap server and update folder cache record in database * * @param Felamimail_Model_Folder $_folder * @param Felamimail_Backend_Imap|boolean $_imap * @return Felamimail_Model_Folder */ public function getIMAPFolderCounter(Felamimail_Model_Folder $_folder) { $folder = $_folder instanceof Felamimail_Model_Folder ? $_folder : Felamimail_Controller_Folder::getInstance()->get($_folder); $imap = Felamimail_Backend_ImapFactory::factory($folder->account_id); // get folder values / status from imap server $counter = $imap->examineFolder(Felamimail_Model_Folder::encodeFolderName($folder->globalname)); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($counter, TRUE)); } // check validity $folder->cache_uidvalidity = $folder->imap_uidvalidity; $folder->imap_uidvalidity = $counter['uidvalidity']; $folder->cache_uidvalidity = empty($folder->cache_uidvalidity) ? $folder->imap_uidvalidity : $folder->cache_uidvalidity; $folder->imap_totalcount = $counter['exists']; $folder->imap_status = Felamimail_Model_Folder::IMAP_STATUS_OK; $folder->imap_timestamp = Tinebase_DateTime::now(); return $folder; }
/** * get imap backend * * @param Felamimail_Model_Folder $_folder * @return Felamimail_Backend_ImapProxy */ protected function _getImapFromFolder($_folder) { if ($_folder->account_id == $this->_account->getId()) { $imap = $this->_imap; } else { $imap = Felamimail_Backend_ImapFactory::factory($_folder->account_id); } return $imap; }
/** * fetch message summary from IMAP server * * @param string $messageUid * @param string $accountId * @param string $folderId * @return array */ public function getMessageSummary($messageUid, $accountId, $folderId = NULL) { $imap = Felamimail_Backend_ImapFactory::factory($accountId); if ($folderId !== NULL) { try { $folder = Felamimail_Controller_Folder::getInstance()->get($folderId); $imap->selectFolder(Felamimail_Model_Folder::encodeFolderName($folder->globalname)); } catch (Exception $e) { if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not select folder ' . $folder->globalname . ': ' . $e->getMessage()); } } } $summary = $imap->getSummary($messageUid, NULL, TRUE); return $summary; }
/** * delete all messages in one folder -> be careful, they are completly removed and not moved to trash * -> delete subfolders if param set * * @param string $_folderId * @param boolean $_deleteSubfolders * @return Felamimail_Model_Folder * @throws Felamimail_Exception_IMAPServiceUnavailable */ public function emptyFolder($_folderId, $_deleteSubfolders = FALSE) { $folder = $this->_backend->get($_folderId); $account = Felamimail_Controller_Account::getInstance()->get($folder->account_id); $imap = Felamimail_Backend_ImapFactory::factory($account); try { // try to delete messages in imap folder Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Delete all messages in folder ' . $folder->globalname); $imap->emptyFolder(Felamimail_Model_Folder::encodeFolderName($folder->globalname)); } catch (Zend_Mail_Protocol_Exception $zmpe) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $zmpe->getMessage()); throw new Felamimail_Exception_IMAPServiceUnavailable($zmpe->getMessage()); } catch (Zend_Mail_Storage_Exception $zmse) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Folder could be empty (' . $zmse->getMessage() . ')'); } if ($_deleteSubfolders) { $this->deleteSubfolders($folder); } $folder = Felamimail_Controller_Cache_Message::getInstance()->clear($_folderId); return $folder; }
/** * get imap backend and folder (and select folder) * * @param string $_folderId * @param Felamimail_Backend_Folder &$_folder * @param boolean $_select * @param Felamimail_Backend_ImapProxy $_imapBackend * @throws Felamimail_Exception_IMAPServiceUnavailable * @return Felamimail_Backend_ImapProxy */ protected function _getBackendAndSelectFolder($_folderId = NULL, &$_folder = NULL, $_select = TRUE, Felamimail_Backend_ImapProxy $_imapBackend = NULL) { if ($_folder === NULL || empty($_folder)) { $folderBackend = Felamimail_Backend_Folder::getInstance(); $_folder = $folderBackend->get($_folderId); } try { $imapBackend = $_imapBackend === NULL ? Felamimail_Backend_ImapFactory::factory($_folder->account_id) : $_imapBackend; if ($_select) { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Select folder ' . $_folder->globalname); } $backendFolderValues = $imapBackend->selectFolder(Felamimail_Model_Folder::encodeFolderName($_folder->globalname)); } } catch (Zend_Mail_Protocol_Exception $zmpe) { // no imap connection throw new Felamimail_Exception_IMAPServiceUnavailable(); } return $imapBackend; }