/**
  * returns TRUE if filesystem is available
  *
  *  - value is stored in session and registry for caching
  *
  * @return boolean
  */
 public static function isFilesystemAvailable()
 {
     $isFileSystemAvailable = self::get('FILESYSTEM');
     if ($isFileSystemAvailable === null) {
         try {
             $session = Tinebase_Session::getSessionNamespace();
             if (isset($session->filesystemAvailable)) {
                 $isFileSystemAvailable = $session->filesystemAvailable;
                 self::set('FILESYSTEM', $isFileSystemAvailable);
                 return $isFileSystemAvailable;
             }
         } catch (Zend_Session_Exception $zse) {
             $session = null;
         }
         $isFileSystemAvailable = !empty(Tinebase_Core::getConfig()->filesdir) && is_writeable(Tinebase_Core::getConfig()->filesdir);
         if ($session instanceof Zend_Session_Namespace) {
             if (Tinebase_Session::isWritable()) {
                 $session->filesystemAvailable = $isFileSystemAvailable;
             }
         }
         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Filesystem available: ' . ($isFileSystemAvailable ? 'yes' : 'no'));
         }
         self::set('FILESYSTEM', $isFileSystemAvailable);
     }
     return $isFileSystemAvailable;
 }
 /**
  * Gets one entry (by id)
  *
  * @param string $_id
  * @param boolean $_useCache true to get folder from cache
  * @return Tinebase_Record_Interface
  * @throws Tinebase_Exception_NotFound
  * 
  * @todo test to do a getQuota() shoudn't be hardcoded
  * 
  */
 public function get($_id, $_useCache = TRUE)
 {
     $cache = Tinebase_Core::getCache();
     $cacheKey = $this->getFolderCacheId($_id);
     $folderFromCache = $_useCache ? $cache->load($cacheKey) : FALSE;
     if ($folderFromCache) {
         return $folderFromCache;
     }
     $folderDecoded = self::decodeFolderUid($_id);
     if (isset($folderDecoded['accountId'])) {
         $imap = Expressomail_Backend_ImapFactory::factory($folderDecoded['accountId'], TRUE);
         $account = Expressomail_Controller_Account::getInstance()->get($folderDecoded['accountId']);
         $ns_other = preg_replace('/\\/$/', '', $account->ns_other);
         $delimiter = $account->delimiter;
         $folder = $imap->getFolders('', $folderDecoded['globalName'], $this->_accounts[$folderDecoded['accountId']]);
         $acls = $imap->getFolderAcls($folderDecoded['globalName'], TRUE);
         $status = $imap->getFolderStatus($folderDecoded['globalName']);
         if ($status === FALSE) {
             // we can not access folder, create Model as unselectable
             $globalname = $folderDecoded['globalName'];
             $auxlocalname = explode(self::IMAPDELIMITER, $globalname);
             $localname = array_pop($auxlocalname);
             $translate = Tinebase_Translation::getTranslation("Expressomail");
             if (preg_match("/^user\\/[0-9]{11}\$/", $globalname)) {
                 try {
                     $aux = Tinebase_User::getInstance()->getFullUserByLoginName($localname)->toArray();
                     $localname = $aux["accountFullName"];
                 } catch (Exception $exc) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . "::" . __LINE__ . ":: Account with loginName {$localname} not found in the system for loginName to accountFullName translation.");
                 }
             }
             $newFolder = new Expressomail_Model_Folder(array('id' => $_id, 'account_id' => Tinebase_Core::getPreference('Expressomail')->{Expressomail_Preference::DEFAULTACCOUNT}, 'localname' => $localname == 'user' ? $translate->_("Shared Folders") : $localname, 'globalname' => $folderDecoded['globalName'], 'parent' => $globalname === 'user' ? '' : substr($globalname, 0, strrpos($globalname, self::IMAPDELIMITER)), 'delimiter' => self::IMAPDELIMITER, 'is_selectable' => 0, 'has_children' => 1, 'system_folder' => 1, 'imap_status' => Expressomail_Model_Folder::IMAP_STATUS_OK, 'imap_timestamp' => Tinebase_DateTime::now(), 'cache_status' => 'complete', 'cache_timestamp' => Tinebase_DateTime::now(), 'cache_job_lowestuid' => 0, 'cache_job_startuid' => 0, 'cache_job_actions_est' => 0, 'cache_job_actions_done' => 0), true);
             $newFolder->setSharingValues($acls);
             // Saving cache with tags relating to model and account_id for bulky cleaning
             $cache->save($newFolder, $cacheKey, array(self::EXPRESSOMAIL_MODEL_FOLDER, $newFolder->account_id));
             return $newFolder;
         }
         $globalName = $folderDecoded['globalName'];
         if ($globalName == 'INBOX' || $globalName == 'user') {
             $folder[$folderDecoded['globalName']]['parent'] = '';
         } else {
             $folder[$folderDecoded['globalName']]['parent'] = substr($globalName, 0, strrpos($globalName, self::IMAPDELIMITER));
         }
         /*
          * @todo must see if it is not better do this on the model directly
          */
         $systemFolders = FALSE;
         if (strtolower($globalName) === 'inbox' || strtolower($folder[$folderDecoded['globalName']]['parent']) === 'user') {
             $systemFolders = TRUE;
         } else {
             if (strtolower($folder[$folderDecoded['globalName']]['parent']) === 'inbox' && strtolower($folder[$folderDecoded['globalName']]['localName']) !== 'inbox') {
                 $systemFolders = in_array(strtolower($folder[$folderDecoded['globalName']]['localName']), Expressomail_Controller_Folder::getInstance()->getSystemFolders($folderDecoded['accountId']));
             } else {
                 if (preg_match('/^user\\/[^\\/]+$/i', $folder[$folderDecoded['globalName']]['parent'])) {
                     $systemFolders = in_array(strtolower($folder[$folderDecoded['globalName']]['localName']), Expressomail_Controller_Folder::getInstance()->getSystemFolders($folderDecoded['accountId']));
                 }
             }
         }
         $localName = Expressomail_Model_Folder::decodeFolderName($folder[$folderDecoded['globalName']]['localName']);
         if (preg_match("/^user\\/[0-9]{11}\$/", Expressomail_Model_Folder::decodeFolderName($folder[$folderDecoded['globalName']]['globalName']))) {
             try {
                 $aux = Tinebase_User::getInstance()->getFullUserByLoginName($localName)->toArray();
                 $localName = $aux["accountFullName"];
             } catch (Exception $exc) {
             }
         }
         try {
             $expressomailSession = Expressomail_Session::getSessionNamespace();
         } catch (Zend_Session_Exception $zse) {
             Tinebase_Core::getLogger()->warn(__METHOD__ . "::" . __LINE__ . ":: It was not possible to get Expressomail Session Namespace");
             $expressomailSession = null;
         }
         $userNameSpace = $imap->getUserNameSpace() . self::IMAPDELIMITER;
         $arrDecodedFolder = explode(self::IMAPDELIMITER, $folderDecoded['globalName']);
         if ($folderDecoded['globalName'] === 'INBOX' || $folderDecoded['globalName'] === 'INBOX' . self::IMAPDELIMITER . 'Arquivo Remoto' || substr($folderDecoded['globalName'], 0, strlen($userNameSpace)) === $userNameSpace && (!isset($arrDecodedFolder[2]) || $arrDecodedFolder[2] === 'Arquivo Remoto')) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Getting quota from IMAP for ' . $folderDecoded['globalName']);
             }
             $quota = $imap->getQuota($folderDecoded['globalName']);
             if ($expressomailSession instanceof Zend_Session_Namespace && Tinebase_Session::isWritable()) {
                 $expressomailSession->quota[$folderDecoded['globalName']] = $quota;
             }
         } else {
             if ($arrDecodedFolder[0] === 'INBOX' && isset($arrDecodedFolder[1]) && $arrDecodedFolder[1] === 'Arquivo Remoto') {
                 $globalNameFolder = $arrDecodedFolder[0] . self::IMAPDELIMITER . $arrDecodedFolder[1];
             } else {
                 $globalNameFolder = $arrDecodedFolder[0];
             }
             if ($arrDecodedFolder[0] !== 'INBOX') {
                 $globalNameFolder .= isset($arrDecodedFolder[1]) ? self::IMAPDELIMITER . $arrDecodedFolder[1] : '';
             }
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Getting quota from Session for ' . $folderDecoded['globalName']);
             }
             $quota = 0;
             if ($expressomailSession instanceof Zend_Session_Namespace && isset($expressomailSession->quota) && isset($expressomailSession->quota[$globalNameFolder])) {
                 $quota = $expressomailSession->quota[$globalNameFolder];
             }
         }
         $return = new Expressomail_Model_Folder(array('id' => $_id, 'account_id' => $folderDecoded['accountId'], 'localname' => $localName, 'globalname' => Expressomail_Model_Folder::decodeFolderName($folder[$folderDecoded['globalName']]['globalName']), 'parent' => Expressomail_Model_Folder::decodeFolderName($folder[$folderDecoded['globalName']]['parent']), 'delimiter' => $folder[$folderDecoded['globalName']]['delimiter'], 'is_selectable' => $folder[$folderDecoded['globalName']]['isSelectable'], 'has_children' => $folder[$folderDecoded['globalName']]['hasChildren'], 'system_folder' => $systemFolders, 'imap_status' => Expressomail_Model_Folder::IMAP_STATUS_OK, 'imap_uidvalidity' => $status['uidvalidity'], 'imap_totalcount' => array_key_exists('messages', $status) ? $status['messages'] : '', 'imap_timestamp' => Tinebase_DateTime::now(), 'cache_status' => 'complete', 'cache_totalcount' => array_key_exists('messages', $status) ? $status['messages'] : '', 'cache_recentcount' => array_key_exists('recent', $status) ? $status['recent'] : '', 'cache_unreadcount' => array_key_exists('unseen', $status) ? $status['unseen'] : '', 'cache_timestamp' => Tinebase_DateTime::now(), 'cache_job_lowestuid' => 0, 'cache_job_startuid' => 0, 'cache_job_actions_est' => 0, 'cache_job_actions_done' => 0, 'quota_usage' => !empty($quota) ? $quota['STORAGE']['usage'] : 0, 'quota_limit' => !empty($quota) ? $quota['STORAGE']['limit'] : 0), true);
         $return->setSharingValues($acls);
         // Saving cache with tags relating to model and account_id for bulky cleaning
         $cache->save($return, $cacheKey, array(self::EXPRESSOMAIL_MODEL_FOLDER, $return->account_id));
         return $return;
     }
 }