[dev] Fwd: Hi Jan, I have a question regarding the use of Horde_Stream_Temp when fetching headers

Michael J Rubinsky mrubinsk at horde.org
Tue May 17 13:20:13 UTC 2016


Quoting Jan Schneider <jan at horde.org>:

> ----- Weitergeleitete Nachricht von Herr Klaus Leithoff Stylite AG -----
>   Datum: Tue, 17 May 2016 10:24:46 +0200
> Betreff: Hi Jan, I have a question regarding the use of  
> Horde_Stream_Temp when fetching headers
>      An: Jan Schneider <jan at horde.org>
>
> Hi Jan,
> I have a question regarding the use of Horde_Stream_Temp when  
> fetching headers.
> Outline:
> In our EGroupare-Mail-Module, we implemented the possibility to have  
> a "preview"
> of the first few lines of a mail (like most mobile apps do it) in  
> the list of available
> mails. To achieve that, together with an
> fquery = new Horde_Imap_Client_Fetch_Query();
> fquery->headers(...)
> we query for
> fquery->fullText(peek=>true, length=>5000, start=>0)
>
> when actually retrieving the headers, I bump into a Fatal, when accessing my
> googlemail-account.Imap/Client/Data/Fetch.php Line 628 Call to  
> undefined method Horde_Stream_Temp::toString() (this is within  
> _getHeaders(...) ) we use pear-pear.horde.org/Horde_Imap_Client
> Version 2.29.6
>
> I worked around the problem with:
> Horde_Imap_Client/Horde/Imap/Client/Data/Fetch.php Line 607
> return  
> is_resource($this->_data[$key][$id]->stream)?Horde_Mime_Headers::parseHeaders(stream_get_contents($this->_data[$key][$id]->stream,-1,0)):clone  
> $this->_data[$key][$id];
>
> and Line 628
> return  
> is_resource($this->_data[$key][$id]->stream)?Horde_Mime_Headers::parseHeaders(stream_get_contents($this->_data[$key][$id]->stream,-1,0)):$this->_data[$key][$id]->toString(
>                     array('nowrap' => true)
>                 );
>
> I am aware, that working around the issue is most probably NOT the  
> way the issue should be handeled, and that the way I do it, is
> probably not the most effective or desired way to do it.
> The question is HOW to handle the issue, or avoid it alltogether. It  
> seems to be triggered by the expectation that header-Data may not be  
> of type stream.
>
> The way the temp stream stuff is done, it seems, if string is  
> exeeding a certain length its handeled
> as stream, and this seems not to be expected in _getHeaders
>
> Any idea where to dig in and handle it in a way that it is done properly ?
>
> Best regards
>
> ​Klaus Leithoff STYLITE AG
> ----- Ende der weitergeleiteten Nachricht -----
> -------------------------
> Jan Schneider
> The Horde Project
> http://www.horde.org/
> -- 
> dev mailing list
> Frequently Asked Questions: http://wiki.horde.org/FAQ
> To unsubscribe, mail: dev-unsubscribe at lists.horde.org


I believe that that value is supposed to be a Horde_Mime object of  
some sort, not a Horde_Stream object so there might be some issue  
there in the code path when fetching the fullText of the message. We  
don't fetch the fullText in our code very often, if at all. I'll have  
to look into that part of the issue.

However, fetching the fullText like you are is very inefficient.  
Perhaps there is a reason you have to do it in your use-case, but if  
the goal of this code is just to display some preview text, you most  
likely want to first fetch just the message structure, then see what  
mime part contains the type of message you want to preview -  
HTML/Plain etc..., then you can fetch just that part, also limiting it  
to some maximum length if desired.

As an example:
<code>
$mbox = new Horde_Imap_Client_Mailbox('INBOX');
$imap = $registry->mail->imapOb();
$ids = new Horde_Imap_Client_Ids(array(330941));

// Fetch the Mime structure, along with the header text if desired.
$query = new Horde_Imap_Client_Fetch_Query();
$query->structure();
// If you want the headers...
$query->headerText(array('peek' => true));
$fetch_res = $imap->fetch($mbox, $query, array('ids' => $ids));
$data = $fetch_res->first();

// $headers will be a Horde_Mime_Header object containing all headers.
// (Assuming you set $query->headerText() above).
// You could also use a different HIC_Data_Fetch:: constant to get headers
// in a different format.
$headers = $data->getHeaderText(0,  
Horde_Imap_Client_Data_Fetch::HEADER_PARSE);

// $mime is Horde_Mime_Part containing the MIME *structure* of the message.
$mime = $data->getStructure();

// Find the plain body part:
$part_id = $mime->findBody('plain');
// ...or the html body.
//$part_id = $mime->findBody('html');

// If you want to see the mapping of parts/content-type:
//$type_map = $mime->contentTypeMap();


// The (currently empty) MIME part for the part we are interested in.
$text_mime = $mime->getPart($part_id);

// Fetch the contents of the part from the IMAP server:
$query = new Horde_Imap_Client_Fetch_Query();
// If you want to know the full size of the body part.
$query->bodyPartSize($part_id);

// get the bodypart, decode it if possible
// and truncate to 15 characters.
$query->bodyPart($part_id, array(
     'decode' => true,
     'peek' => true,
     'length' => 15));
$fetch_res = $imap->fetch($mbox, $query, array('ids' => $ids));
$data = $fetch_res->first();

// Get the contents, and ensure it's decoded properly.
$text = $data->getBodyPart($part_id);
if (!$data->getBodyPartDecode($part_id)) {
     $text_mime->setContents($text);
     $text = $text_mime->getContents();
}
echo $text;

// The FULL size of the part on the server.
var_dump($data->getBodyPartSize($part_id));
</code>

Hope this helps. Our ActiveSync code uses a LOT of imap processing for  
various parts if you want to see more code, look at  
Horde_ActiveSync_Imap_Message, H_A_S_Imap_Adapter,  
H_A_S_Imap_MessageBodyData, and H_A_S_Mime_Iterator.


-- 
mike
The Horde Project
http://www.horde.org
https://www.facebook.com/hordeproject
https://www.twitter.com/hordeproject
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5751 bytes
Desc: S/MIME Signature
URL: <https://lists.horde.org/archives/dev/attachments/20160517/4134eeba/attachment.bin>


More information about the dev mailing list