/**
  * Returns instance of Tinebase_Config
  *
  * @return Tinebase_Config
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * get available vacation message templates
  * 
  * @return array
  */
 public function getVacationMessageTemplates()
 {
     return $this->getTemplates(Felamimail_Config::getInstance()->{Felamimail_Config::VACATION_TEMPLATES_CONTAINER_ID});
 }
 /**
  * testGetRegistryData
  *
  * @see 0010251: do not send unused config data to client
  */
 public function testGetRegistryData()
 {
     $regData = $this->_json->getRegistryData();
     $this->assertFalse(isset($regData['defaults']));
     $supportedFlags = Felamimail_Config::getInstance()->featureEnabled(Felamimail_Config::FEATURE_TINE20_FLAG) ? 6 : 5;
     $this->assertEquals($supportedFlags, $regData['supportedFlags']['totalcount']);
 }
 /**
  * save message in tinebase cache
  * - only cache message headers if received during the last day
  * 
  * @param Felamimail_Model_Message $_message
  * @param Felamimail_Model_Folder $_folder
  * @param array $_messageData
  * 
  * @todo do we need the headers in the Tinebase cache?
  */
 protected function _saveMessageInTinebaseCache(Felamimail_Model_Message $_message, Felamimail_Model_Folder $_folder, $_messageData)
 {
     if (!$_message->received->isLater(Tinebase_DateTime::now()->subDay(3))) {
         return;
     }
     $memory = function_exists('memory_get_peak_usage') ? memory_get_peak_usage(true) : memory_get_usage(true);
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' caching message ' . $_message->getId() . ' / memory usage: ' . $memory / 1024 / 1024 . ' MBytes');
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_message->toArray(), TRUE));
     }
     $cacheId = 'getMessageHeaders' . $_message->getId();
     Tinebase_Core::getCache()->save($_messageData['header'], $cacheId, array('getMessageHeaders'));
     // prefetch body to cache
     if (Felamimail_Config::getInstance()->get(Felamimail_Config::CACHE_EMAIL_BODY, TRUE) && $_message->size < $this->_maxMessageSizeToCacheBody) {
         $account = Felamimail_Controller_Account::getInstance()->get($_folder->account_id);
         $mimeType = $account->display_format == Felamimail_Model_Account::DISPLAY_HTML || $account->display_format == Felamimail_Model_Account::DISPLAY_CONTENT_TYPE ? Zend_Mime::TYPE_HTML : Zend_Mime::TYPE_TEXT;
         Felamimail_Controller_Message::getInstance()->getMessageBody($_message, null, $mimeType, $account);
     }
 }
 /**
  * get message body
  * 
  * @param string|Felamimail_Model_Message $_messageId
  * @param string $_partId
  * @param string $_contentType
  * @param Felamimail_Model_Account $_account
  * @return string
  */
 public function getMessageBody($_messageId, $_partId, $_contentType, $_account = NULL)
 {
     $message = $_messageId instanceof Felamimail_Model_Message ? $_messageId : $this->get($_messageId);
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Get Message body (part: ' . $_partId . ') of message id ' . $message->getId() . ' (content type ' . $_contentType . ')');
     }
     $cacheBody = Felamimail_Config::getInstance()->get(Felamimail_Config::CACHE_EMAIL_BODY, TRUE);
     if ($cacheBody) {
         $cache = Tinebase_Core::getCache();
         $cacheId = $this->_getMessageBodyCacheId($message, $_partId, $_contentType, $_account);
         if ($cache->test($cacheId)) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Getting Message from cache.');
             }
             return $cache->load($cacheId);
         }
     }
     $messageBody = $this->_getAndDecodeMessageBody($message, $_partId, $_contentType, $_account);
     // activate garbage collection (@see 0008216: HTMLPurifier/TokenFactory.php : Allowed memory size exhausted)
     $cycles = gc_collect_cycles();
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Current mem usage after gc_collect_cycles(' . $cycles . ' ): ' . memory_get_usage() / 1024 / 1024);
     }
     if ($cacheBody) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Put message body into Tinebase cache (for 24 hours).');
         }
         $cache->save($messageBody, $cacheId, array('getMessageBody'), 86400);
     }
     return $messageBody;
 }
 /**
  * get vacation message from template file
  * 
  * @param string $templateId
  * @return string
  * 
  * @todo generalize and move to Tinebase_FileSystem / Node controller
  */
 protected function _getMessageFromTemplateFile($templateId)
 {
     $template = Tinebase_FileSystem::getInstance()->searchNodes(new Tinebase_Model_Tree_Node_Filter(array(array('field' => 'id', 'operator' => 'equals', 'value' => $templateId))))->getFirstRecord();
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($template->toArray(), TRUE));
     }
     $templateContainer = Tinebase_Container::getInstance()->getContainerById(Felamimail_Config::getInstance()->{Felamimail_Config::VACATION_TEMPLATES_CONTAINER_ID});
     $path = Tinebase_FileSystem::getInstance()->getContainerPath($templateContainer) . '/' . $template->name;
     $templateHandle = Tinebase_FileSystem::getInstance()->fopen($path, 'r');
     $message = stream_get_contents($templateHandle);
     Tinebase_FileSystem::getInstance()->fclose($templateHandle);
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $message);
     }
     return $message;
 }
 /**
  * open socket connection
  *
  * @param $host
  * @param $port
  * @param $errno
  * @param $errstr
  * @return resource
  */
 protected function _openSocket($host, $port, &$errno, &$errstr)
 {
     if (Felamimail_Config::getInstance()->get(Felamimail_Config::IMAP_ALLOW_SELF_SIGNED_TLS_CERT)) {
         $result = @stream_socket_client($host . ':' . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, stream_context_create(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true))));
     } else {
         $result = @fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
     }
     return $result;
 }
 /**
  * tine20FlagEnabled
  *
  * @param array|Felamimail_Model_Message $message
  * @return bool
  */
 public function tine20FlagEnabled($message = null)
 {
     if (Felamimail_Config::getInstance()->featureEnabled(Felamimail_Config::FEATURE_TINE20_FLAG)) {
         if ($message && isset($message['header']['user-agent'])) {
             $userAgentHeader = $message['header']['user-agent'];
             foreach ((array) $userAgentHeader as $userAgent) {
                 if (strpos($userAgent, "Tine 2.0") !== false) {
                     return true;
                 }
             }
         }
     }
     return false;
 }
 /**
  * use html purifier to remove 'bad' tags/attributes from html body
  *
  * @param string $_content
  * @param string $messageId
  * @return string
  */
 protected function _purifyBodyContent($_content, $messageId)
 {
     if (!defined('HTMLPURIFIER_PREFIX')) {
         define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/../../library/HTMLPurifier'));
     }
     $config = Tinebase_Core::getConfig();
     $path = $config->caching && $config->caching->active && $config->caching->path ? $config->caching->path : Tinebase_Core::getTempDir();
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Purifying html body. (cache path: ' . $path . ')');
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Current mem usage before purify: ' . memory_get_usage() / 1024 / 1024);
     }
     // add custom schema for passing message id to URIScheme
     $configSchema = HTMLPurifier_ConfigSchema::makeFromSerial();
     $configSchema->add('Felamimail.messageId', NULL, 'string', TRUE);
     $config = HTMLPurifier_Config::create(NULL, $configSchema);
     $config->set('HTML.DefinitionID', 'purify message body contents');
     $config->set('HTML.DefinitionRev', 1);
     // @see: http://htmlpurifier.org/live/configdoc/plain.html#Attr.EnableID
     $config->set('Attr.EnableID', TRUE);
     $config->set('Attr.IDPrefix', 'felamimail_inline_');
     // @see: http://htmlpurifier.org/live/configdoc/plain.html#HTML.TidyLevel
     $config->set('HTML.TidyLevel', 'heavy');
     // some config values to consider
     /*
     $config->set('Attr.EnableID', true);
     $config->set('Attr.ClassUseCDATA', true);
     $config->set('CSS.AllowTricky', true);
     */
     $config->set('Cache.SerializerPath', $path);
     $config->set('URI.AllowedSchemes', array('http' => true, 'https' => true, 'mailto' => true, 'data' => true, 'cid' => true));
     $config->set('Felamimail.messageId', $messageId);
     $this->_transformBodyTags($config);
     // add uri filter
     // TODO could be improved by adding on demand button if loading external resources is allowed
     //   or only load uris of known recipients
     if (Felamimail_Config::getInstance()->get(Felamimail_Config::FILTER_EMAIL_URIS)) {
         $uri = $config->getDefinition('URI');
         $uri->addFilter(new Felamimail_HTMLPurifier_URIFilter_TransformURI(), $config);
     }
     // add cid uri scheme
     require_once dirname(dirname(__FILE__)) . '/HTMLPurifier/URIScheme/cid.php';
     $purifier = new HTMLPurifier($config);
     $content = $purifier->purify($_content);
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Current mem usage after purify: ' . memory_get_usage() / 1024 / 1024);
     }
     return $content;
 }