Baby steps... baby steps...
I'm working through it function by function, line by line and am getting a lot closer to converting from the imap c-clent functions to zend\mail.
I could use a little assistance figuring out exactly what this function is supposed to return. I think I have figured out the individual lines, but the overall picture is eluding me.
(The comments in each of these functions are from the original script except for the #'s to coincide with my understanding below.)
//search for specific mime type parts....encoding is the desired encoding.
function getPart($mid, $mimeType, $encoding=false, $struct=null, $partNumber=false) {
#1
if(!$struct && $mid)
$struct=@imap_fetchstructure($this->mbox, $mid);
//Match the mime type.
#2
if($struct
&& strcasecmp($mimeType, $this->getMimeType($struct))==0
&& (!$struct->ifdparameters
|| !$this->findFilename($struct->dparameters))) {
#3
$partNumber=$partNumber?$partNumber:1;
#4
if(($text=imap_fetchbody($this->mbox, $mid, $partNumber))) {
if($struct->encoding==3 or $struct->encoding==4) //base64 and qp decode.
$text=$this->decode($text, $struct->encoding);
$charset=null;
#5
if ($encoding) { //Convert text to desired mime encoding...
if ($struct->ifparameters && $struct->parameters) {
foreach ($struct->parameters as $p) {
if (!strcasecmp($p->attribute, 'charset')) {
$charset = trim($p->value);
break;
}
}
}
#6
$text = $this->mime_encode($text, $charset, $encoding);
}
return $text;
}
}
//Do recursive search
$text='';
if($struct && $struct->parts) {
while(list($i, $substruct) = each($struct->parts)) {
if($partNumber)
$prefix = $partNumber . '.';
if(($result=$this->getPart($mid, $mimeType, $encoding, $substruct, $prefix.($i+1))))
$text.=$result;
}
}
return $text;
}
Here's what I understand the function getPart to be doing:
#1
if $struct is null or empty AND $mid (message ID) is NOT null) get the structure of the message
#2
if $struct is NOT null
AND the structure did not return a predefined $mimeType
AND (there is not an array of objects corresponding to the parameters of the Content-disposition
OR can't locate a filename)
#3
if there's a part number use it, otherwise use 1
#4
if can retrieve the section (by $partNumber) of the specified message (by $mid)
then if the encoding is either base64 or quoted-printable decode it as an 8 bit string
#5
if an encoding type has been defined then
if an array of object parameters exists and is not null then
loop through the parameters to determine the charset
#6
mime encode the string $text (the decoded content of $partnumber) using
the $charset and $encoding method found in the structure
And here are the functions referenced:
function getMimeType($struct) {
$mimeType = array('TEXT', 'MULTIPART', 'MESSAGE', 'APPLICATION', 'AUDIO', 'IMAGE', 'VIDEO', 'OTHER');
if(!$struct || !$struct->subtype)
return 'TEXT/PLAIN';
return $mimeType[(int) $struct->type].'/'.$struct->subtype;
}
/**
* Searches the attribute list for a possible filename attribute. If
* found, the attribute value is returned. If the attribute uses rfc5987
* to encode the attribute value, the value is returned properly decoded
* if possible
*
* Attribute Search Preference:
* filename
* filename*
* name
* name*
*/
function findFilename($attributes) {
foreach (array('filename', 'name') as $pref) {
foreach ($attributes as $a) {
if (strtolower($a->attribute) == $pref)
return $a->value;
// Allow the RFC5987 specification of the filename
elseif (strtolower($a->attribute) == $pref.'*')
return Format::decodeRfc5987($a->value);
}
}
return false;
}
function decode($text, $encoding) {
switch($encoding) {
case 1:
$text=imap_8bit($text);
break;
case 2:
$text=imap_binary($text);
break;
case 3:
// imap_base64 implies strict mode. If it refuses to decode the
// data, then fallback to base64_decode in non-strict mode
$text = (($conv=imap_base64($text))) ? $conv : base64_decode($text);
break;
case 4:
$text=imap_qprint($text);
break;
}
return $text;
}
function mailbox_encode($mailbox) {
if (!$mailbox)
return null;
// Properly encode the mailbox to UTF-7, according to rfc2060,
// section 5.1.3
elseif (function_exists('mb_convert_encoding'))
return mb_convert_encoding($mailbox, 'UTF7-IMAP', 'utf-8');
else
// XXX: This function has some issues on some versions of PHP
return imap_utf7_encode($mailbox);
}
I just don't understand what it's doing overall... when all is said and done, what is $text?