/**
  * Tears down the fixture
  * This method is called after a test is executed.
  *
  * @access protected
  */
 protected function tearDown()
 {
     foreach ($this->_createdFolders as $foldername) {
         $this->_controller->delete($this->_account->getId(), $foldername);
     }
     // delete all remaining folders from cache of account
     $folderBackend = new Expressomail_Backend_Folder();
     // TODO delete folders
     $folders = array();
     foreach ($folders as $folder) {
         $folderBackend->delete($folder);
     }
 }
 /**
  * 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 all folders for ActiveSync
  *
  * @param string|Expressomail_Model_Account $_account
  * @return array
  */
 public function getFoldersAS($_account)
 {
     $account = $_account instanceof Expressomail_Model_Account ? $_account : Expressomail_Controller_Account::getInstance()->get($_account);
     return $this->_backend->getAllFoldersAS($account);
 }
 /**
  * Update folderBackend imapstatus
  * @param Syncroton_Model_IFolder $folder
  * @return true | false | null on exception
  */
 public function setFolderImapStatus(Syncroton_Model_IFolder $folder)
 {
     $folderDecoded = Expressomail_Backend_Folder::decodeFolderUid($folder->bigfolderid);
     $imapStatus = Expressomail_Controller_Folder::getFolderImapStatus($folderDecoded['accountId'], $folderDecoded['globalName']);
     if ($imapStatus === false) {
         return null;
     }
     // Exception Raised
     if (serialize($imapStatus) !== $folder->imapstatus) {
         $folderBackend = Syncroton_Registry::getFolderBackend();
         $folder->imapstatus = serialize($imapStatus);
         $updateStatus = $folderBackend->update($folder);
         return true;
     }
     return false;
 }
 /**
  * get folder ids of all inboxes for accounts of current user
  *
  * @return array
  */
 protected function _getFolderIdsOfAllInboxes()
 {
     $accounts = Expressomail_Controller_Account::getInstance()->search();
     $folderFilter = new Expressomail_Model_FolderFilter(array(array('field' => 'account_id', 'operator' => 'in', 'value' => $accounts->getArrayOfIds()), array('field' => 'localname', 'operator' => 'equals', 'value' => 'INBOX')));
     $folderBackend = new Expressomail_Backend_Folder();
     $folderIds = $folderBackend->search($folderFilter, NULL, TRUE);
     return $folderIds;
 }
 /**
  * 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;
 }
 /**
  * Compare order of Expressomail_Model_Message acording to Tinebase_Model_Pagination
  * @param Expressomail_Model_Message $msg1
  * @param Expressomail_Model_Message $msg2
  * @return int
  *
  * @todo Convert int security value in Expressomail_Smime to corresponding string type
  */
 public function compare($msg1, $msg2)
 {
     switch ($this->_pagination->sort) {
         case 'received':
             // Integer
             $value1 = $msg1[$this->_pagination->sort];
             $value2 = $msg2[$this->_pagination->sort];
         case 'sent':
             // Integer
             $value1 = isset($value1) ? $value1 : $msg1['header']['date'];
             $value2 = isset($value2) ? $value2 : $msg2['header']['date'];
             $value1 = intval(Expressomail_Message::convertDate($value1)->format("U"));
             $value2 = intval(Expressomail_Message::convertDate($value2)->format("U"));
         case 'size':
             // Integer
             $value1 = isset($value1) ? $value1 : intval($msg1[$this->_pagination->sort]);
             $value2 = isset($value2) ? $value2 : intval($msg2[$this->_pagination->sort]);
             return $this->compareIntegers($value1, $value2);
         case 'folder_id':
             // Strings
             $folders = array();
             $translate = Tinebase_Translation::getTranslation('Expressomail');
             foreach (array($msg1, $msg2) as $msg) {
                 $folder = Expressomail_Backend_Folder::decodeFolderUid($msg[$this->_pagination->sort]);
                 // Optimization! Only create the account object once for every sort operation.
                 $account = array_key_exists($folder['accountId'], $this->_accountMap) ? $this->_accountMap[$folder['accountId']] : ($this->_accountMap[$folder['accountId']] = Expressomail_Controller_Account::getInstance()->get($folder['accountId']));
                 $aux1 = explode('/', $folder['globalName']);
                 $aux2 = '';
                 foreach ($aux1 as $value) {
                     $aux2 .= $translate->_($value) . '/';
                 }
                 $folders[] = $account->name . '/' . substr($aux2, 0, strlen($aux2) - 1);
             }
             list($value1, $value2) = $folders;
             //TODO: Should use a static method implemented on Model_Message or Expressomail_Smime
         //TODO: Should use a static method implemented on Model_Message or Expressomail_Smime
         case 'smime':
             $value1 = isset($value1) ? $value1 : $this->processSmimeValue($msg1['structure']);
             $value2 = isset($value2) ? $value2 : $this->processSmimeValue($msg2['structure']);
         case 'flags':
             // Strings
             if (!isset($value1)) {
                 sort($msg1['flags']);
                 $value1 = implode(',', $msg1['flags']);
             }
             if (!isset($value2)) {
                 sort($msg2['flags']);
                 $value2 = implode(',', $msg2['flags']);
             }
         case 'subject':
             // Strings
             $value1 = isset($value1) ? $value1 : $msg1['header'][$this->_pagination->sort];
             $value2 = isset($value2) ? $value2 : $msg2['header'][$this->_pagination->sort];
         case 'id':
             // Strings
             $value1 = isset($value1) ? $value1 : $msg1[$this->_pagination->sort];
             $value2 = isset($value2) ? $value2 : $msg2[$this->_pagination->sort];
             return $this->compareStrings($value1, $value2);
         case 'sender':
         case 'to':
         case 'from_name':
         case 'from_email':
             // Strings
             list($header, $field) = explode('_', $this->_pagination->sort);
             $field = empty($field) ? 'email' : $field;
             $address1 = Expressomail_Message::convertAddresses($msg1['header'][$header]);
             $address2 = Expressomail_Message::convertAddresses($msg2['header'][$header]);
             return $this->compareStrings(isset($address1[0]) && array_key_exists($field, $address1[0]) ? $address1[0][$field] : '', isset($address2[0]) && array_key_exists($field, $address2[0]) ? $address2[0][$field] : '');
     }
 }