/**
  * factory function to return a selected account/imap backend class
  *
  * @param   string|Expressomail_Model_Account $_accountId
  * @return  Expressomail_Backend_ImapProxy
  * @throws  Expressomail_Exception_IMAPInvalidCredentials
  */
 public static function factory($_accountId, $_readOnly = FALSE)
 {
     $accountId = $_accountId instanceof Expressomail_Model_Account ? $_accountId->getId() : $_accountId;
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Getting IMAP backend for account id ' . $accountId);
     }
     if (!isset(self::$_backends[$accountId])) {
         // get imap config from account
         $account = $_accountId instanceof Expressomail_Model_Account ? $_accountId : Expressomail_Controller_Account::getInstance()->get($_accountId);
         $imapConfig = $account->getImapConfig();
         // we need to instantiate a new imap backend
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Connecting to server ' . $imapConfig['host'] . ':' . $imapConfig['port'] . ' (' . (array_key_exists('ssl', $imapConfig) ? $imapConfig['ssl'] : 'none') . ')' . ' with username ' . $imapConfig['user']);
         }
         try {
             self::$_backends[$accountId] = new Expressomail_Backend_ImapProxy($imapConfig, $_readOnly);
             if (Tinebase_Core::get(Tinebase_Core::SERVER_CLASS_NAME) !== 'ActiveSync_Server_Http') {
                 Expressomail_Controller_Account::getInstance()->updateCapabilities($account, self::$_backends[$accountId]);
             }
         } catch (Expressomail_Exception_IMAPInvalidCredentials $feiic) {
             // add account and username to Expressomail_Exception_IMAPInvalidCredentials
             $feiic->setAccount($account)->setUsername($imapConfig['user']);
             throw $feiic;
         }
     }
     return self::$_backends[$accountId];
 }
 /**
  * factory function to return a selected account/imap backend class
  *
  * @param   string|Expressomail_Model_Account $_accountId
  * @return  Expressomail_Backend_Sieve
  */
 public static function factory($_accountId)
 {
     $accountId = $_accountId instanceof Expressomail_Model_Account ? $_accountId->getId() : $_accountId;
     if (!isset(self::$_backends[$accountId])) {
         $account = $_accountId instanceof Expressomail_Model_Account ? $_accountId : Expressomail_Controller_Account::getInstance()->get($accountId);
         // get imap config from account to connect with sieve server
         $sieveConfig = $account->getSieveConfig();
         // we need to instantiate a new sieve backend
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Connecting to server ' . $sieveConfig['host'] . ':' . $sieveConfig['port'] . ' (secure: ' . (array_key_exists('ssl', $sieveConfig) && $sieveConfig['ssl'] !== FALSE ? $sieveConfig['ssl'] : 'none') . ') with user ' . $sieveConfig['username']);
         }
         $expressomailConfig = Expressomail_Config::getInstance();
         $sieveBackendDefinition = $expressomailConfig->getDefinition(Expressomail_Config::SIEVEBACKEND);
         $backendClassName = self::$_availableBackends[$sieveBackendDefinition['default']];
         $expressomailSettings = $expressomailConfig->get(Expressomail_Config::EXPRESSOMAIL_SETTINGS);
         $backendName = isset($expressomailSettings[Expressomail_Config::SIEVEBACKEND]) ? $expressomailSettings[Expressomail_Config::SIEVEBACKEND] : $sieveBackendDefinition['default'];
         if ($sieveBackendName != $sieveBackendDefinition['default']) {
             if (Tinebase_Helper::checkClassExistence(self::$_availableBackends[$backendName], true)) {
                 $backendClassName = self::$_availableBackends[$backendName];
             }
         }
         self::$_backends[$accountId] = new $backendClassName($sieveConfig);
     }
     return self::$_backends[$accountId];
 }
 /**
  * test get smtp config
  */
 public function testGetSmtpConfig()
 {
     $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
     $account = new Expressomail_Model_Account(array('type' => Expressomail_Model_Account::TYPE_SYSTEM));
     $accountSmtpConfig = $account->getSmtpConfig();
     if (array_key_exists('primarydomain', $smtpConfig)) {
         $this->assertContains($smtpConfig['primarydomain'], $accountSmtpConfig['username']);
     }
     if (TestServer::getInstance()->getConfig()->mailserver) {
         $this->assertEquals(TestServer::getInstance()->getConfig()->mailserver, $accountSmtpConfig['hostname']);
     }
 }
 /**
  * constructor
  * 
  * @param   string|Expressomail_Model_Account  $_accountId     the expressomail account id
  * @param   boolean $_readData
  */
 public function __construct($_accountId, $_readData = TRUE)
 {
     $this->_rulesBackend = new Tinebase_Backend_Sql(array('modelName' => 'Expressomail_Model_Sieve_Rule', 'tableName' => 'expressomail_sieve_rule'));
     $this->_vacationBackend = new Tinebase_Backend_Sql(array('modelName' => 'Expressomail_Model_Sieve_Vacation', 'tableName' => 'expressomail_sieve_vacation'));
     $this->_accountId = $_accountId instanceof Expressomail_Model_Account ? $_accountId->getId() : $_accountId;
     if (empty($this->_accountId)) {
         throw new Expressomail_Exception('No accountId has been set.');
     }
     if ($_readData) {
         $this->readScriptData();
     }
 }
 /**
  * factory function to return a selected account/imap backend class
  *
  * @param   string|Expressomail_Model_Account $_accountId
  * @return  Expressomail_Backend_Sieve
  */
 public static function factory($_accountId)
 {
     $accountId = $_accountId instanceof Expressomail_Model_Account ? $_accountId->getId() : $_accountId;
     if (!isset(self::$_backends[$accountId])) {
         $account = $_accountId instanceof Expressomail_Model_Account ? $_accountId : Expressomail_Controller_Account::getInstance()->get($accountId);
         // get imap config from account to connect with sieve server
         $sieveConfig = $account->getSieveConfig();
         // we need to instantiate a new sieve backend
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Connecting to server ' . $sieveConfig['host'] . ':' . $sieveConfig['port'] . ' (secure: ' . (array_key_exists('ssl', $sieveConfig) && $sieveConfig['ssl'] !== FALSE ? $sieveConfig['ssl'] : 'none') . ') with user ' . $sieveConfig['username']);
         }
         self::$_backends[$accountId] = new Expressomail_Backend_Sieve($sieveConfig);
     }
     return self::$_backends[$accountId];
 }
 /**
  * send mail via transport (smtp)
  *
  * @param Zend_Mail $_mail
  * @param Expressomail_Model_Account $_account
  * @param boolean $_saveInSent
  * @param Expressomail_Model_Message $_message
  */
 protected function _sendMailViaTransport(Zend_Mail $_mail, Expressomail_Model_Account $_account, Expressomail_Model_Message $_message = NULL, $_saveInSent = false)
 {
     $smtpConfig = $_account->getSmtpConfig();
     if (!empty($smtpConfig) && array_key_exists('hostname', $smtpConfig)) {
         $transport = new Expressomail_Transport($smtpConfig['hostname'], $smtpConfig);
         // send message via smtp
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' About to send message via SMTP ...');
         }
         Tinebase_Smtp::getInstance()->sendMessage($_mail, $transport);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' successful.');
         }
         // append mail to sent folder
         if ($_saveInSent) {
             $this->_selectSentFolder($_message, $_account);
             $this->_saveInSent($transport, $_account, $this->_getAdditionalHeaders($_message));
         }
         if ($_message !== NULL) {
             // add reply/forward flags if set
             if (!empty($_message->flags) && ($_message->flags == Zend_Mail_Storage::FLAG_ANSWERED || $_message->flags == Zend_Mail_Storage::FLAG_PASSED) && $_message->original_id instanceof Expressomail_Model_Message) {
                 Expressomail_Controller_Message_Flags::getInstance()->addFlags($_message->original_id, array($_message->flags));
             }
         }
     } else {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not send message, no smtp config found.');
     }
 }
 /**
  * Get all folders (ActiveSync Tunning)
  * @param string $accountId
  * @return array
  */
 public function getAllFoldersAS(Expressomail_Model_Account $account)
 {
     $resultArray = array();
     $accountId = $account->getId();
     foreach ($this->_getFoldersFromIMAP($account) as $folder) {
         $folderId = self::encodeFolderUid($folder['globalName'], $accountId);
         if ($folder['globalName'] == 'INBOX' || $folder['globalName'] == 'user') {
             $parentId = '0';
         } else {
             $parentId = self::encodeFolderUid(substr($folder['globalName'], 0, strrpos($folder['globalName'], self::IMAPDELIMITER)), $accountId);
         }
         $resultArray[$folderId]['folderId'] = $folderId;
         $resultArray[$folderId]['parentId'] = $parentId;
         $resultArray[$folderId]['displayName'] = $folder['localName'];
         $resultArray[$folderId]['isSelectable'] = $folder['isSelectable'];
         $resultArray[$folderId]['type'] = '';
     }
     foreach ($this->_getFoldersFromIMAP($account, '*') as $folder) {
         $folderId = self::encodeFolderUid($folder['globalName'], $accountId);
         if ($folder['globalName'] == 'INBOX' || $folder['globalName'] == 'user') {
             $parentId = '0';
         } else {
             $parentId = self::encodeFolderUid(substr($folder['globalName'], 0, strrpos($folder['globalName'], self::IMAPDELIMITER)), $accountId);
         }
         $resultArray[$folderId]['folderId'] = $folderId;
         $resultArray[$folderId]['parentId'] = $parentId;
         $resultArray[$folderId]['displayName'] = $folder['localName'];
         $resultArray[$folderId]['isSelectable'] = $folder['isSelectable'];
         $resultArray[$folderId]['type'] = '';
     }
     return $resultArray;
 }
 /**
  * invalidate cache for this account
  *
  * @param Expressomail_Model_Account $_account
  * @return void
  */
 protected function _invalidateAccountCache(Expressomail_Model_Account $_account)
 {
     Tinebase_Core::setupCache();
     $cache = Tinebase_Core::getCache();
     $cacheId = Tinebase_Core::createCacheId(array(Tinebase_Core::getUser()->accountId, $_account->getId()));
     $_filter = new Expressomail_Model_AccountFilter(array());
     //Cleans cache generate in user login, when the method seach is called with a empty filter.
     $cacheSearch = Tinebase_Core::createCacheId(array('Expressomail_Controller_Account_search', Tinebase_Core::getUser()->accountId, $_filter->toArray()));
     $cache->remove($cacheId);
     $cache->remove($cacheSearch);
 }
 /**
  * get exception data (account + username) as array
  * 
  * @return array
  */
 public function toArray()
 {
     return array('account' => $this->_account !== NULL ? $this->_account->toArray() : array(), 'username' => $this->_username);
 }
 /**
  * Checks if imapd is cyrus and if it supports ANNOTATEMORE extension
  *
  * @param Expressomail_Model_Account $_record Account Model
  * @return boolean true if imapd supports ANNOTATEMORE extension
  */
 protected function _checkSharedSeenSupport(Expressomail_Model_Account $_record)
 {
     // TODO: check cyrus imapd version
     // ANNOTATEMORE extension is a draft that originated the METADATA extension RFC5464, maybe we
     // should be using the later.
     $imapConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::IMAP, new Tinebase_Config_Struct());
     if (Tinebase_EmailUser::IMAP_CYRUS === ucfirst(strtolower($imapConfig->backend)) && $_record->hasCapability('ANNOTATEMORE')) {
         return TRUE;
     }
     return FALSE;
 }
 /**
  * test get username not email as login name
  */
 public function testGetUsernameNotEmailAsLoginName()
 {
     $imapConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::IMAP, new Tinebase_Config_Struct())->toArray();
     $account = new Expressomail_Model_Account(array('type' => Expressomail_Model_Account::TYPE_SYSTEM));
     $useUsernameAsLoginName = isset($imapConfig['useEmailAsLoginName']) ? !$imapConfig['useEmailAsLoginName'] : TRUE;
     if (!$useUsernameAsLoginName) {
         $validator = new Zend_Validate_EmailAddress();
         $this->assertFalse($validator->isValid($account->getUsername()));
     }
 }
 /**
  * test change / delete of account
  */
 public function testChangeDeleteAccount()
 {
     $system = $this->_getSystemAccount();
     unset($system['id']);
     $system['type'] = Expressomail_Model_Account::TYPE_USER;
     $account = $this->_json->saveAccount($system);
     $accountRecord = new Expressomail_Model_Account($account, TRUE);
     $accountRecord->resolveCredentials(FALSE);
     if (TestServer::getInstance()->getConfig()->mailserver) {
         $this->assertEquals(TestServer::getInstance()->getConfig()->mailserver, $account['host']);
     }
     $this->_json->changeCredentials($account['id'], $accountRecord->user, 'neuespasswort');
     $account = $this->_json->getAccount($account['id']);
     $accountRecord = new Expressomail_Model_Account($account, TRUE);
     $accountRecord->resolveCredentials(FALSE);
     $this->assertEquals('neuespasswort', $accountRecord->password);
     $this->_json->deleteAccounts($account['id']);
 }