/**
  * Sets up the fixture.
  * This method is called before a test is executed.
  *
  * @access protected
  */
 protected function setUp()
 {
     $this->_account = Expressomail_Controller_Account::getInstance()->search()->getFirstRecord();
     $this->_controller = Expressomail_Controller_Folder::getInstance();
     $this->_imap = Expressomail_Backend_ImapFactory::factory($this->_account);
     // fill folder cache first
     $this->_controller->search($this->_getFolderFilter(''));
 }
 /**
  * get imap backend and folder (and select folder)
  *
  * @param string                    $_folderId
  * @param Expressomail_Backend_Folder &$_folder
  * @param boolean                   $_select
  * @param Expressomail_Backend_ImapProxy   $_imapBackend
  * @throws Expressomail_Exception_IMAPServiceUnavailable
  * @throws Expressomail_Exception_IMAPFolderNotFound
  * @return Expressomail_Backend_ImapProxy
  */
 protected function _getBackendAndSelectFolder($_folderId = NULL, &$_folder = NULL, $_select = TRUE, Expressomail_Backend_ImapProxy $_imapBackend = NULL)
 {
     if ($_folder === NULL || empty($_folder)) {
         $folderBackend = new Expressomail_Backend_Folder();
         $_folder = $folderBackend->get($_folderId);
     }
     try {
         $imapBackend = $_imapBackend === NULL ? Expressomail_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(Expressomail_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 Expressomail_Exception_IMAPFolderNotFound($zmse->getMessage());
     } catch (Zend_Mail_Protocol_Exception $zmpe) {
         throw new Expressomail_Exception_IMAPServiceUnavailable($zmpe->getMessage());
     }
     return $imapBackend;
 }
 /**
  * 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 = Expressomail_Backend_ImapFactory::factory($_folder->account_id);
     }
     return $imap;
 }
 /**
  * get folder IMAP status (MESSAGES RECENT UNSEEN)
  * 
  * @param string $_accountId
  * @param string $_globalname
  * @return string imap_status | false
  *
  */
 public static function getFolderImapStatus($_accountId, $_globalname)
 {
     $imap = Expressomail_Backend_ImapFactory::factory($_accountId);
     try {
         return $imap->getFolderStatus($_globalname);
     } catch (Exception $e) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Exception: ' . $e->getMessage());
         }
         return false;
     }
 }
 /**
  * append mail to send folder
  *
  * @param Expressomail_Transport $_transport
  * @param Expressomail_Model_Account $_account
  * @param array $_additionalHeaders
  * @return void
  */
 protected function _saveInSent(Expressomail_Transport $_transport, Expressomail_Model_Account $_account, $_additionalHeaders = array())
 {
     try {
         $mailAsString = $_transport->getRawMessage(NULL, $_additionalHeaders);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' About to save message in sent folder (' . $this->_sentFolder . ') ...');
         }
         Expressomail_Backend_ImapFactory::factory($_account)->appendMessage($mailAsString, $this->_sentFolder);
         Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Saved sent message in "' . $this->_sentFolder . '".');
     } catch (Zend_Mail_Protocol_Exception $zmpe) {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not save sent message in "' . $this->_sentFolder . '".' . ' 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 "' . $this->_sentFolder . '".' . ' Please check if a folder with this name exists.' . '(' . $zmse->getMessage() . ')');
     }
 }
 /**
  * get imap changed entries
  *
  * @param string $_accountId
  * @param string $box
  * @param string $lastmodseq
  * @return array
  */
 public function getImapChangedEntries($_accountId, $box, $lastmodseq)
 {
     $result = array();
     $imap = Expressomail_Backend_ImapFactory::factory($_accountId);
     try {
         $result = $imap->fetchIdsChangedSinceModSeq($box, $lastmodseq);
     } catch (Exception $e) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Exception: ' . $e->getMessage());
         }
         return false;
     }
     return $result;
 }
 /**
  * get folder cache counter like total and unseen
  *
  * @param  string  $_folderId  the folderid
  * @return array
  */
 public function getFolderCounter($_folderId)
 {
     if ($_folderId instanceof Expressomail_Model_Folder) {
         $exists = $_folderId->cache_totalcount;
         $unseen = $_folderId->cache_unreadcount;
     } else {
         $folder = self::decodeFolderUid($_folderId);
         $imap = Expressomail_Backend_ImapFactory::factory($folder['accountId']);
         $counter = $imap->examineFolder($folder['globalName']);
         $exists = $counter['exists'];
         $unseen = $counter['unseen'];
     }
     return array('cache_totalcount' => $exists, 'cache_unreadcount' => $unseen);
 }
 /**
  * get imap backend and catch exceptions
  *
  * @param Expressomail_Model_Account $_account
  * @param boolean $_throwException
  * @return boolean|Expressomail_Backend_ImapProxy
  * @throws Expressomail_Exception_IMAP|Expressomail_Exception_IMAPInvalidCredentials
  */
 protected function _getIMAPBackend(Expressomail_Model_Account $_account, $_throwException = FALSE)
 {
     $result = FALSE;
     try {
         $result = Expressomail_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 (Expressomail_Exception_IMAPInvalidCredentials $feiic) {
         $message = 'Wrong user credentials (' . $feiic->getMessage() . ')';
     }
     if (!$result) {
         $message .= ' for account ' . $_account->name;
         if ($_throwException) {
             throw isset($feiic) ? $feiic : new Expressomail_Exception_IMAP($message);
         } else {
             Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $message);
         }
     }
     return $result;
 }
 /**
  * test change pw + credential cache
  */
 public function testChangePasswordAndUpdateCredentialCache()
 {
     $testConfig = Zend_Registry::get('testConfig');
     $account = clone $this->_account;
     unset($account->id);
     $account->type = Expressomail_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 = Expressomail_Backend_ImapFactory::factory($account);
         $imapAccountConfig = $account->getImapConfig();
         $imap->connectAndLogin((object) $imapAccountConfig);
     } catch (Expressomail_Exception_IMAPInvalidCredentials $e) {
         $loginSuccessful = FALSE;
     }
     $this->assertTrue($loginSuccessful, 'wrong credentials');
 }
 /**
  * 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);
     Expressomail_Backend_ImapFactory::factory($this->_account->getId())->appendMessage($mailAsString, $_folder);
 }
 /**
  * Deletes entries
  *
  * @param string|integer|Tinebase_Record_Interface|array $_id
  * @return void
  * @return int The number of affected rows.
  */
 public function delete($_id)
 {
     $_id = $_id instanceof Expressomail_Model_Message ? array($_id->getId()) : $_id;
     if (is_array($_id)) {
         foreach ($_id as $id) {
             $decodedIds = self::decodeMessageId($id);
             $globalname = Expressomail_Backend_Folder::decodeFolderUid($decodedIds['folderId']);
             $accountId = $decodedIds['accountId'];
             $imap = Expressomail_Backend_ImapFactory::factory($accountId);
             $imap->expunge($globalname['globalName']);
             $return = count($_id);
         }
     }
     return $return;
 }
 /**
  * Returns registry data of expressomail.
  * 
  * @see Tinebase_Application_Json_Abstract
  *
  * @return mixed array 'variable name' => 'data'
  *        
  * @todo get default account data (host, port, ...) from preferences?
  */
 public function getRegistryData()
 {
     try {
         $accounts = $this->searchAccounts('');
     } catch (Exception $e) {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not get accounts: ' . $e->getMessage());
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $e->getTraceAsString());
         }
         $accounts = array('results' => array(), 'totalcount' => 0);
     }
     $supportedFlags = Expressomail_Controller_Message_Flags::getInstance()->getSupportedFlags();
     $extraSenderAccounts = array();
     foreach ($accounts['results'] as $key => $account) {
         try {
             // build a imap backend so the system folder can be created if necessary
             $accountModel = Expressomail_Controller_Account::getInstance()->get($account['id']);
             $accountModel->resolveCredentials(FALSE);
             // force update the user credentials
             $imapConfig = $imapConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::IMAP, new Tinebase_Config_Struct());
             $config = new stdClass();
             $config->{'host'} = $imapConfig->{'host'};
             $config->{'port'} = $imapConfig->{'port'};
             $config->{'ssl'} = $imapConfig->{'ssl'};
             $config->{'user'} = $accountModel->getUsername();
             $config->{'password'} = $accountModel->{'password'};
             $imap = Expressomail_Backend_ImapFactory::factory($account['id']);
             if ($imap->createDefaultImapSystemFoldersIfNecessary($config)) {
                 try {
                     // add the namespace 'INBOX/' to the new folders
                     $capabilities = $imap->getCapabilityAndNamespace();
                     Expressomail_Controller_Account::getInstance()->updateNamespacesAndDelimiter($accountModel, $capabilities);
                     // update account info in backend and session
                     if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                         Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Updating capabilities for account: ' . $accountModel->name);
                     }
                     Expressomail_Controller_Account::getInstance()->getBackend()->update($accountModel);
                     // save capabilities in SESSION
                     Expressomail_Session::getSessionNamespace()->account[$accountModel->getId()] = $capabilities;
                 } catch (Exception $zdse) {
                     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $zdse->getTraceAsString());
                     }
                 }
             }
         } catch (Exception $e) {
             if (Tinebase_Core::isLogLevel(Zend_Log::ERR)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Exception: ' . $e->getMessage());
             }
         }
         try {
             $extraSenderAccounts = Expressomail_Controller_Folder::getInstance()->getUsersWithSendAsAcl($account['id']);
         } catch (Expressomail_Exception_IMAPFolderNotFound $ex) {
             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                 Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . $ex->getMessage());
             }
             // Ignore this exception here, it happens when INBOX folder is unaccessible.
         } catch (Expressomail_Exception_IMAPServiceUnavailable $ex) {
             // Ignoring this Exception here.
         }
         unset($account['host']);
         unset($account['port']);
         unset($account['ssl']);
         unset($account['smtp_hostname']);
         unset($account['smtp_port']);
         unset($account['smtp_ssl']);
         unset($account['smtp_auth']);
         $accounts['results'][$key] = $account;
     }
     $result = array('extraSenderAccounts' => $extraSenderAccounts, 'accounts' => $accounts, 'supportedFlags' => array('results' => $supportedFlags, 'totalcount' => count($supportedFlags)), 'aspellDicts' => Tinebase_Core::getConfig()->aspellDicts);
     // TODO: get balanceid cookie name from config
     $balanceIdCookieName = 'BALANCEID';
     if (isset($_COOKIE[$balanceIdCookieName])) {
         $result['balanceId'] = array('cookieName' => $balanceIdCookieName, 'cookieValue' => $_COOKIE[$balanceIdCookieName]);
     }
     $result['vacationTemplates'] = $this->getVacationMessageTemplates();
     $config = Tinebase_Core::getConfig();
     $result['useKeyEscrow'] = $config->certificate->active && $config->certificate->useKeyEscrow;
     $config = Expressomail_Controller::getInstance()->getConfigSettings(false);
     // add autoSaveDraftsInterval to client registry
     $result['autoSaveDraftsInterval'] = $config->autoSaveDraftsInterval;
     // add reportPhishingEmail to client registry
     $result['reportPhishingEmail'] = $config->reportPhishingEmail;
     return $result;
 }
 /**
  * Sets up the fixture.
  * This method is called before a test is executed.
  *
  * @access protected
  */
 protected function setUp()
 {
     // get (or create) test accout
     $this->_account = Expressomail_Controller_Account::getInstance()->search()->getFirstRecord();
     $this->_oldSieveVacationActiveState = $this->_account->sieve_vacation_active;
     try {
         $this->_oldSieveData = new Expressomail_Sieve_Backend_Sql($this->_account);
     } catch (Tinebase_Exception_NotFound $tenf) {
         // do nothing
     }
     $this->_json = new Expressomail_Frontend_Json();
     $this->_imap = Expressomail_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.serpro.gov.br';
 }