Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
fix(Felamimail/Message): fix structure caching for non-multipart mails
Browse files Browse the repository at this point in the history
  • Loading branch information
pschuele committed Sep 30, 2021
1 parent 5b8a73f commit ec25157
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 43 deletions.
18 changes: 12 additions & 6 deletions tests/tine20/Felamimail/Controller/MessageTest.php
@@ -1,4 +1,5 @@
<?php

/**
* Tine 2.0 - http://www.tine20.org
*
Expand All @@ -10,12 +11,7 @@
*/

/**
* Test helper
*/
require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';

/**
* Test class for Tinebase_Group
* Test class for Felamimail_Controller_Message
*/
class Felamimail_Controller_MessageTest extends Felamimail_TestCase
{
Expand Down Expand Up @@ -1814,4 +1810,14 @@ public function testRewriteMessageSubject2()
'flags mismatch: ' . print_r($rewrittenMessageFromCache->toArray(), true));

}

public function testNonMultipartStructure()
{
$cachedMessage = $this->messageTestHelper('non_multipart.eml');
$message = $this->_getController()->getCompleteMessage($cachedMessage, null, Zend_Mime::TYPE_TEXT);

$this->assertStringContainsString('111111', $message->body);
$this->assertEquals('text/html', $message->structure['contentType'], print_r($message->structure, true));
$this->assertEquals(1, print_r($message->structure['partId'], true));
}
}
20 changes: 20 additions & 0 deletions tests/tine20/Felamimail/files/non_multipart.eml
@@ -0,0 +1,20 @@
Return-Path: <info@somemail.com>
Received: from mail1.somemail.com ([127.0.0.1])
by localhost (mail1.somemail.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id v71GBLEcEudf for <info@domain.de>;
Tue, 7 Feb 2017 18:13:39 +0000 (UTC)
Received: from localhost (unknown [181.122.199.184])
by mail1.somemail.com (Postfix) with ESMTPA id C5A7B82A43
for <info@domain.de>; Tue, 7 Feb 2017 18:13:38 +0000 (UTC)
To: info@domain.de
From: Gelbes Branchenbuch <info@somemail.com>
Date: Tue, 07 Feb 2017 18:12:28 +0000
Message-Id: <1486491148-d777f4e4d090c3d7ea13816f80cb8131Gelbes Branchenbuch
<info@somemail.com>>
X-Tine20TestMessage: non_multipart.eml
Subject: Notification
MIME-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<b>Code:&nbsp;</b>111111
92 changes: 55 additions & 37 deletions tine20/Felamimail/Model/Message.php
Expand Up @@ -280,7 +280,7 @@ protected function _getBodyContent($messagePart, &$data)
/**
* gets record related properties
*
* @param string _name of property
* @param string $_name of property
* @throws Tinebase_Exception_UnexpectedValue
* @return mixed value of property
*/
Expand All @@ -302,48 +302,71 @@ public function __get($_name)
*/
protected function _fetchStructure()
{
$cacheId = $this->_getStructureCacheId();
if ($structureFromCache = $this->_getStructureFromCache()) {
return $structureFromCache;
}

if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(
__METHOD__ . '::' . __LINE__ . ' Getting message structure from IMAP server.');

$result = array();
if ($this->_structureFetchCount < 5) {
try {
$this->_structureFetchCount++;
$summary = Felamimail_Controller_Cache_Message::getInstance()->getMessageSummary($this->messageuid, $this->account_id, $this->folder_id);
$result = $summary['structure'];
} catch (Zend_Mail_Protocol_Exception $zmpe) {
// imap server might have gone away
Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__
. ' IMAP protocol error during summary fetching: ' . $zmpe->getMessage());
}
} else {
if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) Tinebase_Core::getLogger()->warn(
__METHOD__ . '::' . __LINE__ . ' Message structure fetching failed (5 times) ... message might be broken on IMAP server');
$summary = Felamimail_Controller_Cache_Message::getInstance()->getMessageSummary($this->messageuid, $this->account_id, $this->folder_id);
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(
__METHOD__ . '::' . __LINE__ . ' broken summary: ' . print_r($summary, true));
}
$this->_setStructure($result);
return $result;
}

/**
* @return array|null
*/
protected function _getStructureFromCache()
{
$cache = Tinebase_Core::getCache();
$cacheId = $this->_getStructureCacheId();
if ($cache->test($cacheId)) {
if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(
__METHOD__ . '::' . __LINE__ . ' Getting message structure from cache: ' . $cacheId);
$result = $cache->load($cacheId);
return $cache->load($cacheId);
} else {
if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(
__METHOD__ . '::' . __LINE__ . ' Getting message structure from IMAP server.');

$result = array();
if ($this->_structureFetchCount < 5) {
try {
$this->_structureFetchCount++;
$summary = Felamimail_Controller_Cache_Message::getInstance()->getMessageSummary($this->messageuid, $this->account_id, $this->folder_id);
$result = $summary['structure'];
} catch (Zend_Mail_Protocol_Exception $zmpe) {
// imap server might have gone away
Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__
. ' IMAP protocol error during summary fetching: ' . $zmpe->getMessage());
}
} else {
if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) Tinebase_Core::getLogger()->warn(
__METHOD__ . '::' . __LINE__ . ' Message structure fetching failed (5 times) ... message might be broken on IMAP server');
$summary = Felamimail_Controller_Cache_Message::getInstance()->getMessageSummary($this->messageuid, $this->account_id, $this->folder_id);
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(
__METHOD__ . '::' . __LINE__ . ' broken summary: ' . print_r($summary, true));
// maybe we find the structure of part 1 (non-multipart message)
$cacheId = $this->_getStructureCacheId([
'partId' => 1,
]);
if ($cache->test($cacheId)) {
if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(
__METHOD__ . '::' . __LINE__ . ' Getting message structure from cache: ' . $cacheId);
return $cache->load($cacheId);
}
$this->_setStructure($result);
}
return $result;

return null;
}

/**
* get cache id for structure
*
*
* @param array $structure
* @return string
*/
protected function _getStructureCacheId()
protected function _getStructureCacheId($structure = [])
{
return 'messageStructure' . $this->folder_id . $this->messageuid;
$partId = ! empty($structure['partId']) ? $structure['partId'] : 0;
return 'messageStructure' . $this->folder_id . '_' . $this->messageuid . '_' . $partId;
}

/**
Expand All @@ -355,13 +378,8 @@ protected function _setStructure($structure)
{
if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(
__METHOD__ . '::' . __LINE__ . ' structure: ' . print_r($structure, true));
if (! empty($structure['partId'])) {
if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
. ' Don\'t cache structure of subparts');
return;
}

$cacheId = $this->_getStructureCacheId();

$cacheId = $this->_getStructureCacheId($structure);

if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Caching message structure: ' . $cacheId);
Tinebase_Core::getCache()->save($structure, $cacheId, array('messageStructure'), 86400); // 24 hours
Expand Down

0 comments on commit ec25157

Please sign in to comment.