I'm trying to figure out what this piece of code is doing:
$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;
} //end while
}// end if
return $text;
The comments in the code says it is a recursive search and it is at the end of function getPart.
The function getPart searches through the email headers for specific mime type parts.
$struct is the result of imap_fetchstructure for the specific message header.
$mid is the message ID.
$mimeType is the MIME Type being sought after.
$encoding is the message's encoding.
I'm mostly confused by the seemingly embedded functions in this line:
while(list($i, $substruct) = each($struct->parts))
This is where my naivete in PHP comes into play. I tried looking at php.net and RTFM, but the entry for "list" doesn't include a good example of the results to help me decipher exactly how it works.
If I knew how to do a return from a foreach statement and continue processing through, this probably wouldn't be necessary.
By that, I mean, say my foreach statement looks like this:
$partNumber = null; //(re)sets $partNumber to null
foreach (new RecursiveIteratorIterator($this->zmail->getMessage($mid)) as $part) {
$partNumber = $partNumber?$partNumber:1; //if there's a part number use it, otherwise use 1
if ($text = $mailmsg->getPart($partNumber)->getContent()) {
$encoding = null; //(re)set encoding to null
$charset=null; //(re)set $charset as null
if (isset($part->contenttransferencoding)) { //does the encoding field exist
$encoding = $part->contenttransferencoding; //get the encoding type
if ($encoding //if $encoding is NOT null
&& ($encoding == 'base64' || $encoding == 'quoted-printable')) { //AND if $encoding is either base64 or qp
$text = $this->decode($text, $encoding); //decode the content accordingly
$charset = $part->getHeaderField('Content-Type', 'charset'); //determine the $charset to be used
$text = $this->mime_encode($text, $charset, $encoding); //mime encode the string $text (the decoded content of $partnumber) using the $charset and $encoding method found in the message $part
}
} //end if encoding field exists
} //end if the content of the message can be retrieved (by $partNumber) of the specified message (by $mid)
$partNumber++; //increase $partNumber by 1
return $text;
} // end foreach going through each mail part
Essentially, I get a message header and want to go through each part looking for a specific type of content and when I find it I need to encode/decode it as appropriate and then pass that content on to another function. If I understand my own code correctly , when the code hits the line "return $text" it exits the foreach loop and sends the value of $text back to the calling function, right? Assuming that's correct, how do I send the value of $text back to the calling function without exiting the foreach loop?
Does my question make sense?
Appreciate all the help you guys have offered so far on this little project of mine and am looking forward to your responses to this little inquiry.