/**
  * 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];
 }
 /**
  * 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];
 }
 /**
  * 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);
 }
 /**
  * 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 = $this->_createExpressomailModelAccountCacheId($_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 = $this->_createExpressomailControllerAccountSearchCacheId($_filter);
     $cache->remove($cacheId);
     $cache->remove($cacheSearch);
 }