/**
  * factory function to return a selected account/imap backend class
  *
  * @param   string|Felamimail_Model_Account $_accountId
  * @return  Felamimail_Backend_ImapProxy
  * @throws  Felamimail_Exception_IMAPInvalidCredentials
  */
 public static function factory($_accountId)
 {
     $accountId = $_accountId instanceof Felamimail_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 Felamimail_Model_Account ? $_accountId : Felamimail_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 Felamimail_Backend_ImapProxy($imapConfig);
             Felamimail_Controller_Account::getInstance()->updateCapabilities($account, self::$_backends[$accountId]);
         } catch (Felamimail_Exception_IMAPInvalidCredentials $feiic) {
             // add account and username to Felamimail_Exception_IMAPInvalidCredentials
             $feiic->setAccount($account)->setUsername($imapConfig['user']);
             throw $feiic;
         }
     }
     return self::$_backends[$accountId];
 }
 /**
  * test get smtp config
  */
 public function testGetSmtpConfig()
 {
     $smtpConfig = Tinebase_Config::getInstance()->getConfigAsArray(Tinebase_Config::SMTP);
     $account = new Felamimail_Model_Account(array('type' => Felamimail_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|Felamimail_Model_Account  $_accountId     the felamimail account id
  * @param   boolean $_readData
  */
 public function __construct($_accountId, $_readData = TRUE)
 {
     $this->_rulesBackend = new Tinebase_Backend_Sql(array('modelName' => 'Felamimail_Model_Sieve_Rule', 'tableName' => 'felamimail_sieve_rule'));
     $this->_vacationBackend = new Tinebase_Backend_Sql(array('modelName' => 'Felamimail_Model_Sieve_Vacation', 'tableName' => 'felamimail_sieve_vacation'));
     $this->_accountId = $_accountId instanceof Felamimail_Model_Account ? $_accountId->getId() : $_accountId;
     if (empty($this->_accountId)) {
         throw new Felamimail_Exception('No accountId has been set.');
     }
     if ($_readData) {
         $this->readScriptData();
     }
 }
 /**
  * test get smtp config
  */
 public function testGetSmtpConfig()
 {
     $this->markTestSkipped('this test has to be refactored');
     $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
     $account = new Felamimail_Model_Account(array('type' => Felamimail_Model_Account::TYPE_SYSTEM));
     $accountSmtpConfig = $account->getSmtpConfig();
     if (isset($smtpConfig['primarydomain']) || array_key_exists('primarydomain', $smtpConfig)) {
         $this->assertContains($smtpConfig['primarydomain'], $accountSmtpConfig['username']);
     }
     if (TestServer::getInstance()->getConfig()->mailserver) {
         $this->assertEquals(TestServer::getInstance()->getConfig()->mailserver, $accountSmtpConfig['hostname']);
     }
 }
 /**
  * factory function to return a selected account/imap backend class
  *
  * @param   string|Felamimail_Model_Account $_accountId
  * @return  Felamimail_Backend_Sieve
  */
 public static function factory($_accountId)
 {
     $accountId = $_accountId instanceof Felamimail_Model_Account ? $_accountId->getId() : $_accountId;
     if (!isset(self::$_backends[$accountId])) {
         $account = $_accountId instanceof Felamimail_Model_Account ? $_accountId : Felamimail_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: ' . ((isset($sieveConfig['ssl']) || array_key_exists('ssl', $sieveConfig)) && $sieveConfig['ssl'] !== FALSE ? $sieveConfig['ssl'] : 'none') . ') with user ' . $sieveConfig['username']);
         }
         self::$_backends[$accountId] = new Felamimail_Backend_Sieve($sieveConfig);
     }
     return self::$_backends[$accountId];
 }
 /**
  * sieve test helper
  *
  * @param array $_sieveData
  * @return array
  */
 protected function _sieveTestHelper($_sieveData, $_isMime = FALSE)
 {
     $this->_setTestScriptname();
     // check which save fn to use
     if (isset($_sieveData['reason']) || array_key_exists('reason', $_sieveData)) {
         $resultSet = $this->_json->saveVacation($_sieveData);
         $this->assertEquals($this->_account->email, $resultSet['addresses'][0]);
         $_sieveBackend = Felamimail_Backend_SieveFactory::factory($this->_account->getId());
         if (preg_match('/dbmail/i', $_sieveBackend->getImplementation())) {
             $translate = Tinebase_Translation::getTranslation('Felamimail');
             $this->assertEquals(sprintf($translate->_('Out of Office reply from %1$s'), Tinebase_Core::getUser()->accountFullName), $resultSet['subject']);
         } else {
             $this->assertEquals($_sieveData['subject'], $resultSet['subject']);
         }
         if ($_isMime) {
             $this->assertEquals(html_entity_decode('unittest vacation message', ENT_NOQUOTES, 'UTF-8'), $resultSet['reason']);
         } else {
             $this->assertEquals($_sieveData['reason'], $resultSet['reason']);
         }
     } else {
         if (isset($_sieveData[0]['action_type']) || array_key_exists('action_type', $_sieveData[0])) {
             $resultSet = $this->_json->saveRules($this->_account->getId(), $_sieveData);
             $this->assertEquals($_sieveData, $resultSet);
         }
     }
     return $resultSet;
 }
 /**
  * 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;
 }
 /**
  * test reset account capabilities
  */
 public function testResetAccountCapabilities()
 {
     $capabilities = $this->_controller->updateCapabilities($this->_account);
     $account = clone $this->_account;
     $account->host = 'unittest.org';
     $account->type = Felamimail_Model_Account::TYPE_USER;
     $this->_controller->update($account);
     $this->assertFalse(array_key_exists($this->_account->getId(), Felamimail_Session::getSessionNamespace()->account), print_r(Felamimail_Session::getSessionNamespace()->account, TRUE));
 }
 /**
  * test reset account capabilities
  */
 public function testResetAccountCapabilities()
 {
     $capabilities = $this->_controller->updateCapabilities($this->_account);
     $account = clone $this->_account;
     $account->host = 'unittest.org';
     $account->type = Felamimail_Model_Account::TYPE_USER;
     $this->_controller->update($account);
     $this->assertFalse(array_key_exists($this->_account->getId(), $_SESSION['Felamimail']), print_r($_SESSION['Felamimail'], TRUE));
 }
 /**
  * remove folders from cache that no longer exist on the imap server
  * 
  * @param Felamimail_Model_Account $_account
  * @param string $_parentFolder
  * @param array $_imapFolderIds if empty, remove all found cached folders
  */
 protected function _removeFromCache(Felamimail_Model_Account $_account, $_parentFolder = NULL, $_imapFolderIds = array())
 {
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . '');
     }
     $filterData = array(array('field' => 'account_id', 'operator' => 'equals', 'value' => $_account->getId()));
     if ($_parentFolder !== NULL) {
         $filterData[] = array('field' => 'parent', 'operator' => 'equals', 'value' => $_parentFolder);
     }
     $filter = new Felamimail_Model_FolderFilter($filterData);
     $cachedFolderIds = $this->_backend->search($filter, NULL, TRUE);
     if (count($cachedFolderIds) > count($_imapFolderIds)) {
         // remove folders from cache
         $idsToRemove = array_diff($cachedFolderIds, $_imapFolderIds);
         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Removing ' . count($idsToRemove) . ' folders from cache.');
         }
         $this->delete($idsToRemove);
     }
 }
 /**
  * send mail via transport (smtp)
  * 
  * @param Zend_Mail $_mail
  * @param Felamimail_Model_Account $_account
  * @param boolean $_saveInSent
  * @param Felamimail_Model_Message $_message
  * @param array $_nonPrivateRecipients
  */
 protected function _sendMailViaTransport(Zend_Mail $_mail, Felamimail_Model_Account $_account, Felamimail_Model_Message $_message = null, $_saveInSent = false, $_nonPrivateRecipients = array())
 {
     $smtpConfig = $_account->getSmtpConfig();
     if (!empty($smtpConfig) && (isset($smtpConfig['hostname']) || array_key_exists('hostname', $smtpConfig))) {
         $transport = new Felamimail_Transport($smtpConfig['hostname'], $smtpConfig);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             $debugConfig = $smtpConfig;
             $whiteList = array('hostname', 'username', 'port', 'auth', 'ssl');
             foreach ($debugConfig as $key => $value) {
                 if (!in_array($key, $whiteList)) {
                     unset($debugConfig[$key]);
                 }
             }
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' About to send message via SMTP with the following config: ' . print_r($debugConfig, true));
         }
         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->_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 Felamimail_Model_Message) {
                 Felamimail_Controller_Message_Flags::getInstance()->addFlags($_message->original_id, array($_message->flags));
             }
             // add email notes to contacts (only to/cc)
             if ($_message->note) {
                 $this->_addEmailNote($_nonPrivateRecipients, $_message->subject, $_message->getPlainTextBody());
             }
         }
     } else {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not send message, no smtp config found.');
     }
 }
