Net_IMAP::getRawHeaders problem

From: Date: Sat, 13 Aug 2005 01:04:10 +0000
Subject: Net_IMAP::getRawHeaders problem
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-20403@lists.php.net to get a copy of this message
The function, getRawHeaders, fails when there are untagged responses prior to the FETCH response because it assumes that the first response is the correct one. Attached is a patch which returns the correct response. The patch also includes the addition of an $unfold parameter to the getParsedHeaders() method. When set to false, headers are not unfolded. The default is to unfold headers, as is always done in version 1.0.3. Headers such as SpamAssassin's X-Spam-Report and some Received headers are meant to be human-parsable, and unfolding them makes them much less readable. xn

--- /home/cwarden/Net_IMAP-1.0.3/IMAP.php 2004-09-25 08:06:07.000000000 -0700 +++ IMAP.php 2005-08-12 17:20:01.344087256 -0700 @@ -247,8 +247,13 @@ if(strtoupper( $ret["RESPONSE"]["CODE"]) != "OK" ){ return new PEAR_Error($ret["RESPONSE"]["CODE"] . ", " . $ret["RESPONSE"]["STR_CODE"]); } - $ret=$ret["PARSED"][0]["EXT"]["BODY[HEADER]"]["CONTENT"]; - return $ret; + // Find the FETCH response. There may be untagged responses before + // it. + foreach ($ret['PARSED'] as $serverResponse) { + if ($serverResponse['COMMAND'] == 'FETCH' && $serverResponse['NRO'] == $msg_id) { + return $serverResponse['EXT']['BODY[HEADER]']['CONTENT']; + } + } } @@ -264,16 +269,17 @@ * @param $msg_id Message number * @return mixed Either array of headers or false on error */ - function getParsedHeaders($msg_id) + function getParsedHeaders($msg_id, $unfold = true) { $ret=$this->getRawHeaders($msg_id); + $n = $unfold ? '' : "\0"; $raw_headers = rtrim($ret); - $raw_headers = preg_replace("/\r\n[ \t]+/", ' ', $raw_headers); // Unfold headers + $raw_headers = preg_replace("/\r\n([ \t]+)/", "$n\$1", $raw_headers); // Unfold headers $raw_headers = explode("\r\n", $raw_headers); foreach ($raw_headers as $value) { $name = substr($value, 0, $pos = strpos($value, ':')); - $value = ltrim(substr($value, $pos + 1)); + $value = str_replace("\0", "\r\n", ltrim(substr($value, $pos + 1))); if (isset($headers[$name]) AND is_array($headers[$name])) { $headers[$name][] = $value; } elseif (isset($headers[$name])) {
« previous php.pear.general (#20403) next »