+ class msg_header {
+ /** msg_header contains generic variables for values that **/
+ /** could be in a header. **/
+
+ var $type0, $type1, $boundary, $charset, $encoding, $size;
+ var $to, $from, $date, $cc, $bcc, $reply_to, $subject;
+ var $id, $mailbox, $description;
+ var $entity_id, $message_id, $charset;
+ }
+
+ class message {
+ /** message is the object that contains messages. It is a recursive
+ object in that through the $entities variable, it can contain
+ more objects of type message. See documentation in mime.txt for
+ a better description of how this works.
+ **/
+ var $header;
+ var $entities;
+
+ function addEntity ($msg) {
+ $this->entities[count($this->entities)] = $msg;
+ }
+ }
+
+
+
+ /* --------------------------------------------------------------------------------- */
+ /* MIME DECODING */
+ /* --------------------------------------------------------------------------------- */
+
+ // This function gets the structure of a message and stores it in the "message" class.
+ // It will return this object for use with all relevant header information and
+ // fully parsed into the standard "message" object format.
+ function mime_structure ($imap_stream, $header) {
+ global $debug_mime;
+ sqimap_messages_flag ($imap_stream, $header->id, $header->id, "Seen");
+
+ $id = $header->id;
+ fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n");
+ $read = fgets ($imap_stream, 10000);
+ $response = substr($read, 0, 4);
+ while ($response != "a001") {
+ $bodystructure .= $read;
+ $read = fgets ($imap_stream, 10000);
+ $response = substr($read, 0, 4);
+ }
+ // $read = strtolower($bodystructure);
+ $read = $bodystructure;
+
+ if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
+ // isolate the body structure and remove beginning and end parenthesis
+ $read = trim(substr ($read, strpos(strtolower($read), "bodystructure") + 13));
+ $read = trim(substr ($read, 0, -1));
+ $end = mime_match_parenthesis(0, $read);
+ while ($end == strlen($read)-1) {
+ $read = trim(substr ($read, 0, -1));
+ $read = trim(substr ($read, 1));
+ $end = mime_match_parenthesis(0, $read);
+ }
+
+ if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
+
+ $msg = mime_parse_structure ($read, 0);
+ $msg->header = $header;
+ return $msg;
+ }
+
+ // this starts the parsing of a particular structure. It is called recursively,
+ // so it can be passed different structures. It returns an object of type
+ // $message.
+ // First, it checks to see if it is a multipart message. If it is, then it
+ // handles that as it sees is necessary. If it is just a regular entity,
+ // then it parses it and adds the necessary header information (by calling out
+ // to mime_get_elements()
+ function mime_parse_structure ($structure, $ent_id) {
+ global $debug_mime;
+ if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>\n";
+ $msg = new message();
+ if (substr($structure, 0, 1) == "(") {
+ $ent_id = mime_new_element_level($ent_id);
+ $start = $end = -1;
+ if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
+ do {
+ if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>";
+ $start = $end+1;
+ $end = mime_match_parenthesis ($start, $structure);
+
+ $element = substr($structure, $start+1, ($end - $start)-1);
+ $ent_id = mime_increment_id ($ent_id);
+ $newmsg = mime_parse_structure ($element, $ent_id);
+ $msg->addEntity ($newmsg);
+ } while (substr($structure, $end+1, 1) == "(");