Beispiel #12
0
 /**
  * set vacation active field for account
  * 
  * @param string|Felamimail_Model_Account $_account
  * @param boolean $_vacationEnabled
  * @return Felamimail_Model_Account
  */
 public function setVacationActive(Felamimail_Model_Account $_account, $_vacationEnabled)
 {
     $account = $this->get($_account->getId());
     if ($account->sieve_vacation_active != $_vacationEnabled) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating sieve_vacation_active = ' . $_vacationEnabled . ' for account: ' . $account->name);
         }
         $account->sieve_vacation_active = (bool) $_vacationEnabled;
         // skip all special update handling
         $account = $this->_backend->update($account);
     }
     return $account;
 }
 /**
  * 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);
 }
 /**
  * get folder filter
  *
  * @return Felamimail_Model_FolderFilter
  */
 protected function _getFolderFilter($_globalname = 'INBOX')
 {
     return new Expressomail_Model_FolderFilter(array(array('field' => 'globalname', 'operator' => 'equals', 'value' => $_globalname), array('field' => 'account_id', 'operator' => 'equals', 'value' => $this->_account->getId())));
 }
 /**
  * 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);
 }
Beispiel #16
0
 /**
  * send mail via transport (smtp)
  * 
  * @param Zend_Mail $_mail
  * @param Felamimail_Model_Account $_account
  * @param boolean $_saveInSent
  * @param Felamimail_Model_Message $_message
  * @param array $_nonPrivateRecipients
  */
 protected function _sendMailViaTransport(Zend_Mail $_mail, Felamimail_Model_Account $_account, Felamimail_Model_Message $_message = NULL, $_saveInSent = false, $_nonPrivateRecipients = array())
 {
     $smtpConfig = $_account->getSmtpConfig();
     if (!empty($smtpConfig) && array_key_exists('hostname', $smtpConfig)) {
         $transport = new Felamimail_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->_saveInSent($transport, $_account, $_message !== NULL ? array('Bcc' => $_message->bcc) : array());
         }
         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 Felamimail_Model_Message) {
                 Felamimail_Controller_Message_Flags::getInstance()->addFlags($_message->original_id, array($_message->flags));
             }
             // add email notes to contacts (only to/cc)
             if ($_message->note) {
                 $this->_addEmailNote($_nonPrivateRecipients, $_message->subject, $_message->getPlainTextBody());
             }
         }
     } else {
         Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not send message, no smtp config found.');
     }
 }