- /** OK, we have the header information, now lets decide what to do with it **/
- if ($ent_type0 == "multipart") {
- $y = 0;
- while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") {
- $ent_body[$y] = $body[$j];
- $y++;
- $j++;
- }
- $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
- $entities = $ent;
- } else {
- $j++;
- $entity_body = "";
- while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
- $entity_body .= $body[$j];
- $j++;
- }
- $count = count($entities);
- $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
- }
- }
- $i++;
- }
+ /** Setting up the objects that have the structure for the message **/
+
+ class msg_header {
+ /** msg_header contains generic variables for values that **/
+ /** could be in a header. **/
+
+ var $type0 = '', $type1 = '', $boundary = '', $charset = '';
+ var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
+ var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
+ var $id = 0, $mailbox = '', $description = '', $filename = '';
+ var $entity_id = 0, $message_id = 0, $name = '';
+ }
+
+ 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 = array();
+
+ function addEntity ($msg) {
+ $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");
+ //
+ // This should use sqimap_read_data instead of reading it itself
+ //
+ $read = fgets ($imap_stream, 10000);
+ $response = substr($read, 0, 4);
+ $bodystructure = "";
+ while ($response != "a001") {
+ $bodystructure .= $read;
+ $read = fgets ($imap_stream, 10000);
+ $response = substr($read, 0, 4);
+ }
+ $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) == "(");